# Lowest price in an array

Hi everyone.
Please could anyone point me in the right direction.
I would just like to plot where the lowest Low was for a given up trend. Quite simple,but for some reason I'm struggling with this.

Thanks
Kevin

``````Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

MA_500 = MA(C, 500);
MA_up = MA_500 >  Ref(MA_500,-1);
MA_dn = MA_500 <  Ref(MA_500,-1);

Plot(MA_500,"",colorAqua,styleLine);

Lowest_price =MA_up AND LowestSince(MA_up,L);
Lowest_price =MA_up AND Lowest(L);
Lowest_price =MA_up AND ValueWhen(MA_up,LowestSince(MA_dn,L));
Lowest_price =MA_up AND LowestSinceBars(MA_up,L);
//Lowest_price =MA_up AND Valuewhen(MA_up , LowestSince(MA_up, L), 0);

PlotShapes(IIf(Lowest_price,shapeUpArrow,0),colorRed,0,LLV(L,20),-20);

``````

Similar to here

->

``````/// @link https://forum.amibroker.com/t/lowest-price-in-an-array/25361/2
MA_500 = MA(C, 500);
MA_up = Cross(MA_500, Ref(MA_500,-1));
MA_dn = Cross(Ref(MA_500,-1), MA_500);

is_lastbar = BarIndex() == LastValue(BarIndex());

dt = DateTime();
ll_dt = Ref(dt, -LowestSinceBars(MA_up,L));
valwhen_ll_dt = ValueWhen(MA_dn OR is_lastbar, ll_dt, 0);

Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
Plot(MA_500,"",colorAqua,styleLine);
PlotShapes((dt == valwhen_ll_dt) * shapeSmallSquare, colorRed, 0, L );
``````
2 Likes

Thanks for this @fxshrat .The similar code is great too, but couldn't change it to what I need. I see what you coded is actually what I asked for, but what I asked is incorrect. Apologies for that. It took me an hour to understand that...hehehe.
I'm looking for the Lowest trough that was made in an uptrend. I tried to create the trough, and add it to your code, with no success.

/// @link Lowest price in an array - #2 by fxshrat
MA_500 = MA(C, 500);
MA_up = Cross(MA_500, Ref(MA_500,-1));
MA_dn = Cross(Ref(MA_500,-1), MA_500);

Zig_Z = Zig(C,1.5);
TRS = Cross(Zig_Z,Ref(Zig_Z,-1));
Low_VAL = ValueWhen(TRS,L);
Low_VAL1 = ValueWhen(TRS==Low_VAL,Low_VAL);

is_lastbar = BarIndex() == LastValue(BarIndex());

dt = DateTime();
ll_dt = Ref(dt, -LowestSinceBars(MA_up,L));
//ll_dt = Ref(dt, -LowestSinceBars(MA_up,Low_VAL));

valwhen_ll_dt = ValueWhen(MA_dn OR is_lastbar, ll_dt, 0);

Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
Plot(MA_500,"",colorAqua,styleLine);
Plot(Zig_Z,"",colorAqua,styleLine);

PlotShapes((dt == valwhen_ll_dt) * shapeSmallSquare, colorRed, 0, L );
PlotShapes((Low_VAL1) * shapeSmallSquare, colorWhite, 0, L );

Hi everyone.
Please could i get some help on this issue, if possible? I'm trying to get the lowest zig up cross for a given up trend. Thank you in advance!

``````/// @link Lowest price in an array - #2 by fxshrat
MA_500 = MA(C, 500);
MA_cross_up = Cross(MA_500, Ref(MA_500,-1));
MA_cross_dn = Cross(Ref(MA_500,-1), MA_500);

MA_up = MA_500 > Ref(MA_500,-1);

Zig_Z = Zig(C,1.5);
TRS = Cross(Zig_Z,Ref(Zig_Z,-1)) AND MA_up;
Low_VAL = ValueWhen(TRS,L);
Low_VAL1 = ValueWhen(TRS==Low_VAL,Low_VAL);

//is_lastbar = BarIndex() == LastValue(BarIndex());
//LLVup = Valuewhen(MA_cross_up OR is_lastbar, LowestSince(MA_cross_dn, TRS), 0);
//PlotShapes(L == LLVup * shapeSmallSquare, colorWhite, 0, L );

Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
Plot(MA_500,"",colorAqua,styleLine);
Plot(Zig_Z,"",colorAqua,styleLine);

