Category Archives: Free EasyLanguage

Testing Seasonal Tendencies with an EasyLanguage Template

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:

  1. Buy array
  2. SellShort array
  3. Sell array
  4. 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),

// next you want the arrays that go together to have the same
// index value - take a look at this

buyDates[1] = 0415;sellDates[1] = 0515;
buyDates[2] = 0605;sellDates[2] = 0830;

shortDates[1] = 0915;buyToCoverDates[1] = 1130;
shortDates[2] = 0215;buyToCoverDates[2] = 0330;

// 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
if currentMMDD[1] < buyDates[n] and currentMMDD >= buyDates[n] Then
buyNow = True;

//Short Seasonal Entries toggle
shortNow = False;
for n = 1 to numshortDates
if currentMMDD[1] < shortDates[n] and currentMMDD >= shortDates[n] Then
shortNow = True;
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.

Here is the rest of the code:

inputs: dollarProfit(5000),dollarLoss(3000);
arrays: buyDates[100](0),sellDates[100](0),

vars: n(0),mp(0),currentMMDD(0),

vars: buyNow(False),shortNow(False),

// 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;

shortDates[1] = 0915;buyToCoverDates[1] = 1130;
shortDates[2] = 0215;buyToCoverDates[2] = 0330;
numshortDates = 2;
numbuyToCoverDates = 2;

mp = marketPosition;
currentMMDD = month(d of tomorrow)*100 + dayOfMonth(d of tomorrow);

//Long Seasonal Entries toggle
buyNow = False;
for n = 1 to numBuyDates
if currentMMDD[1] < buyDates[n] and currentMMDD >= buyDates[n] Then
buyNow = True;

//Short Seasonal Entries toggle
shortNow = False;
for n = 1 to numshortDates
if currentMMDD[1] < shortDates[n] and currentMMDD >= shortDates[n] Then
shortNow = True;

//Long Seasonal Exits toggle
sellNow = False;
if mp = 1 Then
for n = 1 to numSellDates
if currentMMDD[1] < sellDates[n] and currentMMDD >= sellDates[n] Then
sellNow = True;

//Short Seasonal Exits toggle
buyToCoverNow = False;
if mp = -1 Then
for n = 1 to numBuyToCoverDates
if currentMMDD[1] < buyToCoverDates[n] and currentMMDD >= buyToCoverDates[n] Then
buyToCoverNow = True;

// Long entry execution
if buyNow = True then
buy("Seas-Buy") next bar at open;
// Long exit execution
if mp = 1 then
if sellNow then
sell("Long Exit") next bar at open;

// Short entry execution
if shortNow then
sellShort("Seas-Short") next bar at open;
// short exit execution
if mp = -1 then
if buyToCoverNow then
buyToCover("short Exit") next bar at open;

Complete Seasonal Template EasyLanguage Code

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.

Helpful Code to Accurately Back Test Day Trading Systems with EasyLanguage

The Clear Out Pattern

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 RT
With “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.

inputs: maxDailyLoss(1000),startTime(0935);
inputs: clrOutBuyPer(.10),clrOutShortPer(.10);

vars: coBuy(False),coShort(False),canTrade(0);
vars: beginOfDayProfit(0),beginOfDayTotTrades(0),mp(0);

if t = startTime then
coBuy = False;
coShort = False;
beginOfDayProfit = netProfit;
beginOfDayTotTrades = totalTrades;

canTrade = iff(t >=startTime and t < sess1EndTime,1,0);

if t >= startTime and h > highD(1) + clrOutBuyPer*(highD(1)-lowD(1)) then
coShort = True;

if t >= startTime and l < lowD(1) - clrOutShortPer*(highD(1)-lowD(1)) then
coBuy = True;

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
coShort = False;

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
coBuy = False;

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
coBuy = False;
coShort = False;
beginOfDayProfit = netProfit;
beginOfDayTotTrades = totalTrades;
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
coShort = False;
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.

The ES 500 (futures) Seasonal Day Trade

Complete Strategy based on Sheldon Knight and William Brower Research

In my Easing Into EasyLanguage:  Hi-Res Edition, I discuss the famous statistician and trader Sheldon Knight and his  K-DATA Time Line.  This time line enumerated each day of the year using the following nomenclature:

First Monday in December = 1stMonDec

