Simple Coding Help - No Promise.

yusi

Well-Known Member
Is it possible to plot pivot points for HHV and LLV between two crosses of Averages. Red line is 9MA and blue line is 20MA.
Josh, there is a conceptual oddity in your request. The High/Low point of the MA can only be known for certain after the next MA cross completes ("between two crosses of Averages"). Let's say that the fast MA is above the slow MA -- here every new high of the fast MA will change the pivot; a redraw will take place.

Hope it is clear, else will post a chart.
 
Thank You Bhanushali ji.

Beautifuly depicted price levels. Actually I wanted Peaks and Valleys for the smalle ma (9MA).
Let's try to find out how to do it.
@josh1; @yusi ; Draft code is given below which is able to mark the pivot high and low of MA9. However, need to figure out how to put it in loop (currently it is giving Error/Warning 502 - You are calling Plot()/PlotOHLC() function over 500 times, it is highly inefficient. Reduce number of calls.) to plot the linearrays (so that no overlapping of extension lines and MA lines are clean). Senior's please help.
Code:
_SECTION_BEGIN("Pivot Points of MA Hi and Lo");
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", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

_SECTION_BEGIN("MA");

MA9 = MA(C,9);
MA20 = EMA(MA( C, 9),20 );
Plot( MA9, "MA9", colorRed, styleline );
Plot( MA20, "MA20", colorbluegrey, styleline );

PeriodUp = MA9>MA20;
PeriodDn = MA9<MA20;


MAHi = HHV(MA9, PeriodDn);   
MALo = LLV(MA9,PeriodUp);


Plot(MAHi, "",colorOrange, styledots | styleNoLine|stylenorescale) ;
Plot(MALo, "",colorbrightgreen, styledots | styleNoLine|stylenorescale) ;

_SECTION_END()
PkVlyof9MA.png
 
can somebody please check the below afl. whenever i am putting it on chart its compressed the chart vertically. it is possible to correct it ? please help

Code:
_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 ), "", colorLightGrey, styleLine,styleNoRescale, Null, 0 );
     Plot( IIf( DayCond, UB, Null ), "", colorLightGrey, styleLine, styleNoRescale, Null, 2 );
    
     Plot( IIf( DayCond, PV, Null), "", colorLightGrey, styleLine, styleNoRescale, Null, 0 );
     Plot( IIf( DayCond, PV, Null), "", colorLightGrey, styleLine, styleNoRescale, Null, 2 );
    
     Plot( IIf( DayCond, LB, Null), "", colorLightGrey, styleLine, styleNoRescale, Null, 0 );
     Plot( IIf( DayCond, LB, Null), "", colorLightGrey, styleLine, styleNoRescale, Null, 2 );
_SECTION_END();
 
@josh1; @yusi ; Draft code is given below which is able to mark the pivot high and low of MA9. However, need to figure out how to put it in loop (currently it is giving Error/Warning 502 - You are calling Plot()/PlotOHLC() function over 500 times, it is highly inefficient. Reduce number of calls.) to plot the linearrays (so that no overlapping of extension lines and MA lines are clean). Senior's please help.
Code:
_SECTION_BEGIN("Pivot Points of MA Hi and Lo");
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", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

_SECTION_BEGIN("MA");

MA9 = MA(C,9);
MA20 = EMA(MA( C, 9),20 );
Plot( MA9, "MA9", colorRed, styleline );
Plot( MA20, "MA20", colorbluegrey, styleline );

PeriodUp = MA9>MA20;
PeriodDn = MA9<MA20;


MAHi = HHV(MA9, PeriodDn);  
MALo = LLV(MA9,PeriodUp);


Plot(MAHi, "",colorOrange, styledots | styleNoLine|stylenorescale) ;
Plot(MALo, "",colorbrightgreen, styledots | styleNoLine|stylenorescale) ;

_SECTION_END()
View attachment 42040
Try to limit the loop with FirstVisibleValue and LastVisibleValue
Will be useful if the chart is used only for visual scanning,
for other cases can suppress the plot

.
 
Thank You Bhanushali ji.

