[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] Re: Looping Question for Bar Count



PureBytes Links

Trading Reference Links

You're welcome.

Please find below a formula that can drive you on a good track to 
calculate percentage.

I'am looking forward your final version. It' s a good idea.

Best regards

_SECTION_BEGIN("ZigZag Retracement");
function GetXSupport(Lo, Percentage, Back)
{
return ((BarCount - 1) - LastValue(TroughBars(Lo, Percentage,Back)));
}
function GetYSupport(Lo, Percentage, Back)
{
return (LastValue(Trough(Lo, Percentage, back)));
}

function GetXResistance(Hi, Percentage, Back)
{
return ((BarCount - 1) -LastValue(PeakBars(Hi, Percentage, Back)));
}
function GetYResistance(Hi, Percentage, Back)
{
return (LastValue(Peak(Hi, Percentage, Back)));
}

//////////////////////////////////////////////////////////////////
Per = Param("Period", 0.44, .1, 20, .001);
Period = Param("Look back", 10, 1, BarCount-1);
ShowRet = ParamToggle("Show Retracement values", "No|Yes",1);
Price = ParamList("Price to follow:", "Close|High|Low", 0);
if(Price=="Close") ZigP = Zig(C, per);
else if(Price=="High") ZigP = Zig(H, per);
else ZigP = Zig(L, per);