Second Friday in April = 2ndFriApr

Third Wednesday in March = 3rdWedMar

This enumeration or encoding was used to determine if a certain week of the month and the day of week held any seasonality tendencies.  If you trade index futures you are probably familiar with Triple Witching Days.

Four times a year, contracts for stock options, stock index options, and stock index futures all expire on the same day, resulting in much higher volumes and price volatility. While the stock market may seem foreign and complicated to many people, it is definitely not “witchy”, however, it does have what is known as “triple witching days.”

Triple witching, typically, occurs on the third Friday of the last month in the quarter. That means the third Friday in March, June, September, and December. In 2022, triple witching Friday are March 18, June 17, September 16, and December 16

Other days of certain months also carry significance.  Some days, such as the first Friday of every month (employment situation), carry even more significance.   In 1996, Bill Brower wrote an excellent article in Technical Analysis of Stocks and Commodities.  The title of the article was The S&P 500 Seasonal Day Trade.  In this article, Bill devised 8 very simple day trade patterns and then filtered them with the Day of Week in Month.  Here are the eight patterns as he laid them out in the article.

  1. Pattern 1:  If tomorrow’s open minus 30 points is greater than today’s close, then buy at market.
  2. Pattern 2:  If tomorrow’s open plus 30 points is less than today’s close, then buy at market.
  3. Pattern 3:  If tomorrow’s open minus 30 points is greater than today’s close, then sell short at market.
  4. Pattern 4:  If tomorrow’s open plus 30 points is less than today’s close, then sell short at market.
  5. Pattern 5:  If tomorrow’s open plus 10 points is less than today’s low, then buy at market.
  6. Pattern 6:  If tomorrow’s open minus 20 points is greater than today’s high, then sell short at today’s close stop.
  7. Pattern 7:  If tomorrow’s open minus 40 points is greater than today’s close, then buy at today’s low limit.
  8. Pattern 8:  If tomorrow’s open plus 70 points is less than today’s close, then sell short at today’s high limit.

This article was written nearly 27 years ago when 30 points meant something in the S&P futures contract.   The S&P was trading around the 600.00 level.  Today the  e-mini S&P 500 (big S&P replacement) is trading near 4000.00 and has been higher.  So 30, 40 or 70 points doesn’t make sense.  To bring the patterns up to date, I decided to use a percentage of ATR in place of a single point.  If today’s range equals 112.00 handles or in terms of points 11200 and we use 5%, then the basis would equate to 11200 = 560 points or 5.6 handles.  In the day of the article the range was around 6 handles or 600 points.  So. I think using 1% or 5% of ATR could replace Bill’s point values.  Bill’s syntax was a little different than the way I would have described the patterns.  I would have used this language to describe Pattern1 – If tomorrow’s open is greater than today’s close plus 30 points, then buy at market – its easy to see we are looking for a gap open greater than 30 points here.  Remember there is more than one way to program an idea.  Let’s stick with Bills syntax.

  • 10 points = 1 X (Mult)  X ATR
  • 20 points = 2 X (Mult)  X ATR
  • 30 points = 3 X (Mult)  X ATR
  • 40 points = 4 X (Mult)  X ATR
  • 50 points = 5 X (Mult)  X ATR
  • 70 points =7 X (Mult)  X ATR

We can play around with the Mult to see if we can simulate similar levels back in 1996.

// atrMult will be a small percentage like 0.01 or 0.05
atrVal = avgTrueRange(atrLen) * atrMult;

//original patterns
//use IFF function to either returne a 1 or a 0
//1 pattern is true or 0 it is false

patt1 = iff(open of tomorrow - 3 * atrVal > c,1,0);
patt2 = iff(open of tomorrow + 3 * atrVal < c,1,0);
patt3 = iff(open of tomorrow - 3 * atrVal > c,1,0);
patt4 = iff(open of tomorrow + 3 * atrVal < c,1,0);
patt5 = iff(open of tomorrow + 1 * atrVal < l,1,0);
patt6 = iff(open of tomorrow - 2 * atrVal > h,1,0);
patt7 = iff(open of tomorrow - 4 * atrVal > c,1,0);
patt8 = iff(open of tomorrow + 7 * atrVal < c,1,0);

William Brower’s DoWInMonth Enumeration

