The correct way to calculate the number of trades
When we were working with the trend-following strategy in Chapter 12, Sample Strategy – Trend-Following, we only opened new positions, and each opening closed the previously open one. This is normal for always-in-the-market strategies. In this case, indeed, the number of trades coincides with the number of executed orders.
In our example with a limit and a stop order, we use two orders to actually perform just one trade: buying and then exiting the market with a profit or loss. Therefore, we should only use the amount of entry orders to calculate the average trade. How can we distinguish between opening and closing orders?
There are multiple ways of doing that. One of the possible options would be adding another key to the order with values of Entry
or Exit
, but we will use a different approach: we will add a new attribute to the tradingSystemMetadata
class, which will hold the actual number of trades, and we will update...