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

Help with some code - please

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

    Help with some code - please

    Hi all,

    I am trying to sum the output from a foreach statement. Specifically, I would like to sum the unrealized Profit and loss from each position in the account. I can get the code to print each positions PnL perfectly (see the code below) but that's where I get stuck. How to sum...

    Any help will be appreciated.

    current code:

    foreach (Position pos in Account.Positions)
    {
    Print("PnL = $"+pos.GetUnrealizedProfitLoss(PerformanceUnit.Cur rency));
    }

    #2
    Hello Clubsport640,

    Welcome to the forums!

    Position.GetUnrealizedProfitLoss() will return a double. If you would like to collect it, you could create a variable of type double and then add each value returned from Position.GetUnrealizedProfitLoss().

    As an example:

    Code:
    double myTotalPnL = 0;
    foreach (Position pos in Account.Positions)
    	myTotalPnL += pos.GetUnrealizedProfitLoss(PerformanceUnit.Currency);
    Print(myTotalPnL);
    GetUnrealizedProfitLoss() documentation is openly available here - https://ninjatrader.com/support/help...profitloss.htm

    We also have openly available resources that explain C# syntax used in NinjaScript in our NinjaTrader 7 help guide. I will provide a link below.

    C# Syntax in NinjaScript - https://ninjatrader.com/support/help...sic_syntax.htm

    If you have any additional questions, please don't hesitate to ask.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim,

      Thanks for your help - worked great.

      Cheers

      James

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by love2code2trade, Yesterday, 01:45 PM
      4 responses
      28 views
      0 likes
      Last Post love2code2trade  
      Started by funk10101, Today, 09:43 PM
      0 responses
      7 views
      0 likes
      Last Post funk10101  
      Started by pkefal, 04-11-2024, 07:39 AM
      11 responses
      37 views
      0 likes
      Last Post jeronymite  
      Started by bill2023, Yesterday, 08:51 AM
      8 responses
      44 views
      0 likes
      Last Post bill2023  
      Started by yertle, Today, 08:38 AM
      6 responses
      26 views
      0 likes
      Last Post ryjoga
      by ryjoga
       
      Working...
      X