Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

GetProfitLoss value???

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

    GetProfitLoss value???

    Hello

    I get strange GetProfitLoss() values.

    I go long or short 100 shares of AMZN and get averaged filled price like this:

    ***
    protected override void OnOrderUpdate(IOrder order)
    {
    if (golongOrder != null && golongOrder.Token == order.Token)
    {
    if (order.OrderState == OrderState.Filled)
    {
    Print("golong order filled: " + golongOrder.Filled + " @ " + golongOrder.AvgFillPrice);
    golongOrder = null;
    }
    }
    }

    ***

    this part seems ok.

    Now I check in OnBarUpdate()

    ***
    if (Position.MarketPosition != MarketPosition.Flat)
    {
    mypl = Position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
    Print(Position.MarketPosition.ToString() + " " + myposition.ToString() + " status: " + status.ToString()+ " mypl: " + mypl.ToString() + " close: " + Close[0].ToString());
    }
    ***
    where mypl and myposition are global variables (double and int)

    And here is the output of the script (starts from the bottom):

    ***

    exitlong order filled: 100 @ 127.81
    Long 100 status: 1 mypl: -1.9999999999996 close: 127.79
    Long 100 status: 1 mypl: -1.00000000000051 close: 127.8
    Long 100 status: 1 mypl: -4.00000000000063 close: 127.77
    Long 100 status: 1 mypl: -1.9999999999996 close: 127.79
    Long 100 status: 1 mypl: -10.9999999999999 close: 127.7
    Long 100 status: 1 mypl: -18.9999999999998 close: 127.62
    Long 100 status: 1 mypl: -26.9999999999996 close: 127.54
    Long 100 status: 1 mypl: -24.0000000000009 close: 127.57
    Long 100 status: 1 mypl: -23.0000000000004 close: 127.58
    golong order filled: 100 @ 127.57
    exitshort order filled: 100 @ 127.57
    Short 100 status: -1 mypl: -6.00000000000023 close: 127.63
    Short 100 status: -1 mypl: -2.00000000000102 close: 127.59
    Short 100 status: -1 mypl: -3.00000000000011 close: 127.6
    Short 100 status: -1 mypl: 0 close: 127.57
    Short 100 status: -1 mypl: 0.999999999999091 close: 127.56
    goshort order filled: 100 @ 127.55
    exitlong order filled: 100 @ 127.63
    Long 100 status: 1 mypl: -6.99999999999932 close: 127.56
    Long 100 status: 1 mypl: -6.00000000000023 close: 127.57
    Long 100 status: 1 mypl: -11.999999999999 close: 127.51
    golong order filled: 100 @ 127.52
    exitshort order filled: 100 @ 127.52
    Short 100 status: -1 mypl: -3.00000000000011 close: 127.55
    Short 100 status: -1 mypl: 6.99999999999932 close: 127.45
    goshort order filled: 100 @ 127.41

    ***

    The first line (from the bottom) shows that short position is entered 100 @ 127.41,
    Then update called from OnBarUpdate()
    Short 100 status: -1 mypl: 6.99999999999932 close: 127.45, here 6.999 is output from Position.GetProfitLoss(Close[0], PerformanceUnit.Currency).

    I tried also various PerformanceUnit but every time I get number that I can not relate to average entry price and current Close.

    Please suggest what I do wrong.

    #2
    Hello sergeysamsonov,

    First thing I would do here is try without global variables. You may use them in NinjaScript but they are not supported. Please try with privately declared variables and let us know if it works the way you expect.
    Ryan M.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    569 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    330 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    548 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    548 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X