Daily Bar Daytrade to 5 Minute Bars — Not So Easy!
Like in the previous post, I designed a simple optimization framework trying to determine the best day of the week to apply a long only volatility based open range break out algorithm on natural gas. You might first ask why natural gas? Its a good market that has seasonal tendencies that can actually be day-traded – there is enough volatility and volume. Anyway, by simply slapping setExitOnClose at the end of your code turns your algorithm into one that exits every day at the settlement (fictional value that is derived via formula) price. You can always use LIB (look inside bar) to help determine the magnitude of intraday swings and there respective chronological order. But you are still getting out at the settlement price – which isn’t accurate. However, if you are trying to get a set of parameters that gets you into the ballpark, then this is a very acceptable approach. But you will always want to turn you daily bar strategy with LIB intro an intraday version for automation and accuracy purposes.
Day of Week Analysis On Intraday Data
When you optimize the day of week on daily bars, the first bar of the week is usually Monday. When testing with intraday bars on futures, the trading actually starts on Sunday evening. If your daily bar analysis reveals the best day of week is Wedneday, then you must inform TradeStation to take the trade from the opening on Tuesday night through the close on Wenesday. If you are executing a market order, then you just need to tell TradeStation to execute at Tuesday night’s open (1800 eastern standard time). The TRICK to allow trading throughout the 24 hour session (1800 to 1700) on just a particular day of the week is to capture the day of the week on the first bar of the trading session. Here is some bookkeeping you can take care of on the first bar of the trading session.
Here, I check to see if the first bar’s time stamp is equal to the sessionStarttime + barInterval. Remember TradeStation shows the close time of each bar as the time stamp. If adding a barInterval to the opening time results in a non-time, then use the calcTime function ( 1800 + 60 = 1860 – a non-time.) I first store the open of the session in my variable todaysOpen. Why not just use openD(0). Well openD(0) is great for sessions that don’t span midnight. If they span midnight then the openD returns the open of the12:00 am bar. Here is the output from August 9th with the open on August 8th at 1800. The two other values are midnight on August 8th and August 7th. So the openD, highD, lowD, and closeD functions involve the 24 hour clock and not the session.
print(todaysOpen:5:5," ",openD(0):5:5," ",openD(1):5:5); 18:00 midnite[0] midnite[1] -------------------------------------- 1230808 2.797 2.712 2.588
On the first bar of the trading session I also capture the day of the week. The days of the week, using intraday data on a 24 hour session, range from Sunday to Thursday, instead of Monday thru Friday. CanBuy is turned on if the day of week equals the input provided by the user. When using daily bars and you optimize the day of the week, remember if you chose 2 or Tuesday you really mean Wednesday. If today is Tuesday, then buy next bar at… This will generate trades only on Wednesdays. When testing on intraday data you do not need to make a modification for the day of week. If the first bar of the trading session is Tuesday, then you will actually trade what is considered the Wednesday session that starts on Tuesday evening at 18:00.
What About Holidays?
Here is a quick trick to not trade on Holidays. The daily bar does not include preempted sessions in its database, whereas intraday data does. So, if at 18:00 the date of the prior bar does not match the date of the daily bar located in the Data2 slot, then you know that the prior day was preempted and you should not trade today. In other words, Data2 is missing. Remember, you only want to trade on days that are included in the daily bar database in your attempt to replicate the daily bar strategy.
date close moving average 1230703.00 data2 2.57382 2.70300 1230704.00 missing data2 2.57382 2.70300 <-- no trade 1230705.00 data2 2.57485 2.65100 <-- matches
How to count the daily bars since entry.
Here again you need to do some bookkeeping. If you are long on the first bar of the day, then you have been in the trade for at least a day. If you are long on two first bars of the trading session, then you have been long for two days. I use the variable, curTradeDays, to count the number of days we have been in the trade. If on the first bar of the trading session we are flat, then I reset curTradeDays to zero. Otherwise, I increment the variable and this only occurs once a day. You can optimize the daysInTrade input, but for this post we are just interested in getting out at the close on the day of entry.
Controlling one entry per day.
We only want one long entry per day in an attempt to replicate the daily bar system. There are several ways to do this, but comparing the current bar’s marketPosition to the prior bar’s value will inform you if a position has been transitioned from flat to long. If the prior bar’s position if flat and the current bar is now long, then we can turn our canBuy off or to false. If we get stopped out later and the market rallies again to our break out level, we will not execute at that level, because the trade directive will not be issued. Why not use the entriesToday function? Again midnight cause this function to reset. Say you go long at 22:00 and you do not want to enter another position until the tomorrow at 18:00. EntriesToday will forget you entered the long position at 22:00.
1220726.00 2350.00 entries today 1.00 1220726.00 2355.00 entries today 1.00 1220727.00 0.00 entries today 0.00 < not true!
Cannot execute on the first bar of the day – does it matter?
Since this is an open range break out, we need to know what the open is prior to our break out calculation. Could the break out level be penetrated on the first 5 minute bar? Sure, but I bet it is rare. I tested this and it only occurred 4 or 5 times. In a back-test, if this occurs and the subsequent bar’s open is still above the break out level, TradeStation will convert the stop order to a market order and you will buy the open of the 2nd five minute bar. In real time trading, you must tell TradeStation to wait until the end of the first 5 minute bar before issuing the trade directive to replicate your back testing. If this is an issue, you can test with 5 minute bars, but execute on 1 minute bars. In other words, have your testing chart and an additional execution chart. Skipping the first 5 minute bar may be advantageous. Many times you will get a surge at the open and then the market falls back. By the time the first 5 minute bar concludes the market may be trading back below your break out level. You might bypass the bulge. But your still in the game. Here is how you can test the significance of this event.
By the end of the first bar of the day we know the open, high, low and close of the day thus far. We can test to see if the high would have penetrated the break out level and also calculate the profit or loss of the trade. In a back-test you will not miss this trade if the next bar’s open is still above the break out level. If the next bar’s open is below the break out level, then you may have missed a fake out break out. Again this is a rare event.
The rest of the code in its entirety
We are using the setStopLoss and setProfitTarget functions in this code. But remember, if your continuous contract goes negative, then these functions will not work properly.
This type of programming is tricky, because you must use tricks to get EasyLanguage to do what you want it to do. You must experiment, debug, and program ideas to test the limitations of EasyLanguage to hone your craft.
Discover more from George Pruitt
Subscribe to get the latest posts sent to your email.