The Day of Week In A Month is represented by a two digit number.  The first digit is the week rank and the second number is day of the week.  I thought this to be very clever, so I decided to program it.    I approached it from a couple of different angles and I actually coded an encoding method that included the week rank, day of week, and month (1stWedJan) in my Hi-Res Edition.   Bill’s version didn’t need to be as sophisticated and since I decided to use TradeStation’s optimization capabilities I didn’t need to create a data structure to store any data.  Take a look at the code and see if it makes a little bit of sense.

newMonth = False;
newMonth = dayOfMonth(d of tomorrow) < dayOfMonth(d of today);
atrVal = avgTrueRange(atrLen) * atrMult;
if newMonth then
startTrading = True;
monCnt = 0;
tueCnt = 0;
wedCnt = 0;
thuCnt = 0;
friCnt = 0;
weekCnt = 1;

if not(newMonth) and dayOfWeek(d of tomorrow) < dayOfWeek(d of today) then
weekCnt +=1;

dayOfWeekInMonth = weekCnt * 10 + dayOfWeek(d of tomorrow);
Simple formula to week rank and DOW

NewMonth is set to false on every bar.  If tomorrow’s day of month is less than today’s day of month, then we know we have a new month and newMonth is set to true.  If we have a new month, then several things take place: reinitialize the code that counts the number Mondays, Tuesdays, Wednesdays, Thursdays and Fridays to 0 (not used for this application but can be used later,) and set the week count weekCnt to 1.  If its not a new month and the day of week of tomorrow is less than the day of the week today (Monday = 1 and Friday = 5, if tomorrow is less than today (1 < 5)) then we must have a new week on tomorrow’s bar.  To encode the day of week in month as a two digit number is quite easy – just multiply the week rank (or count) by 10 and add the day of week (1-Monday, 2-Tuesday,…)  So the third Wednesday would be equal to 3X10+3 or 33.

Use Optimization to Step Through 8 Patterns and 25 Day of Week in Month Enumerations

Stepping through the 8 patterns is a no brainer.  However, stepping through the 25 possible DowInAMonth codes or enumerations is another story.  Many times you can use an equation based on the iterative process of going from 1 to 25.  I played around with this using the modulus function, but decided to use the Switch-Case construct instead.  This is a perfect example of replacing math with computer code.  Check this out.

case 1 to 5:
value2 = mod(dowInMonthInc,6);
value3 = 10;
case 6 to 10:
value2 = mod(dowInMonthInc-5,6);
value3 = 20;
case 11 to 15:
value2 = mod(dowInMonthInc-10,6);
value3 = 30;
case 16 to 20:
value2 = mod(dowInMonthInc-15,6);
value3 = 40;
case 21 to 25:
value2 = mod(dowInMonthInc-20,6);
value3 = 50;
Switch-Case to Step across 25 Enumerations

Here we are switching on the input (dowInMonthInc).  Remember this value will go from 1 to 25 in increments of 1. What is really neat about EasyLanguage’s implementation of the Switch-Case is that it can handle ranges.  If the dowInMonthInc turns out to be 4 it will fall within the first case block (case 1 to 5).  Here we know that if this value is less than 6, then we are in the first week so I set the first number in the two digit dayOfWeekInMonth representation to 1.  This is accomplished by setting value3 to 10.  Now you need to extract the day of the week from the 1 to 25 loop.  If the dowInMonthInc is less than 6, then all you need to do is use the modulus function and the value 6.

  • mod(1,6)  = 1
  • mod(2,6) = 2
  • mod(3,6) = 3

This works great when the increment value is less than 6.  Remember:

  • 1 –> 11 (first Monday)
  • 2 –> 12 (first Tuesday)
  • 3 –> 13 (first Wednesday)
  • 6 –> 21 (second Monday)
  • 7 –> 22 (second Tuesday).

So, you have to get a little creative with your code.  Assume the iterative value is 8.  We need to get 8 to equal 23 (second Wednesday).  This value falls into the second case, so Value3 = 20 the second week of the month.  That is easy enough.  Now we need to extract the day of week – remember this is just one solution, I guarantee there are are many.

mod(dowInMonthInc – 5, 6) – does it work?

