Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position Status

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

    Position Status

    I see a few examples getting the market position. I'm getting an error with the same code here...

    protected override void OnBarUpdate()
    {
    price = Close[0];
    if (Position.MarketPosition != MarketPosition.Flat)
    {
    // ToDo: Implement This
    }
    }

    Severity Code Description Project File Line Suppression State
    Error CS0120 An object reference is required for the non-static field, method, or property 'Position.MarketPosition' NinjaTrader.Custom C:\Users\user1\Documents\NinjaTrader 8\bin\Custom\Indicators\ChartTraderCustomTest.cs 98 Active

    I know what this error normally means. Means to have an instance of an object but I'm seeing examples using it like this...


    #2
    Hello Jalley,

    Thank you for your post.

    You are receiving this message because Position.MarketPosition cannot be called in an Indicator and must be called in a Strategy. Position.MarketPosition represents position-related information that pertains to an instance of a strategy.

    Please see this help guide link for more information about Position.MarketPosition - https://ninjatrader.com/support/help...8/position.htm

    Let us know if we may further assist.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Is there a way to do this without a strategy since the indicator I wrote itself places orders and if another order is placed when in the middle of an active trade it causes orange orphaned orders and State.Terminated, removing custom controls.
      Last edited by jalley; 09-02-2020, 01:53 PM.

      Comment


        #4
        Hello jalley,

        Thank you for your note.

        If you are using the Add-On approach to send orders from an indicator using an account object, you could check your Account position by using <Account>.Positions.

        Please see this help guide link about how Positions can be used to check your Account position - https://ninjatrader.com/support/help...ns_account.htm

        Also, please see the attached indicator example for how this can be accomplished.

        Let us know if we may further assist
        Attached Files
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Ok, I have everything working except for a couple issues.

          Everything is done through the custom indicator and no add-on's or strategies. It can use either the ATM (works ok) or not (issues below)

          I'm having issues with State.Terminated occuring during user gui changes. For instance, my extended controls place an order and then if any of the order buttons other than cancel are pressed it goes to State.Terminated and removes all my custom controls and places a orphaned orange order pending that can't be cancelled.

          Second issue is placing a stop loss order after an entry order. It places it fine but as soon as the entry order is filled the stop loss order disappears, (code below):

          In my PlaceOrder method:

          if (OrderActionType == "Long")
          {
          stopOrder = myAccount.CreateOrder(chartTrader.Instrument, OrderAction.Sell, OrderType.StopLimit, OrderEntry.Automated, TimeInForce.Gtc, 1, 0, stopOffsetPrice, ocoGuid.ToString(), "stopOrder", Core.Globals.MaxDate, null);
          }
          else
          {
          stopOrder = myAccount.CreateOrder(chartTrader.Instrument, OrderAction.Buy, OrderType.StopLimit, OrderEntry.Automated, TimeInForce.Gtc, 1, 0, stopOffsetPrice, ocoGuid.ToString(), "stopOrder", Core.Globals.MaxDate, null);
          }
          myAccount.Submit(new[] { entryOrder });
          // myAccount.Submit(new[] { stopOrder }); // I tried it here... places it but disappears as soon as the entryOrder fills


          // Then I tried it like this and get an error Order '<OrderNum here>' can't be submitted: order status is CancelPending. affected Order: Sell 1 StopLimit @ 10575.5 x 0
          private void OnAccountExecutionUpdate(object sender, ExecutionEventArgs e)
          {
          if ( (stopOrder != null) && (e.Execution.Instrument == instrument)
          && ( e.MarketPosition == MarketPosition.Long) || (e.MarketPosition == MarketPosition.Short) )
          {
          myAccount.Submit(new[] { stopOrder });
          stopOrder = null;
          }
          }

          Please keep in mind that I'm trying to do this without using addons or strategies. Very close.. just these two issues. I partially addressed the State.Terminated one by disabling all the controls except Close to prevent State.Terminated from happening but this is not a good approach.

          Thank you in advance!
          Last edited by jalley; 09-03-2020, 12:05 AM.

          Comment


            #6
            Hello jalley,

            Thank you for that information.

            There are multiple instances that State.Terminated is called within a NinjaScript's lifecycle. The lifecycle calls State.SetDefaults and State.Terminated at least twice to acquire various default property values before running through its full state management. For a visual representation of the NinjaScript Lifecycle, please see the help guide linked below.

            NinjaScript Lifecycle - https://ninjatrader.com/support/help...fecycle_of.htm

            To ensure that an object is removed/disposed only at the last call of State.Terminated, you could first instantiate the object within State.DataLoaded. Then you could check if that object is not null in State.Terminated, followed by disposing of the object. Please see the attached CustomTimer example that demonstrates this.

            Are there any errors appearing on the Log tab of the Control Center when the script terminates and the controls are removed from the chart?

            Please see the attached indicator example 'ClickLimitOrderPlusStop' demonstrating how a limit order may be placed using a mouse click. Once that order is executed, a stop-loss order will be placed 10 ticks below the entry order.

            Let us know if we may further assist
            Attached Files
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              Hello Brandon, I'm trying to check if the position is long or short in an indicator.
              I found this answer from Patrick.

              Indicator knows market position
              PHP Code:
              foreach (Account acct in NinjaTrader.Cbi.Account.Accounts)
              {
                if (acct.Positions != null)
                {
                  foreach (Position pos in acct.Positions)
                  {
                    Print(pos.Account.Name + " " + pos.Instrument + " " + pos.MarketPosition + " " + pos.Quantity + " " + pos.AveragePrice);
                  }
                }
              } 
              

              Is there away to do it without the nested loops?


              I have a method with loops already and I originally tried like this but it's not working
              PHP Code:
                  private void MoveSL()
                  {
                    RealPosition position = null;
              
                    if (position.MarketPosition == MarketPosition.Long)
                    {
                      lock (account.Orders)
                      {
                        foreach (Order moveSLOrder in account.Orders)
                        {
                          // DoX;
                        }
                      }
                    }
                    else if (position.MarketPosition == MarketPosition.Short)
                    {
                      lock (account.Orders)
                      {
                        foreach (Order moveSLOrder in account.Orders)
                        {
                          // DoY;
                        }
                      }
                    }
                  } 
              

              Then following Patrick's snippet
              PHP Code:
                  private void MoveSL()
                  {
                    foreach (Account acct in NinjaTrader.Cbi.Account.Accounts)
                    {
                      if (acct.Positions != null)
                      {
                        foreach (Position pos in acct.Positions)
                        {
                          if (pos.MarketPosition == MarketPosition.Long)
                          {
                            lock (account.Orders)
                            {
                              foreach (Order moveSLOrder in account.Orders)
                              {
                                // doX;
                              }
                            }
                          }
                          else if (pos.MarketPosition == MarketPosition.Short)
                          {
                            lock (account.Orders)
                            {
                              foreach (Order moveSLOrder in account.Orders)
                              {
                                // doY;
                              }
                            }
                          }
                        }
                      }
                    }
                  } 
              
              I get this error
              NinjaScript File Error Code Line Column
              myTickHunter.cs 'NinjaTrader.Cbi.Account' does not contain a definition for 'Accounts' CS0117 1234 53
              What's the fix Accounts?

              I found the Accounts fix from
              getting account name list

              PHP Code:
              foreach(Account a in Account.All) {
                 //DoX;
              } 
              
              PHP Code:
                  private void MoveSL()
                  {
                    foreach (Account acct in Account.All)
                    {
                      if (acct.Positions != null)
                      {
                        foreach (Position pos in acct.Positions)
                        {
                          if (pos.MarketPosition == MarketPosition.Long)
                          {
                            lock (account.Orders)
                            {
                              foreach (Order moveSLOrder in account.Orders)
                              {
                                // doX;
                              }
                            }
                          }
                          else if (pos.MarketPosition == MarketPosition.Short)
                          {
                            lock (account.Orders)
                            {
                              foreach (Order moveSLOrder in account.Orders)
                              {
                                // doY;
                              }
                            }
                          }
                        }
                      }
                    }
                  } 
              

              What simpler code would you suggest as valid substitute to the nested foreach loops? Thanks!
              Last edited by PaulMohn; 03-17-2022, 05:50 AM.

              Comment


                #8
                Hello PaulMohn,

                Thanks for your note.

                To access the position of an account from an indicator you would need to use the AddOn approach Account class in the indicator.

                As seen in the Account class Positions help guide page linked below, a loop would be needed to loop through each position in the <Account>.Positions collection to get information about the positions on that specified account.

                See the help guide documentation below.

                <Account>.Positions: https://ninjatrader.com/support/help...ns_account.htm

                Let us know if we may assist further.



                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                571 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                331 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                549 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                550 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X