Beautifuly depicted price levels. Actually I wanted Peaks and Valleys for the smalle ma (9MA).
Let's try to find out how to do it.
@Happy_Singh; Thank you very much for your suggestion.
@josh1; Finally, below is the code. Hope I am right this time!. Thnx.
@yusi; Thank you for your last query on the subject. The "PivotLast - Use Last Pivots" toggle would answer that requirement.
Code:
_SECTION_BEGIN("Pivot Points of MA Hi and Lo");
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", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

_SECTION_BEGIN("MA");

MA9 = MA(C,9);
MA20 = EMA(MA( C, 9),20 );
Plot( MA9, "MA9", colorRed, styleline );
Plot( MA20, "MA20", colorbluegrey, styleline );

PeriodUp = MA9>MA20;
PeriodDn = MA9<MA20;


MAHi = HHV(MA9, PeriodDn);  
MALo = LLV(MA9,PeriodUp);


//Plot(MAHi, "",colorOrange, styledots | styleNoLine|stylenorescale) ;
//Plot(MALo, "",colorbrightgreen, styledots | styleNoLine|stylenorescale) ;

LB=9;//Param("Lookback Period",9,2,30,1);

HiMA2 = ValueWhen(IIf(MAHi AND BarsSince(MAHi) < LB,MAHi, Null), MAHi);
LoMA2 = ValueWhen(IIf(MALo AND Barssince(MALo) < LB, MALo, Null), MALo);


_SECTION_END();

_SECTION_BEGIN("Pivot High Low System");

//Below code is freely available on internet, modified for the requirement.

xx=BarIndex();x=xx;Lx=LastValue(x);
nbar=Param("N Pivot Bars",3,2,50,1);
npiv=Param("N Pivots lookback period",1,1,50,1);
PivotLast=ParamToggle("Use Last Pivots","Off|On",0);
markerStyle=ParamToggle("Marker Style [Last Bar]","Off|On",0);


if (PivotLast)

{

   fc=1;
    pk=HiMA2>Ref(HHV(HiMA2,nbar*fc),-1) AND Ref(HHV(HiMA2,nbar),nbar)<=HiMA2;
    pk=pk AND Lx-ValueWhen(pk,x)>nbar*fc;
    tr=L<Ref(LLV(LoMA2,nbar*fc),-1) AND Ref(LLV(LoMA2,nbar),nbar)>=LoMA2;
    tr=tr AND Lx-ValueWhen(tr,x)>nbar*fc;
}

else
{
    fc=2;
    pk=HiMA2>Ref(HHV(HiMA2,nbar*fc),-1) AND Ref(HHV(HiMA2,nbar),nbar)<=HiMA2;
    pk=pk AND Lx-ValueWhen(pk,x)>nbar*fc;
    tr=LoMA2<Ref(LLV(LoMA2,nbar*fc),-1) AND Ref(LLV(LoMA2,nbar),nbar)>=LoMA2;
    tr=tr AND Lx-ValueWhen(tr,x)>nbar*fc;

}

px0=ValueWhen(pk,x,0); tx0=ValueWhen(tr,x,0);
px1=ValueWhen(pk,x,1); tx1=ValueWhen(tr,x,1);
px2=ValueWhen(pk,x,2); tx2=ValueWhen(tr,x,2);
px3=ValueWhen(pk,x,3); tx3=ValueWhen(tr,x,3);
ph0=ValueWhen(pk,HiMA2,0); tl0=ValueWhen(tr,LoMA2,0);
ph1=ValueWhen(pk,HiMA2,1); tl1=ValueWhen(tr,LoMA2,1);
ph2=ValueWhen(pk,HiMA2,2); tl2=ValueWhen(tr,LoMA2,2);
ph3=ValueWhen(pk,HiMA2,3); tl3=ValueWhen(tr,LoMA2,3);

if(markerstyle)

{

miny=Status("axisminy");
maxy=Status("axismaxy");
for (i=1;i<=npiv;i++)

{
    rr=Ref(ValueWhen(pk,HiMA2,i),-nbar);
    rr=IIf(rr>maxy OR rr<miny,Null,rr);
    ss=Ref(ValueWhen(tr,LoMA2,i),-nbar);
    ss=IIf(ss>maxy OR ss<miny,Null,ss);
    Plot(rr,"",colororange,styleNoLine|styleDots,0,0,0);  
    Plot(ss,"",colorbrightgreen,styleNoLine|styleDots,0,0,0);

    PlotText("Peak "+ WriteVal(rr,1.2), BarCount+2, SelectedValue(rr),colororange,colorDefault,-4);
    PlotText("Valley "+ WriteVal(ss,1.2), BarCount+2, SelectedValue(ss),colorbrightgreen,colordefault,-4);

}
}

