Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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
    Paul H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by traderqz, Today, 12:06 AM
    6 responses
    12 views
    0 likes
    Last Post traderqz  
    Started by Skifree, Today, 03:41 AM
    3 responses
    12 views
    0 likes
    Last Post Skifree
    by Skifree
     
    Started by traderqz, Yesterday, 09:06 AM
    5 responses
    33 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by guillembm, Today, 11:25 AM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by owensd, 04-21-2024, 11:34 PM
    9 responses
    34 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Working...
    X