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

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 ZenCortexCLICK, Today, 04:58 AM
    0 responses
    5 views
    0 likes
    Last Post ZenCortexCLICK  
    Started by sidlercom80, 10-28-2023, 08:49 AM
    172 responses
    2,281 views
    0 likes
    Last Post sidlercom80  
    Started by Irukandji, Yesterday, 02:53 AM
    2 responses
    18 views
    0 likes
    Last Post Irukandji  
    Started by adeelshahzad, Today, 03:54 AM
    0 responses
    8 views
    0 likes
    Last Post adeelshahzad  
    Started by Barry Milan, Yesterday, 10:35 PM
    3 responses
    13 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Working...
    X