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 CarlTrading, 03-31-2026, 09:41 PM
    1 response
    43 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    20 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    30 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    48 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    38 views
    0 likes
    Last Post CarlTrading  
    Working...
    X