else

{
   rr=ValueWhen(pk,HiMA2);
   rr1=IIf(rr AND BarsSince(pk)>nbar,rr,Null);
   rr2=IIf(rr AND BarsSince(pk)<=nbar,rr,Null);
   ss=ValueWhen(tr,LoMA2);
   ss1=IIf(ss AND BarsSince(tr)>nbar,ss,Null);
   ss2=IIf(ss AND BarsSince(tr)<=nbar,ss,Null);
   Plot(rr1,"",colororange,styleDots|stylenoline|stylenorescale);
   Plot(rr2,"",colororange,styleDots|styleNoLine|stylenorescale);
   Plot(ss1,"",colorbrightgreen,styleDots|stylenoline|stylenorescale);
   Plot(ss2,"",colorbrightgreen,styleDots | styleNoLine|stylenorescale);

    PlotText("Peak "+ WriteVal(rr1,1.2), BarCount+2, SelectedValue(rr1),colororange,colorDefault,-4);
    PlotText("Valley "+ WriteVal(ss1,1.2), BarCount+2, SelectedValue(ss1),colorbrightgreen,colordefault,-4);

}


_SECTION_END();
PkVlyof9MA_final.png
 
Last edited:

josh1

Well-Known Member
@Happy_Singh; Thank you very much for your suggestion.
@josh1; Finally, below is the code. Hope I am right this time!. Thnx.
@yusi; Thank you for your last query on the subject. The "PivotLast - Use Last Pivots" toggle would answer that requirement.
Code:
_SECTION_BEGIN("Pivot Points of MA Hi and Lo");
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", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

_SECTION_BEGIN("MA");

MA9 = MA(C,9);
MA20 = EMA(MA( C, 9),20 );
Plot( MA9, "MA9", colorRed, styleline );
Plot( MA20, "MA20", colorbluegrey, styleline );

PeriodUp = MA9>MA20;
PeriodDn = MA9<MA20;


MAHi = HHV(MA9, PeriodDn); 
MALo = LLV(MA9,PeriodUp);


//Plot(MAHi, "",colorOrange, styledots | styleNoLine|stylenorescale) ;
//Plot(MALo, "",colorbrightgreen, styledots | styleNoLine|stylenorescale) ;

LB=9;//Param("Lookback Period",9,2,30,1);

HiMA2 = ValueWhen(IIf(MAHi AND BarsSince(MAHi) < LB,MAHi, Null), MAHi);
LoMA2 = ValueWhen(IIf(MALo AND Barssince(MALo) < LB, MALo, Null), MALo);


_SECTION_END();

_SECTION_BEGIN("Pivot High Low System");

//Below code is freely available on internet, modified for the requirement.

xx=BarIndex();x=xx;Lx=LastValue(x);
nbar=Param("N Pivot Bars",3,2,50,1);
npiv=Param("N Pivots lookback period",1,1,50,1);
PivotLast=ParamToggle("Use Last Pivots","Off|On",0);
markerStyle=ParamToggle("Marker Style [Last Bar]","Off|On",0);


if (PivotLast)

{

   fc=1;
    pk=HiMA2>Ref(HHV(HiMA2,nbar*fc),-1) AND Ref(HHV(HiMA2,nbar),nbar)<=HiMA2;
    pk=pk AND Lx-ValueWhen(pk,x)>nbar*fc;
    tr=L<Ref(LLV(LoMA2,nbar*fc),-1) AND Ref(LLV(LoMA2,nbar),nbar)>=LoMA2;
    tr=tr AND Lx-ValueWhen(tr,x)>nbar*fc;
}

