But how should one code this? It seems absolutely impossible to code this in "real time", i.e. such that I enter and exit trades as soon as events occur, because for a given day, I don't know which happened sooner: the high or the low.
For this reason, isn't it the case that I have to look at the market after the market closes on each day, and then place my trade according to the next day's opening price? If this is the case, how do you actually implement the Donchian Trend system in code?
Here is one idea that I have:
- The market has closed on a particular day. I look at the high/low for the day. I calculate the moving averages based on the closing prices for this and past days. If we have the highest high/lowest low for the past 20-days (and assuming I currently don't have any trade on and that the moving average filter is satisfied), I enter a new trade by buying/selling on the next day's opening price. I continue to repeat this process for all days going forward.
- I deal with the 10-day exit breakout in a similar way.
- However, I can deal with the 2-ATR stop loss in "real time", i.e. as soon as it occurs because I'm entering trades on market open, and I can see on a given day using the high and low prices whether the stop was hit.