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.
Comment