Count Swing High in the past 200 days

Hi all.

I want to count how many unique swing high in the past 200 bars (lookbackperiod I've set before).

This is what my code

lookbackPeriod_daily = 200;
nbar = 5;

High_daily	= Ref(H,-1);
resistance_nbar_daily	=	HHV(Ref(H,-1),nbar);

swingHigh_cond_daily			= High_daily >= Ref(resistance_nbar_daily,-1) AND Ref(resistance_nbar_daily,nbar) < High_daily;
swingHigh_value_daily			= ValueWhen(swingHigh_cond_daily,High_daily);

firstBar = IIf(lookbackPeriod_daily == 0,0,Max(0,BarCount - 1 - lookbackPeriod_daily));

for(i = BarCount - 1; i > firstBar; i--)
{
	swingHigh_value_daily = 0;
	totalUniqueSwingHigh_daily = 0;
	
	if(swingHigh_value_daily[i-1] != swingHigh_value_daily[i])

		totalUniqueSwingHigh_daily = swingHigh_value_daily[i] + 1;
}

and the result still empty, eventhough there are enough bars.

![image|690x404](upload://xTr21Ax8psKfkI7aJ0XM9bSYCWI.png) 

Kindly need your help, guys.
Thanks

you could just use:

lookbackPeriod_daily = 200;
nbar = 5;

High_daily = Ref( H, -1 );
resistance_nbar_daily =	HHV( Ref( H, -1 ), nbar );

swingHigh_cond_daily = High_daily >= Ref( resistance_nbar_daily, -1 ) AND Ref( resistance_nbar_daily, nbar ) < High_daily;
nswinghighLastTwoHunderdDays = LastValue( Sum( swingHigh_cond_daily, lookbackPeriod_daily ) );
1 Like

Thanks alot @empottasch you save my day :grinning_face_with_smiling_eyes: :pray:

By the way @empottasch

if I want to count how many last value of "Close" greater than all unique swing high in the past 200 bars. how can I do that ?

If I just use code

swingHigh_value_daily			= ValueWhen(swingHigh_cond_daily,High_daily);

total_resistance_daily =  Sum(LastValue(C) > swingHigh_value_daily ,lookbackPeriod_daily);

It will count last "Close" price greter than all duplicate swingHigh_value.
How can I have an array that contains unique swing High value ? so that I can compare with last value of Close price

Thanks for your help

case

you can do that as follows:

n2 = LastValue( Sum( IIf( swingHigh_cond_daily AND LastValue( C ) > swingHigh_value_daily, 1, 0 ), lookbackPeriod_daily ) );
1 Like

Thanks alot @empottasch it's super clear. so the key is using Lastvalue right ? instead of creating new array contain unique value.

if you are only interested in the last value then this is the fastest way and you get the value for the last 200 bars. If you remove LastValue then each bar will contain the Sum over the previous 200 bars

I would advise if you are just starting out with coding to plot everything in the chart and also use the "Interpretation window" so you can output the value. Then you can see the results of you actions, see for example:

lookbackPeriod_daily = 200;
nbar = 5;

High_daily = Ref( H, -1 );
resistance_nbar_daily =	HHV( Ref( H, -1 ), nbar );

swingHigh_cond_daily = High_daily >= Ref( resistance_nbar_daily, -1 ) AND Ref( resistance_nbar_daily, nbar ) < High_daily;
swingHigh_value_daily			= ValueWhen( swingHigh_cond_daily, High_daily );

nswinghighLastTwoHunderdDays = LastValue( Sum( swingHigh_cond_daily, lookbackPeriod_daily ) );
"nswinghighLastTwoHunderdDays: "+ nswinghighLastTwoHunderdDays;

n2 = LastValue( Sum( IIf( swingHigh_cond_daily AND LastValue( C ) > swingHigh_value_daily, 1, 0 ), lookbackPeriod_daily ) );
"n2: "+ n2;
n2a = Sum( IIf( swingHigh_cond_daily AND LastValue( C ) > swingHigh_value_daily, 1, 0 ), lookbackPeriod_daily );
"n2a: "+ n2a;

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 1 );

PlotShapes( shapeSmallCircle * swingHigh_cond_daily, ColorRGB( 255, 0, 0 ), 0, H, 10 );

Plot( High_daily, "", colorAqua, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( resistance_nbar_daily, "", colorRed, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
1 Like

Thank for your help @empottasch will try it :slightly_smiling_face:

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