0% found this document useful (0 votes)
59 views5 pages

Supertrend AFL for Swing Trading

Uploaded by

pranjpatil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views5 pages

Supertrend AFL for Swing Trading

Uploaded by

pranjpatil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/* 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 on MA");

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 )
) ));

HaClose=(O+H+L+C)/4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );
//PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "" + Name(), colorBlack, styleCandle |
styleNoLabel );
SetChartBkGradientFill(ParamColor("Upper
Chart",colorDarkOliveGreen),ParamColor("Lower Chart",colorBlack));
newday=Day()!=Ref(Day(),-1);
SetSortColumns(-2);
GfxSetTextColor( colorGrey40);
GfxSetBkColor(colorDarkOliveGreen);
GfxSelectFont("Tahoma", Status("pxheight")/60 );
GfxSelectFont("Tahoma", Status("pxheight")/40 );
GfxTextOut( "Pranjal Patil", Status("pxwidth")/2.5, Status("pxheight")/8 );
///////////////////////////////////////////// SUPERTRENT
FUNCTION /////////////////////////////////////
Period = Param("Periods", 10, 1, 50 );
Multiplier = Param("Multiplier ", 2, 1, 10 );
Len = Param("Len sma", 40, 2, 200, 1 );
ResistanceColor = ParamColor("Resistance", colorRed);
SupportColor = ParamColor( "Support", colorGreen);
Style = ParamStyle("Line Style", styleThick);
Sma2=HaClose; //MA( close, 2 );

//Super trend of high plot upper band


function FunctionSTH (Period, Multiplier,Len)
{
ATR_Val=ATR(Period);

UpperBand=LowerBand=final_UpperBand=final_LowerBand=SuperTrendh=0;

SmaH=MA( H, Len );
SmaL=MA( L, Len );

///////// CALCULATE SUPERTRENTH //////////


for( i = Period; i < BarCount; i++ )
{
UpperBand[i]=SmaH[i] + Multiplier*ATR_Val[i];
LowerBand[i]=SmaH[i] - Multiplier*ATR_Val[i];
final_UpperBand[i] = IIf( ((UpperBand[i]<final_UpperBand[i-1]) OR
(Sma2[i-1]>final_UpperBand[i-1])), (UpperBand[i]), final_UpperBand[i-1]);
final_LowerBand[i] = Iif( ((LowerBand[i]>final_LowerBand[i-1]) OR
(Sma2[i-1]<final_LowerBand[i-1])), (LowerBand[i]), final_LowerBand[i-1]);

SuperTrendh[i] = IIf(((SuperTrendh[i-1]==final_UpperBand[i-1]) AND


(Sma2[i]<=final_UpperBand[i])),final_UpperBand[i],
IIf(((SuperTrendh[i-1]==final_UpperBand[i-1]) AND
(Sma2[i]>=final_UpperBand[i])),final_LowerBand[i],
IIf(((SuperTrendh[i-1]==final_LowerBand[i-1]) AND
(Sma2[i]>=final_LowerBand[i])),final_LowerBand[i],
IIf(((SuperTrendh[i-1]==final_LowerBand[i-1]) AND
(Sma2[i]<=final_LowerBand[i])),final_UpperBand[i],0))));

SuperTrendh = IIf(Sma2 < SuperTrendh, SuperTrendh, Null);

Return SuperTrendh;
}
SuperTrendh = FunctionSTH(Period,Multiplier,Len);
Plot( SuperTrendh, "SuperTrendH", IIf( SuperTrendh>Sma2, ResistanceColor,
ResistanceColor), Style);
//Super trend of Low Sma plot Lower band
function FunctionSTL (Period, Multiplier,Len)
{
ATR_Val=ATR(Period);

UpperBand=LowerBand=final_UpperBand=final_LowerBand=SuperTrendL=0;

SmaH=MA( H, Len );
SmaL=MA( L, Len );

///////// CALCULATE SUPERTRENTL //////////


for( i = Period; i < BarCount; i++ )
{
UpperBand[i]=SmaL[i] + Multiplier*ATR_Val[i];
LowerBand[i]=SmaL[i] - Multiplier*ATR_Val[i];
final_UpperBand[i] = IIf( ((UpperBand[i]<final_UpperBand[i-1]) OR
(Sma2[i-1]>final_UpperBand[i-1])), (UpperBand[i]), final_UpperBand[i-1]);
final_LowerBand[i] = Iif( ((LowerBand[i]>final_LowerBand[i-1]) OR
(Sma2[i-1]<final_LowerBand[i-1])), (LowerBand[i]), final_LowerBand[i-1]);

SuperTrendL[i] = IIf(((SuperTrendL[i-1]==final_UpperBand[i-1]) AND


(Sma2[i]<=final_UpperBand[i])),final_UpperBand[i],
IIf(((SuperTrendL[i-1]==final_UpperBand[i-1]) AND
(Sma2[i]>=final_UpperBand[i])),final_LowerBand[i],
IIf(((SuperTrendL[i-1]==final_LowerBand[i-1]) AND
(Sma2[i]>=final_LowerBand[i])),final_LowerBand[i],
IIf(((SuperTrendL[i-1]==final_LowerBand[i-1]) AND
(Sma2[i]<=final_LowerBand[i])),final_UpperBand[i],0))));

SuperTrendL = IIf(Sma2 > SuperTrendL, SuperTrendL, Null);


Return SuperTrendL;
}
SuperTrendL = FunctionSTL(Period,Multiplier,Len);
Plot( SuperTrendL, "SuperTrendL", IIf( SuperTrendL<Sma2, SupportColor,
SupportColor), Style);

PlotOHLC( SuperTrendL, SuperTrendL, SuperTrendh, SuperTrendh, "",


colorturquoise,styleCloud );
_SECTION_END();

_SECTION_BEGIN("MA1");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1, 10 );
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ),
ParamStyle("Style") );
Plot( MA( P, 5 ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ),
ParamStyle("Style") );
//Medium Trend
Plot(FunctionSTL(10,1,20),"medium",colorBlue);
Plot(FunctionSTH(10,1,20),"medium",colorWhite);
PlotOHLC( FunctionSTL(10,1,20), FunctionSTL(10,1,20), FunctionSTH(10,1,20),
FunctionSTH(10,1,20), "", colorBrown,styleCloud );
//Short Trend
Plot(FunctionSTL(10,1,5),"medium",colorAqua);
Plot(FunctionSTH(10,1,5),"medium",colorOrange);
PlotOHLC( FunctionSTL(10,1,5), FunctionSTL(10,1,5), FunctionSTH(10,1,5),
FunctionSTH(10,1,5), "", colorLightGrey,styleCloud );

_SECTION_END();

///////////////CPR///////////////////////
//////////////////////////////
/////////////////////////////////////////////
_SECTION_BEGIN( "CPR" );
dtDayStartComp = TimeFrameCompress( DateTime(), inDaily, compressOpen );
dtDayStartExp = TimeFrameExpand( dtDayStartComp, inDaily, expandFirst );
dtDayEndComp = TimeFrameCompress( DateTime(), inDaily, compressLast );
dtDayEndExp = TimeFrameExpand( dtDayEndComp, inDaily, expandFirst );
DayCond = DateTime() >= dtDayStartExp AND DateTime() < dtDayEndExp;

// Previous Days High Low Close


PDH = TimeFrameGetPrice( "H", inDaily, -1 );
PDL = TimeFrameGetPrice( "L", inDaily, -1 );
PDC = TimeFrameGetPrice( "C", inDaily, -1 );

PV = ( PDH + PDL + PDC ) / 3;


LB = ( PDH + PDL ) / 2;
UB = 2 * PV - LB;

Plot( IIf( DayCond, UB, Null ), "UB", colorAqua, styleLine, Null, Null, 0 );
Plot( IIf( DayCond, UB, Null ), "UB", colorAqua, styleLine, Null, Null, 2 );

Plot( IIf( DayCond, PV, Null), "Pivot", colorAqua, styleLine, Null, Null,
0 );
Plot( IIf( DayCond, PV, Null), "Pivot", colorAqua, styleLine, Null, Null,
2 );
Plot( IIf( DayCond, LB, Null), "LB", colorAqua, styleLine, Null, Null, 0 );
Plot( IIf( DayCond, LB, Null), "LB", colorAqua, styleLine, Null, Null, 2 );
_SECTION_END();

_SECTION_BEGIN( "Pivot Levels" );


//PDH = TimeFrameGetPrice( "H", inDaily, -1 );
//PDL = TimeFrameGetPrice( "L", inDaily, -1 );
//PDC = TimeFrameGetPrice( "C", inDaily, -1 );

//PV = ( PDH + PDL + PDC ) / 3;


R1 = 2 * PV - PDL;
R2 = PV + PDH - PDL;
S1 = 2 * PV - PDH;
S2 = PV - PDH + PDL;

Plot( IIf( DayCond, R1, Null ), "R1", colorRed, styleLine, Null, Null, 0 );
Plot( IIf( DayCond, R1, Null ), "R1", colorRed, styleLine, Null, Null, 2 );

Plot( IIf( DayCond, R2, Null ), "R2", colorOrange, styleLine, Null, Null,
0 );
Plot( IIf( DayCond, R2, Null ), "R2", colorOrange, styleLine, Null, Null,
2 );

Plot( IIf( DayCond, S1, Null ), "S1", colorGreen, styleLine, Null, Null,
0 );
Plot( IIf( DayCond, S1, Null ), "S1", colorGreen, styleLine, Null, Null,
2 );

Plot( IIf( DayCond, S2, Null ), "S2", colorLime, styleLine, Null, Null, 0 );
Plot( IIf( DayCond, S2, Null ), "S2", colorLime, styleLine, Null, Null, 2 );
_SECTION_END();

_SECTION_BEGIN( "To view Pivots of Next Day as Today closes" );


shift = 10; //Number of Bars to be shifted, could use a Param if needed
bi = BarIndex();
lvbi = LastValue( bi );
LAx0 = lvbi - shift + 1;
LAx1 = lvbi;

// Present Days High Low Close


// These levels will be dynamic as day progresses and would ahow up in the
no bar region after the last visible Bar for shifted number of bars (i.e. 10 in
this case)
// Do use "Bar Replay" to figure out what I mean
dH = TimeFrameGetPrice( "H", inDaily, 0 );
dL = TimeFrameGetPrice( "L", inDaily, 0 );
dC = TimeFrameGetPrice( "C", inDaily, 0 );

//Central Pivots shifted for Next Day


dPV = ( dH + dL + dC ) / 3;
LB = ( dH + dL ) / 2;
UB = 2 * dPV - LB;

y = LastValue( dPV );
la = LineArray( LAx0, y, LAx1, y );
Plot( la, "PV", colorAqua, styleLine, Null, Null, shift );
y = LastValue( LB );
la = LineArray( LAx0, y, LAx1, y );
Plot( la, "LB", colorAqua, styleLine, Null, Null, shift );

y = LastValue( UB );
la = LineArray( LAx0, y, LAx1, y );
Plot( la, "UB", colorAqua, styleLine, Null, Null, shift );

//Pivot Levels shifted for Next Day


R1 = 2 * dPV - dL;
R2 = dPV + dH - dL;
S1 = 2 * dPV - dH;
S2 = dPV - dH + dL;

y = LastValue( R1 );
la = LineArray( LAx0, y, LAx1, y );
Plot( la, "R1", colorRed, styleLine, Null, Null, shift );

y = LastValue( R2 );
la = LineArray( LAx0, y, LAx1, y );
Plot( la, "R2", colorOrange, styleLine, Null, Null, shift );

y = LastValue( S1 );
la = LineArray( LAx0, y, LAx1, y );
Plot( la, "S1", colorGreen, styleLine, Null, Null, shift );

y = LastValue( S2 );
la = LineArray( LAx0, y, LAx1, y );
Plot( la, "S2", colorLime, styleLine, Null, Null, shift );
_SECTION_END();

Common questions

Powered by AI

Shifting pivot lines for future forecasts, as described in the AFL script, allows traders to visualize potential future support and resistance levels. By projecting pivot lines for the next trading day, traders can estimate the future price action context and preemptively adjust their strategies. This approach enhances readiness to market changes and optimizes trade execution by considering dynamic market conditions forecasted through past data analysis .

The Central Pivot Range (CPR) is incorporated into a trading strategy as a predictive tool that measures the equilibrium point of market price. It is calculated using the previous day's high, low, and close prices to establish central and support-resistance lines (Pivot, Upper Band, Lower Band). CPR acts as a dynamic price level, influencing potentially pivotal movements in the current day's trading by indicating the direction and strength of market trends .

Changing the 'Period' affects how many past data points are used to calculate ATR, impacting how quickly the Supertrend responds to price changes. A lower period results in a more sensitive and responsive indicator. Altering the 'Multiplier' scales the ATR's influence on the Supertrend bands; a higher multiplier will make the bands wider, decreasing the likelihood of frequent trend changes and reducing false signals .

Pivot levels aid traders in planning their intraday trading strategy by providing key levels of support and resistance derived from the previous day's price data. These levels, such as R1, R2 for resistance, and S1, S2 for support, help traders anticipate potential turning points where the price may reverse or accelerate, guiding them in deciding entry, exit, and stop-loss points effectively .

Heikin-Ashi is used in the Supertrend AFL script to smooth out price data, thereby reducing market noise and potentially confirming trend reversals with greater reliability. By adjusting the displayed candle values using prior data, Heikin-Ashi can clarify trends, helping traders make more informed decisions by better identifying the underlying market direction .

SuperTrendh and SuperTrendL are two functions used to calculate the upper and lower Supertrend bands, respectively. SuperTrendh calculates the resistance or the upper band, whereas SuperTrendL calculates the support or the lower band. These functions help identify market trends by indicating potential reversal points; when the price is above SuperTrendh, it signifies a bullish trend, and when it's below SuperTrendL, it indicates a bearish trend .

In the Supertrend indicator, resistance and support are visualized through color coding. Typically, the upper band (SuperTrendh) is colored red to indicate resistance, while the lower band (SuperTrendL) is green to signify support. This color differentiation helps traders quickly assess potential reversal points where the market may experience a change in direction .

The Supertrend indicator functions in swing trading by providing visual buying and selling signals based on trend direction. It utilizes the Average True Range (ATR) to calculate the volatility of the market. The indicator can be adapted for different timeframes by adjusting the multiplier and the period used in the ATR calculation, which affects the sensitivity of the indicator to market movements .

The use of historical data compression and expansion techniques in intraday analysis provides a structured method to observe trends over various timescales, allowing traders to assess short-term and long-term trend convergence or divergence effectively. However, these techniques can also lead to missing finer details of market movements within the compressed data period, potentially overlooking short-lived but critical price actions. Therefore, while they offer valuable insights for broad trend identification, care must be taken not to overlook nuances crucial for intraday decision-making .

The Average True Range (ATR) plays a crucial role in calculating the Supertrend indicator by measuring market volatility. It serves as a critical component in determining the upper and lower bands of the Supertrend, which adjust according to the market's volatility levels, making the indicator highly responsive to changes .

You might also like