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 Rapine Heihei, Today, 08:19 PM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by Rapine Heihei, Today, 08:25 PM
    0 responses
    5 views
    0 likes
    Last Post Rapine Heihei  
    Started by f.saeidi, Today, 08:01 PM
    1 response
    4 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by Rapine Heihei, Today, 07:51 PM
    0 responses
    6 views
    0 likes
    Last Post Rapine Heihei  
    Started by frslvr, 04-11-2024, 07:26 AM
    5 responses
    97 views
    1 like
    Last Post caryc123  
    Working...
    X