View Full Version : Strategy did not close a position after overnight
ddurocher
05-27-2010, 07:52 AM
I am testing a strategy in a sim account. Yesterday the strategy entered a long position. This morning, based on the strategy it should have exited this long position but did not do so.
If it makes any difference I made some code changes to the strategy last night.
Today I am running the strategy against 5 instruments including the one it entered the position on yesterday. However in the strategies window once started the strategy it did not show any of the information for this instrument like position,avg price etc. I'm new to using the strategies but expected I would have seen this informtion as I had an open position.
Where should I be looking?
Using 6.5
Thanks,
David
NinjaTrader_Bertrand
05-27-2010, 08:01 AM
David, as first step when testing and working with NS strategies, please ensure the strategy vs account position in sync as needed - http://www.ninjatrader-support.com/HelpGuideV6/StrategyPositionVsAccountPosition.html
Next, it would be helpful to add print statements or visual confirmations to your strategy code, this way you would know if a condition indeed evaluated to true or not -
http://www.ninjatrader.com/support/forum/showthread.php?t=3418
Also as you started up the strategy, was ExitOnClose for example activated? This would mean there would be no historical position to exit, as it was closed at the session end time you've setup in the Chart Properties.
http://www.ninjatrader-support.com/HelpGuideV6/ExitOnClose.html
ddurocher
05-27-2010, 09:17 AM
No, ExitOnClose was not selected, either yesterday or this morning when I started the strategy.
Since I opened NT this morning, my positions tab shows I'm long 150 units, the same 150 units the strategy entered into yesterday. When I started the strategy this morning on the same instrument, the strategy does not show that there is any position at all. Let me explicitly ask this time, should the strategy be showing the position from the previous day when I open it the next morning? I've never done this before so while I expected that it should have, I don't know for sure.
So to get them in sync, according to the link you gave me, are you saying that I have to enter another long position in addition to the one I've already entered? Not particulary useful if I want to hold a position over night.
NinjaTrader_Bertrand
05-27-2010, 09:28 AM
David, which order types are you using? If you don't go with market orders you could get a different fill (no fill here) as you restart the strategy and it cycles again through it historical fill processing and calculations, then the trade could have triggered realtime, but might not show up in a backtest.
http://www.ninjatrader-support.com/HelpGuideV6/BacktestAStrategy.html
If the strategy and account positions are in sync at restart, there would be need to sync the account position up.
ddurocher
05-27-2010, 09:54 AM
I'm using EnterLong and ExitLong, which as I understand are market orders.
So let me ask again, if the account and strategy are sychronized at startup, then I should see the position in the strategies tab for the applicable strategy/instrument, as well as on the positions tab?
NinjaTrader_Bertrand
05-27-2010, 10:26 AM
Thanks for clarifying - yes if both are in sync you would see the strategies tab position being reflected by the account position you're holding. Likely this was really influenced by the coding changes you did, so as you started a new strategy instance it would not trigger the long yesterday to be held overnight.
ddurocher
05-27-2010, 11:04 AM
OK, so for now we will assume that the account and the strategy got out of sync due to me making some code changes last night.
Not sure what this might mean but I thought this was Interesting. When I went into positions and told NT to flatten the position, the strategy for the same instrument stopped running ...
So, if this happens to me again in the future, is there no way to force the strategy to recognize the existing position?
NinjaTrader_RyanM
05-27-2010, 11:30 AM
Yes, closing position here will also stop the strategy. To keep the strategy running you would have to place the corresponding closing order manually. (Can't use Close or Flatten Everything)
There's no way to state what the strategy position is. The NinjaScript strategy is run on each historical bar for the 1st hour of the session (it will actually run on all historical data loaded in a chart) to determine the current position state it would be in if it had been running live since the start of the session.
ddurocher
05-27-2010, 11:58 AM
Yes ... I read through some other related threads that I found.
But, if the account and strategy were out of sync why would flattening the position that the strategy was not in sync with cause the strategy to stop running? Seems odd to me.
And would it be more correct to say the strategy is run on historical bars unless I have
if (Historical)
return;
just after onbarupdate? Which I do have btw ...
So is a possible answer to somehow tell the strategy how many bars to look back to get it back in sync with the account?
NinjaTrader_RyanM
05-27-2010, 01:21 PM
Close in this context operates similarly to the close button on the superdom. (http://www.ninjatrader-support.com/HelpGuideV6/ActionButtons3.html)
Clicking on the "CLOSE" button with your left mouse button will close (http://www.ninjatrader-support.com/HelpGuideV6/ClosingAPositionStrategy.html) the current position and cancel any working orders associated to the instrument/account combination.
More info on closing a position or strategy here. (http://www.ninjatrader-support.com/HelpGuideV6/ClosingAPositionStrategy.html)
Yes, if you have that statement then the strategy will not run against Historical data.
There's no way to explicitly declare the strategy position. If you're only running it on real time data then the position state at the time of starting the strategy should be flat.