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 selu72, Today, 02:01 PM
      1 response
      4 views
      0 likes
      Last Post NinjaTrader_Zachary  
      Started by WHICKED, Today, 02:02 PM
      2 responses
      8 views
      0 likes
      Last Post WHICKED
      by WHICKED
       
      Started by f.saeidi, Today, 12:14 PM
      8 responses
      21 views
      0 likes
      Last Post f.saeidi  
      Started by Mikey_, 03-23-2024, 05:59 PM
      3 responses
      50 views
      0 likes
      Last Post Sam2515
      by Sam2515
       
      Started by Russ Moreland, Today, 12:54 PM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_Erick  
      Working...
      X