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

Multi Instrument Current Net PNL for the day

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

    Multi Instrument Current Net PNL for the day

    Hello NT Support,

    I have a multi instrument strategy where I'm trying to track on each bar close, the current net PNL for the day to monitor the highs and lows (similar to MFE / MAE) across both closed positions and open positions. For example I need to be able to answer the question:

    On this date the strategy made $X in net PNL however during that day the net pnl was as low as $Y and as high as $T.
    Net PNL being both closed trades and open unrealized PNL.

    I understand I can iterate through my current open positions to get the unrealized PNL with something similar to the following:

    Note: TradedSymbols is an internal collection I use to track every instrument I'm trading along with specific settings for that instrument and how it trades it.
    Code:
    double pnl = 0;
    
    for (int i = 0; i < TradedSymbols.Count; i++)
    {
        pnl += Positions[i].GetUnrealizedProfitLoss(PerformanceUnit.Currency, Closes[i][0]);
    }
    I assume the above will give me the unrealized PNL for all open positions at that moment in time. The question is how would I (or can I) get the closed PNL for that trading day? I would assume I could do something like:

    Code:
    SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit;
    But my assumption is that this will include all trades for prior days. Can the current day be captured or do I have to manually track this in code? Thanks.

    #2
    If we can't capture the realized PNL for the day only then perhaps it's good enough to look at thee AllTrades.TradesPerformance before any trades are taken for the day as the starting point then subtract that from the same value throughout the day including the unrealized pnl? Just wondering if there is a more straightforward way. Thanks.

    Comment


      #3
      Ok that seems to work, answered my own question, the following worked for those that find this in the future:

      Code:
      double BalanceLow = double.MaxValue;
      double BalanceHigh = 0;
      double BalancePriorDay = 0;
      
      private void CollectAccountInfo()
      {
          double pnl = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit - BalancePriorDay;
      
          for (int i = 0; i < TradedSymbols.Count; i++)
          {
              if(Positions[i].MarketPosition != MarketPosition.Flat)
                  pnl += Positions[i].GetUnrealizedProfitLoss(PerformanceUnit.Currency, Closes[i][1]);
          }
      
          BalanceLow = Math.Min(BalanceLow, pnl);
          BalanceHigh= Math.Max(BalanceHigh, pnl);
      }
      
      protected override void OnBarUpdate()
      {
          if[BarsInProgress == 0)
              CollectAccountInfo();
      
          if(BarsInProgress == 0 && BarsArray[0].IsFirstBarOfSession)
          {
              BalanceLow = double.MaxValue;
              BalanceHigh = 0;
              BalancePriorDay = SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit;
          }
      }
      This is a modified/simplified version for example purposes....
      Last edited by fxRichard; 01-28-2021, 06:09 AM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by junkone, Today, 11:37 AM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by frankthearm, Yesterday, 09:08 AM
      11 responses
      41 views
      0 likes
      Last Post frankthearm  
      Started by quantismo, 04-17-2024, 05:13 PM
      5 responses
      35 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by proptrade13, Today, 11:06 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Started by love2code2trade, 04-17-2024, 01:45 PM
      4 responses
      34 views
      0 likes
      Last Post love2code2trade  
      Working...
      X