Need help to solve error in intraday AFL

#1
This is intraday trading strategy AFL,which open 4 trades at opening bell based on ROC and position score. (I am using 15 min. time frame)

It seems that there is error in reversetrade code,When freshbuy signal generate and trade get open ..the SL is buycandle's low..at where i want to go short if sl triggers.. else will close the trade at closing bell..

Error: under portfolio backtesting, i am facing 3 problem

-Reversetrade signals generates by considering any previous fresh trade's low/high crossover.while valid crossover is fresh buy/short must be in current day.

-How to code this afl so that under portfolio backtest,Reverse trade take place only in those stock are selected and traded current day with fresh buy/fresh short signal.

-at closing bell i need to close all active trade,But this AFL open new 4 trade at a time of closing bell and close the trade next day.not getting where m i making mistake.

it would be great help if someone can guide me to solve errors

HTML:
FreshTradeTime=TimeNum() >= 091500 AND TimeNum() <= 094500 ;
ReverseTradeTime=TimeNum() >= 091500 AND TimeNum() <= 144500;
CloseALL = TimeNum() >=152500;	

FreshBuy=ROC(C,1)> 1;
FreshShort=ROC(C,1)< -1;
ReverseBuy=  Cross(C,ValueWhen(FreshShort,H));
ReverseShort=Cross(ValueWhen(FreshBuy,L),C);

Buy = FreshBuy OR ReverseBuy;
BuyStop=IIf(FreshBuy, Cross(ValueWhen(FreshBuy,L),C),Cross(ValueWhen(FreshShort,L),C)); 
Sell= BuyStop==1 OR CloseALL;

Short=  FreshShort OR ReverseShort ;
ShortStop=IIf(FreshShort, Cross(C,ValueWhen(FreshShort,H)),Cross(C,ValueWhen(FreshBuy,H)));
Cover= ShortStop==1 OR CloseALL ;

BuyPrice=IIf(FreshBuy,ValueWhen(FreshBuy,C),ValueWhen(FreshShort,H));
ShortPrice=IIf(Freshshort,ValueWhen(Freshshort,C),ValueWhen(Freshbuy,L));


Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);

/////////////////////////
e= 100000;
PositionSize= -25;
PositionScore=abs(ROC(C,1));
SetOption("MaxOpenPositions", 4 );
 

Similar threads