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 inanazsocial, Today, 01:15 AM
      0 responses
      2 views
      0 likes
      Last Post inanazsocial  
      Started by trilliantrader, 04-18-2024, 08:16 AM
      5 responses
      22 views
      0 likes
      Last Post trilliantrader  
      Started by Davidtowleii, Today, 12:15 AM
      0 responses
      3 views
      0 likes
      Last Post Davidtowleii  
      Started by guillembm, Yesterday, 11:25 AM
      2 responses
      9 views
      0 likes
      Last Post guillembm  
      Started by junkone, 04-21-2024, 07:17 AM
      9 responses
      70 views
      0 likes
      Last Post jeronymite  
      Working...
      X