PlotShapes((TRS) * shapeSmallSquare, colorWhite, 0, L );
``````

Hi everyone,
Please could anyone help me, with this. I'm really struggling to plotshapes, for the lowest pivot (zig cross up),that forms when a new major peak forms. This is highlighted with the red dots and the blue shapes. I'm looking for the lowest pivot , when there is a major peak pivot, then when again when the next major peak pivot starts, etc etc etc. This can repaint, as in the 1st arrow, which is fine.
Please could someone, help, as I cannot seem to solve this. Thanks in advance!

``````/// @link Lowest price in an array - #2 by fxshrat
MA_500 = MA(C, 500);
MA_cross_up = Cross(MA_500, Ref(MA_500,-1));
MA_cross_dn = Cross(Ref(MA_500,-1), MA_500);

MA_up = MA_500 > Ref(MA_500,-1);
MA_dn = MA_500 < Ref(MA_500,-1);

Zig_Z       = Zig(C,1.5);
TRS         = Cross(Zig_Z,Ref(Zig_Z,-1)) AND MA_up;

major_peaks = Peak(Zig_Z,3);
New_peak    = Cross(major_peaks,Ref(major_peaks,-1)) OR Cross(Ref(major_peaks,-1),major_peaks);
vc          = Cum( New_peak ) - ValueWhen( MA_dn, Cum( New_peak ) )AND MA_up;

//Lowest_trough = ValueWhen(vc , LowestSince(New_peak,L));
//PlotShapes(IIf(Lowest_trough,shapeCircle,0),colorGreen,0,major_peaks,10);

Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

Plot(MA_500,"",colorAqua,styleLine);
Plot(Zig_Z,"",colorAqua,styleLine);
Plot(major_peaks,"",colorYellow,styleLine,0,0,0,0,4);

PlotShapes((TRS) * shapeSmallSquare, colorWhite, 0, L );
PlotShapes(IIf(New_peak,shapeCircle,0),colorRed,0,major_peaks,20);
PlotShapes(IIf(vc,shapeCircle,0),colorLightBlue,0,major_peaks,10);
``````

Hi everyone.
I'm still trying to get a solution to my problem, unfortunately. I have tried two versions as you can see in the code below, and still can't seem to get this. Basically, I'm looking for the lowest Pivot made, within the yellow horizontal line or blue dotted area.
Any assistance would be greatly appreciated. Thanks very much.

``````/// @link Lowest price in an array - #2 by fxshrat
MA_500 = MA(C, 500);
MA_cross_up = Cross(MA_500, Ref(MA_500,-1));
MA_cross_dn = Cross(Ref(MA_500,-1), MA_500);

MA_up = MA_500 > Ref(MA_500,-1);
MA_dn = MA_500 < Ref(MA_500,-1);

Zig_Z       = Zig(C,1.5);
TRS         = Cross(Zig_Z,Ref(Zig_Z,-1)) AND MA_up;
TRS_val     = ValueWhen(TRS,L) AND MA_up;

major_peaks   = Peak(Zig_Z,3);
New_peak      = Cross(major_peaks,Ref(major_peaks,-1)) OR Cross(Ref(major_peaks,-1),major_peaks);
vc            = Cum( New_peak ) - ValueWhen( MA_dn, Cum( New_peak ) )AND MA_up;
Peak_line     = major_peaks == Ref(major_peaks,-1);

Peak_Array               = Peak_line ;
ResetCondition           = Peak_line AND New_peak;
Peak_array_segment       = Sum( Peak_Array, BarsSince( ResetCondition ) + 1 );

/////version 1////

Lowest_pivot1             = ValueWhen(Peak_array_segment,LowestSince(Peak_array_segment,TRS_val));
//PlotShapes(IIf(Lowest_pivot1,shapeCircle,0),colorGreen,0,L,25);

////version 2/////

StartBar = New_peak;
EndBar = Ref(New_peak,1);
Lowest_pivot2 = ValueWhen( EndBar, LowestSince( StartBar, TRS_val ) );
//PlotShapes(IIf(Lowest_pivot2,shapeCircle,0),colorGreen,0,major_peaks,20);

Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
Plot(MA_500,"",colorAqua,styleLine);
Plot(Zig_Z,"",colorAqua,styleLine);
Plot(major_peaks,"",colorYellow,styleLine,0,0,0,0,4);

