Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

MarketPosition not updating correctly if Strategy is removed and attached.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    MarketPosition not updating correctly if Strategy is removed and attached.

    I have a strategy that opens a long position when the price reaches the user input buy price, and exits when the the user input stop-loss price or profit target is reached. The strategy is executed on every Tick.

    See the sample code below. (For simplicity, I have removed additional entry conditions.)

    Code:
    protected override void OnBarUpdate()
    {
    	if( Position.MarketPosition == MarketPosition.Flat )
    	{
    		if( Close[0] <= BuyPrice )
    		{
                            // I have removed additional conditions like RSI check for simplicity.
    			EnterLong();		
    			Print("Long Position opened.");
    		}
    	}
    	else
    	{
    		if( Close[0] >= TargetPrice )
    		{
    			ExitLong();
    			Print("Long Position exited at profit.");
    		}
    		else if( Close[0] <= StopLossPrice )
    		{
    			ExitLong();
    			Print("Long Position exited at stoploss.");			
    		}		
    	}
    }
    The strategy works accurately when attached to the chart.

    However, say if a long position is open, and I remove and reattach the strategy, the Position.MarketPosition value is Flat instead of expected Long.

    How do I ensure that when I reattach the strategy to the chart, I get the correct Position.MarketPosition ?

    #2
    Hello firstlanetech,

    Thanks for your post and welcome to the NinjaTrader forums.

    You would need to set the strategies start behavior to match what you would like to do. There are a few choices. Please see the helpguide link regarding "syncing" your strategy to your account position when starting your strategy. https://ninjatrader.com/support/help..._positions.htm

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    90 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    137 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    120 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    72 views
    0 likes
    Last Post PaulMohn  
    Working...
    X