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 sastrades, Today, 09:59 AM
    1 response
    11 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by DawnTreader, 05-08-2024, 05:58 PM
    14 responses
    45 views
    0 likes
    Last Post DawnTreader  
    Started by ldanenberg, Yesterday, 03:19 PM
    3 responses
    13 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by chemo, Today, 12:05 PM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by codeowl, 02-11-2019, 05:47 AM
    24 responses
    683 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Working...
    X