value2 = mod(8-5,6) = 3 -> value3 = value1  +  value2 -> value3 = 23.  It worked.   Do you see the pattern below.

  • case   6 to 10 – mod(dowInMonthInc –  5, 6)
  • case 11 to 15 – mod(dowInMonthInc – 10, 6)
  • case 16 to 20- mod(dowInMonthInc – 15, 6)
  • case 21 to25 – mod(dowInMonthInc – 20, 6)

Save Optimization Report as Text and Open with Excel

Here are the settings that I used to create the following report.  If you do the math that is a total of 200 iterations.

Seasonal Day Trader Settings

I opened the Optimization Report and saved as text.  Excel had no problem opening it.

Optimization results output to Excel and cleaned up.

I created the third column by translating the second column into our week of month and day of week vernacular.  These results were applied to 20 years of ES.D (day session data.)  The best result was Pattern #3 applied to the third Friday of the month (35.)  Remember the 15th DowInMonthInc  equals the third (3) Friday (5).  The top patterns predominately occurred on a Thursday or Friday.  

Here is the complete code for you to play with.

inputs: atrLen(10),atrMult(.05),patternNum(1),dowInMonthInc(1);

vars: patt1(0),patt2(0),patt3(0),patt4(0),

vars: atrVal(0),dayOfWeekInMonth(0),startTrading(false),newMonth(False);;

vars: monCnt(0),tueCnt(0),wedCnt(0),thuCnt(0),friCnt(0),weekCnt(0);

newMonth = False;
newMonth = dayOfMonth(d of tomorrow) < dayOfMonth(d of today);
atrVal = avgTrueRange(atrLen) * atrMult;
if newMonth then
startTrading = True;
monCnt = 0;
tueCnt = 0;
wedCnt = 0;
thuCnt = 0;
friCnt = 0;
weekCnt = 1;

if not(newMonth) and dayOfWeek(d of tomorrow) < dayOfWeek(d of today) then
weekCnt +=1;

dayOfWeekInMonth = weekCnt * 10 + dayOfWeek(d of tomorrow);

//print(date," ", dayOfMonth(d)," " ,dayOfWeek(d)," ",weekCnt," ",monCnt," ",dayOfWeekInMonth);

//original patterns

patt1 = iff(open of tomorrow - 3 * atrVal > c,1,0);
patt2 = iff(open of tomorrow + 3 * atrVal < c,1,0);
patt3 = iff(open of tomorrow - 3 * atrVal > c,1,0);
patt4 = iff(open of tomorrow + 3 * atrVal < c,1,0);
patt5 = iff(open of tomorrow + 1 * atrVal < l,1,0);
patt6 = iff(open of tomorrow - 2 * atrVal > h,1,0);
patt7 = iff(open of tomorrow - 4 * atrVal > c,1,0);
patt8 = iff(open of tomorrow + 7 * atrVal < c,1,0);

case 1 to 5:
value2 = mod(dowInMonthInc,6);
value3 = 10;
case 6 to 10:
value2 = mod(dowInMonthInc-5,6);
value3 = 20;
case 11 to 15:
value2 = mod(dowInMonthInc-10,6);
value3 = 30;
case 16 to 20:
value2 = mod(dowInMonthInc-15,6);
value3 = 40;
case 21 to 25:
value2 = mod(dowInMonthInc-20,6);
value3 = 50;

value1 = value3 + value2 ;

//print(d," ",dowInMonthInc," ",dayOfWeekInMonth," ",value1," ",value2," ",value3," ",mod(dowInMonthInc,value2));

if value1 = dayOfWeekInMonth then
if patternNum = 1 and patt1 = 1 then buy("Patt1") next bar at open;
if patternNum = 2 and patt2 = 1 then buy("Patt2") next bar at open;
if patternNum = 3 and patt3 = 1 then sellShort("Patt3") next bar at open;
if patternNum = 4 and patt4 = 1 then sellShort("Patt4") next bar at open;
if patternNum = 5 and patt5 = 1 then buy("Patt5") next bar at low limit;
if patternNum = 6 and patt6 = 1 then sellShort("Patt6") next bar at close stop;
if patternNum = 7 and patt7 = 1 then buy("Patt7") next bar at low limit;
if patternNum = 8 and patt8 = 1 then sellShort("Patt8") next bar at high stop;

The Full Monty of the ES-Seasonal-Day Trade