//////////////////////////////////////////////////////////////////
Plot(C, "", IIf(O>=C, colorDarkRed, colorGreen), ParamStyle("Price
Style",styleBar,maskPrice));
Plot(ZigP, "Zig", colorGold, styleThick);
//////////////////////////////////////////////////////////////////

xs1 = GetXSupport(ZigP, .01, 1);
xr1 = GetXResistance(ZigP, .01, 1);
ys1 = GetYSupport(ZigP, .01, 1);
yr1 = GetYResistance(ZigP, .01, 1);

if(xs1 < xr1)
{
x = LineArray(xs1, ys1, BarCount - 1, LastValue(ZigP));
Down = (yr1 - LastValue(ZigP)) / (yr1 - ys1);
DnBars = BarCount - 1 - xr1;
Plot(x, "", colorRed, styleDots);
PlotText(StrFormat("%.3f (%.0f)", Down, DnBars), (xs1 + BarCount -1)/2,
(ys1+LastValue(ZigP))/2, colorWhite);
}
else
{
x = LineArray(xr1, yr1, BarCount - 1, LastValue(ZigP));
Up = (LastValue(ZigP) - ys1) / (yr1 - ys1);
UpBars = BarCount - 1 - xs1;
Plot(x, "", colorRed, styleDots);
PlotText(StrFormat("%.3f (%.0f)", Up, UpBars), (xr1 + BarCount -1)/2,
(yr1+LastValue(ZigP))/2, colorWhite);
}
Plot( 1, "", IIf( xs1 > xr1, colorGreen,
colorRed),styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );
if(ShowRet)
for(i=2; i<=Period+1; i++)
{
xs0 = GetXSupport(ZigP, .01, i);
xs1 = GetXSupport(ZigP, .01, i-1);
ys0 = GetYSupport(ZigP, .01, i);
ys1 = GetYSupport(ZigP, .01, i-1);

xr0 = GetXResistance(ZigP, .01, i);
xr1 = GetXResistance(ZigP, .01, i-1);
yr0 = GetYResistance(ZigP, .01, i);
yr1 = GetYResistance(ZigP, .01, i-1);

xs = LineArray(xs0, ys0, xs1, ys1, 0);
Plot(xs, "", colorGold, styleLine|styleDashed);
xr = LineArray(xr0, yr0, xr1, yr1, 0);
Plot(xr, "", colorGold, styleLine|styleDashed);
if(xs1 < xr1)
{
Up = (yr1 - ys1) / (yr0 - ys1);
Down = (yr0 - ys1) / (yr0 - ys0);
UpBars = xr1 - xs1;
DnBars = xs1 - xr0;
}
else
{
Up = (yr1 - ys0) / (yr0 - ys0);
Down = (yr1 - ys1) / (yr1 - ys0);
UpBars = xr1 - xs0;
DnBars = xs1 - xr1;
}
PlotText(StrFormat("%.3f (%.0f)", Up, UpBars), (xr1 + 
xr0)/2,(yr1+yr0)/2,colorWhite);
PlotText(StrFormat("%.3f (%.0f)", Down, DnBars), (xs1 + 
xs0)/2,(ys1+ys0)/2,colorWhite);
//Plot(LineArray(xs0, ys0, BarCount-1, ys0), "", colorGreen,styleDashed);
//Plot(LineArray(xr0, yr0, BarCount-1, yr0), "", colorRed, styleDashed);

}

str = StrFormat(" (Bars to END=%.0f)\n", BarCount - 1 - BarIndex());
Title =FullName()+" ("+Name()+") - "+Date()+" - Open: "+O+", Hi:
"+H+", Lo:
"+L+", Close: "+C+StrFormat(" (%.2f %.2f%%)", C-Ref(C, -1),
SelectedValue(ROC(C, 1)))+str;
WriteIf(1, "\nNote Fibonacci numbers:\nPrimary numbers: 0.618, 0.786,
1.27 AND
1.618","");
WriteIf(1, "Secondary numbers: 0.382, 0.50, 1.00, 2.00, 2.24, 2.618 and
3.14","");

_SECTION_END();

wooziwog a écrit :
>
> Thanks Reinsley - I worked with what you provided and was able to get
> HH - LL and H-L bar counts. I see someone else has expanded the code
> so I am adding to their improvements. Now I am trying to figure out
> how to calculate the percentage price change between pk and tr. The
> final part will be to properly place the text labels so they do not
> stack on top of each other.
> Thanks again for your code savvy.
>
> David K.
>
> //Addition Price Selection (H, L, C,or pk H and tr L)
> //added Pivot Prices
> //Additons, Changes and Corrections to Reinsley Code;
>
> //Peak-Peak,Trough-Trough, Peak-Trough and Trough-Peak Count
> Price = ParamList("Price to follow:", "Close|High|Low|zHiLo", 3);
> pct = Param( "Pivot %", 5, 0.10, 60, 0.10 );
> ATRPar = Param ( "ATRPar Text Shift", 0.20, 0, 30, 0.10 );
> ppr = ParamToggle("Pivot Price Labels","Off|On",0);
> betweenfromsel = ParamToggle("Betw Pk/Tr|FromTo Pk/Tr","Betw Pk-Pk
> and Tr-Tr|From Pk-Pk and Tr-Tr",1);
> textoffset=Param("textoffset",0.12,0.001,5,0.001);
> //textoffset=Param("textoffset",0.001,0.0001,0.02,0.0001);//Forex
> ////////////////////
> SetBarsRequired( 999999,999999);
> pk=PeakBars(H,pct)==0; tr=TroughBars(L,pct)==0;
> zHi=Zig(H,pct); zLo=Zig(L,pct); HLAvg=(zHi+zLo)/2;
> zp=IIf(pk,zHi,IIf(tr,zLo,IIf(HLAvg>Ref(HLAvg,-1),H,L)));
> ////////////////////
>
> if(Price=="Close") P=Zig(C,pct);
> else if(Price=="High")P=Zig(H,pct);
> else if(Price=="Low") P=Zig(L,pct);
> else P=Zig(zp,pct);
>
> pR=Ref(P,-1)<P AND P>Ref(P,1);
> pS=Ref(P,-1)>P AND P<Ref(P,1);
> ////////////////////////////////////////////////
> pkdetect = PeakBars(P, pct) == 0;
> trdetect = TroughBars(P, pct) == 0;
>
> pk = PeakBars(P, pct) ;
> tr = TroughBars(P, pct) ;
>
> txtH = pk + ATRPar*ATR( 2 ); //changed to ATRPar for descriptiveness
> txtL = tr + ATRPar*ATR( 2 ); //changed to ATRPar for descriptiveness
>
> for ( i = 0; i < BarCount; i++ )
> {
> if ( pkdetect [i]) //Peak detection
> {
> PlotText( "" + (pk [i-1]+betweenfromsel) + " Bars(Pk-Pk)", i, H [i] +
> txtH[i], colorRed ); //bars "from/between" the peaks
> PlotText( "" + (tr [i]-pk [i]-1+betweenfromsel) +" Bars(Tr-Pk)", i, H
> [i] + txtH[i]+textoffset, colorRed );//bars "from/between"
> trough "to" peak
> }
> if ( trdetect [i]) //Trough detection
> {
> PlotText( "" + (tr [i-1]+betweenfromsel) + " Bars(Tr-Tr)", i, L [i] -
> txtL[i], colorGreen); //bars "from/between" the troughs
> PlotText( "" + (pk [i]-tr [i]-1+betweenfromsel) + " Bars(Pk-Tr)", i,
> L [i] - txtL[i]-textoffset, colorGreen); //bars "from/between"
> peak "to" trough
> }
> if(ppr==1)
> {
> if(pR [i]) PlotText("" + H [i] + " Hi", i, H [i] + txtH[i]-
> textoffset, colorRed);
> if(pS [i]) PlotText("" + L [i] + " Lo", i, L [i] - txtL[i]
> +textoffset, colorGreen);
> }
> }
>
> zC = Zig(P, pct);
> bcol = IIf( C > O, colorGreen, IIf( C < O, colorRed, colorBlack));
> Plot( zC, "", 2, 1 | styleNoLabel );
> Plot( C, "Peak-Peak and Trough-Trough Count", bcol, 64 );
>
> GraphXSpace = 5;
>
> --- In amibroker@xxxxxxxxxxxxxxx <mailto:amibroker%40yahoogroups.com>, 
> reinsley <reinsley@xxx> wrote:
> >
> > Hello,
> >
> > Adapted from your code. The count is from peak to peak and from
> trough
> > to trough.
> > You can easily adapt to see from peak to trough and reverse.
> > Hope this helps.
> >
> > Best regards
> >
> >
> >
> > //Bar count Pk To Pk and Tr to Tr
> >
> > pct = Param( "Pivot %", 0.1, 0.10, 60, 0.10 );
> > vs = Param ( "Pivot $ V Shift", 1.50, 0, 30, 0.10 );
> >
> > pkdetect = PeakBars( C, pct ) == 0;
> > trdetect = TroughBars( C, pct ) == 0;
> >
> > pk = PeakBars( C, pct ) ;
> > tr = TroughBars( C, pct ) ;
> >
> > txtH = pk + vs * ATR( 2 );
> > txtL = tr + vs * ATR( 2 );
> >
> > for ( i = 0; i < BarCount; i++ )
> > {
> > if ( pkdetect [i] )
> > PlotText( "" + pk [i-1] + " Bars", i, H [i] + txtH[i], 25 );
> >
> > if ( trdetect [i] )
> > PlotText( "" + tr [i-1] + " Bars", i, L [i] - txtL[i], 42 );
> > }
> >
> > zC = Zig( C, pct );
> >
> > bcol = IIf( C > O, colorGreen, IIf( C < O, colorRed, colorWhite ) );
> > Plot( C, "", bcol, 128 );
> > Plot( zC, "", 45, 1 | styleNoLabel );
> >
> > GraphXSpace = 15;
> >
> >
> > wooziwog a écrit :
> > >
> > > I am trying to label the barcounts between each peak and trough
> using
> > > a loop. The problem I have is that the most recent bar count
> between
> > > the peak and trough appears as the bar count between all peaks and
> > > troughs. I have tried a number of ways to fix the problem but have
> > > not had any success. Any help in solving this puzzle will be
> greatly
> > > appreciated.
> > >
> > > Thanks,
> > >
> > > David K.
> > >
> > > ///Bar Count
> > >
> > > bi = SelectedValue(BarIndex());
> > > sbi = BarIndex();
> > > x1=BarCount-1;
> > > bcol=IIf(C>O,colorLime,IIf(C<O,colorRed,colorWhite));
> > > Plot(C,"",bcol,128);
> > > dec = (Param("Decimals",2,0,7,1)/10)+1;
> > > ////////////////////
> > > _SECTION_BEGIN("Bar Count");
> > > pct=Param("Pivot %",0.80,0.10,60,0.10);
> > > Zigl = ParamToggle("Zig Line","Off|On",1);
> > > hLb=Param("High Look Back",1,1,30,1);
> > > lLb=Param("Low Look Back",1,1,30,1);
> > > vs = Param ("Pivot $ V Shift",1.50,0,30,0.10);
> > > ////////////////////
> > > SetBarsRequired( 999999,999999);
> > > pk=PeakBars(H,pct)==0; tr=TroughBars(L,pct)==0;
> > > zHi=Zig(H,pct); zLo=Zig(L,pct); HLAvg=(zHi+zLo)/2;
> > > zp=IIf(pk,zHi,IIf(tr,zLo,IIf(HLAvg>Ref(HLAvg,-1),H,L)));
> > > za=Zig(zp,pct);
> > > if(Zigl==1)Plot(za,"",45,1|styleNoLabel);
> > > ////////////////////
> > > SetBarsRequired( 999999,999999);
> > > pk=PeakBars(H,pct)==0;
> > > tr=TroughBars(L,pct)==0;
> > > zH=Zig(H,pct); zL=Zig(L,pct); HLAvg=(zH+zL)/2;
> > > zp=IIf(pk,zH,IIf(tr,zL,IIf(HLAvg>Ref(HLAvg,-1),H,L)));
> > > za=Zig(zp,pct);
> > > ////////////////////
> > > if(Zigl==1)Plot(za,"",45,1|styleNoLabel);
> > > PlotShapes(shapeDownArrow*pk,colorBrightGreen,0,H,-15);
> > > PlotShapes(shapeUpArrow*tr,colorYellow,0,L,-15);
> > > ////////////////////
> > > pR=Ref(za,-1)<za AND za>Ref(za,1);
> > > xr0=SelectedValue(ValueWhen(pR,sbi,hLb));
> > > yr0=SelectedValue(ValueWhen(pR,zp,hLb));
> > > ////////////////////
> > > pS=Ref(za,-1)>za AND za<Ref(za,1);
> > > xs0=SelectedValue(ValueWhen(pS,sbi,lLb));
> > > ys0=SelectedValue(ValueWhen(pS,zp,lLb));
> > > ////////////////////
> > > bcount = abs(xr0-xs0);
> > > bc=LastValue(bcount,1);
> > > txtH = pk + vs*ATR(2);
> > > txtL = tr + vs*ATR(2);
> > > ////////////////////
> > > for( i = 0; i < BarCount; i++ )
> > > {
> > > if(pR [i]) PlotText("" + bc[i] +" Bars", i, H [i] + txtH[i], 25);
> > > if(pS [i]) PlotText("" + bc[i] +" Bars", i, L [i] - txtL[i], 42);
> > > }
> > > _SECTION_END();
> > >
> > > Title = EncodeColor(55)+ Title = Name() + " " + EncodeColor(55)
> > > + Date() + " " + EncodeColor(3) + "{{INTERVAL}} " +
> > > EncodeColor(55)+ " Open = "+ EncodeColor(10)+ NumToStr(O,dec) +
> > > EncodeColor(55)+ " High = "+ EncodeColor(43)+ NumToStr(H,dec) +
> > > EncodeColor(55)+ " Low = "+ EncodeColor(32)+ NumToStr(L,dec) +
> > > EncodeColor(55)+ " Close = "+ EncodeColor(55)+ NumToStr(L,dec) +
> > > EncodeColor(55)+ " xs0 = "+ EncodeColor(43)+ NumToStr(xs0,1.0) +
> > > EncodeColor(32)+ " xr0 = "+ EncodeColor(32)+ NumToStr(xr0,1.0) +
> > > EncodeColor(55)+ " ys0 = "+ EncodeColor(43) + NumToStr(ys0,1.2) +
> > > EncodeColor(32)+ " yr0 = "+ EncodeColor(32)+ NumToStr(yr0,1.2);
> > > GraphXSpace=10;
> > >
> > >
> >
>
>  



------------------------------------

**** IMPORTANT ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

*********************
TO GET TECHNICAL SUPPORT from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com
*********************

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html

*********************************
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/