else
{
    fc=2;
    pk=HiMA2>Ref(HHV(HiMA2,nbar*fc),-1) AND Ref(HHV(HiMA2,nbar),nbar)<=HiMA2;
    pk=pk AND Lx-ValueWhen(pk,x)>nbar*fc;
    tr=LoMA2<Ref(LLV(LoMA2,nbar*fc),-1) AND Ref(LLV(LoMA2,nbar),nbar)>=LoMA2;
    tr=tr AND Lx-ValueWhen(tr,x)>nbar*fc;

}

px0=ValueWhen(pk,x,0); tx0=ValueWhen(tr,x,0);
px1=ValueWhen(pk,x,1); tx1=ValueWhen(tr,x,1);
px2=ValueWhen(pk,x,2); tx2=ValueWhen(tr,x,2);
px3=ValueWhen(pk,x,3); tx3=ValueWhen(tr,x,3);
ph0=ValueWhen(pk,HiMA2,0); tl0=ValueWhen(tr,LoMA2,0);
ph1=ValueWhen(pk,HiMA2,1); tl1=ValueWhen(tr,LoMA2,1);
ph2=ValueWhen(pk,HiMA2,2); tl2=ValueWhen(tr,LoMA2,2);
ph3=ValueWhen(pk,HiMA2,3); tl3=ValueWhen(tr,LoMA2,3);

if(markerstyle)

{

miny=Status("axisminy");
maxy=Status("axismaxy");
for (i=1;i<=npiv;i++)

{
    rr=Ref(ValueWhen(pk,HiMA2,i),-nbar);
    rr=IIf(rr>maxy OR rr<miny,Null,rr);
    ss=Ref(ValueWhen(tr,LoMA2,i),-nbar);
    ss=IIf(ss>maxy OR ss<miny,Null,ss);
    Plot(rr,"",colororange,styleNoLine|styleDots,0,0,0); 
    Plot(ss,"",colorbrightgreen,styleNoLine|styleDots,0,0,0);

    PlotText("Peak "+ WriteVal(rr,1.2), BarCount+2, SelectedValue(rr),colororange,colorDefault,-4);
    PlotText("Valley "+ WriteVal(ss,1.2), BarCount+2, SelectedValue(ss),colorbrightgreen,colordefault,-4);

}
}

else

{
   rr=ValueWhen(pk,HiMA2);
   rr1=IIf(rr AND BarsSince(pk)>nbar,rr,Null);
   rr2=IIf(rr AND BarsSince(pk)<=nbar,rr,Null);
   ss=ValueWhen(tr,LoMA2);
   ss1=IIf(ss AND BarsSince(tr)>nbar,ss,Null);
   ss2=IIf(ss AND BarsSince(tr)<=nbar,ss,Null);
   Plot(rr1,"",colororange,styleDots|stylenoline|stylenorescale);
   Plot(rr2,"",colororange,styleDots|styleNoLine|stylenorescale);
   Plot(ss1,"",colorbrightgreen,styleDots|stylenoline|stylenorescale);
   Plot(ss2,"",colorbrightgreen,styleDots | styleNoLine|stylenorescale);

    PlotText("Peak "+ WriteVal(rr1,1.2), BarCount+2, SelectedValue(rr1),colororange,colorDefault,-4);
    PlotText("Valley "+ WriteVal(ss1,1.2), BarCount+2, SelectedValue(ss1),colorbrightgreen,colordefault,-4);

}


_SECTION_END();
View attachment 42050
Excllent......... :up: This is what I wanted.. Thank you very much.

@yusi - Yes. This will repaint the chart. However, I am going to use Peak and Valley of smaller average when in the next rally it crosses previous peak or in next fall it crosses previous trough. So re painting is accepted.
@Happy_Singh - Thank you for your suggestion
 
can somebody please check the below afl. whenever i am putting it on chart its compressed the chart vertically. it is possible to correct it ? please help
@primitivetrader; Your code on its own works perfectly fine without any vertical compression. Ensure there are no other plot codes in same afl without " |stylenorescale " expression. Thnx.
 
Dear @Happy_Singh , if it is not a very big thing, can you please paste an AFL that displays magnified time of the instrument?

This is not time left but the actual time.

Thank you a lot.
 
Dear @Happy_Singh , if it is not a very big thing, can you please paste an AFL that displays magnified time of the instrument?

This is not time left but the actual time.

Thank you a lot.
Do you want the cpu clock time or the time stamp of the selected candle?


.
 

Similar threads