How to control session exits and disables

I have code that uses the session iterator to determine if trading should continue or exit. I’m day trading, so I need to exit my trades before the end of session of each day. I’m able to successfully do that but it always disables my strategy. I only want it to disable on Fridays. I have IsExitOnSessionCloseStrategy = false. The code for closing my trades are:

if (Position != null
&& Position.MarketPosition != MarketPosition.Flat)
Position.Close(“Exiting active position at market”);
cancelAllOrders(); // where I close any orders that are not null

This same code is also performed if (State == State.Terminated)

Is there anything I can do to control when the strategy is disabled?

Assuming you have already called GetNextSession()

if (sessionIterator.ActualSessionEnd.DayOfWeek == DayOfWeek.Friday)
{ 
}

If you want to keep the strategy on, but disable it from trading then you can just add in a flag to only trade within a certain time. For example, only around 0930 to 1600.

1 Like

I’m not seeing anything from what you wrote that would actually disable the strategy. I’m not sure if there is code for that, but I could be wrong. When it disables, is there an error message? Once it gets disabled, the only way to enable it again is to manually click it and turn it back on. So you wouldn’t want it to be disabled, and like Walee said, you could make a condition for the times you WANT it to trade.

As always, make sure you test the strategy over and over to make sure there are no bugs and unwanted effects before trading live.

Just a guess but he’s using Position.Close()

This is not documented as part of the managed or indeed unmanaged strategy order methods. Therefore I believe it’s simply acting like the ‘close’ button is pressed, which will disable the strategy.

Try changing to exitLong/Short with Position.Quantity

1 Like

Ah. Never would have thought they have a button that would simulate the close button. Why would you have that and not a way to automate turning your strategy on?

I use managed mode, and i close everything at a certain time with ExitLong and ExitShort, my strategies stay enabled…i even have the backup in Ninjatrader settings just incase my strategy was not invoked at all due to low liquidity (no ticks coming in) and even then as long as the strategies have no open positions, it does not disable the strategy…but if the strategy had an open position then that strategy would get disabled if ninjatrader closes the position due to my EOD setting. Not sure how ninjatrader treats the closure of limit and stop orders, in my case i only use enter and exit API and i literally have my ninjatrader running with no issues for the entire week.

1 Like

agreed. i have mine set to trade from 0945 - 1545hrs