Data Mining May or May Not Link Causality
A study between ice cream sales and crime rate demonstrated a high level of correlation. However, it would be illogical to assume that buying more ice cream leads to more crime. There are just too many other factors and variables involved to draw a conclusion. So, data mining with EasyLanguage may or may not lead to anything beneficial. One thing is you cannot hang your hat completely on this type of research. A reader of my books asked if there was evidence that pointed to the best time to enter and exit a day trade. Is it better to enter in the morning or in the afternoon or are there multiple trading windows throughout the day? I thought I would try to answer the question using TradeStation’s optimization capabilities.
Create a Search Space of Different Window Opening Times and Open Duration
My approach is just one of a few that can be used to help answer this question. To cut down on time and the size of this blog we will only look at day trading the @ES.D from the long side. The search space boundaries can be defined by when we open the trading window and how long we leave it open. These two variables will be defined by inputs so we can access the optimization engine. Here is how I did it with EasyLanguage code.
The openWindowTime input is the basis from where we open the trading window. We are working with the @ES.D with an open time of 9:30 AM eastern. The openWindowOffset will be incremented in minutes equivalent to the data resolution of the chart, five minutes. We will start by opening the window at 9:35 and leave it open for 60 minutes. The next iteration in the optimization loop will open the window at 9:40 and keep it open for 60 minutes as well. Here are the boundaries that I used to define our search space.
- window opening times offset: 5 to 240 by 5 minutes
- window opening duration: 60 to 240 by 5 minutes
A total of 1739 iterations will span our search space. The results state that waiting for twenty minutes before buying and then exiting 190 minutes later, worked best. But also entering 90 minutes after the open and exiting 4 hours later produced good results as well (no trade execution fee were utilized.) Initially I was going to limit entry to once per day, but then I thought it might be worthwhile to enter a fresh position, if the first one is stopped out, or pyramid if it hasn’t. I also thought, each entry should have its own protective stop amount. Would entering later require a small stop – isn’t most of the volatility, on average, expressed during the early part of the day.
Build a Strategy that Takes on a Secondary Trade as a New Position or One that is Pyramided.
This is not a simple strategy. It sounds simple and it requires just a few lines of code. But there is a trick in assigning each entry with its own exit. As you can see there is a potential for trade overlap. You can get long 20 minutes after the open and then add on 70 minutes (90 from the open) later. If the first position hasn’t been stopped out, then you will pyramid at the second trade entry. You have to tell TradeStation to allow this to happen.
System Rules
- Enter long 20 minutes after open
- Enter long 90 minutes after open
- Exit 1st entry 190 minutes later or at a fixed $ stop loss
- Exit 2nd entry 240 minutes later or at a fixed $ stop loss
- Make sure you are out at the end of the day
Sounds pretty simple, but if you want to use different stop values for each entry, then the water gets very muddy.
AvgEntryPrice versus EntryPrice
Assume you enter long and then you add on another long position. If you examine EntryPrice you will discover that it reflects the initial entry price only. The built-in variable AvgEntryPrice will be updated with the average price between the two entries. If you want to key off of the second entry price, then you will need to do a little math.
avgEntryPrice = (entry price 1 + entry price 2) / 2
or ap = (ep1 + ep2) /2
Using this formula and simple algebra we can arrive at ep2 using this formula: ep2 = 2*ap – ep1. Since we already know ep1 and ap, ep2 is easy to get to. We will need this information and also the functionality of from entry. You tie entries and exits together with the keywords from entry. Here are the entry and exit trade directives.
The trade entry directives are rather simple, but you must use the calcTime function to arrive at the correct entry and exit times. Here we are using the benchmark, openTime and the offsets of entryTime1Offset and entryTime2Offset. This function adds (or subtracts if the offset is negative) the offset to the benchmark. This takes care of when the trading windows open, but you must add the entry1TimeOffset to exit1TimeOffset to calculate the duration the trading window is to remain open. This goes for the second entry window as well.
Now let’s look at the exit directives. Notice how I exit the 1st buy entry with the code from entry (“1st buy”). This ties the entry and exit directives together. This is pretty much straightforward as well. The tricky part arrives when we try to apply different money management stops to each entry. Exiting from the 1st buy requires us to simply subtract the $ in terms of points from entryPrice. We must use our new equation to derive the 2nd entry price when two contracts are concurrent. But what if we get stopped out of the first position prior to entering the second position? Should we continue using the formula. No. We need to fall back to entryPrice or avgEntryPrice: when only one contract or unit is in play, these two variables are equal. We initially assign the variable value1 to the avgEntryPrice and only use our formula when currentShares = 2. This code will work a majority of the time. But take a look at this trade:
This is an anomaly, but anomalies can add up. What happened is we added the second position and the market moved down very quickly – too quickly for the correct entry price to be updated. The stop out (2nd loss) was elected by using the 1st entry price, not the second. You can fix this with the following two solutions:
- Increase data resolution and hope for an intervening bar
- Force the second loss to occur on the subsequent trading bar after entry. This means you will not be stopped out on the bar of entry but will have to wait five minutes or whatever bar interval you are working with.
OK – Now How Do We Make this a Viable Trading System
If you refer back to the optimization results you will notice that the average trade (before execution costs) was around $31. Keep in mind we were trading every day. This is just the beginning of your research – did we find a technical advantage? No. We just found out that you can enter and exit at different times of the trading day, and you can expect a positive outcome. Are there better times to enter and exit? YES. You can’t trade this approach without adding some technical analysis – a reason to enter based on observable patterns. This process is called FILTERING. Maybe you should only enter after range compression. Or after the market closed up on the prior day, or if the market was an NR4 (narrow range 4.) I have added all these filters so you can iterate across them all using the optimization engine. Take a look:
Let’s Search – And Away We Go
I will optimize across the different patterns and range analysis and different $ stops for each entry (1st and 2nd.)
Best Total Profit
Trade when today’s open is greater than yesterday’s close and don’t worry about the volatility. Use $550 for the first entry and $600 for the second.
Best W:L Ratio and Respectable Avg. Trade
This curve was created by waiting for yesterday’s close to be below the prior day’s and yesterday being an NR4 (narrow range 4). And using a $500 protective stop for both the 1st and 2nd entries.
Did We Find the Holy Grail? Gosh No!
This post served two purposes. One, how to set up a framework for data mining and two, create code that can handle things that aren’t apparently obvious – entryPrice versus avgEntryPrice!
Discover more from George Pruitt
Subscribe to get the latest posts sent to your email.
George, do you offer a discounted price for buying multiple books? If so please provide details, thanks.
Hi Alex,
I did not see your comment from early July. So sorry about that. I can give discounts on PDF version of my books only. So yes, if a PDF is acceptable.
Best and apologies,
George
How can I purchase the PDF version?
Also can I pay with PayPal?