I think this could provide a means to much more in-depth analysis.  I think the Patterns could be changed up.  I would like to thank William (Bill) Brower for his excellent article, The S&P Seasonal Day Trade in Stocks and Commodities, August 1996 Issue, V.14:7 (333-337).  The article is copyright by Technical Analysis Inc.  For those not familiar with Stocks and Commodities check them out at

Please email me with any questions or anything I just got plain wrong.  George

Can Futures Traders Trust Continuous Contracts? [Part – 2]

Recap from Part -1

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.

  1. used my function to locate the First Notice Date in crude
  2. used the same function to print out exact EasyLanguage syntax
  3. chose to roll eight days before FND and had the function print out pure EasyLanguage
  4. the output created array assignments and loaded the calculated roll points in YYYMMDD format into the array
  5.  visually inspected non-adjusted continuous contracts that were spliced eight days before FND
  6. 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
condition1 = true;
arrCnt = arrCnt + 1;
if marketPosition = 1 then
sell("LongRollExit") this bar on close;
buy("LongRollEntry") next bar at open;
if marketPosition = -1 then
buyToCover("ShrtRollExit") this bar on close;
sellShort("ShrtRollEntry") next bar at open;

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.


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?

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.

Can Futures Traders Trust Continuous Contracts? [Part – 1]

 Well You Have To, Don’t You?

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
firstMonthPrint = true;
If(dayOfMonth(date[1]) < 25 and dayOfMonth(date) > 25 ) and firstMonthPrint = false then
firstMonthPrint = true;
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
value1 = value1 + 1;
firstMonthPrint = true;
If(dayOfMonth(date[1]) < 25 and dayOfMonth(date) > 25 ) and firstMonthPrint = false then
value1 = value1 + 1;
// print(date[4]+19000000:8:0);
firstMonthPrint = true;

// example of output

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.

Trading the Equity Curve – Part 1 of N?

Only Trade the Best Segments of the Equity Curve – Cut Out Drawdown and Take Advantage of Run Ups! Really?

Equity curve feedback has been around for many years and seems highly logical, but one can’t get an industry-wide agreement on its benefit.  The main problem is to know when to turn trading off and then back on as you track the equity curve.  The most popular approach is to use a moving average of the equity curve to signal system participation.   When the equity curve moves below 30, 60, or 90 period-moving average of equity, then just turn it off and wait until the curve crosses back above the average.  This approach will be investigated in Part 2 of this series.  Another approach is to stop trading once the curve enters a drawdown that exceeds a certain level and then start back up once the equity curve recovers.  In this post, this method will be investigated.

Programmers Perspective

How do you go about programming this tool to start with.  There are probably multiple ways of accomplishing this task, but the two I have most often observed were the two pass process and the inline simultaneous tracking of the synthetic and actual equity curves.  The two pass process generates an unadulterated equity curve and stores the equity and trades either in memory or in a file.  The second part of the process monitors the external equity curve along with the external trades synchronously and while trading is turned on, the trades are executed as they occur chronologically.  When trading is turned off, the synthetic equity curve and trades are processed along the way.   The second method is to create, which I have coined (maybe others too!), a synthetic equity curve and synthetic trades.  I have done this in my TradingSimula_18 software by creating a SynthTrade Class.  This class contains all the properties of every trade and in turn can use this information to create a synthetic equity curve.  The synthetic equity curve and trades are untouched by the real time trading.

Start Simple

The creation of an equity curve monitor and processor is best started using a very simple system.  One market algorithm that enters and exits on different dates, where pyramiding and scaling in or out are not allowed.  The first algorithm that I tested was a mean reversion system where you buy after two consecutive down closes followed by an up close and then waiting one day.  Since I tested the ES over the past 10 years you can assume the trend is up. I must admit that the day delay was a mistake on my behalf.  I was experimenting with a four bar pattern and somehow forgot to look at the prior day’s action.  Since this is an experiment it is OK!

if marketPosition <> 1 and 
(c[2] < c[3] and c[3] < c[4] and c[1]  > =  c[2]) then
buy next bar at open;

//The exit is just as simple -
//get out after four days (includeing entry bar) on the next bars open - no stops or profit objectives.  

If barsSinceEntry > 2 then sell next bar at open;
Simple Strategy to test Synthetic Trading Engine

Here is the unadulterated equity curve using $0 for execution costs.

Non adjusted equity curve of our simple mean reversion system. Wait for a pull back and then a rally before entering.

The Retrace and Recover Method

In this initial experiment, trading is suspended once you reach a draw down of 10% from the peak of the equity curve and then resumes trading once a rally of 15% of the subsequent valley.  Here is an intriguing graphic.

Green means ON. Red means OFF. The lower curve is the resultant curve.

I did this analysis by hand with Excel and it is best case scenario.  Meaning that when trading is turned back on any current synthetic position is immediately executed in the real world.  This experiment resulted in nearly the same drawdown but a large drop in overall equity curve growth – $75K.

Put the Synthetic Equity Curve Engine to the Test

Now that I had the confirmed results of the experiment, I used them as the benchmark against my TS-18 Synthetic Trade Engine.  But before I installed the Equity Curve algorithm, I needed to make sure my synthetic trades lined up exactly with the real equity curve.  The synthetic curve should align 100% with the real equity curve.  If it doesn’t, then there is a problem.  This is another reason to start with a simple trading strategy.

Take a look here where I print out the Synthetic Equity curve on  a daily basis and compare it with the end result of the analysis.

Synth. matches Reality

Now let’s see if it worked.

Testing with Synth. Equity Curve Trading Turned ON!

The equity curves are very similar.  However, there is a difference and this is caused by how one re-enters after trading is turned back on.  In this version I tested waiting for a new trade signal which might take a few days.  You could re-enter in three different ways:

  1. Automatically enter synthetic position on the next bar’s open
  2. Wait for a new trade signal
  3. Enter immediately if you can get in at a better price

Using the 10% Ret. and 15% Rec. algorithm didn’t help at all.  What if we test 10% and 10%.

10% Ret. and 10% Rec.

Now that performed better – more profit and less draw down.  Now that I have the synthetic engine working on simple algorithms we can do all sorts of equity curve analysis.  In the next installment in this series I will make sure the TS-18 Synthetic Engine can handle more complicated entry and exit algorithms.  I have already tested a simple longer term trend following strategy on a medium sized portfolio and the synthetic engine worked fine.  The retracement/recovery algorithm at 10%/15% did not work and I will go into the “whys” in my next post.

Data Aliasing with Minute Bars

Why It Is Important to Connect Variables with Correct Time Frame

I had a question about data aliasing from a reader of this blog.  Here is the debug code I used in the form of an Indicator.

//vars: myCloseData1(0),myCloseData2(0),
// myRSIData1(0),myRSIData2(0);

vars: myCloseData1(0),myCloseData2(0,data2),

myCloseData1 = close of data1;
myCloseData2 = close of data2;

myRSIData1 = rsi(close of data1,14);
myRSIData2 = rsi(close of data2,14);

Print(d," ",t," --------------- ");

print(" myCloseData1[0]: ",myCloseData1[0]," myCloseData2[0]: ",myCloseData2[0]);
print(" myCloseData1[1]: ",myCloseData1[1]," myCloseData2[1]: ",myCloseData2[1]);
print(" myCloseData1[2]: ",myCloseData1[2]," myCloseData2[2]: ",myCloseData2[2]);
print(" myCloseData1[3]: ",myCloseData1[3]," myCloseData2[3]: ",myCloseData2[3]);

print(" myRSIData1[0]: ",myRSIData1[0]," myRSIData2[0]: ",myRSIData2[0]);
print(" myRSIData1[1]: ",myRSIData1[1]," myRSIData2[1]: ",myRSIData2[1]);
print(" myRSIData1[2]: ",myRSIData1[2]," myRSIData1[2]: ",myRSIData2[2]);
print(" myRSIData1[3]: ",myRSIData1[3]," myRSIData1[3]: ",myRSIData2[3]);
Illustrating Difference Between Data Aliasing and Not Data Aliasing

If you have a higher resolution as Data 1 (5 minute in this case) than Data 2 (15 minute), then you must use Data Aliasing if you are going to use a variable to represent a price or a function output.  With out Data Aliasing all data references are in terms of Data 1.  When the 15 minute bar closes then the current [0] and one bar back[ 1] will be correct – going back further in time will reflect correct data.  During the 15 minute bar only the last value [0] will show the correct reading of the last completed 15 minute bar.  Once you tie the variable to its correct time frame variableName(0, Data2), you can then reference historic bars in the same fashion as if it were Data1.  This includes price references and function output values.

Check this chart out and see if it makes sense to you.  I dedicate a portion of a Tutorial on Data Aliasing in my latest book due out next week – Easing Into EasyLanguage – Advanced Topics.

Difference between using Data Aliasing and Not using Data Aliasing

A Tribute to Murray and His Inter-Market Research

Murray Ruggiero’s Inter-Market Research

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

Inputs: InterSet(2),LSB(0),Type(1),LenTr(4),LenInt(4),Relate(0);
Vars: MarkInd(0),InterInd(0);

If Type=0 Then
InterInd=Close of Data(InterSet)-CLose[LenInt] of Data(InterSet);

If Type=1 Then
InterInd=Close of Data(InterSet)-Average(CLose of Data(InterSet),LenInt);

if Relate=1 then
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;

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;

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.

Using Gradient to Shade Between Plots

Quickly Analyze Market Metrics with Gradient Based Shading

This is a simple indicator but it does involve some semi-advanced topics.  Just to let you know I am working on the third book in the Easing Into EasyLanguage series.  If you haven’t check out the first two, you might just want to head over to amazon and check those out.  This topic falls in the spectrum of the ideas that I will be covering in the Advanced Topics edition.  Also to let you know I just published the 2nd Edition of Trend Following Systems: A DIY Project – Batteries Included.  Check this out if you want to learn some Python and also see some pretty cool Trend Following algorithms – I include EasyLanguage too!

Shading Between Keltner Channels with RSI Intensity

The code that follows demonstrates how to shade between plots and adjust gradient in terms of the RSI reading.  I compiled this with MultiCharts, so I assume it will work there too – just let me know if it doesnt.  I found this code somewhere on the web when researching shading.  If I knew the original author I would definitely give full credit.   The code is rather simple, setting up the chart is just slightly more difficult.  The Keltner Channel was used to define the shading boundaries.  You could have just as easily used Bollinger Bands or anything that provided a range around the market.  Here’s the code.

inputs:  KeltnerLength( 90 ), KeltnerWid( 5 ), RSILength( 14 ), overbought( 70 ), oversold( 30 ); 
var: Avg( 0 ), Shift( 0 ), LowerBand( 0 ), UpperBand( 0 ), MyRSI( 0 ) ;

// Keltner

Avg = AverageFC( c, KeltnerLength ) ;
Shift = KeltnerWid * AvgTrueRange( Keltnerlength ) ;
UpperBand = Avg + Shift ;
LowerBand = Avg - Shift ;

Plot11( UpperBand, "UpperBand" ) ;
Plot12( LowerBand, "LowerBand" ) ;
Plot13( Avg, "MidLine" ) ;

// RSI

MyRSI = xaverage(RSI( c, RSILength ), 7) ;

var: projrsi(0);

// Get projected RSI in terms of the Upper and Lower Bands

projrsi = Avg + .01 * (UpperBand - LowerBand) * (MyRSI - 50) * 2.5;
if false then plot14( projrsi, "RSI" );

// Gradient background

var: barspacing( getappinfo( aibarspacing ) );
var: gradcolr(0);
// Remember how to use the IFF function?
gradcolr = iff( MyRSI > 50, GradientColor( projrsi, Avg, UpperBand, black, red),
GradientColor(projrsi, LowerBand, Avg, green, black) );

plot91( UpperBand, "ugrad", gradcolr, default, barspacing);
plot92( LowerBand, "lgrad");

// Show Bar - increase transparency of data to 100% so
// shading does not overlap the bar charts

plot4( c, "c");
plot5( h, "h");
plot6( l, "l");
Code to Shade with Gradient Based on a RSI Reading

That is a little bit of code that does a lot of work.  Here are the key lines and their explanations.

projrsi = Avg + .01 * (UpperBand – LowerBand) * (MyRSI – 50) * 2.5;

Remember the RSI outputs values between 0 and 100 – oscillates.  Assume RSI is in oversold territory at 24.

UpperBand = 16273 and LowerBand = 15023 and Avg = 15648

Let’s do the math:

  1. projrsi = 15468 + 0.01 * (16273 – 15023) * (24 – 50) * 2.5
  2. projrsi = 15468 + 0.01 * 1250  * – 26 * 2.5
  3. projrsi = 15468 + 12.5 * -65
  4. projrsi = 15468 – 165
  5. projrsi = 15308

Basically all this math is doing is keeping the RSI reading within the bounds of the Keltner Upper and Lower Channels.  You want a high RSI reading to be near the Upper Channel and a low RSI reading to be near the Lower Channel.   You can change up the formula to make more sense.

projrsi = Avg + (MyRSI – 50)/100 * (UpperBand – LowerBand) * 2.5

I have worked with computer graphics for many years and this is really a very neat formula.  The generic formula to constrain a value within a boundary is;

projrsi = LowerBand + (MyRSI / 100) * (UpperBand – LowerBand)

Here you take the LowerBand and add the percentage of the MyRSI/100 times the range.  This works too.  But the original formula scales or intensifies the RSI reading so you get much wider gradient spectrum.  The AVG is used as the center of gravity and the RSI is converted in terms of the middle 50 line.  A positive number, anything > 50, is then scaled higher in the range and a negative number, anything < 50 is scaled lower in the range.  In other words it makes a prettier and more informative picture.

The other important line in the code is

gradcolr = iff( MyRSI > 50, GradientColor( projrsi, Avg, UpperBand, black, red),
GradientColor(projrsi, LowerBand, Avg, green, black) );

This code uses the IFF function which basically replicates this

If MyRSI > 50 then

     gradColor = GradientColor(projrsi, Avg, UpperBand, black, red)


gradColor = GradientColor(projrsi,Avg,LowerBand,green,black);

GradientColor Function

GradientColor( dValue, dMin, dMax, nFromColor, nToColor )


Returns a specific color from a user defined gradient color range, such as Blue to White


  • dValue = value being passed-in that is within the specified Min/Max range of values
  • dMin = Starting value for the gradient range, where the nFromColor is displayed
  • dMax = Ending value for the gradient range, where the nToColor is displayed
  •  nFromColor = Starting color of the gradient
  • nToColor = Ending color of the gradient

Since the gradient shading will cover up your bars you will need to plot the bars as well.

Chart SetUp

The close should be POINT and the other inputs LINES.

Don’t Forget To Fade Out Your Data Chart

That’s it.  Like I stated earlier – I will be including things like this in the Advanced Topics edition.  I should have it wrapped sometime in July or August.


Using EXCEL VBA to Combine Equity Curves from TradeStation

A Poor Man’s Equity Curve Merger

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

vars: monthlyEqu(0),priorMonthEqu(0);
vars: totalEquity(0);

if month(date) <> month(date[1]) then
monthlyEqu = totalEquity[1] - priorMonthEqu;
priorMonthEqu = totalEquity[1];
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.

@EC, 7-2008,-5106.24,-5106.24
@EC, 8-2008, 0.00,-5106.24
@EC, 9-2008, 0.00,-5106.24
@EC,10-2008, 0.00,-5106.24
@EC,11-2008, 0.00,-5106.24
@EC, 1-2009, 0.00,20050.02
@EC, 2-2009, 0.00,20050.02
@EC, 3-2009, 0.00,20050.02
@EC, 4-2009, 0.00,20050.02
@EC, 5-2009, 0.00,20050.02
@EC, 6-2009, 0.00,20050.02
@EC, 7-2009, 0.00,20050.02
@EC, 8-2009, 0.00,20050.02
@EC, 9-2009, 0.00,20050.02
@EC,10-2009, 0.00,20050.02
@EC,11-2009, 0.00,20050.02
@EC, 1-2010, 0.00,27787.53
@EC, 2-2010, 0.00,27787.53
@EC, 3-2010, 0.00,27787.53
@EC, 4-2010, 0.00,27787.53
@EC, 5-2010, 0.00,27787.53
@EC, 6-2010, 0.00,27787.53
@EC, 7-2010, 0.00,27787.53
@EC, 8-2010, 0.00,27787.53
@EC, 9-2010,6018.76,33806.29
Indicator output

Part 2: Getting the Data into Excel

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
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
'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


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.

Excel Workbooks – One Empty with Macro and one Filled With this Post Data.