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 AnnBarnes, Today, 12:17 PM
      0 responses
      1 view
      0 likes
      Last Post AnnBarnes  
      Started by Lopat, 03-05-2023, 01:19 PM
      4 responses
      166 views
      0 likes
      Last Post Sam2515
      by Sam2515
       
      Started by f.saeidi, Today, 12:14 PM
      0 responses
      2 views
      0 likes
      Last Post f.saeidi  
      Started by giulyko00, Today, 12:03 PM
      0 responses
      4 views
      0 likes
      Last Post giulyko00  
      Started by AttiM, 02-14-2024, 05:20 PM
      12 responses
      213 views
      0 likes
      Last Post DrakeiJosh  
      Working...
      X