PlotShapes((TRS) * shapeSmallSquare, colorWhite, 0, L );
PlotShapes(IIf(New_peak,shapeCircle,0),colorRed,0,major_peaks,20);
PlotShapes(IIf(vc,shapeCircle,0),colorLightBlue,0,major_peaks,10);

``````

Hi everyone.
Okay latest version, with no luck as yet. Still trying to plot the lowest trough(zig cross) since a new peak has formed(red dots), in an uptrend (trailing long stop). I think I'm having issues with the startbar and endbar in the formula below.
Any assistance would be greatly appreciated.

``````function vstop_func(trBull,trBear)
{
trailArray[0]=C[0];
for(i=1;i<BarCount;i++)
{
prev=trailArray[i-1];

if(C[i]>prev AND C[i-1]>prev)
{
trailArray[i]=Max(prev,C[i]-trBull[i]);
}
else if(C[i]<prev AND C[i-1]< prev)
{
trailArray[i]=Min(prev,C[i]+trBear[i]);
}
else if (C[i]>prev)
{
trailArray[i]=C[i]-trBull[i];
}
else
{
trailArray[i]=C[i]+trBear[i];
}
}
return trailArray;
}
//////////////////////////////////////////////////////////////////
////////////////////////Short Term Trail/////////////////////////
ST_TRAIL=Param("ST Trail (hrs)",2,1,10,1)*60;
ST_TRAIL_1=in1Minute*ST_TRAIL;
TimeFrameSet(ST_TRAIL_1);
ST_Periods=Param("ST_periods",25,1,500,1);
ST_dev=Param("ST_deviation",2.5,0.05,4,0.05);
ST_trBull=ST_dev*ATR(ST_Periods);
ST_trBear=ST_dev*ATR(ST_Periods);
ST_trailArray = vstop_func(ST_trBull,ST_trBear);
ST_ts=IIf(ST_trailArray>C,ST_trailArray,Null);
ST_tl=IIf(ST_trailArray<C,ST_trailArray,Null);
TimeFrameRestore();
ST_ts=TimeFrameExpand(ST_ts,ST_TRAIL_1,expandLast);
ST_tl=TimeFrameExpand(ST_tl,ST_TRAIL_1,expandLast);
Plot(ST_ts , "", colorPink,0,0,0,0,0,3 );
Plot(ST_tl , "", colorPink,0,0,0,0,0,3 );

Zig_Z       = Zig(C,2);
TRS         = Cross(Zig_Z,Ref(Zig_Z,-1)) AND ST_tl;
TRS_val     = ValueWhen(TRS,L) AND ST_tl;

major_peaks   = Peak(Zig_Z,3);
New_peak      = Cross(major_peaks,Ref(major_peaks,-1)) OR Cross(Ref(major_peaks,-1),major_peaks);
Peak_line     = major_peaks == Ref(major_peaks,-1);

Up_peak        = Cross(major_peaks,Ref(major_peaks,-1));
dn_peak        = Cross(Ref(major_peaks,-1),major_peaks);

StartBar = up_peak;
EndBar = Ref(up_peak,1);
myL = ValueWhen( EndBar, LowestSince( StartBar, TRS_val ) ) AND ST_tl;
PlotShapes((myL ) * shapeSmallSquare, colorRed, 0, L );

//Next_up_peak = Ref(Up_peak,1) == ST_tl;
//is_lastbar   = BarIndex() == LastValue(BarIndex());
//dt = DateTime() ;
//ll_dt =Ref(dt, -LowestSinceBars(Up_peak,L));
////valwhen_ll_dt = ValueWhen(Next_up_peak OR is_lastbar, ll_dt, 0);
//valwhen_ll_dt = ValueWhen(Next_up_peak , ll_dt , 0);
//PlotShapes((dt == valwhen_ll_dt AND ST_tl ) * shapeSmallSquare, colorRed, 0, L );

Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
Plot(Zig_Z,"",colorAqua,styleLine,0,0,0,0,3);
Plot(major_peaks,"",colorYellow,styleLine,0,0,0,0,4);

//PlotShapes((TRS) * shapeSmallSquare, colorWhite, 0, L );
PlotShapes(IIf(New_peak,shapeCircle,0),colorRed,0,major_peaks,20);
PlotShapes(IIf(Peak_line,shapeSmallCircle,0),colorLightBlue,0,major_peaks,10);
``````

Hi everyone.
Please could I get some help on my above problem, it would be much appreciated!
Thanks again.

