I want to do "Only 1 trade per day" within the time period and "allow same bar exit".
By research, found that the exrem function with limitation can't perform same bar exit so use a for loop to replace the exrem function.
But after for loop used, the "Only1 trade per day" part can't function well, so seek help here
How to amend it ?
Many Thanks!
tn_cond = TimeNum() >= 40000;
new_day = tn_cond AND ! Ref(tn_cond,-1);
tn = TimeNum();
besttimestart = param( "BEST start ", 181500, 180000, 184500, 1500 );
besttimeclose = param( "BEST close ", 24500, 20000, 24500, 1500 );
startTime = besttimestart;
endTime = besttimeclose;
timeOK = tn >= startTime OR tn <= endTime;
//..................................................................................
MarketClose = IIf(timeOK,False,True);
tpbuff=param("Take Profit Buff",20,0,20,5);
ctbuff=Optimize("Cut Loss Buff",30,20,50,5);
//cutbuff
ma10= MA ( C, 10);
NN=1;
Buyeee = Ref(C,-2)<=Ref(ma10,-2) AND Ref(C,-1)>Ref(ma10,-1) AND timeOK;
Selleee = Ref(C,-1)<Ref(ma10,-1)- tpbuff ;
Shorteee = Ref(C,-2)>=Ref(ma10,-2) AND Ref(C,-1)<Ref(ma10,-1) AND timeOK ;
Covereee = Ref(C,-1)>Ref(ma10,-1) +tpbuff;
buyinprice=ValueWhen(Buyeee,O);
shortinprice=ValueWhen(Shorteee,O);
_Buy=Buyeee;
_Sell=Selleee OR L<buyinprice-ctbuff OR NOT timeOK;
_Short=Shorteee;
_Cover=Covereee OR H>shortinprice+ctbuff OR NOT timeOK;
SellPrice =IIf(L<buyinprice-ctbuff, (buyinprice -ctbuff), O);
CoverPrice=IIf(H>shortinprice+ctbuff, (shortinprice +ctbuff),O);
_Buy = _Buy AND Sum( _Buy OR _Short, BarsSince( new_day ) + 1 ) <= NN;
_Short = _Short AND Sum( _Short OR _Buy, BarsSince( new_day ) + 1 ) <= NN;
Buy = Sell = Short = Cover = Null;
LongFlag = ShortFlag = 0; //Simple Flag arrays to identify whether in a Long or Short position
//Using Loop to generate signals
for( i = 0; i < BarCount; i++ )
{
//Long Positions
if( _Buy[ i ] AND LongFlag == 0 )
{
Buy[ i ] = 1;
LongFlag = 1; //To record that we are in Long position
}
if( _Short[ i ] AND LongFlag == 1 )
{
Sell[ i ] = 1; //Selling-off the Long position
LongFlag = 0; //Reseting LongFlag back to False, to denote that we are no longer in "Long" position
}
//Short Positions
if( _Short[ i ] AND ShortFlag == 0 )
{
Short[ i ] = 1;
ShortFlag = 1; //To record that we are in Short position
}
if( _Buy[ i ] AND ShortFlag == 1 )
{
Cover[ i ] = 1; //Covering the Short position
ShortFlag = 0; //Reseting ShortFlag back to False, to denote that we are no longer in "Short" position
}
}