Course Name: Mean Reversion Strategies In Python, Section No: 4, Unit No: 4, Unit type: Notebook
Please explain all calculations the Section 4, unit 4, starting from "Long positions and short positions" computation in the Jupiter notebook. To me it looks like creative accounting rather than generating BUY/SELL/DO_NOTHING commands for order sending system.
The provided explanation just tell you to do smth without explaining rationale behind the computation.
Hi Gleb,
There is a difference between the structure of a code written for back-testing and the structure of a code written for live trading. The latter one is shaped based on event-driven basis as the algorithm assesses every update on input one by one and then, as you said, buy-sell-do nothing trading signals are produced for each assessment separately. However, during the back-testing, you label the past data as 1, 0, -1 based on conditions(your strategy) and then calculate the PnL by multiplying the positions with returns (e.g if you went long, 1 * return produces profit if the return is positive; if you went short, -1 * return produces profit if the return is negative). As for the computation, in the final stage, actually, there is no computation. Instead, there are conditions for generating positions as 1, 0, -1 (like if long_entry = True then long_position = 1 and this condition, as with others, is applied to entire data).
Hope this helps
Hi Suleyman,
Thank you very much for your answer.
I took subsequent sections and the explanations clarified my confusion, for the most parts.
So it seems this way for computing PnL is as if it is a strategy that checks for trading signal every day, but does not accumulate a long/short position if the position is not flat.
That is, suppose that day before yesterday we were flat and there was no buy/sell signal either. So nothing happens on that day. Then suppose yesterday there was buy signal (closing price < lower band) so we enter long position of size 1. Next, suppose today is still a buy signal, however we do nothing because we are already long of size 1.
Also, computing PnL using daily returns implicitly assumes that you enter position at the daily closing price. I think it is a big approximation; in reality one should use best bid/best offer prices of the limit order book. However for daily frequencies this approximation could be OK.
Overall, is it a good proxy to compute PnL of the strategy? It certainly vectorizes as you don't have time step dependency, therefore you can process large datasets fairly quickly.
Hi Gleb,
You're welcome.
Frankly, you explained the logic better than me:)
You are right about the drawback of the assumption that we enter the position on the close price. Here, the main purpose is finding a profitable strategy or assessing approximately how profitable a strategy is. When you find a profitable strategy, you need to adapt it to the real world by considering the spread, transaction costs and other costs (maybe financing?). Since these costs are subject to change (e.g. bid-ask spread is not the same for every asset, or transaction costs can be different for different platforms), you can add these factors based on the assets and the platform you will choose for trading after finding a good strategy.
It is better to include these factors in your strategy, but you can certainly use backtesting results as a proxy even if you don't consider the real-world factors. For example, you can multiply the returns with a rate approximating to spread+transaction costs.
I hope this helps.
Hi Suleyman,
Thank you for your answer.
Is it a reasonable proxy? Can you rely on it as a rule of thumb?It is better to include these factors in your strategy, but you can certainly use backtesting results as a proxy even if you don't consider the real-world factors.
Kinds regards,
Gleb
Hi Gleb,
To avoid misunderstanding, I assumed that you can use the result as a proxy even if you don't include the factors such as the spread, transaction costs etc., in your strategy since this strategy is a medium frequency strategy based on daily data. Also, this can be a good proxy for paper trading, not for live trading. Considering these two points, you can use the result, but I cannot say that you can rely on it as a rule of thumb. Eventually, you have to consider the real-world factors.
To summarise, it is ok that you don't include the transaction costs and the spread factors in your strategy if it is not a strategy for high-frequency trading. Eventually, if it is a good strategy, it will produce profit during paper trading, and then you can go live.
I hope I could clarify my point.
Best Regards,
Suleyman
Hi Suleyman,
Thank you very much for your response.
Seems like one can use this kind of vectorizable strategies as proxy to see quickly if a strategy works at all. If it does, you try closer-to-reality event-driven strategies. If they work, you move you strategy to production (paper-trading followed by the live trading).
Best regards,
Gleb
Hi Gleb,
It seems everything is clear. Perfect clarification!
Best regards,
Suleyman