Have you tried the techniques described here: How do I debug my formula?

I think using an Exploration or the interactive Debugger to see all your intermediate values would probably be quite helpful.

You should use Trough() not Peak() function.

1 Like

Hi @mradtke , thanks for the reply. Thanks for the useful info. I may need to look into that. I have so many versions of this problem, I'm actually quite lost.
Thanks for the input @fxshrat, I went back to a previous formula of yours which is pretty close to what I need. I've tried 4 versions of this, from this method alone.Lines 36 -39. Following the logic, I really would have thought the 1st version would have worked
But I'm missing something. Sorry to be such a pain with this. Thanks so much!!!

``````_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", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

tmfrm = in1Minute*240;
TimeFrameSet(tmfrm);
ema1 = EMA(C, 10);
ema2 = EMA(C, 50);
TimeFrameRestore();

Zig_Z               = Zig(C,1.5);
Trough_up_cross     = Cross(Zig_Z,Ref(Zig_Z,-1));
Trough_up_cross_val = ValueWhen(Trough_up_cross,L);

Plot(Zig_Z,"",colorWhite,styleLine,0,0,0,0,3);
//PlotShapes((TRS) * shapeSmallSquare, colorWhite, 0, L );

ema1 = TimeFrameExpand(ema1, tmfrm, expandmode = expandLast);
ema2 = TimeFrameExpand(ema2, tmfrm, expandmode);

Plot(ema1,"ema1",colorAqua);
Plot(ema2,"ema2",colorYellow);

/// Plot HHV and LLV of upcross/dncross
upcrss = Cross( ema1, ema2 );
dncrss = Cross( ema2, ema1 );
is_lastbar = BarIndex() == LastValue(BarIndex());

/// Don't use for Backtesting!!!!

//Lowest_trough_uptrend = Valuewhen(HHVup OR is_lastbar, LowestSince(upcrss, Trough_up_cross_val), 0);
//Lowest_trough_uptrend = Valuewhen(ema1 < ema2 OR is_lastbar, LowestSince(ema1 > ema2, Trough_up_cross_val), 0);
Lowest_trough_uptrend = Valuewhen(dncrss OR is_lastbar, LowestSince(ema1 > ema2, Trough_up_cross_val), 0);

Plot( IIf(ema1 > ema2, HHVup, Null), "Price", colorGreen );
Plot( IIf(ema1 < ema2, LLVup, Null), "Price", colorRed );

Plot( C, "Price", colorDefault, styleCandle );
PlotShapes( (H == HHVup) * shapeSmallSquare, colorGreen, layer = 0, y = H, dist=12 );
PlotShapes( (L == LLVup) * shapeSmallSquare, colorRed, layer, y = L, -dist );
PlotShapes( (L == Lowest_trough_uptrend) * shapeSmallSquare, colorPink, 0,L);

``````

Hi everyone,
any ideas, on my problem above? @fxshrat I tried using trough, but can't seem to solve it? would you mind sharing the code of that screen shot, if possible.
Have a great day everyone!

Hi Kevin,
i dont't immediately have a solution for this but problem here is that when upcross occurs lowest trough value will be value from before upcross occured...

First of all, im new to programming and AB so that being said
don't think this is exactly what you want but it might help.
this code will plot first trough ( in uptrend), but if later on a lower trough forms it wil also be plotted.
So multiple troughs here are possible.

``````Trough_up_cross_val = Trough(c,2);
ltup=0;
for(i=0;i<BarCount-1;i++)
{

if(ema1[i]>ema2[i])
{
ltup[i]=LastValue(ValueWhen(Trough_up_cross_val[i]!=Trough_up_cross_val[i-1],Trough_up_cross_val[i]));  // get al trough values

if(ltup[i]==0 AND ltup[i-1]>0)
{
ltup[i]=ltup[i-1];                                      //filling up array avoiding zero's
}
if(ltup[i]>0 AND ltup[i-1]>0 )
{
ltup[i]=Min(ltup[i],ltup[i-1]);                          //if new trough is lower, overwrite
}
}
}

PlotShapes( (C == ltup) * shapeSmallSquare, colorBlue, 0,L);
``````

this will return the low of bar after trough, so not lowest point

hope this helped a little bit and was good exercise for me too.

Good luck !

While charts can be very helpful for visualization, I find that it's often easier to understand a problem by using an Exploration so that I can see all the important values in table form. Have you done that yet?

3 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.