Building trading algorithms from scratch. Part IV.
Trading is a risky activity. If you follow anything written in the article, you are responsible for the results. The result of this article may be decreased deposit and lost funds. By reading below and making any actions, you agree with our Disclaimer, Policies, and Risk Warning.
Welcome back to our algorithmic journey! This time we are getting closer and closer to the end and a final decision whether or not to use the algorithm we are building.
As always, I start my new article with fixes of the previous mistakes (life is tough, so is algorithmic trading). Last time, when I made a code, I put the data recording block in the wrong place, so to say. Where is this place? Right below the function that submits orders. In real algorithmic trading and simulations as well, it takes some time for an algorithm to submit and execute orders. Placing the data export function right below the order submission leads to some data collapse and mistakes in exported values. For example, the order may be submitted, but not executed, so the position is still flat when we get to the record data function. As a result, the data export block messes with the data. There may be the contrary situation as well – when you try to close the position, but the record data function still thinks you have the position. So, in the new version of the code, which is available here, I fixed it. As a result, some of my dimension profiles changed.
Williams %R profile with the wrong placement of the data export function
Williams %R profile with the right placement of the data export function
So, I had to run and analyze all the profiles once again, which is fine because algorithmic trading is an iterative process, after all.
After testing several filters, I stayed with only one of them. In particular, I added condition so that the distance between High and Close is no more than 0.5 ATR for long trades (I did the same with Close and Low for short trades). The code can be found here.
Equity curve of VSH_4 on January 2010 - January 2018 on GC
Adding time filters
The profit factor of the strategy is 1.18, which is still low, and the equity curve shows some periods when the algorithm performed poorly. So, are there any other ways to improve it? Sure there are. What we are going to do next is a time filter.
Why time filter makes sense almost for any algorithm? Because the logic that you use as the foundation for your trading cannot work all the time. There are many breaking points in terms of time, when markets may behave differently. For example, Friday is said to be the best day for the stock. So statistically, it may be a bad idea to short stocks on Friday.
The most important breaking point that can disrupt your algorithm is the opening of the US session. When traders from the US wake up and start trading, a lot of different things happen at the same time – volume surges, speed of the market accelerates, trends may change, because American traders may have a different opinion on the events that were discounted before the US session, etc. In other words, the US opening session almost always affects algorithms in one way or another.
Ok, so we want to check the timing of our entries. How do we do this? First, in the analysis window, we shift from Exit time to Entry time.
Changing Time base from Exit time to Entry Time
Then we shift from Daily period to Day of Week period, and see the following chart.
Changing Period from Daily to Day of week - Profits per days for VSH_4
Ok, what do we see here? We see that the trades that were open on Tuesday, Friday, and Saturday bring us the lowest net profit. What do we want to do with this information? Well, I’m going to exclude all the trades that are made on Friday and Saturday. Why do I exclude Friday trades and leave Tuesday trades in the algorithm? The reason is the so-called stable area. If you check our Tuesday neighbors – they are pretty strong days. However, if you check Friday neighbors, you will see that one of them is particularly bad. Because of this reason I want to exclude both of the days and keep Tuesday trades.
What do we get after this additional filter? You can check out the equity curve below (code is available here).
Equity curve of VSH_5 on January 2010 - January 2018 on GC
Well, it’s getting better – we see less and less periods of bad performance. What’s next? Now let’s consider hours. There might be a question – why did we consider days before hours? Well, it’s just my preference. You can shift the order, though I think moving from broad filter to narrower ones is a good strategy. So, what do we see on the hourly chart?
Profits per hour for VSH_5
We see, that 18:00 hours (6 PM) trades produce terrible results. How do you think, what is this time? Well, not surprisingly, it’s the US opening in my local time. In this code, I made an hourly filter to avoid trading during those hours. Please note, that the way you work with Hours in NinjaEditor and in Strategy Analyzer can be different.
Equity curve of VSH_5.1 on January 2010 - January 2018 on GC
Finally, we got 1.3 Profit factor! It’s a small victory, I believe, but there are still a lot of things we can do. For now, let’s briefly analyze what we have done so far with entries and exits efficiencies.
Efficiency shows you how much does the price goes against you in the overall movement captured by the position. It’s much easier to explain efficiency with an example. Suppose you enter long position at $100. The price then falls to $90, goes to $130, and you exit it at $110. The entry efficiency for long trades is calculated using the formula:
(maximum price seen - entry price) / (maximum price seen - minimum price seen)
In our example, your efficiency is 75%. In other words, in the movement of $40 – only 25% (or $10) were against you.
Efficiency for the exits shows the similar thing – how close is your exit to maximum favorable excursion and is calculated using the formula:
So, what is the average entry efficiency for our algorithm? As you can see below, for positive trades it’s about 80%, which is really good. So far it means, that we are doing a good job in terms of entries since entries for negative trades will almost always have bad entry efficiency.
Entry efficiency chart for VSH_5.1 on January 2010 - January 2018 on GC
Ok, so we are good with entries, what about exits? Well, as you can see below, it’s about 50% for positive trades and about 20% for negative trades. And this is the area where we can try and improve our results.
Exit efficiency chart for VSH_5.1 on January 2010 - January 2018 on GC
In our next article, we will focus on exit analysis and consider ways to spot weaknesses, test stop losses and take profits, and start a big and most hard topic - dynamic trades modeling. See you soon!