AFL Coding guidance in case Looping is in the code & others:

Hello Everyone,
Happy New Year. So I have been working on the below AFL couple of weeks now. This AFL works fine. But problem is-

  1. I want to use different conditions for Sell & Cover along with 0. But I couldn't. For example,
    if (H-L)>average(H-L) of all candle then sell/cover. Please tell me what to do here?
  2. Also I want to add one more condition for Buy & Short: if (H-L)<average(H-L) of all candle only then Buy or Short [along with if other conditions as per bellow AFL meet]
  3. I have tried a single trailing stops for both Buy & Short in the below afl. If you guys could tell me how to improve it would be really great.
// 15 Minute Time Frame:
TimeFrameSet(in15Minute);
BuyCondition1 = L == Ref(L,-1); BuyCondition2 = Ref(O,-1) != Ref(C,-1); BuyCondition3 = C > (H+L)/2; 
BuyCondition4 = C>50; BuyCondition5 = C<1500; 
TimeFrameRestore(); 

TimeFrameExpand(BuyCondition1,in15Minute); TimeFrameExpand(BuyCondition2,in15Minute); TimeFrameExpand(BuyCondition3,in15Minute); 
TimeFrameExpand(BuyCondition4,in15Minute); TimeFrameExpand(BuyCondition5,in15Minute); 

BuyCond1 = TimeFrameExpand(BuyCondition1,in15Minute) AND TimeFrameExpand(BuyCondition2,in15Minute) AND TimeFrameExpand(BuyCondition3,in15Minute) 
          AND TimeFrameExpand(BuyCondition4,in15Minute) AND TimeFrameExpand(BuyCondition5,in15Minute) ;
          
Sell = 0;
BuyPrice = ValueWhen(BuyCond1,Open);

TimeFrameSet(in15Minute);
ShortCondition1 = H == Ref(H,-1); ShortCondition2 = Ref(O,-1) != Ref(C,-1); ShortCondition3 = C < (H+L)/2; 
ShortCondition4 = C>50; ShortCondition5 = C<1500; 

TimeFrameRestore(); 

TimeFrameExpand(ShortCondition1,in15Minute); TimeFrameExpand(ShortCondition2,in15Minute); TimeFrameExpand(ShortCondition3,in15Minute); 
TimeFrameExpand(ShortCondition4,in15Minute); TimeFrameExpand(ShortCondition5,in15Minute);


ShortCond1 = TimeFrameExpand(ShortCondition1,in15Minute) AND TimeFrameExpand(ShortCondition2,in15Minute) AND TimeFrameExpand(ShortCondition3,in15Minute)
            AND TimeFrameExpand(ShortCondition4,in15Minute) AND TimeFrameExpand(ShortCondition5,in15Minute) ;
            
Cover = 0 ;
ShortPrice = ValueWhen(ShortCond1,Open); 



//Trailing Stoploss setting
StopLevel = Param("trailing stop %", 3, 0.1, 10, 0.1)/400;

trailARRAY = Null;
Buy = False; Short = False; Sell = False; Cover = False;
//Price Variable Assignation
Price_at_Buy = 0; Price_at_Short = 0; slhit = False;

