Enable trade only when RSI goes within a range

Strategy: Intraday buy when RSI > 70 and sell when RSI < 30 in its simplified form.

Requirement: The above trade should only get enabled when RSI comes between 30 and 70 on any new trading day. Basically, if my market starts at 0900 and RSI is already over 70 or under 30, no trade should be taken. Signals should get activated only when after 0900 RSI comes between 30 and 70 range.

How do I code the above condition for enabling requirement in AFL?

Thanks in advance.

Welcome to the Forum. You can get familiar by going through this post carefully.

1 Like

Your intention is unclear. Anyways, is this what you are looking for?

_SECTION_BEGIN( "RSI Cross" );
	 //Based on https://forum.amibroker.com/t/add-a-moving-average-to-volume-on-a-all-in-one-price-chart/583/8
	 SetChartOptions( 0, chartShowDates );
	 
	 RSIPaneHght = Param( "Percentage of RSI pane height (in %)", 20, 1, 100, 1 );
	 UprThres = Param( "RSI Upper Threshold", 70, 1, 100, 1 );
	 LwrThres = Param( "RSI Lower Threshold", 30, 1, 100, 1 );
	 arr = ParamField( "Price Field", 3 );
	 PerRSI = Param( "RSI", 14, 1, 252 );
	 
	 aRSI = RSIa( arr, PerRSI );
	 PaneHghtMin = LowestVisibleValue( aRSI );
	 PaneHghtMax = HighestVisibleValue( aRSI ) * 100 / RSIPaneHght;
	 
	 Buy = Cross( aRSI, 70 );
	 Short = Cross( 30, aRSI );
	 Cover = Buy;
	 Sell = Short;	 
	 
	 Plot( C, "Price", colorDefault, styleCandle );
	 
	 Plot( UprThres, "Upper Threshold", colorBlueGrey, styleDashed | styleOwnScale | styleNoLabel, PaneHghtMin, PaneHghtMax );
	 Plot( aRSI, "RSI", ColorRGB( 70, 160, 255 ), styleOwnScale | styleNoLabel, PaneHghtMin, PaneHghtMax, 0, 0, 2 );
	 Plot( LwrThres, "Lower Threshold", colorBlueGrey, styleDashed | styleOwnScale | styleNoLabel, PaneHghtMin, PaneHghtMax );
	 
	 PlotShapes( IIf( Buy, shapeSmallUpTriangle, shapeNone ), colorBrightGreen, 0, L, -12, 0 );
	 PlotShapes( IIf( Short, shapeSmallDownTriangle, shapeNone ), colorRed, 0, H, -12, 0 );
	 
	 Title = StrFormat( "{{DATE}}\nO %1.2f H %1.2f L %1.2f C %1.2f\nRSI(%1.0f) %1.2f", O, H, L, C, PerRSI, aRSI );
_SECTION_END();

Hope this piece of code would help you learn AFL. :slight_smile: