Have you discovered a seasonal tendency but can’t figure out how to test it?
Are there certain times of the year when a commodity increases in price and then recedes? In many markets this is the case. Crude oil seems to go up during the summer months and then chills out in the fall. It will rise once again when winter hits. Early spring might show another price decline. Have you done your homework and recorded certain dates of the year to buy/sell and short/buyToCover. Have you tried to apply these dates to a historical back test and just couldn’t figure it out? In this post I am going to show how you can use arrays and loops to cycle through the days in your seasonal database (database might be too strong of a term here) and apply long and short entries at the appropriate times during the past twenty years of daily bar data.
Build the Quasi-Database with Arrays
If you are new to EasyLanguage you may not yet know what arrays are or you might just simply be scared of them. Now worries here! Most people bypass arrays because they don’t know how to declare them, and if they get passed that, how to manipulate them to squeeze out the data they need. You may not be aware of it, but if you have programmed in EasyLanguage the least bit, then you have already used arrays. Check this out:
if high > high[1] and low > low[1] and average(c,30) > average(c,30)[1] then buy next bar at open
In reality the keywords high and low are really arrays. They are lists that contain the entire history of the high and low prices of the data that is plotted on the chart. And just like with declared arrays, you index these keywords to get historic data. the HIGH[1] means the high of yesterday and the HIGH[2] means the high of the prior day. EasyLanguage handles the management of these array-like structures. In other words, you don’t need to keep track of the indexing – you know the [1] or [2] stuff. The declaration of an array is ultra-simple once you do it a few times. In our code we are going to use four arrays:
Buy array
SellShort array
Sell array
BuyToCover array
Each of these arrays will contain the month and day of month when a trade is to be entered or exited. Why not the year? We want to keep things simple and buy/short the same time every year to see if there is truly a seasonal tendency. The first thing we need to do is declare the four arrays and then fill them up.
// use the keyword arrays and : semicolon // next give each array a name and specify the // max number of elements that each array can hold // the [100] part. Each array needs to be initialized // and we do this by placing a zero (0) in parentheses arrays: buyDates[100](0),sellDates[100](0), shortDates[100](0),buyToCoverDates[100](0);
// next you want the arrays that go together to have the same // index value - take a look at this
// note the buyDates[1] has a matching sellDates[1] // buyDates[2] has a matching sellDates[2] // -- and -- // shortDates[1] has a matching buyToCoverDates[1] // shortDates[2] has a matching buyToCoverDates[2]
Our simple database has been declared, initialized and populated. This seasonal strategy will buy on:
April 15th and Exit on May 15th
June 5th and Exit on August 30th
It will sellShort on:
September 15th and Cover on November 30th
February 15th and Cover on March 30th
You could use this template and follow the pattern to add more dates to your database. Just make sure nothing overlaps.
Now, each chart has N dates of history plotted from the left to right. TradeStation starts out the test from the earliest date to the last date. It does this by looping one day at a time. The first thing we need to do is convert the bar date (TradeStation represents dates in a weird format – YYYMMDD – Jan 30, 2022 is represented by the number 1220130 – don’t ask why!!) to a format like the data that is stored in our arrays. Fortunately, we don’t have to deal with the year and EasyLanguage provides two functions to help us out.
Month(Date) = the month (1-12) of the current bar
DayOfMonth(Date) = the day of the month of the current bar
All we need to do is use these functions to convert the current bar’s date into terms of our database, and then test that converted date against our database. Take a look:
//convert the date into our own terms //say the date is December 12 //the month function returns 12 and the day of month returns 12 // 12*100 + 12 = 1212 --> MMDD - just waht we need //notice I look at the date of tomorrow because I want to take //action on the open of tomorrow.
currentMMDD = month(d of tomorrow)*100 + dayOfMonth(d of tomorrow);
//You might need to study this a little bit - but I am looping through each //array to determine if a trade needs to be entered. //Long Seasonal Entries toggle buyNow = False; for n = 1 to numBuyDates Begin if currentMMDD[1] < buyDates[n] and currentMMDD >= buyDates[n] Then Begin buyNow = True; end; end;
//Short Seasonal Entries toggle shortNow = False; for n = 1 to numshortDates Begin if currentMMDD[1] < shortDates[n] and currentMMDD >= shortDates[n] Then Begin shortNow = True; end; end;
Date conversion and looping thru Database
This code might look a little daunting, but it really isn’t. The first for-loop starts at 1 and goes through the number of buyDates. The index variable is the letter n. The loop starts at 1 and goes to 2 in increments of 1. Study the structure of the for-loop and let me know if you have any questions. What do you think this code is doing.
if currentMMDD[1] < buyDates[n] and currentMMDD >= buyDates[n] Then
As you know the 15th of any month may fall on a weekend. This code basically says, ” Okay if today is less than the buyDate and tomorrow is equal to or greater than buyDate, then tommorrow is either going to be the exact day of the month or the first day of the subsequent week (the day of month fell on a weekend.) If tomorrow is a trade date, then a conditional buyNow is set to True. Further down in the logic the trade directive is issued if buyNow is set to True.
Total of 4 loops – 2 for each long/short entry and 2 for each long/short exit.
// fill the arrays with dates - remember we are not pyramiding here // use mmdd format buyDates[1] = 0415;sellDates[1] = 0515; buyDates[2] = 0605;sellDates[2] = 0830; numBuyDates = 2; numSellDates = 2;
mp = marketPosition; currentMMDD = month(d of tomorrow)*100 + dayOfMonth(d of tomorrow);
//Long Seasonal Entries toggle buyNow = False; for n = 1 to numBuyDates Begin if currentMMDD[1] < buyDates[n] and currentMMDD >= buyDates[n] Then Begin buyNow = True; end; end;
//Short Seasonal Entries toggle shortNow = False; for n = 1 to numshortDates Begin if currentMMDD[1] < shortDates[n] and currentMMDD >= shortDates[n] Then Begin shortNow = True; end; end;
//Long Seasonal Exits toggle sellNow = False; if mp = 1 Then Begin for n = 1 to numSellDates Begin if currentMMDD[1] < sellDates[n] and currentMMDD >= sellDates[n] Then Begin sellNow = True; end; end; end;
//Short Seasonal Exits toggle buyToCoverNow = False; if mp = -1 Then Begin for n = 1 to numBuyToCoverDates Begin if currentMMDD[1] < buyToCoverDates[n] and currentMMDD >= buyToCoverDates[n] Then Begin buyToCoverNow = True; end; end; end;
// Long entry execution if buyNow = True then begin buy("Seas-Buy") next bar at open; end; // Long exit execution if mp = 1 then begin if sellNow then begin sell("Long Exit") next bar at open; end; end;
// Short entry execution if shortNow then begin sellShort("Seas-Short") next bar at open; end; // short exit execution if mp = -1 then begin if buyToCoverNow then begin buyToCover("short Exit") next bar at open; end; end;
Does it work? It does – please take my word for it. IYou can email me with any questions. However, TS 10 just crashed on me and is wanting to update, but I need to kill all the processes before it can do a successful update. Remember to always export your new code to an external location. I will post an example on Monday Jan 30th.
This pattern has been around for many years, and is still useful today in a day trading scheme. The pattern is quite simple: if today’s high exceeds yesterday’s high by a certain amount, then sell short as the market moves back through yesterday’s high. There are certain components of yesterday’s daily bar that are significant to day traders – the high, the low, the close and the day traders’ pivot. Yesterday’s high is considered a level of resistance and is often tested. Many times the market has just enough momentum to carry through this resistance level, but eventually peters out and then the bears will jump in and push the market down even more. The opposite is true when the bulls take over near the support level of yesterday’s low. Here is an example of Clear Out short and buy.
1st the high of yesterday is cleared out and then the low of yesterday.
How Do You Program this Simple Pattern?
The programming of this strategy is rather simple, if you are day trading. The key components are toggles that track the high and low of the day as the market penetrate the prior day’s high and low. Once the toggles are flipped on, then order directives can be placed. A max. trade stop loss can easily be installed via the SetStopLoss(500) function. You will also want to limit the number of entries, because in a congestive phase, this pattern could fire off multiple times. Once you intuitively program this, you will almost certainly run into an issue where a simple “trick” will bail you out. Remember the code does exactly what you tell it to do. Take a look at these trades.
When Back Testing TradeStation will convert stop orders to market orders.
On a Back Test, Stop Orders are Converted to Market Orders if Price Exceeds the Stop Level
In these example trades, the first trade is accurate as it buys yesterday’s low + one tick and then gets stopped out. Once a long is entered, the system logic requires the market to trade back below yesterday’s low before a long another entry is signaled at yesterday’s low. Here as you can see, the initial buy toggle is set to True and when a long position is entered the buy toggle is turned off. The market knee jerks back below yesterday’s low and stops out your long position. Since TradeStation’s paradigm is based on “next bar” execution, a long entry doesn’t occur as the wide bar crosses back up through yesterday’s low. This is a “bang-bang” situation as it happened very quickly. In a perfect world, you should have been quickly stopped out and re-entered back long at your price. However, the toggle isn’t turned back on until the low of the current bar falls a short distance below yesterday’s low. Since this toggle isn’t set before the market takes off, you don’t get your price. The toggle is eventually turned on and a buy stop order is issued and you can tell you get a ton of slippage. You actually buy the next bar’s open after the bar where the toggle was turned on. I dropped down to a one minute bar and still didn’t get the trade. A 10 second bar did generate the exit and re-entry at the correct levels, however. It did this because, the 10 second bar turned the toggle on in time for the stop order to be generated accurately.
Using a 10-second bar an accurate exit and entry were generated.
Okay – Can you rely on a 5 minute bar then?
Five minute bar data has been the staple of day trading systems for many years. However, if you want to test “bang-bang” algorithms you are probably better off dropping down to a N-seconds bar. However, this strategy as a whole is not “bang-bang” so with a little trick you can get more accurate entries and exits.
What’s the Trick?
In real-time trading, buy-stop orders below the market are rejected. So, the second and third trades that were presented would never have taken place. But, the backtest reflects the trades, and if you include execution costs, the performance might nudge you into not trading a possibly viable system. You can take advantage of the “next bar” paradigm by forcing the close of the current bar to be below a buy-stop price and above a sell short stop price. Does this trade look better? Again in a perfect world, you would have re-entered long on the wide bar that stopped us out. But I guarantee you a fast market condition was in effect. All a broker has to say to you when you complain about a fill is, “Sorry Dude! It was a fast market. Not held!” I can’t tell you how many times I requested a printout of fills over a few seconds from my brokers. It is like when a football coach tosses the RED FLAG. During the Pit Days you had a chance to get a fill cash adjustment because the broker was human and maybe he or she didn’t react quickly enough. But when electronic trade matching took over, an adjustment was highly unlikely. Heck, you sign off on this when you accept the terms of electronic trading. Fills are rarely made better.
The second and third trade don’t occur because you force the buy stop order to be valid.
How Does the Trick Affect Performance?
Here are the results over the past four months on different time frame resolutions.
10 Seconds Resolution.
10 seconds bar would be the most accurate if slippage is acceptable. And that is a big assumption on “bang-bang” days.
1 minute bar resolution.
The one minute bar is close but September is substantially off. Probably some “bang-bang” action.
5 minute bar resolution with “Trick”
This is close to the 10-second bar result. Fast market or “bang-bang” trades were reduced or eliminated with the “trick”.
5 minute bar resolution without “Trick.”
Surprisingly, the 5 minute bar without the “Trick” resembles the 10 seconds results. But we know this is not accurate as trades are fired off in a manner that goes against reality.
The two following table shows the impact of a $15 RT comm./slipp. per trade charge.
Without “Trick” and $15 RTWith “Trick” and $15 RT
Okay, Now That We Have That Figured Out How Do You Limit Trading After a Daily Max. Loss
Another concept I wanted to cover was limiting trades after a certain loss level was suffered on a daily basis. In the code, I only wanted to enter trades as long as the max. daily loss was less than or equal to $1,000 A fixed stop of $500 on a per trade basis was utilized as well. So, if you suffered two max. stop losses right off the bat ($1,000), you could still take one more trade. Now if you had a $500 winner and two $500 losses you could still take another trade.
Ouch! Two max losses, but still could take a third trade. Ouch again – stupid system.Should I take that second trade? I just suffered three losses in a row. What to do? What to do? Damn straight you better that trade.
If you are going to trade a system, you better trade it systematically!
Now Onto the Code
//Illustrate trade stoppage after a certain loss has been //experienced and creating realistic stop orders.
if t = startTime then begin coBuy = False; coShort = False; beginOfDayProfit = netProfit; beginOfDayTotTrades = totalTrades; end;
canTrade = iff(t >=startTime and t < sess1EndTime,1,0);
if t >= startTime and h > highD(1) + clrOutBuyPer*(highD(1)-lowD(1)) then begin coShort = True; end;
if t >= startTime and l < lowD(1) - clrOutShortPer*(highD(1)-lowD(1)) then begin coBuy = True; end;
mp = marketPosition;
if canTrade = 1 and coShort and netProfit >= beginOfDayProfit - maxDailyLoss and c > highD(1) - minMove/priceScale then sellShort next bar at highD(1) - minMove/priceScale stop;
if mp = -1 then // toggle to turn off coShort - must wait for set up begin coShort = False; end;
if canTrade = 1 and coBuy and netProfit >= beginOfDayProfit - maxDailyLoss and c < lowD(1) + minMove/priceScale then buy next bar at lowD(1) + minMove/priceScale stop;
if mp = 1 then begin coBuy = False; end;
setStopLoss(500); setExitOnClose;
Strategy in its Entirety
You need to capture the NetProfit sometime during the day before trading commences. This block does just that.
if t = startTime then begin coBuy = False; coShort = False; beginOfDayProfit = netProfit; beginOfDayTotTrades = totalTrades; end;
Snippet that captures NetProfit at start of day
Now all you need to do is compare the current netProfit (EL keyword) to the beginOfDayProfit (user variable). If the current netProfit >= beginOfDayProfit – maxDailyLoss (notice I programmed greater than or equal to), then proceed with the next trade. The rest of the logic is pretty self explanatory, but to drive the point home, here is how I make sure a proper stop order is placed.
if canTrade = 1 and coShort and netProfit >= beginOfDayProfit - maxDailyLoss and c > highD(1) - minMove/priceScale then sellShort next bar at highD(1) - minMove/priceScale stop;
if mp = -1 then // toggle to turn off coShort - must wait for set up begin coShort = False; end;
Notice how I use the current bars Close - C and How I toggle coShort to False
If You Like This – Make Sure You Get My Hi-Res Edition of Easing Into EasyLanguage
This is a typical project I discuss in the second book in the Easing Into EasyLanguage Trilogy. I have held over the BLACK FRIDAY special, and it will stay in effect through December 31st. Hurry, and take advantage of the savings. If you see any mistakes, or just want to ask me a question, or have a comment, just shoot me an email.
I had to wrap up Part -1 rather quickly and probably didn’t get my ideas across, completely. Here is what we did in Part – 1.
used my function to locate the First Notice Date in crude
used the same function to print out exact EasyLanguage syntax
chose to roll eight days before FND and had the function print out pure EasyLanguage
the output created array assignments and loaded the calculated roll points in YYYMMDD format into the array
visually inspected non-adjusted continuous contracts that were spliced eight days before FND
appended dates in the array to match roll points, as illustrated by the dip in open interest
Step 6 from above is very important, because you want to make sure you are out of a position on the correct rollover date. If you are not, then you will absorb the discount between the contracts into your profit/loss when you exit the trade.
Step 2 – Create the code that executes the rollover trades
Here is the code that handles the rollover trades.
// If in a position and date + 1900000 (convert TS date format to YYYYMMDD), // then exit long or short on the current bar's close and then re-enter // on the next bar's open
if d+19000000 = rollArr[arrCnt] then begin condition1 = true; arrCnt = arrCnt + 1; if marketPosition = 1 then begin sell("LongRollExit") this bar on close; buy("LongRollEntry") next bar at open; end; if marketPosition = -1 then begin buyToCover("ShrtRollExit") this bar on close; sellShort("ShrtRollEntry") next bar at open; end;
end;
Code to rollover open position
This code gets us out of an open position during the transition from the old contract to the new contract. Remember our function created and loaded the rollArr for us with the appropriate dates. This simulation is the best we can do – in reality we would exit/enter at the same time in the two different contracts. Waiting until the open of the next bar introduces slippage. However, in the long run this slippage cost may wash out.
Step 3 – Create a trading system with entries and exits
The system will be a simple Donchian where you enter on the close when the bar’s high/low penetrates the highest/lowest low of the past 40 bars. If you are long, then you will exit on the close of the bar whose low is less than the lowest low of the past 20 bars. If short, get out on the close of the bar that is greater than the highest high of the past twenty bars. The first test will show the result of using an adjusted continuous contract rolling 8 days prior to FND
Nice Trade. Around August 2014
This test will use the exact same data to generate the signals, but execution will take place on a non-adjusted continuous contract with rollovers. Here data2 is the adjusted continuous contract and data1 is the non-adjusted.
Same Trade but with rollovers
Still a very nice trade, but in reality you would have to endure six rollover trades and the associated execution costs.
Conclusion
Here is the mechanism of the rollover trade.
Roll out of old contract and roll into new contract
And now the performance results using $30 for round turn execution costs.
No-Rollovers
No Rollovers?
Now with rollovers
Many more trades with the rollovers!
The results are very close, if you take into consideration the additional execution costs. Since TradeStation is not built around the concept of rollovers, many of the trade metrics are not accurate. Metrics such as average trade, percent wins, average win/loss and max Trade Drawdown will not reflect the pure algorithm based entries and exits. These metrics take into consideration the entries and exits promulgated by the rollovers. The first trade graphic where the short was held for several months should be considered 1 entry and 1 exit. The rollovers should be executed in real time, but the performance metrics should ignore these intermediary trades.
I will test these rollovers with different algorithms, and see if we still get similar results, and will post them later. As you can see, testing on non-adjusted data with rollovers is no simple task. Email me if you would like to see some of the code I used in this post.
When I worked at Futures Truth, we tested everything with our Excalibur software. This software used individual contract data and loaded the entire history (well, the part we maintained) of each contract into memory and executed rollovers at a certain time of the month. Excalibur had its limitations as certain futures contracts had very short histories and rollover dates had to be predetermined – in other words, they were undynamic. Over the years, we fixed the short history problem by creating a dynamic continuous contract going back in time for the number of days required for a calculation. We also fixed the database with more appropriate rollover frequency and dates. So in the end, the software simulated what I had expected from trading real futures contracts. This software was originally written in Fortran and for the Macintosh. It also had limitations on portfolio analysis as it worked its way across the portfolio, one complete market at a time. Even with all these limitations, I truly thought that the returns more closely mirrored what a trader might see in real time. Today, there aren’t many, if any, simulation platforms that test on individual contracts. The main reasons for this are the complexity of the software, and the database management. However, if you are willing to do the work, you can get close to testing on individual contract data with EasyLanguage.
Step 1 – Get the rollover dates
This is critical as the dates will be used to roll out of one contract and into another. In this post, I will test a simple strategy on the crude futures. I picked crude because it rolls every month. Some data vendors use a specific date to roll contracts, such as Pinnacle data. In real time trading, I did this as well. We had a calendar for each month, and we would mark the rollover dates for all markets traded at the beginning of each month. Crude was rolled on the 11th or 12th of the prior month to expiration. So, if we were trading the September 2022 contract, we would roll on August 11th. A single order (rollover spread) was placed to sell (if long) the September contract and buy the October contract at the market simultaneously. Sometimes we would leg into the rollover by executing two separate orders – in hopes of getting better execution. I have never been able to find a historic database of when TradeStation performs its rollovers. When you use the default @CL symbol, you allow TradeStation to use a formula to determine the best time to perform a rollover. This was probably based on volume and open interest. TradeStation does allow you to pick several different rollover triggers when using their continuous data.
You can choose type of trigger – (3) Dynamic or (4) Time based.
I am getting ahead of myself, because we can simply use the default @CL data to derive the rollover dates (almost.) Crude oil is one of those weird markets where LTD (last trade days) occurs before FND (first notice day.) Most markets will give you a notice before they back up a huge truck and dump a 1000 barrels of oil at your front door. With crude you have to be Johnny on the spot! Rollover is just a headache when trading futures, but it can be very expensive headache if you don’t get out in time. Some markets are cash settled so rollover isn’t that important, but others result in delivery of the commodity. Most clearing firms will help you unwind an expired contract for a small fee (well relatively small.) In the good old days your full service broker would give you heads up. They would call you and say, “George you have to get out of that Sept. crude pronto!” Some firms would automatically liquidate the offending contract on your behalf – which sounds nice but it could cost you. Over my 30 year history of trading futures I was caught a few times in the delivery process. You can determine these FND and LTD from the CME website. Here is the expiration description for crude futures.
Trading terminates 3 business day before the 25th calendar day of the month prior to the contract month. If the 25th calendar day is not a business day, trading terminates 4 business days before the 25th calendar day of the month prior to the contract month.
You can look this up on your favorite broker’s website or the handy calendars they send out at Christmas. Based on this description, the Sept. 2022 Crude contract would expire on August 20th and here’s why
August 25 is Tuesday
August 24 is Monday- DAY1
August 21 is Friday – DAY2
August 20 is Thursday – DAY3
This is the beauty of a well oiled machine or exchange. The FND will occur exactly as described. All you need to do is get all the calendars for the past ten years and find the 25th of the month and count back three business days. Or if the 25 falls on a weekend count back four business days. Boy that would be chore, would it not? Luckily, we can have the data and an EasyLanguage script do this for us. Take a look at this code and see if it makes any sense to you.
Case "@CL": If dayOfMonth(date) = 25 and firstMonthPrint = false then begin print(date[3]+19000000:8:0); firstMonthPrint = true; end; If(dayOfMonth(date[1]) < 25 and dayOfMonth(date) > 25 ) and firstMonthPrint = false then begin print(date[4]+19000000:8:0); firstMonthPrint = true; end;
Code to printout all the FND of crude oil.
I have created a tool to print out the FND or LTD of any commodity futures by examining the date. In this example, I am using a Switch-Case to determine what logic is applied to the chart symbol. If the chart symbol is @CL, I look to see if the 25th of the month exists and if it does, I print the date 3 days prior out. If today’s day of month is greater than 25 and the prior day’s day of month is less than 25, I know the 25th occurred on a weekend and I must print out the date four bars prior. These dates are FN dates and cannot be used as is to simulate a rollover. You had best be out before the FND to prevent the delivery process. Pinnacle Date rolls the crude on the 11th day of the prior month for its crude continuous contracts. I aimed for this day of the month with my logic. If the FND normally fell on the 22nd of the month, then I should back up either 9 or 10 business days to get near the 11th of the month. Also I wanted to use the output directly in an EasyLanguage strategy so I modified my output to be exact EasyLanguage.
Case "@CL": If dayOfMonth(date) = 25 and firstMonthPrint = false then begin value1 = value1 + 1; print("rollArr[",value1:1:0,"]=",date[9]+19000000:8:0,";"); firstMonthPrint = true; end; If(dayOfMonth(date[1]) < 25 and dayOfMonth(date) > 25 ) and firstMonthPrint = false then begin value1 = value1 + 1; print("rollArr[",value1:1:0,"]=",date[10]+19000000:8:0,";"); // print(date[4]+19000000:8:0); firstMonthPrint = true; end;
Code to print our 9 or 10 bars prior to FND in actual EasyLanguage
Now. that I had the theoretical rollover dates for my analysis I had to make sure the data that I was going to use matched up exactly. As you saw before, you can pick the rollover date for your chart data. And you can also determine the discount to add or subtract to all prior data points based on the difference between the closing prices at the rollover point. I played around with the number of days prior to FND and selected non adjusted for the smoothing of prior data.
Actual data I simulated rollovers with.
How did I determine 8 days Prior to First Notice Date? I plotted different data using a different number of days prior and determined 8 provided a sweet spot between the old and new contract data’s open interest. Can you see the rollover points in the following chart? Ignore the trades – these were a beta test.
The Open Interest Valley is the rollover date.
The dates where the open interest creates a valley aligned very closely with the dates I printed out using my FND date finder function. To be safe, I compared the dates and fixed my array data to match the chart exactly. Here are two rollover trades – now these are correct.
Using an adjusted continuous contract you would not see these trades.
This post turned out to be a little longer than I thought, so I will post the results of using an adjusted continuous contract with no rollovers, and the results using non-adjusted concatenated contracts with rollovers. The strategy will be a simple 40/20 bar Donchian entry/exit. You maybe surprised by the results – stay tuned.
Well it’s been a year, this month, that Murray passed away. I was fortunate to work with him on many of his projects and learned quite a bit about inter-market convergence and divergence. Honestly, I wasn’t that into it, but you couldn’t argue with his results. A strategy that he developed in the 1990s that compared the Bond market with silver really did stand the test of time. He monitored this relationship over the years and watched in wane. Murray replaced silver with $UTY.
The PHLX Utility Sector Index (UTY) is a market capitalization-weighted index composed of geographically diverse public utility stocks.
He wrote an article for EasyLanguage Mastery by Jeff Swanson where he discussed this relationship and the development of inter-market strategies and through statistical analysis proved that these relationships added real value.
I am currently writing Advanced Topics, the final book in my Easing Into EasyLanguage trilogy, and have been working with Murray’s research. I am fortunate to have a complete collection of his Futures Magazine articles from the mid 1990s to the mid 2000s. There is a quite a bit of inter-market stuff in his articles. I wanted, as a tribute and to proffer up some neat code, to show the performance and code of his Bond and $UTY inter-market algorithm.
Here is a version that he published a few years ago updated through June 30, 2022 – no commission/slippage.
Murray’s Bond and $UTY inter-market Strategy
Not a bad equity curve. To be fair to Murray he did notice the connection between $UTY and the bonds was changing over the past couple of year. And this simple stop and reverse system doesn’t have a protective stop. But it wouldn’t look much different with one, because the system looks at momentum of the primary data and momentum of the secondary data and if they are in synch (either positively or negatively correlated – selected by the algo) an order is fired off. If you simply just add a protective stop, and the momentum of the data are in synch, the strategy will just re-enter on the next bar. However, the equity curve just made a new high recently. It has got on the wrong side of the Fed raising rates. One could argue that this invisible hand has toppled the apple cart and this inter-market relationship has been rendered meaningless.
Murray had evolved his inter-market analysis to include state transitions. He not only looked at the current momentum, but also at where the momentum had been. He assigned the transitions of the momentum for the primary and secondary markets a value from one to four and he felt this state transition helped overcome some of the coupling/decoupling of the inter-market relationship.
However, I wanted to test Murray’s simple strategy with a fixed $ stop and force the primary market to move from positive to negative or negative to positive territory while the secondary market is in the correct relationship. Here is an updated equity curve.
George’s Adaptation and using a $4500 stop loss
This equity curve was developed by using a $4500 stop loss. Because I changed the order triggers, I reoptimized the length of the momentum calculations for the primary and secondary markets. This curve is only better in the category of maximum draw down. Shouldn’t we give Murray a chance and reoptimize his momentum length calculations too! You bet.
Murray Length Optimizations
These metrics were sorted by Max Intraday Draw down. The numbers did improve, but look at the Max Losing Trade value. Murray’s later technology, his State Systems, were a great improvement over this basic system. Here is my optimization using a slightly different entry technique and a $4500 protective stop.
Standing on the Shoulders of a Giant
This system, using Murray’s overall research, achieved a better Max Draw Down and a much better Max Losing Trade. Here is my code using the template that Murray provided in his articles in Futures Magazine and EasyLanguage Mastery.
// Code by Murray Ruggiero // adapted by George Pruitt
If Type=0 Then Begin InterInd=Close of Data(InterSet)-CLose[LenInt] of Data(InterSet); MarkInd=CLose-CLose[LenTr]; end;
If Type=1 Then Begin InterInd=Close of Data(InterSet)-Average(CLose of Data(InterSet),LenInt); MarkInd=CLose-Average(CLose,LenTr); end;
if Relate=1 then begin If InterInd > 0 and MarkInd CROSSES BELOW 0 and LSB>=0 then Buy("GO--Long") Next Bar at open; If InterInd < 0 and MarkInd CROSSES ABOVE 0 and LSB<=0 then Sell Short("GO--Shrt") Next Bar at open;
end; if Relate=0 then begin If InterInd<0 and MarkInd CROSSES BELOW 0 and LSB>=0 then Buy Next Bar at open; If InterInd>0 and MarkInd CROSSES ABOVE 0 and LSB<=0 then Sell Short Next Bar at open; end;
Here the user can actually include more than two data streams on the chart. The InterSet input allows the user to choose or optimize the secondary market data stream. Momentum is defined by two types:
Type 0: Intermarket or secondary momentum simply calculated by close of data(2) – close[LenInt] of date(2) and primary momentum calculated by close – close[LenTr]
Type 1: Intermarket or secondary momentum calculated by close of data(2) – average( close of data2, LenInt) and primary momentum calculated by close – average(close, LenTr)
The user can also input what type of Relationship: 1 for positive correlation and 0 for negative correlation. This template can be used to dig deeper into other market relationships.
George’s Modification
I simply forced the primary market to CROSS below/above 0 to initiate a new trade as long the secondary market was pointing in the right direction.
If InterInd > 0 and MarkInd CROSSES BELOW 0 and LSB>=0 then Buy("GO--Long") Next Bar at open; If InterInd < 0 and MarkInd CROSSES ABOVE 0 and LSB<=0 then Sell Short("GO--Shrt") Next Bar at open;
Using the keyword CROSSES
This was a one STATE transition and also allowed a protective stop to be used without the strategy automatically re-entering the trade in the same direction.
Thank You Murray – we sure do miss you!
Murray loved to share his research and would want us to carry on with it. I will write one or two blogs a year in tribute to Murray and his invaluable research.
Sometimes you just want to create a combined equity curve of several markets and for one reason or another you don’t want to use Maestro. This post will show you an indicator that you can insert into your TradeStation chart/strategies that will output monthly cumulative equity readings. After that I refresh my VBA skills a little bit by creating a VBA Macro/Script that will take the output and parse it into a table where the different months are the rows and the different market EOM equities, for those months, will be the columns. Then all the rows will be summed and then finally a chart will be produced. I will zip the Exel .xlsm and include it at the end of this post.
Part 1: Output Monthly Data to Print Log
You can determine the end of the month by comparing the current month of the date and the month of the prior date. If they are different, then you know you are sitting on the first trading day of the new month. You can then reach back and access the total equity as of yesterday. If you want you can also track the change in month equity. Here is the indicator code in EasyLanguage.
// Non plotting indicator that needs to be applied to // a chart that also has a strategy applied // one that produces a number of trades
if month(date) <> month(date[1]) then begin monthlyEqu = totalEquity[1] - priorMonthEqu; priorMonthEqu = totalEquity[1]; print(getSymbolName,",",month(date[1]):2:0,"-",year(date[1])+1900:4:0,",",monthlyEqu,",",totalEquity); end; totalEquity = i_ClosedEquity;
Indicator that exports the EOM equity values
The interesting part of this code is the print statement. You can use the month and year functions to extract the respective values from the date bar array. I use a formatted print to export the month and year without decimals. Remember 2021 in TradeStation is represented by 121 and all you have to do is add 1900 to get a more comfortable value. The month output is formatted with :2:0 and the year with :4:0. The first value in the format notation informs the computer to allow at least 2 or 4 values for the month and the year respectively. The second value following the second colon informs the computer that you do not want any decimals values (:0). Next insert the indicator into all the charts in your workspace. Here’s a snippet of the output on one market. I tested this on six markets and this data was the output generated. Some of the markets were interspersed and that is okay. You may have a few months of @EC and then a few months of @JY and then @EC again.
If you use this post’s EXCEL workbook with the macro you may need to tell EXCEL to forget any formatting that might already be inside the workbook. Open my workbook and beware that it will inform/warn you that a macro is located inside and that it could be dangerous. If you want to go ahead and enable the macro go ahead. On Sheet1 click in A1 and place the letter “G”. Then goto the Data Menu and then the Data Tools section of the ribbon and click Text to Columns. A dialog will open and ask you they type of file that best describes your data. Click the Delimited radio button and then next. You should see a dialog like this one.
Clear Any Left Over Formatting
Now copy all of the data from the Print-Log and paste it into Column A. If all goes right everything will be dumped in the appropriate rows and in a single column.
The fields will be dumped into a single column
First select column A and go back to the Data Menu and the Data Tools on the Ribbon. Select Delimited and hit next, Choose comma because we used commas to separate our values. Click Next again. Eventually you will get to this dialog.
Make sure you use Date [MDY] for the second column of data.If you chose Date format for column 2 with MDY, then it will be imported into the appropriate column in a date format. This makes charting easier. If all goes well then you will have four columns of data – a column for Symbol, Date, Delta-EOM and EOM. Select column B and right click and select Format Cells.
Clean Up column B by formatting cells and customizing the date format
Once you format the B column in the form of MM-YYYY you will have a date like 9-2007, 10-2007, 11-2007…
Running the VBA Macro/Script
On the Ribbon in EXCEL goto the Developer Tab. If you don’t see it then you will need to install it. Just GOOGLE it and follow their instructions. Once on the Develop Tab goto the Code category and click the Visual Basic Icon. Your VBA IDE will open and should like very similar to this.
VBA IDE [integrated development environment]If all goes according to plan after you hit the green Arrow for the Run command your spreadsheet should like similar to this.
EOM table with Months as Rows and Individual Market EOMs as columns
The Date column will be needed to be reformatted again as Date with Custom MM-YYYY format. Now copy the table by highlighting the columns and rows including the headings and then goto to Insert Menu and select the Charts category.
This is what you should get.
Merged Equity on Closed Trade Basis
This is the output of the SuperTurtle Trading system tested on the currency sector from 2007 to the present.
VBA Code
I have become very spoiled using Python as it does many things for you by simply calling a function. This code took my longer to develop than I thought it would, because I had to back to the really old school of programming (really wanted to see if I could do this from scratch) to get this done. You could of course eliminate some of my code by calling spread sheet functions from within EXCEL. I went ahead and did it the brute force way just to see if I could do it.
Sub combineEquityFromTS()
'read data from columns 'symbol, date, monthlyEquity, cumulativeEquity - format 'create arrays to hold each column of data 'use nested loops to do all the work 'brute force coding, no objects - just like we did in the 80s
Dim symbol(1250) As String Dim symbolHeadings(20) As String Dim myDate(1250) As Long Dim monthlyEquity(1250) As Double Dim cumulativeEquity(1250) As Double
'read the data from the cells dataCnt = 1 Do While Cells(dataCnt, 1) <> "" symbol(dataCnt) = Cells(dataCnt, 1) myDate(dataCnt) = Cells(dataCnt, 2) monthlyEquity(dataCnt) = Cells(dataCnt, 3) cumulativeEquity(dataCnt) = Cells(dataCnt, 4) dataCnt = dataCnt + 1 Loop dataCnt = dataCnt - 1
'get distinct symbolNames and use as headers symbolHeadings(1) = symbol(1) numSymbolheadings = 1 For i = 2 To dataCnt - 1 If symbol(i) <> symbol(i + 1) Then newSymbol = True For j = 1 To numSymbolheadings If symbol(i + 1) = symbolHeadings(j) Then newSymbol = False End If Next j If newSymbol = True Then numSymbolheadings = numSymbolheadings + 1 symbolHeadings(numSymbolheadings) = symbol(i + 1) End If End If Next i
'Remove duplicate months in date array 'have just one column of month end dates
Cells(2, 7) = myDate(1) dispRow = 2 numMonths = 1 i = 1 Do While i <= dataCnt foundDate = False For j = 1 To numMonths If myDate(i) = Cells(j + 1, 7) Then foundDate = True End If Next j If foundDate = False Then numMonths = numMonths + 1 Cells(numMonths + 1, 7) = myDate(i) End If i = i + 1 Loop 'put symbols across top of table 'put "date" and "cumulative" column headings in proper 'locations too
Cells(1, 7) = "Date" For i = 1 To numSymbolheadings Cells(1, 7 + i) = symbolHeadings(i) Next i
numSymbols = numSymbolheadings Cells(1, 7 + numSymbols + 1) = "Cumulative" 'now distribute the monthly returns in their proper 'slots in the table dispRow = 2 dispCol = 7 For i = 1 To numSymbols For j = 2 To numMonths + 1 For k = 1 To dataCnt foundDate = False If symbol(k) = symbolHeadings(i) And myDate(k) = Cells(j, 7) Then Cells(dispRow, dispCol + i) = cumulativeEquity(k) dispRow = dispRow + 1 Exit For End If 'for later use 'If j > 1 Then ' If symbol(k) = symbolHeadings(i) And myDate(k) < Cells(j, 7) And myDate(k) > Cells(j - 1, 7) Then ' dispRow = dispRow + 1 ' End If 'End If Next k Next j dispRow = 2 Next i 'now accumulate across table and then down For i = 1 To numMonths cumulative = 0 For j = 1 To numSymbols cumulative = cumulative + Cells(i + 1, j + 7) Next j Cells(i + 1, 7 + numSymbols + 1) = cumulative Next i End Sub
This a throwback to the 80s BASIC on many of the family computers of that era. If you are new to VBA you can access cell values by using the keyword Cells and the row and column that points to the data. The first thing you do is create a Module named combineEquityFromTS and in doing so it will create a Sub combineEquityFromTS() header and a End Sub footer. All of your code will be squeezed between these two statements. This code is ad hoc because I just sat down and started coding without much forethought.
Use DIM to Dimension an Arrays
I like to read the date from the cells into arrays so I can manipulate the data internally. Here I create five arrays and then loop through column one until there is no longer any data. The appropriate arrays are filled with their respective data from each row.
Dimension and Load Arrays
Once the data is in arrays we can start do parse it. The first thing I want is to get the symbolHeadings or markets. I know the first row has a symbol name so I go ahead and put that into the symbolHeadings array. I kept track of the number of rows in the data with the variable dataCnt. Here I use nested loops to work my way down the data and keep track of new symbols as I encounter them. If the symbol changes values, I then check my list of stored symbolHeadings and if the new symbol is not in the list I add it.
Parse different symbols from all data
Since all the currencies will have the same month values I wanted to compress all the months into a single discrete month list. This isn’t really all that necessary since we could have just prefilled the worksheet with monthly date values going back to 2007. This algorithm is similar to the one that is used to extract the different symbols. Except this time, just for giggles, I used a Do While Loop.
Squash Monthly List Down
As well as getting values from cells you can also put values into them. Here I run through the list of markets and put them into Cells(1, 7+i). When working with cells you need to make sure you get the offset correct. Here I wanted to put the market names in Row A and Columns: H, I, J, K, L, M.
Column H = 8
Column I = 9
Column J = 10
Column K =11
Column L = 12
Column M = 13
Put Markets as Column Headers
Cells are two dimensional arrays. However if you are going to use what is on the worksheet make sure you are referencing the correct data. Here I introduce dispRow and dispCol as the anchor points where the data I need to reference starts out.
Three nested loops – Whopee.
Here I first parse through each symbol and extract the EOM value that matches the symbol name and month-year value. So if I am working on @EC and I need the EOM for 07-2010, I fist loop through the month date values and compare the symbol AND myDate (looped through with another for-loop) with the month date values. If they are the same then I dump the value in the array on to the spreadsheet. And yes I should have used this:
For j = dispRow to numMonths-1
Instead of –
For j = 2 to numMonths-1
Keeping arrays in alignment with Cells can be difficult. I have a hybrid approach here. I will clean this up later and stick just with arrays and only use Cells to extract and place data. The last thing you need to do is sum each row up and store that value in the Cumulative column.
Across and then Down to sum accumulated monthly returns
Conclusion
This was another post where we relied on another application to help us achieve our objective. If you are new to EXCEL VBA (working with algorithms that generate trades) you can find out more in my Ultimate Algorithmic Trading System Toolbox book. Even if you don’t get the book this post should get you started on the right track.
In writing the Hi-Res edition of Easing Into EasyLanguage I should have included this sample program. I do point out the limitations of the EntryPrice keyword/function in the book. But recently I was tasked to create a pyramiding scheme template that used minute bars and would initiate a position with N Shares and then pyramid up to three times by adding on N Shares at the last entry price + one 10 -Day ATR measure as the market moves in favor of the original position. Here is an example of just such a trade.
Pyramid 3 Times After Initial Trade Entry. Where’s the EntryPrice?
EntryPrice only contains the original entry price. So every time you add on a position, the EntryPrice doesn’t reflect this add on price. I would like to be able to index into this keyword/function and extract any EntryPrice. If you enter at the market, then you can keep track of entry prices because a market order is usually issued from an if-then construct:
//Here I can keep track of entry prices because I know //exactly when and where they occur.
if c > c[1] and value1 > value2 then begin buy("MarketOrder") next bar at market; lastEntryPrice = open of next bar; end;
Last Entry Price tracking is easy if using Market Orders
But what if you are using a stop or limit order. You don’t know ahead of time where one of these types of orders will hit up. It could be the next bar or it could be five bars later or who knows.
AvgEntryPrice Makes Up for the Weakness of EntryPrice
AvgEntryPrice is a keyword/function that returns the average of the entries when pyramiding. Assume you buy at 42.00 and pyramid the same number of shares at 46.50 – AvgEntryPrice will be equal to (42.00 + 46.50) / 2 = 44.25. With this information you can determine the two entry prices. You already know the original price. Take a look at this code.
// remember currentShares and avgEntryPrice ARE EasyLanguage Keywords/Functions if mp[1] = mp and mp = -1 and currentShares > curShares then begin totShorts = totShorts + 1; if currentShares > initShares then begin lastEntryPrice = totShorts * avgEntryPrice - entryPriceSums; entryPriceSums = entryPriceSums + lastEntryPrice; print(d," Short addon ",lastEntryPrice," ",totShorts," ",avgEntryPrice," ",entryPriceSums); end; end;
Calculating the true LastEntryPrice
Remember currentShares is a keyword/function and it is immediately updated when more shares are added or taken off. CurShares is my own variable where I keep track of the prior currentShares , so if currentShares (real number of shares) is greater than the prior curShares (currentShares) then I know 100%, a position has been pyramided as long the the mp stays the same. If currentShares increases and mp stays constant, then you can figure out the last entry price where the pyramid takes place. First you tick totShorts up by 1. If currentShares > initShares, then you know you are pyramiding so
Don’t believe me. Let’s test it. Remember original entry was 42.00 and the add on was at 46.50. TotShorts now equals 2.
Initial entryPrice = 42.00 so entryPriceSums is set to 42.00
After pyramiding avgEntryPrice is set to 44.25
lastEntryPrice = 2 * 44.25 – 42.00 = 46.50
entryPriceSums is then set to 42.00 + 46.50 or 88.50
So every time you add on a position, then you flow through this logic and you can keep track of the actual last entry price even if it is via a limit or stop order.
But wait there is more. This post is also a small glimpse into what I will be writing about in the Easing Into EasyLanguage: Advanced Topics. This system definitely falls into what I discussed in the Hi-Res Edition. Here is where we tip over into Advanced Topics. The next book is not about creating dialogs or trading apps using OOEL (object oriented EasyLanguage), but we do use some of those topics to do some rather complicated back testing things.
Now that we know how to calculate the lastEntryPrice wouldn’t it be really cool if we could keep track of all of the entryPrices during the pyramid stream. If I have pyramided four times, I would like to know entryPrice 1, entryPrice 2, entryPrice 3 and entryPrice 4.
EntryPrice Vector
Dr. VectorLove or How I Learned to Stop Worrying and Love Objects
I have discussed vectors before but I really wanted to discuss them more. Remember Vectors are just lists or arrays that don’t need all the maintenance. Yes you have to create them which can be a pain, but once you learn and forget it twenty times it starts to sink in. Or just keep referring back to this web page.
Using elsystem.collections;
vars: Vector entryPriceVector(Null);
once Begin entryPriceVector = new Vector; end;
The Bare Minimum to Instantiate a Vector
Type – “Using elsystem.collections; “
Declare entryPriceVector as a Vector and set it equal to Null
Use Once and instantiate entryPriceVector by using the keyword new < object type>;
A Vector is part of elsystem’s collection objects. Take a look at this updated code,
if mp[1] = mp and mp = -1 and currentShares > curShares then begin totShorts = totShorts + 1; if currentShares > initShares then begin lastEntryPrice = totShorts * avgEntryPrice - entryPriceSums; entryPriceVector.push_back(lastEntryPrice); entryPriceSums = entryPriceSums + lastEntryPrice; print(d," Short addon ",lastEntryPrice," ",entryPrice," ",entryPrice(1)," ",totShorts," ",avgEntryPrice," ",entryPriceSums," ",entryPriceVector.back() astype double," ",entryPriceVector.count asType int); if not(entryPriceVector.empty()) then begin for m = 0 to entryPriceVector.count-1 begin print(entryPriceVector.at(m) astype double); end; end; end; end;
LastEntryPrice and Pushing It onto the Vector and Then Printing Out the Vector
After the lastEntryPrice is calculated it is pushed onto the entryPriceVector using the function (method same thing but it is attached to the Vector class).push_back(lastEntryPrice);
entryPriceVector.push_back(lastEntryPrice);
So every time a new lastEntryPrice is calculated it is pushed onto the Vector at the back end. Now if the entryPriceVector is not empty then we can print its contents by looping and indexing into the Vector.
if not(entryPriceVector.empty()) then begin for m = 0 to entryPriceVector.count-1 begin print(entryPriceVector.at(m) astype double); end; end;
Looping through a Vector and Printing Out its Contents
Remember if you NOT a boolean value then it turns it to off/on or just the opposite of the boolean value. If entryPriceVector is not empty then proceed. entryPriceVector.count holds the number of values stuffed into the vector. You can index into the Vector by using .at(m), If you want to print out the value of the Vector .at(m), then you will need to typecast the Vector object as what ever it is holding. We know we are pushing numbers with decimals (double type) onto the Vector so we know we can evaluate them as a double type. Just remember you have to do this when printing out the values of the Vector.
Okay you can see where we moved into an Advanced Topics area with this code. But it really becomes useful when trying to overcome some limitations of EasyLanguage. Remember keep an eye open for Advanced Topics sometime in the Spring.
Methods are wonderful tools that are just like functions, but you can put them right into your Analysis Technique and they can share the variables that are defined outside the Method. Here is an example that I have posted previously. Note: This was in response to a question I got on Jeff Swanson’s EasyLanguage Mastery Facebook Group.
{'(' Expected line 10, column 12 } //the t in tradeProfit. // var: double tradeProfit;
vars: mp(0); array: weekArray[5](0);
method void dayOfWeekAnalysis() {method definition} var: double tradeProfit; begin If mp = 1 and mp[1] = -1 then tradeProfit = (entryPrice(1) - entryPrice(0))*bigPointValue; If mp = -1 and mp[1] = 1 then tradeProfit = (entryPrice(0) - entryPrice(1))*bigPointValue; weekArray[dayOfWeek(entryDate(1))] = weekArray[dayOfWeek(entryDate(1))] + tradeProfit; end;
Buy next bar at highest(high,9)[1] stop; Sellshort next bar at lowest(low,9)[1] stop;
mp = marketPosition; if mp <> mp[1] then dayOfWeekAnalysis(); If lastBarOnChart then Begin print("Monday ",weekArray[1]); print("Tuesday ",weekArray[2]); print("Wednesday ",weekArray[3]); print("Thursday ",weekArray[4]); print("Friday ",weekArray[5]); end;
PowerEditor Cannot Handle Method Syntax
Convert Method to External Function
Sounds easy enough – just remove Method and copy code and put into a new function. This method keeps track of Day Of Week Analysis. So what is the function going to return? It needs to return the performance metrics for Monday, Tuesday, Wednesday, Thursday and Friday. That is five values so you can’t simply assign the Function Name a single value – right?
tradeProfit = -999999999; If mp = 1 and mp[1] = -1 then tradeProfit = (entryPrice(1) - entryPrice(0))*bigPointValue; If mp = -1 and mp[1] = 1 then tradeProfit = (entryPrice(0) - entryPrice(1))*bigPointValue; if tradeProfit <> -999999999 then weekArray[dayOfWeek(entryDate(1))] = weekArray[dayOfWeek(entryDate(1))] + tradeProfit; print(d," ",mp," ",mp[1]," ",dayOfWeek(entryDate(1)),tradeProfit," ",entryDate," ",entryDate(1)," ",entryPrice(0)," ",entryPrice(1));
DayOfWeekAnalysis = 1;
Simple Function - What's the Big Deal
Looks pretty simple and straight forward. Take a look at the first line of code. Notice how I inform the function to expect an array of [n] length to passed to it. Also notice I am not passing by value but by reference. Value versus reference – huge difference. Value is a scalar value such as 5, True or a string. When you pass by reference you are actually passing a pointer to actual location in computer memory – once you change it – it stays changed and that is what we want to do. When you pass a variable to an indicator function you are simple passing a value that is not modified within the body of the function. If you want a function to modify and return more than one value you can pass the variable and catch it as a numericRef. TradeStation has a great explanation of multiple output functions.
Multiple Output Function per EasyLanguage
Some built-in functions need to return more than a single value and do this by using one or more output parameters within the parameter list. Built-in multiple output functions typically preface the parameter name with an ‘o’ to indicate that it is an output parameter used to return a value. These are also known as ‘input-output’ parameters because they are declared within a function as a ‘ref’ type of input (i.e. NumericRef, TrueFalseRef, etc.) which allows it output a value, by reference, to a variable in the EasyLanguage code calling the function.
I personally don’t follow the “O” prefacing, but if it helps you program then go for it.
Series Function – What Is It And Why Do I Need to Worry About It?
A series function is a specialized function that refers to a previous function value within its calculations. In addition, series functions update their value on every bar even if the function call is placed within a conditional structure that may not be true on a given bar. Because a series function automatically stores its own previous values and executes on every bar, it allows you to write function calculations that may be more streamlined than if you had to manage all of the resources yourself. However, it’s a good idea to understand how this might affect the performance of your EasyLanguage code.
Seems complicated, but it really isn’t. It all boils down to SCOPE – not the mouthwash. See when you call a function all the variables inside that function are local to that particular function – in other words it doesn’t have a memory. If it changes a value in the first call to the function, it has amnesia so the next time you call the function it forgets what it did just prior – unless its a series function. Then it remembers. This is why I can do this:
If mp = 1 and mp[1] = -1 then tradeProfit = (entryPrice(1) - entryPrice(0))*bigPointValue; If mp = -1 and mp[1] = 1 then tradeProfit = (entryPrice(0) - entryPrice(1))*bigPointValue;
I Can Refer to Prior Values - It Has A Memory
Did you notice TradeProfit = -99999999 and then if it changes then I accumulate it in the correct Day Bin. If I didn’t check for this then the values in the Day Bin would be accumulated with the values returned by EntryPrice and ExitPrice functions. Remember this function is called on every bar even if you don’t call it. I could have tested if a trade occurred and passed this information to the function and then have the function access the EntryPrice and ExitPrice values. This is up to your individual taste of style. One more parameter for readability, or one less parameter for perhaps efficiency?
This Is A Special Function – Array Manipulator and Series Type
When you program a function like this the EasyLanguage Dev. Environment can determine what type of function you are using. But if you need to change it you can. Simply right click inside the editor and select Properites.
Function Properties – AutoDetect Selected
How Do You Call Such a “Special” Function?
The first thing you need to do is declare the array that you will be passing to the function. Use the keyword Array and put the number of elements it will hold and then declare the values of each element. Here I create a 5 element array and assign each element zero. Here is the function wrapper.
Buy next bar at highest(high,9)[1] stop; Sellshort next bar at lowest(low,9)[1] stop; mp = marketPosition; newTrade = False; //if mp <> mp[1] then newTrade = true;
value1 = dayOfWeekAnalysis(weekArray); If lastBarOnChart then Begin print("Monday ",weekArray[1]); print("Tuesday ",weekArray[2]); print("Wednesday ",weekArray[3]); print("Thursday ",weekArray[4]); print("Friday ",weekArray[5]); end;
Wrapper Function - Notice I only Pass the Array to the Function
Okay that’s how you convert a Method from EasyLanguage into a Function. Functions are more re-uasable, but methods are easier. But if you can’t use a method you now know how to convert one that uses Array Manipulation and us a “Series” type.
I didn’t say a very good Free System! This code is really cool so I thought I would share with you. Take a look at this rather cool picture.
Six Bar Break Out with Volatility Buffer and Volatility Trailing Stop
Thanks to a reader of this blog (AG), I got this idea and programmed a very simple day trading system that incorporated a volatility trailing stop. I wanted to make sure that I had it programmed correctly and always wanted to draw a box on the chart – thanks to (TJ) from MC forums for getting me going on the graphic aspect of the project.
Since I have run out of time for today – need to get a haircut. I will have to wait till tomorrow to explain the code. But real quickly the system.
Buy x% above first y bar high and then set up a trailing stop z% of y bar average range – move to break-even when profits exceed $w. Opposite goes for the short side. One long and one short only allowed during the day and exit all at the close.
if barCount >= startTradeBars then begin volAmt = average(range,startTradeBars); if barCount = startTradeBars then begin longStop = highToday + breakOutVolPer * volAmt; shortStop = lowToday - breakOutVolPer * volAmt; end; if t < endTradeTime then begin if longsToday = 0 then buy("volOrboL") next bar at longStop stop; if shortsToday = 0 then sellShort("volOrboS") next bar shortStop stop; end;
trailVolAmt = volAmt * trailVolPer; if mp = 1 then begin longsToday +=1; if c > entryPrice + breakEven$/bigPointValue then longTrail = maxList(entryPrice,longTrail); longTrail = maxList(c - trailVolAmt,longTrail); sell("L-TrlX") next bar at longTrail stop; end; if mp = -1 then begin shortsToday +=1; if c < entryPrice - breakEven$/bigPointValue then shortTrail = minList(entryPrice,shortTrail); shortTrail = minList(c + trailVolAmt,shortTrail); buyToCover("S-TrlX") next bar at shortTrail stop; end; end; setExitOnClose;
if barCount >= startTradeBars then begin volAmt = average(range,startTradeBars); if barCount = startTradeBars then begin longStop = highToday + breakOutVolPer * volAmt; shortStop = lowToday - breakOutVolPer * volAmt; for iCnt = 0 to startTradeBars-1 begin plot1[iCnt](longStop,"BuyBO",default,default,default); plot2[iCnt](shortStop,"ShrtBo",default,default,default); end;
end; if t < endTradeTime then begin if longsToday = 0 and h >= longStop then begin mp = 1; mEntryPrice = maxList(o,longStop); longsToday += 1; end; if shortsToday = 0 and l <= shortStop then begin mp = -1; mEntryPrice = minList(o,shortStop); shortsToday +=1; end; plot3(longStop,"BuyBOXTND",default,default,default); plot4(shortStop,"ShrtBOXTND",default,default,default); end;
trailVolAmt = volAmt * trailVolPer;
if mp = 1 then begin if c > mEntryPrice + breakEven$/bigPointValue then longTrail = maxList(mEntryPrice,longTrail);
longTrail = maxList(c - trailVolAmt,longTrail); plot5(longTrail,"LongTrail",default,default,default); end; if mp = -1 then begin if c < mEntryPrice - breakEven$/bigPointValue then shortTrail = minList(mEntryPrice,shortTrail); shortTrail = minList(c + trailVolAmt,shortTrail); plot6(shortTrail,"ShortTrail",default,default,default); end; end;
Cool code for the indicator
Very Important To Set Indicator Defaults Like This
For the BO Box use these settings – its the first 4 plots:
Use these colors and bar high and bar low and set opacity
The box is created by drawing thick semi-transparent lines from the BuyBo and BuyBOXTND down to ShrtBo and ShrtBOXTND. So the Buy components of the 4 first plots should be Bar High and the Shrt components should be Bar Low. I didn’t specify this the first time I posted. Thanks to one of my readers for point this out!
Use bar low for ShrtBo and ShrtBOXTND plots
Also I used different colors for the BuyBo/ShrtBo and the BuyBOXTND/ShrtBOXTND. Here is that setting:
The darker colored line on the last bar of the break out is caused by the overlap of the two sets of plots.
Here is how you set up the trailing stop plots:
Make Dots and Make Then Large – I have Red and Blue Set
Since this is part 1 we are just going to go over a very simple system: SAR (stop and reverse) at highest/lowest high/low for past 20 days.
A 2D Array in EasyLanguage is Immutable
Meaning that once you create an array all of the data types must be the same. In a Python list you can have integers, strings, objects whatever. In C and its derivatives you also have a a data structure (a thing that stores related data) know as a Structure or Struct. We can mimic a structure in EL by using a 2 dimensional array. An array is just a list of values that can be referenced by an index.
array[1] = 3.14
array[2] = 42
array[3] = 2.71828
A 2 day array is similar but it looks like a table
array[1,1], array[1,2], array[1,3]
array[2,1], array[2,2], array[2,3]
The first number in the pair is the row and the second is the column. So a 2D array can be very large table with many rows and columns. The column can also be referred to as a field in the table. To help use a table you can actually give your fields names. Here is a table structure that I created to store trade information.
trdEntryPrice (0) – column zero – yes we can have a 0 col. and row
trdEntryDate(1)
trdExitPrice (2)
trdExitDate(3)
trdID(4)
trdPos(5)
trdProfit(6)
trdCumuProfit(7)
So when I refer to tradeStruct[0, trdEntryPrice] I am referring to the first column in the first row.
This how you define a 2D array and its associate fields.
In EasyLanguage You are Poised at the Close of a Yesterday’s Bar
This paradigm allows you to sneak a peek at tomorrow’s open tick but that is it. You can’t really cheat, but it also limits your creativity and makes things more difficult to program when all you want is an accurate backtest. I will go into detail, if I haven’t already in an earlier post, the difference of sitting on Yesterday’s close verus sitting on Today’s close with retroactive trading powers. Since we are only storing trade information when can use hindsight to gather the information we need.
Buy tomorrow at highest(h,20) stop;
SellShort tomorrow at lowest(l,20) stop;
These are the order directives that we will be using to execute our strategy. We can also run a Shadow System, with the benefit of hindsight, to see where we entered long/short and at what prices. I call it a Shadow because its all the trades reflected back one bar. All we need to do is offset the highest and lowest calculations by 1 and compare the values to today’s highs and lows to determine trade entry. We must also test the open if a gap occurred and we would have been filled at the open. Now this code gets a bit hairy, but stick with it.
if mPos <> 1 then begin if h >= stb1 then begin if mPos < 0 then // close existing short position begin mEntryPrice = tradeStruct[numTrades,trdEntryPrice]; mExitPrice = maxList(o,stb1); tradeStruct[numTrades,trdExitPrice] = mExitPrice; tradeStruct[numTrades,trdExitDate] = date; mProfit = (mEntryPrice - mExitPrice) * bigPointValue - mCommSlipp; cumuProfit += mProfit; tradeStruct[numTrades,trdCumuProfit] = cumuProfit; tradeStruct[numTrades,trdProfit] = mProfit; print(d+19000000:8:0," shrtExit ",mEntryPrice:4:5," ",mExitPrice:4:5," ",mProfit:6:0," ",cumuProfit:7:0); print("-------------------------------------------------------------------------"); end; numTrades +=1; mEntryPrice = maxList(o,stb1); tradeStruct[numTrades,trdID] = 1; tradeStruct[numTrades,trdPOS] = 1; tradeStruct[numTrades,trdEntryPrice] = mEntryPrice; tradeStruct[numTrades,trdEntryDate] = date; mPos = 1; print(d+19000000:8:0," longEntry ",mEntryPrice:4:5); end; end; if mPos <>-1 then begin if l <= sts1 then begin if mPos > 0 then // close existing long position begin mEntryPrice = tradeStruct[numTrades,trdEntryPrice]; mExitPrice = minList(o,sts1); tradeStruct[numTrades,trdExitPrice] = mExitPrice; tradeStruct[numTrades,trdExitDate] = date; mProfit = (mExitPrice - mEntryPrice ) * bigPointValue - mCommSlipp; cumuProfit += mProfit; tradeStruct[numTrades,trdCumuProfit] = cumuProfit; tradeStruct[numTrades,trdProfit] = mProfit; print(d+19000000:8:0," longExit ",mEntryPrice:4:5," ",mExitPrice:4:5," ",mProfit:6:0," ",cumuProfit:7:0); print("---------------------------------------------------------------------"); end; numTrades +=1; mEntryPrice =minList(o,sts1); tradeStruct[numTrades,trdID] = 2; tradeStruct[numTrades,trdPOS] =-1; tradeStruct[numTrades,trdEntryPrice] = mEntryPrice; tradeStruct[numTrades,trdEntryDate] = date; mPos = -1; print(d+19000000:8:0," ShortEntry ",mEntryPrice:4:5); end; end;
Shadow System - Generic forany SAR System
Notice I have stb and stb1. The only difference between the two calculations is one is displaced a day. I use the stb and sts in the EL trade directives. I use stb1 and sts1 in the Shadow System code. I guarantee this snippet of code is in every backtesting platform out there.
All the variables that start with the letter m, such as mEntryPrice, mExitPrice deal with the Shadow System. Theyare not derived from TradeStation’s back testing engine only our logic. Lets look at the first part of just one side of the Shadow System:
if mPos <> 1 then begin if h >= stb1 then begin if mPos < 0 then // close existing short position begin mEntryPrice = tradeStruct[numTrades,trdEntryPrice]; mExitPrice = maxList(o,stb1); tradeStruct[numTrades,trdExitPrice] = mExitPrice; tradeStruct[numTrades,trdExitDate] = date; mProfit = (mEntryPrice - mExitPrice) * bigPointValue - mCommSlipp; cumuProfit += mProfit; tradeStruct[numTrades,trdCumuProfit] = cumuProfit; tradeStruct[numTrades,trdProfit] = mProfit; print(d+19000000:8:0," shrtExit ",mEntryPrice:4:5," ",mExitPrice:4:5," ",mProfit:6:0," ",cumuProfit:7:0); print("-------------------------------------------------------------------------"); end;
mPos and mEntryPrice and mExitPrice belong to the Shadow System
if mPos <> 1 then the Shadow Systems [SS] is not long. So we test today’s high against stb1 and if its greater then we know a long position was put on. But what if mPos = -1 [short], then we need to calculate the exit and the trade profit and the cumulative trade profit. If mPos = -1 then we know a short position is on and we can access its particulars from the tradeStruct 2D array. mEntryPrice = tradeStruct[numTrades,trdEntryPrice]. We can gather the other necessary information from the tradeStruct [remember this is just a table with fields spelled out for us.] Once we get the information we need we then need to stuff our calculations back into the Structure or table so we can regurgitate later. We stuff date in to the following fields trdExitPrice, trdExitDate, trdProfit and trdCumuProfit in the table.
Formatted Print: mEntryPrice:4:5
Notice in the code how I follow the print out of variables with :8:0 or :4:5? I am telling TradeStation to use either 0 or 5 decimal places. The date doesn’t need decimals but prices do. So I format that so that they will line up really pretty like.
Now that I take care of liquidating an existing position all I need to do is increment the number of trades and stuff the new trade information into the Structure.
The same goes for the short entry and long exit side of things. Just review the code. I print out the trades as we go along through the history of crude. All the while stuffing the table.
If LastBarOnChart -> Regurgitate
On the last bar of the chart we know exactly how many trades have been executed because we were keeping track of them in the Shadow System. So it is very easy to loop from 0 to numTrades.
if lastBarOnChart then begin print("Trade History"); for arrIndx = 1 to numTrades begin value20 = tradeStruct[arrIndx,trdEntryDate]; value21 = tradeStruct[arrIndx,trdEntryPrice]; value22 = tradeStruct[arrIndx,trdExitDate]; value23 = tradeStruct[arrIndx,trdExitPrice]; value24 = tradeStruct[arrIndx,trdID]; value25 = tradeStruct[arrIndx,trdProfit]; value26 = tradeStruct[arrIndx,trdCumuProfit];
print("---------------------------------------------------------------------"); if value24 = 1 then begin string1 = buyStr; string2 = sellStr; end; if value24 = 2 then begin string1 = shortStr; string2 = coverStr; end; print(value20+19000000:8:0,string1,value21:4:5," ",value22+19000000:8:0,string2, value23:4:5," ",value25:6:0," ",value26:7:0); end; end;
Add 19000000 to Dates for easy Translation
Since all trade information is stored in the Structure or Table then pulling the information out using our Field Descriptors is very easy. Notice I used EL built-in valueXX to store table information. I did this to make the print statements a lot shorter. I could have just used tradeStruct[arrIndx, trdEntry] or whatever was needed to provide the right information, but the lines would be hard to read. To translate EL date to a normal looking data just add 19,000,000 [without commas].
If you format your PrintLog to a monospaced font your out put should look like this.
PrintLog OutPut
Why Would We Want to Save Trade Information?
The answer to this question will be answered in Part 2. Email me with any other questions…..
Backtesting with [Trade Station,Python,AmiBroker, Excel]. Intended for informational and educational purposes only!
Get All Three Books in the Easing Into EasyLanguage Series - A Day Trade Edition will be Added Later this Year!
Announcement – A Day Trade Edition will be added to my Easing Into EasyLanguage Series this year! This edition will be the fourth installment and will utilize concepts discussed in the Hi-Res and Advanced Topics editions. I will show how to develop and program algorithms that will enter after the open of the day and get out before the market closes. Hence, no overnight exposure. Most examples will be carried out on the mini Dow, Nasdaq, S&P500 and Russel. The programming skills that you will learn can be carried to any market that provides enough bang for the buck to day trade. Look for this edition later this year. But get geared up for it by reading the first three editions in the series now. Get your favorite QUANT the books they need!
The Cover of my latest book. The first in the series.
Hello to All! The Easing Into EasyLanguage Series is now complete with the publication of the Advanced Topics Edition. This series includes three educational editions. Start out with the Foundation Edition. It is designed for the new user of EasyLanguage or for those you would like to have a refresher course. There are 13 tutorials ranging from creating Strategies to PaintBars. Learn how to create your own functions or apply stops and profit objectives. Ever wanted to know how to find an inside day that is also a Narrow Range 7 (NR7?) Now you can, and the best part is you get over 4 HOURS OF VIDEO INSTRUCTION – one for each tutorial. All source code is available too, and if you have TradeStation, so are the workspaces. Plus you can always email George for any questions. george.p.pruitt@gmail.com.
Hi-Res Edition Cover
This book is for those that have read the Foundation Edition or have some experience working with EasyLanguage and the various functions that help make a trading decision. This book’s audience will be those programmers that want to take an idea, that requires an observation of intraday market movements to make a trading decision, and program it accurately. If you have programmed daily bar systems, and you want to drill down and add some components that require additional market information (like what came first – the high or the low), then you have come to the right place. If you want to buy and sell short in the same day and use trade management principles such as profit targets and stop losses then The Hi-Res Edition is the book you need. There are two paradigms that EasyLanguage covers: daily and intraday bar programming. It’s the same language, but the move from daily to intraday programming can be quite difficult. Learn all the essentials and shortcuts with this edition. 5 HOURS OF VIDEO INSTRUCTION in this Hi-Res edition – one for each tutorial. All source code is available too, and if you have TradeStation, so are the workspaces. Plus you can always email George for any questions. george.p.pruitt@gmail.com.
Advanced Topics Cover
Advanced Topics (AT) could cover a vast amount of ideas and concepts and be the length of “War and Peace” on steroids. Since this book is part of the series, I wanted to cover a handful of concepts that included the follow programming constructs. Arrays and their manipulation. Buffers (fixed length arrays) and the tools to maintain buffer elements with formulas for extraction and sorting. Finite State Machines using the switch-case construct and range based case values. Using original text graphic objects and retrieving and analyzing their properties to determine X and Y coordinate values of text location. Seasonality: The Ruggiero/Barna Universal Seasonal and the Sheldon Knight Seasonal methods. In AT, you will also find an introduction to EasyLanguage’s Project Concept and the steps to create one by adding/deleting component files. TradeStation now provides access to fundamental data such as Commitment of Traders – learn how to convert the Net Change indicator into a strategy utilizing the FundValue functionality. If you wanted to find out how to merge multiple time frames into a single indicator, you are in luck! Create a MTF indicator for yourself.
Pick up your copies today – e-Book or paperback format – at Amazon.com