for( i = 1; i < BarCount; i++ )
 {
   trailARRAY[ i ] = trailARRAY[ i - 1];
   if( Price_at_Buy == 0 AND BuyCond1[ i ] AND Price_at_Short == 0) //Checking for Short Entry if not in position
    { 
       trailARRAY[ i ] = High[ i ] * (1 - stoplevel);//trailing Stoploss creation
       Price_at_Buy = C[i];
       Buy[ i ] = True;
    }
    else if( Price_at_Short == 0 AND ShortCond1[ i ] AND Price_at_Buy == 0) //Checking for Short Entry if not in position
    { 
       trailARRAY[ i ] = Low[ i ] * (1 + stoplevel);//trailing Stoploss creation
       Price_at_Short = C[i];
       Short[ i ] = True;
    }
   if( Price_at_Buy > 0)
   {
		//Checking for Trailing Exit
	   if( Low[ i ] <= trailARRAY[ i-1 ] AND NOT Buy[i])
		{
		   Sell[ i ] = 4;//exit code for trailing Stoploss hit
		   SellPrice[ i ] = Min(O[i],trailARRAY[ i ]); //Exit Price with gapdown Handling
		   Price_at_Buy = 0;
		   slhit[i] = True;
		}
		//Updating Trailing if still in position
	   if( Price_at_Buy > 0)
		{   
		   trailARRAY[ i ] = Max( High[ i ] * (1 - stoplevel), trailARRAY[ i ] );
		}	
    }  
    //Short Entry 
    if( Price_at_Short > 0)
   {
		//Checking for Trailing Exit
	   if( High[ i ] >= trailARRAY[ i-1 ] AND NOT Short[i])
		{
		   Cover[ i ] = 4;//exit code for trailing Stoploss hit
		   CoverPrice[ i ] = Max(O[i],trailARRAY[ i ]); //Exit Price with gapUp Handling
		   Price_at_Short = 0;
		   slhit[i] = True;
		}
		//Updating Trailing if still in position
	   if( Price_at_Short > 0)
		{   
		   trailARRAY[ i ] = Min( Low[ i ] * (1 + stoplevel), trailARRAY[ i ] );  
		}
    } 
}

//Shape for Buy:
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40); PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50); 
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); PlotShapes(IIf(Buy, shapedigit1, shapeNone),colorWhite, 0,L, Offset=-80);
//Shape for Short:
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40); PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset= 50); 
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);PlotShapes(IIf(Short, shapedigit1, shapeNone),colorGold, 0,H, Offset=80);
//Shaping Buy & Short T.SL:
Plot(trailARRAY,"Trailing SL",colorAqua, styleLine | styleThick);
//Shaping Sell & Cover:
PlotShapes(IIf(Sell, shapeStar, shapeNone),colorGold, 0, L, Offset=-15); 
PlotShapes(IIf(Cover, shapeStar, shapeNone),colorWhite, 0,L, Offset=-15);

_SECTION_BEGIN("Chart Background Color,Time Grid,On Chart Description,Entry & Exit Bar,Quantity,Trade Time, Charting Style, Exchange Wise Entry & Exit Time");
SetFormulaName("Two High/Low Equal");
SetChartOptions(0,chartShowArrows|chartShowDates);
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
//Coder's Description on Technical Chart: 
GfxSetBkMode(1); X=750; Y=1; Font=7;
GfxSelectFont("Arial",Font*2, 550);GfxSetTextColor(colorYellow);GfxTextOut("Price Action Strategy 1 :",x,y);
GfxSelectFont("Arial",Font*2,550);GfxSetTextColor(colorWhite);GfxTextOut("Two High/Low Equal",x+200,y);
GfxSelectFont("Arial",Font*2, 550);GfxSetTextColor(colorYellow);GfxTextOut("Chart Window Screen Resolution :",x,y+30);
pxwidth = Status("pxwidth"); pxheight = Status("pxheight");
GfxSelectFont("Arial",Font*2, 550); GfxSetTextColor(colorWhite); GfxTextOut("("+pxwidth+","+pxheight+")",x+300,y+30);
GfxSelectFont("Arial",Font*2,True,True,550);GfxSetTextColor(colorBrightGreen);GfxTextOut("Developed by- Aritra K.Sinha",x,y+60);
GfxSelectFont("Arial",Font*2, 550);GfxSetTextColor(colorAqua);GfxTextOut("Email: aritranicknamearka@gmail.com",x+260,y+60);
//Bars, Entry & Exit Candle, Quantity:
SetBarsRequired(sbrAll,sbrAll); // AFL will backtest all available data.
SetPositionSize(1,spsShares); /*Money Management - How much to buy*/ SetTradeDelays(1,0,1,0); /*buy/short will take place in next candle after signal generation & Sell/Cover will take place very moment of signal generation .*/
PositionSize = -100; // invest 100% of portfolio equity in single trade
_SECTION_END();

Thanks in advance. :smiley: