Announcement

Collapse
No announcement yet.

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 caacapital, Today, 10:10 AM
      1 response
      14 views
      0 likes
      Last Post NinjaTrader_ShawnB  
      Started by DiverG20, Today, 10:23 AM
      0 responses
      21 views
      0 likes
      Last Post DiverG20  
      Started by SuneSorgenfrei, Today, 09:29 AM
      0 responses
      19 views
      0 likes
      Last Post SuneSorgenfrei  
      Started by dodoei, Today, 07:24 AM
      0 responses
      14 views
      0 likes
      Last Post dodoei
      by dodoei
       
      Started by thecashguys, Today, 06:13 AM
      0 responses
      13 views
      0 likes
      Last Post thecashguys  
      Working...
      X