//Alphatrend - Trend Follow Strategy
//Coded by Rajandran R - Founder - Marketcalls / Creator - OpenAlgo
//Website : www.openalgo.in / www.marketcalls.in
//Code Inspired from Tradingview - KivancOzbilgic
//Tradingview URL : https://tradingview.com/script/o50NYLAZ-AlphaTrend/
//Alpha Trend tries to solve those problems such as:
//1-To minimize stop losses and overcome sideways market conditions.
//2-To have more accurate BUY/SELL signals during trending market conditions.
//3- To have significant support and resistance levels.
//4- To bring together indicators from different categories that are compatible with each other and make a meaningful combination regarding momentum, trend, volatility , volume and trailing stop loss.
_SECTION_BEGIN("AlphaTrend");
SetBarsRequired(sbrAll,sbrAll);
coeff = Param("Multiplier",0.5,0.1,1,0.1);
AP = Param("Common Period", 20, 1, 100, 1);
iATR = MA(ATR(1),AP);
src = Close;
showsignalsk = True;
novolumedata = False;
upT = Low - iATR * coeff;
downT = High + iATR * coeff;
hlc3= (H+L+C)/3;
rs = RSIa(Close,14);
mf = MFI(14);
AlphaTrend = 0.0;
//AlphaTrend := (novolumedata ? ta.rsi(src, 14) >= 50 :
// ta.mfi(hlc3, 14) >= 50) ? upT < nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : upT :
// downT > nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : downT
for(i=1;i<BarCount;i++)
{
if(novolumedata){
Alphatrend[i] = rs[i]>= 50;
}//end novolumedata
else if(mf[i] >= 50){
if(upT[i] < Alphatrend[i-1])
{
Alphatrend[i] = Alphatrend[i-1] ;
}
else{
Alphatrend[i] = upT[i];
}
}
else
{
if(downT[i] > Alphatrend[i-1])
{
Alphatrend[i] = Alphatrend[i-1] ;
}
else
{
Alphatrend[i] = downT[i];
}
}//end else
}//end for loop
color = IIf(AlphaTrend>Ref(AlphaTrend,-2), colorGreen, IIf(AlphaTrend<Ref(AlphaTrend,-2), colorred,
IIf(Ref(AlphaTrend,-1) > Ref(AlphaTrend,-3), colorblue, colorYellow)));
Buy = Cross(Alphatrend,Ref(Alphatrend,-2));
Sell = Cross(Ref(AlphaTrend,-2),AlphaTrend);
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short = Sell;
Cover = Buy;
//Non Repainting - Signals happens only after the close of the current
Buy = Ref(Buy,-1);
Sell = Ref(Sell,-1);
Short = Ref(Short,-1);
Cover = Ref(Cover,-1);
BuyPrice = ValueWhen(Buy,Open);
sellPrice = ValueWhen(sell,Open);
shortPrice = ValueWhen(Short,Open);
coverPrice = ValueWhen(cover,Open);
buycontinue = Flip(Buy,Sell);
color1 = IIf(buycontinue,colorGreen,colorRed);
cloud = ParamToggle("Cloud Enabled","TRUE|False",0);
if(cloud)
{
PlotOHLC(AlphaTrend,AlphaTrend,Ref(AlphaTrend,-2),Ref(AlphaTrend,-2), "AlphaTrend",color, styleCloud|styleNoLabel);
}
else
{
Plot(Alphatrend,"Alphatrend",color1,styleLine | styleThick);
}
/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(Sell * shapestar, colorBrightGreen, 0, High, 12);
PlotShapes(Cover * shapestar, colorRed, 0, Low, -12);
_SECTION_END();
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", color1, styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
_SECTION_BEGIN("Trading Dashboard");
dashboard = ParamToggle("Show Dashboard","SHOW|HIDE",1);
if(dashboard)
{
fontsize = Param("Font Size",14,8,40,1);
GfxSelectFont("BOOK ANTIQUA", fontsize, 400);
GfxSetTextColor(colorWhite);
GfxSetBkMode(1); //Transparent mode
buycontinue = Flip(Buy,Sell);
sellcontinue = Flip(Sell,Buy);
color = IIf(buycontinue, colorGreen, IIf(sellcontinue, colorRed, colorGrey40));
GfxSelectPen(colorWhite); //pen -> draw outline of the shape
GfxSelectSolidBrush(SelectedValue(color));
width = Status("pxchartwidth");
height = Status("pxchartheight");
GfxRoundRect(20,height-150,320,height-30,10,10);
//Trading Signal Status - e.g Buy Signal or Short Signal
//Profit/Loss for Current Running Trade
sigstatus = WriteIf(buycontinue,"Buy Signal", WriteIf(sellcontinue,"Short Signal", "No Trade(Relax)"));
PNL = IIf(buycontinue,Close-BuyPrice, IIf(sellcontinue, ShortPrice-Close, Null));
//variable sigstatus -> string
//variable PNL -> Array
GfxTextOut("AlphaTrend",30,height-130);
GfxTextOut(sigstatus + " came @ : "+ IIf(buycontinue,BuyPrice, IIf(sellcontinue, ShortPrice, Null)),30,height-110);
GfxTextOut("Profit/Loss :"+SelectedValue(Prec(PNL,2)),30, height-90);
}
_SECTION_END();