Skip to content

Instantly share code, notes, and snippets.

@DTM-SC
Last active October 19, 2019 21:17
Show Gist options
  • Save DTM-SC/e1da705b521800a78b8e9b44e54e7b9a to your computer and use it in GitHub Desktop.
Save DTM-SC/e1da705b521800a78b8e9b44e54e7b9a to your computer and use it in GitHub Desktop.
SuperTrend AFL
/* This AFL is to plot Supertrend for Swing Trading, this can also be sued for Intraday Supertrend, I would suggest
to use the AFL for any time frame based on your strategy planning. The default multiplier is 1 and period used as 7 for ATR, you can use as per your strategy.
*/
_SECTION_BEGIN("Supertrend AFL");
SetChartOptions(0,chartShowArrows|chartShowDates);
SetBarFillColor(IIf(C>O,ParamColor("UP Color", colorGreen),IIf(C<=O,ParamColor("Down Color", colorRed),colorLightGrey)));
Plot(C,"Price",IIf(C>O,ParamColor("Wick UP Color", colorLime),IIf(C<=O,ParamColor("Wick Down Color", colorOrange),colorLightGrey)),styleCandle | styleNoTitle);
_N(Title = "Supertrend AFL\n" + StrFormat("{{INTERVAL}} {{DATE}} \nOpen= %g, HiGH= %g, LoW= %g, Close= %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
///////////////////////////////////////////// SUPERTRENT FUNCTION /////////////////////////////////////
function FunctionST (Period, Multiplier)
{
ATR_Val=ATR(Period);
UpperBand=LowerBand=final_UpperBand=final_LowerBand=SuperTrend=0;
///////// CALCULATE SUPERTRENT //////////
for( i = Period; i < BarCount; i++ )
{
UpperBand[i]=((High[i] + Low[i])/2) + Multiplier*ATR_Val[i];
LowerBand[i]=((High[i] + Low[i])/2) - Multiplier*ATR_Val[i];
final_UpperBand[i] = IIf( ((UpperBand[i]<final_UpperBand[i-1]) OR (Close[i-1]>final_UpperBand[i-1])), (UpperBand[i]), final_UpperBand[i-1]);
final_LowerBand[i] = Iif( ((LowerBand[i]>final_LowerBand[i-1]) OR (Close[i-1]<final_LowerBand[i-1])), (LowerBand[i]), final_LowerBand[i-1]);
SuperTrend[i] = IIf(((SuperTrend[i-1]==final_UpperBand[i-1]) AND (Close[i]<=final_UpperBand[i])),final_UpperBand[i],
IIf(((SuperTrend[i-1]==final_UpperBand[i-1]) AND (Close[i]>=final_UpperBand[i])),final_LowerBand[i],
IIf(((SuperTrend[i-1]==final_LowerBand[i-1]) AND (Close[i]>=final_LowerBand[i])),final_LowerBand[i],
IIf(((SuperTrend[i-1]==final_LowerBand[i-1]) AND (Close[i]<=final_LowerBand[i])),final_UpperBand[i],0))));
}
Plot( SuperTrend, "SuperTrend", (IIf( SuperTrend>Close, ParamColor("Resistance", colorRed ), ParamColor( "Support", colorGreen ))), ParamStyle("Style") | styleThick | styleLine );
Return SuperTrend;
}
Periods_set = Param("Periods", 7, 1, 50 );
Multiplier_set = Param("Multiplier ", 1, 1, 10 );
Multiplier = Multiplier_set;
Period = Periods_set ;
SuperTrend = FunctionST(Period,Multiplier);
_SECTION_END();
@Mech1907
Copy link

well done bro...thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment