Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Getting realized profit in an indicator attached to MA

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

    Getting realized profit in an indicator attached to MA

    Hello,
    I have been using the below syntax to get Unrealized profit for a specific position (in an indicator attached to Market Analyzer) and it works.

    Code:
    if (Instrument.FullName == position.Instrument.FullName)
    unRealizedPnL = position.GetUnrealizedProfitLoss(PerformanceUnit, Close.GetValueAt(CurrentBar));
    However, I cannot find the syntax for getting the realized profit. The below does not work.

    Code:
    if (Instrument.FullName == position.Instrument.FullName)
    RealizedPnL = position.GetRealizedProfitLoss(PerformanceUnit, Close.GetValueAt(CurrentBar));
    Any help?
    Thanks​

    #2
    Hello MSerag,

    Where is the position variable being assigned an object?

    Is this coming from an <Account>.Positions element?

    The realized PnL from an account can be retrieved with <Account>.Get(AccountItem.RealizedProfitLoss, Currency.UsDollar)
    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.

    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.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi again,
      Here is my assignment. I want to attach it to MA and use the if statement if (Instrument.FullName == position.Instrument.FullName) to get the realized profit for this particular instrument.

      Code:
      else if (State == State.DataLoaded)
      {
      // Find our account
      lock (Account.All)
      account = Account.All.FirstOrDefault(a => a.Name == "Sim102");
      
      // Subscribe to account item updates
      if (account != null)
      {
      account.AccountItemUpdate += OnAccountItemUpdate;
      account.PositionUpdate += OnPositionUpdate;
      
      foreach (Position pos in account.Positions)
      if (pos.Instrument == Instrument)
      position = pos;
      
      realizedPnL = account.Get(AccountItem.RealizedProfitLoss, Currency.UsDollar);
      cashValue = account.Get(AccountItem.CashValue, Currency.UsDollar);
      
      }
      }
      else if(State == State.Terminated)
      {
      // Make sure to unsubscribe to the account item subscription
      if (account != null)
      {
      account.AccountItemUpdate -= OnAccountItemUpdate;
      account.PositionUpdate -= OnPositionUpdate;
      }
      }
      }
      
      private void OnAccountItemUpdate(object sender, AccountItemEventArgs e)
      {
      if (e.AccountItem == AccountItem.UnrealizedProfitLoss)
      realizedPnL = e.Value;
      
      if (e.AccountItem == AccountItem.CashValue)
      cashValue = e.Value;
      
      }
      
      private void OnPositionUpdate(object sender, PositionEventArgs e)
      {
      if (e.Position.Instrument == Instrument && e.MarketPosition == MarketPosition.Flat)
      position = null;
      else if (e.Position.Instrument == Instrument && e.MarketPosition != MarketPosition.Flat)
      position = e.Position;
      }

      Comment


        #4
        Hello MSerag,

        An open position has unrealized pnl.

        A close position no longer contributes to the unrealized pnl and becomes realized pnl.

        The realized pnl can be retrieved with Account.Get() as stated in my previous post.

        Unfortunately, this is not separated by instrument.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by alphatango, Yesterday, 05:56 PM
        2 responses
        18 views
        0 likes
        Last Post alphatango  
        Started by alphatango, Today, 06:16 PM
        0 responses
        6 views
        0 likes
        Last Post alphatango  
        Started by alphatango, Yesterday, 07:32 PM
        2 responses
        11 views
        0 likes
        Last Post alphatango  
        Started by several, Yesterday, 03:39 AM
        2 responses
        24 views
        0 likes
        Last Post several
        by several
         
        Started by nintrader310, 01-14-2025, 06:58 PM
        4 responses
        21 views
        0 likes
        Last Post nintrader310  
        Working...
        X