# Breakout from custom range interval

Hello,

I am trying to build a code that opens a position on breakout high of previous range if the range was smaller than previous range. Range = custom number of days.

Here is an example: On the example the range would be constructed from high-low of 5 days.

I did tried to use TimeFrameSet using following code to define the Range, but it is obviously not working:

``````_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

RangeInterval=5*inDaily;

TimeFrameSet(RangeInterval);
Range5D_High	 =	H;
Range5D_Low	 =	L;
Range5D = H-L;
MinRange5D = 	Range5D < Ref(Range5d,-1);
TimeFrameRestore();

Plot (TimeFrameExpand(Range5D_High,RangeInterval,expandLast),"Range5D_High",colorBlack);
Plot (TimeFrameExpand(Range5D_Low,RangeInterval,expandLast),"Range5D_High",colorRed);
``````

Should I use loops? Or what is the optimal solution for this?
Thank you for any tips.

@peterfin I think there are probably many examples if you look around the internet. One way you can define your price range by finding the top and bottom like this,

``````RangePeriods = Param( "Range Periods", 5, 2, 50, 1 );

RangeTop = Ref( HHV( H, RangePeriods ), -1 );

RangeBottom = Ref( LLV( L, RangePeriods ), -1 );

Buy = Cross( C, RangeTop );
Sell = Cross( RangeBottom, C );