Hi guys,
I am experimenting with Forex trading and I struggle to understand how P/L for a given trade is calculated.
In the blackest attached, I have setup a simple algorithm example that enter a long EURJPY position with a USD denominated account on a moving average cross signal and exit after a 5% movement in EURJPY price. I have included a counter to limit the example to one single trade (Entry + Exit) over the period so it is easy to understand what is happening.
I got two questions:
1) I would expect my P/L in USD on that trade to be:
P/L = USD received at Exit - USD paid at Entry
= (_ExitPrice x _Quantity x _ExitConversionRate) - (_EntryPrice * _Quantity * _EntryConversionRate)
= -$2,481.5 Loss for the attached backup
However, QuantConnect shows a $2,304.97 Net Profit for that trade. What am I getting wrong?
2) After the trade has been closed, I would expect the account Equity in USD to remain perfectly flat at the Starting Equity + Realised P/L = $ 102,304.97 level. However, you can see the Strategy Equity continue to slightly fluctuate every day after the trade has been close. How come is that?
I would really appreciate someone shading some light on those questions.
Many thanks
Pi..R
Having looked into it a bit longer, I think I understand what I got wrong in 1), the Conversion Rate at Entry doesn’t matter. The correct way to think of it should be:
P/L in USD = Change in EURJPY Price between Entry and Exit * Quantity * ExitConversionRate
= (_ExitPrice - _EntryPrice) * _Quantity * _ExitConversionRate
= $2,304.97 Profit for the attached backup
But I still don’t get why the Equity value of the portfolio continue to fluctuate after the deal has been closed and would appreciate some help there.
Jared Broad
LEAN models FX as a currency pair swap; holding the long and short values of each currency separately in the cashbook. With 1-1 conversion a long trade buys EUR with JPY; this means +1000 EUR, -1000 JPY.
When the price moves in your favor EUR becomes stronger, say 2-1 you will earn more JPY per EUR than you had originally shorted. i.e. selling the -1000 EUR will buy back +2000 JPY. This clears out the -1000 JPY short and leaves you with a residual profit of 1000 JPY in your cashbook.
The changing portfolio value is the 1000 residual JPY being converted back to USD constantly.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Pi..R
Thank you very much Jared, that’s very helpful!
This approach is a purely conceptual for modelling purpose, right? (i.e. if it was to be live trading, I would not end up with 1000 JPY on my account, it would all be USD with a constant portfolio value post closing the trade?)
Jared Broad
I think so, but it depends on your brokerage. We made LEAN core models closer to professional funds systems rather than retail brokerages etc. I think OANDA closes out the position but please confirm with paper trading.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Pi..R
Thanks Jared, I will check with paper trading when I am done with some other ongoing paper testing.
Meantime I was trying to find a solution to liquidate the residual JPY position when the trade close. See line 96 of the attached backtest - I am trying to execute a market order to sell the residual JPY for USD, but I get an error (if I remove the comment line 96) saying that the USDJPY symbol was not found in my security list. Any idea on how I can achieve that?
Jared Broad
AddForex("USDJPY", Resolution.Daily, Market.Oanda, true, 20m);
I wouldn't recommend trying to liquidate, suggest just worrying about the bigger picture until tested in paper trading. The brokerage might not even have the residual.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Pi..R
You are right, thanks Jared, really appreciate how helpful and responsive you are!
Pi..R
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!