# Get Nearest Swing High Value Above Last Close (Daily)

Hi.

I want to get nearest value (by distance) of swingHigh in the last 40 bars daily comparing with last value of close daily.

I already read and test the code on below topic, but still didn't success.

https://forum.amibroker.com/t/nearest-above-and-nearest-below-price/2826/7

here is what I try to code

``````lookbackPeriod_daily = 40;
high_daily = Ref(H,-1);
close_daily = Ref(C,-1);
firstBar_daily = IIf(lookbackPeriod_daily == 0,0,Max(0,BarCount - 1 - lookbackperiod_daily));
nearest_SwingHigh_value_daily = 1e9;

for(n=firstbar_daily; n < BarCount - 1; n++)
{

if(swingHigh_cond_daily[n] AND NOT IsNull(swingHigh_value_daily[n]))
{
distance_close_daily_swingHigh_daily = High_daily[n] - LastValue(close_daily);

swingHigh_above_close_daily = IIf(distance_close_daily_swingHigh_daily > 0, High_daily[n] ,1e9);
//  swingHigh_below_close_daily = IIf(distance_close_daily_swingHigh_daily < 0, -High_daily[n],1e9);

nearest_SwingHigh_above_value_daily = Min(nearest_SwingHigh_value_daily,swingHigh_above_close_daily);
//  nearest_swingHigh_below_value_daily = Min(nearest_SwingHigh_value_daily,swingHigh_below_close_daily);

Plot(swingHigh_above_close_daily[n],"SwingHigh_"+n,colorRed);
Plot(distance_close_daily_swingHigh_daily[n],"Distance_"+n,colorGreen);

}

}
``````

I already use Min function to compare the distance of each array and
the problem is, I always get value of "2,700" (higher value) instead of "2,490" (nearest value)
please take alook at picture below

Kindly need your help guys, thanks alot

I made an update the code into this

``````firstBar_daily = IIf(lookbackPeriod_daily == 0,0,Max(0,BarCount - 1 - lookbackPeriod_daily));
nearest_SwingHigh_value_daily = 1e9;

for(n=firstbar_daily; n < BarCount - 1; n++)
{
if(swingHigh_cond_daily[n])
{
VarSet("SwingHigh_value_daily_"+n,High_daily[n]);

for(i = 1; i<=totalUniqueSwingHigh_daily; i++) {

x = VarGet("SwingHigh_value_daily_"+n);
distance_close_daily_swingHigh_daily = x - LastValue(Close_daily);

swingHigh_above_close_daily = IIf(distance_close_daily_swingHigh_daily > 0, High_daily[i],1e9);

}
nearest_swingHigh_above_value_daily = Min(nearest_SwingHigh_value_daily,distance_close_daily_swingHigh_daily);
Plot(x,"SwingHigh_"+n,colorRed);

}

}
``````

but still not get the right nearest value.

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