Im trying to create the following stop mechanism:
- Stop if Close > Limit Entry (on the same day as entry)
- If not same day as entry, exit trade if Close > Previous day Close
I tried covering my exits the following way:
BarsInTrade = 0;
for( i = 0 ; i < BarCount ; i++ )
{
if ( BarsInTrade > 0 )
{
BarsInTrade++;
if ( Name() == "TSLA" )
_TRACE(Name() + "(" + i + ") BarsInTrade: " + BarsInTrade[i] + " ");
}
else if (Buy[i] AND BarsInTrade == 0)
{
BarsInTrade = 1;
//if ( Name() == "TSLA" )
// _TRACE(Name() + "(" + i + ") BarsInTrade: " + BarsInTrade[i] + " ");
}
if ( BarsInTrade == 1 AND C[ i ] > BuyPrice[i])
{
if ( Name() == "TSLA" )
_TRACE(Name() + "(" + i + ") Sell1Bar: " + BarsInTrade + " Close: " + C[ i ] + " BuyPrice: " + BuyPrice[i] );
Sell[ i ] = 1;
BarsInTrade = 0;
}
else if ( BarsInTrade > 1 AND C[ i ] > C[ i - 1 ])
{
if ( Name() == "TSLA" )
_TRACE(Name() + "(" + i + ") SellMoreBars: " + BarsInTrade + " Close: " + C[i] + " Close(-1): " + C[i - 1]);
Sell[ i ] = 1;
BarsInTrade = 0;
}
}
When I look at the trace both exits are triggered correctly. However in my backtest there are still trades that should have exited the same bar ( Close > Limit Entry )