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 NullPointStrategies, Today, 05:17 AM
    0 responses
    50 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    126 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    69 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X