Bug in Position object?

In the past I’ve used Position.MarketPosition to determine whether the strategy is Flat, Long, or Short.

This doesn’t work anymore, Position.MarketPosition always returns Flat.

Bug or was always this way and I didn’t notice?

That seems strange. You should be able to use MarketPosition to get the position state.
Position Docs
Position

Are you using more than one instrument in your strategy? Have you compared against all possible sources of MarketPosition, like PositionAccount and Account.Positions? If they all report flat, I would suspect your entry order is not being accepted and you aren’t actually in a position.

For reference, I modified the example I posted in the other thread (note the position state check before exiting) to check MarketPosition and it works fine for me (NT v8.1.5.2).

		protected override void OnBarUpdate()
		{
            //Add your custom strategy logic here.
            if (CurrentBar < 20)
                return;

            // Only enter if at least 10 bars has passed since our last entry
            if ((BarsSinceEntryExecution() > 10 || BarsSinceEntryExecution() == -1) && CrossAbove(SMA(10), SMA(20), 1))
                EnterLong("SMA Cross Entry");

            // Exits position
            if (CrossBelow(SMA(10), SMA(20), 1) && Position.MarketPosition == MarketPosition.Long)
                ExitLong();
        }

No, that is not the case; however, there are three important points I would note.

First, if you are using ATM strategies, that won’t show up in the Position object because those are not strategy trades from the strategy’s perspective, they are external.

Second, if you’re talking about code in an event, such as for instance, OnExecutionUpdate, you should be using the argument sent in the parameter rather than referring to the global “Position” instance. The reason for this is because of the multithreaded nature of the environment - if you want the most up-to-date information, you need to use the parameters instead.

Third, if you’re thinking it will see a position initiated outside of the strategy (such as a position taken by another strategy, or by a human being) then that’s a misunderstanding. The Position object only sees the position of this instance of the strategy itself. The reason for that is so that it works in back-testing, and so that two strategies on the same chart can each understand their own internal sense of position.

5 Likes

I finally figured out the problem. I had CancelAllOrders right after I did ExitLong.

What that ended up doing is cancelling the exit order immediately before it had a chance to execute, hence the position ended up still open.

1 Like

Excellent. Glad you figured it out. As they say, “it’s always the last place you look.”

1 Like