Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position.MarketPosition

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

    Position.MarketPosition

    Hello,
    This block of code in OnMarketData fails:

    if (Position.MarketPosition != MarketPosition.Flat) {
    Draw.Ray(this, "AskLine", false, 1, Close[0]+PriceTarget/Instrument.MasterInstrument.PointValue, 0, Close[0]+PriceTarget/Instrument.MasterInstrument.PointValue, Brushes.Blue, DashStyleHelper.Solid, 2);
    Draw.Ray(this, "BidLine", false, 1, Close[0]-PriceTarget/Instrument.MasterInstrument.PointValue, 0, Close[0]-PriceTarget/Instrument.MasterInstrument.PointValue, Brushes.Red, DashStyleHelper.Solid, 2);
    }

    The failure is related to the test Position.MarketPosition != MarketPosition.Flat. Error is "An object reference for the non-static field... CS0120

    Should this work?

    #2
    Hello morrnel,

    Thanks for your question.

    No, this would not work. There is no Position property in an indicator. NinjaScript strategies have a Position object representing their position but Indicators do not have a position. If you want to access positions on an account within an indicator, you can add an Account to the indicator and access the Account.Positions collection.

    An example indicator that adds an Account is attached.

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

    We look forward to assisting.
    Attached Files

    Comment


      #3
      Hello,

      It is possible to find out in the "OnAccountItemUpdate" event PNL related to specific instrument? I am able to get RealizedProfitLoss, CashValue, UnrealizedProfitLoss - however, they are all related to total PNL. I would like to be able to specify / filter out PNL for particular instrument, so that when I trade multiple instruments - I want indicator to show PNL per instrument... Is this possible?

      Additional question (not sure if new thread is needed) - again from Indicator - it is possible to know the last order that is currently open its Stop Loss and Profit Target prices?

      Thanks

      Comment


        #4
        Hello music_p13,

        The AccountItem is given from the broker and is not instrument specific. You can check the Account.Positions collection for open positions and calculate unrealized PnL of each position with Position.GetUnrealizedProfitLoss and the current price for that instrument. Daily realized PnL per instrument would need to be calculated by tracking executions following Account.Executions/Account.ExecutionUpdate to get the execution price of each order on that instrument, and pairing them together to form trades and to get the realized PnL from each trade.

        From an indicator, you could loop through the Account.Orders collection which would be the orders in the Orders tab of the Control Center, and you can check orders that have an OrderState of Accdepted/Working and their order.Name property to see order's name represents a stop loss or profit target. This would show active orders and the names that represent a target/stop would be your target/stop.

        Order - https://ninjatrader.com/support/help...nt8/?order.htm

        Account.Orders - https://ninjatrader.com/support/help...rs_account.htm

        Account.Executions - https://ninjatrader.com/support/help...executions.htm

        Account.ExecutionUpdate - https://ninjatrader.com/support/help...tionupdate.htm

        Position.GetUnrealizedProfitLoss - https://ninjatrader.com/support/help...profitloss.htm

        Let me know if there is anything else we can do to help.

        Comment


          #5
          Thanks,

          Based on "You can check the Account.Positions collection for open positions and calculate unrealized PnL of each position with Position.GetUnrealizedProfitLoss and the current price for that instrument." - How to know if current Position object and "Open Position" is related to instrument X or instrument Y?

          Thanks

          Comment


            #6
            Hello music_p13,

            While you are looping through Account.Positions, you can check the Instrument property of each Position.

            Position - https://ninjatrader.com/support/help.../?position.htm

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            596 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            343 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            103 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            556 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            554 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X