Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Why Position.MarketPosition not updated ?

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

    Why Position.MarketPosition not updated ?

    I employed the following code to reverse my positions when I click a button within OnButtonClick() method.
    It works like this: if account's position is long 1 contract, it reverses the position by placing an order of 2 contracts sell short. And vice versa when MarketPosition = Short.
    However , the result wasn't as expected. If I have 1 long position and sell 2 short, it resulted to 1 short. there is no issue. But in next step, when I continue to reverse again, it once again short another 2 contract short instead of buying 2 contracts . (Looks like Position.MarketPosition​ not updated from Long to short) . Anything I did wrong here ?

    Thanks !

    {
    if (Position.MarketPosition == MarketPosition.Long)
    {

    {
    ChartControl.Dispatcher.InvokeAsync((Action)(() => {

    Account a = ChartControl.OwnerChart.ChartTrader.Account;

    int quantity0 = Positions[0].Quantity * 2;

    Instrument instr = ChartControl.OwnerChart.ChartTrader.Instrument;

    Order order0 = a.CreateOrder(instr, OrderAction.Sell,
    OrderType.Market, OrderEntry.Automated, TimeInForce.Day, quantity0, 0, 0, "", "Entry1",
    Core.Globals.MaxDate, null);


    a.Submit(new[] { order0 });


    }
    ));
    }

    }

    if (Position.MarketPosition == MarketPosition.Short)

    {
    ChartControl.Dispatcher.InvokeAsync((Action)(() => {

    Account a = ChartControl.OwnerChart.ChartTrader.Account;
    int quantity1 = Positions[1].Quantity * 2;

    Instrument instr = ChartControl.OwnerChart.ChartTrader.Instrument;

    Order order1= a.CreateOrder(instr, OrderAction.BuyToCover,
    OrderType.Market, OrderEntry.Automated, TimeInForce.Day, quantity1, 0, 0, "", "Entry3",
    Core.Globals.MaxDate, null);


    a.Submit(new[] { order1 });

    }
    ));
    }

    }

    #2
    Hello judysamnt7,

    Orders placed directly to an account using the Addon approach will not update strategy native methods such as OnOrderUpdate(), OnExecutionUpdate(), or OnPositionUpdate() and will not update the strategies position, they will only update the account position.

    Attach an event handler to the Account.PositionUpdate event if you want to capture the account position updating.
    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


    Use native strategy methods if you want the strategy position to update.
    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


    Below is a link to examples of submitting orders with button clicks.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      thanks Chelsea. What's exactly Addon you referred to and what's methods are categorized as Non Addon? what's main pro and con between the two approach

      Comment


        #4
        Hello judysamnt7,

        The Addon approach sends and received orders from an Account object with Account.CreateOrder() and Account.Submit().
        This works in any script such as an indicator, market analyzer column, or custom addon, but will not update strategy native methods.

        Native Strategy methods can only be used in a NinjaScript Strategy. EnterLong(), ExitShortStopLimit(), SetStopLoss(), OnOrderUpdate(), OnExecutionUpdate(), OnPositionUpdate() are all native strategy methods.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          So in my case, to reverse existing long positions on my account, I need use PositionsAccount[0].MarketPosition == MarketPosition.Long instead of Position.MarketPosition == MarketPosition.Long ?

          Thanks !

          Comment


            #6
            Hello judysamnt7,

            Yes, if the order is being submitted to the same account as selected for the strategy that would work.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            116 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            61 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            40 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            44 views
            0 likes
            Last Post TheRealMorford  
            Started by Mindset, 02-28-2026, 06:16 AM
            0 responses
            82 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Working...
            X