How to Close Position by "specific time"

Hi, Friends
I would like to close position by variable "Market Close"
When I use the function of Exrem, it can function properly
But when i use for loop, it only stop but not close position.

I can't figure out which part cause the cover/sell can't function properly, so seek help,
Many thanks!

``````
new_day = tn_cond AND ! Ref(tn_cond,-1);

besttimestart = param( "BEST start ", 181500, 180000, 184500, 1500 );
besttimeclose = param( "BEST close ", 15500, 20000, 24500, 1500 );

startTime = besttimestart; // Default 182000 is using start in HHMMSS format
endTime = besttimeclose;  // end in HHMMSS format

timeOK = tn >= startTime OR tn <= endTime;
//..................................................................................
MarketClose = IIf(timeOK,False,True);

_Sell=Ref(C,-1)<=Ref(C,-2) OR   MarketClose;

_Short=Ref(C,-1)<=Ref(C,-2) AND NOT MarketClose;
_Cover=Ref(C,-1)>Ref(C,-2) OR  MarketClose;

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 )
{
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
}
}

``````

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.