I am using OnBarUpdate executions.
I am experiencing situations that my strategy enters position even if the market position is not flat even though I clearly state that position has to be flat in the code.
Plase see attached picture taken from the strategy performance statistics. You can see that strategy enters new long position and at the same day closes previous position which should not happen since in live trading the OnBarUpdate is triggered only at the beginning of the trading day.
Expected behavior: when the trading day starts it checks whether the market position is flat. If not, then nothing happens. If the positions is flat it goes through all the possible symbols and if one of them triggers the entry rule then long position is entered
Below you can see also sample of my code:
protected override void OnBarUpdate()
{
//CTXS
if (Position.MarketPosition == MarketPosition.Flat
&& BarsInProgress == 0
&& High[2] > High[1] && High[1] > Close[2] && Close[2] > Low[2] && Low[2] > High[0] && High[0] > Close[1] && Close[1] > Close[0] && Close[0] > Low[0] )
{
EnterLong(DefaultQuantity, "BUY CTXS");
}
//LIFE
else if (Position.MarketPosition == MarketPosition.Flat
&& BarsInProgress == 1
&& High[2] > High[1] && High[1] > Close[2] && Close[2] > Low[2] && Low[2] > High[0] && High[0] > Close[1] && Close[1] > Close[0] && Close[0] > Low[0] )
{
EnterLong(DefaultQuantity, "BUY LIFE");
}
and then the code goes on and on for other symbols.
Is there any way to prevent the strategy from entering and leaving positions on same day? Because this way it blocks the strategy from executing live orders (getting yellow tab for multiple consecusive trades as seen in attached picture).
Thank you for help.
Comment