All posts by George Pruitt

George Pruitt - Author - Blogger - Programmer - Technician Bachelor of Science in Computer Science from UNC-Asheville Levo Oculos Meos In Montes

Dr. ChatGPT, or How I Learned to Stop Worrying and Love AI.

Embracing AI: The Journey from Skepticism to Synergy with ChatGPT.

Using TradeStation XML output, Python and ChatGPT to create a commercial level Portfolio Optimizer.

As a young and curious child my parents would buy me RadioShack Science Fair Kits, chemistry sets, a microscope and rockets.  I learned enough chemistry to make rotten egg gas.  I grew protozoa to delve into the microscopic world.  I scared my mom and wowed my cousins with the Estes Der Big Red rocket.  But it wasn’t until one Christmas morning I opened the Digital Computer Kit.  Or course you had to put it together before you could even use it – just like the model rockets.  Hey, you got an education assembling this stuff.  Here is a picture of a glorified circuit board.

My first computer.

This really wasn’t a computer, but more of an education in circuit design, but you could get it to solve simple problems “Crossing the River”, decimal to binary, calculating the cube root and 97 other small projects.  These problems were solved by following the various wiring diagrams.  I loved how the small panels would light up with the right answers, but grew frustrated because I couldn’t get beyond the preprogrammed wiring schema.  I had all these problems I wanted to solve but could not figure out the wiring.  Of course, there were real computers out there such as the HAL 9000.  Just kidding.  I would go to the local Radio Shack and stare at the all the computers.  Hoping one day I would have one sitting on my desk.  My Dad was an aircraft electrician (avionics) in the U.S. Navy with a specialty in Inertial Navigation Systems.  He would always want to talk about switches, gyros, some dude named Georg Ohm and oscilloscopes.  I had my mind stuck in space, you know “3-D Space Battle” – the Apple II game, to listen or to learn from his vast knowledge.    A couple of years later and a paper route I had a proper 16K computer, the TI-99-4A.  During this time, I dreamed of a supercomputer that could answer all my questions and solve all my problems.  I thought the internet was the manifestation of this dream, but in fact it was the Large Language Models such as ChatGPT.

Friend or Foe

From a programmer’s perspective AI can be scary, because you might just find yourself out of a job.  From this experiment, I think we are a few years away from this possibility.  Quantum computing, whenever it arrives, might be a viable replacement, but for now I think we are okay.

The Tools You Will Need

You will need a full installation of Python along with Numpy and Pandas installed on your computer if you want ChatGPT to do some serious coding for you.  Python and its associated libraries are simply awesome.   And Chat loves to use these tools to solve a problem.  I pay $20 a month for Chat so I don’t know if you could get the same code as I did if you have the free version.  You should try before signing up.

The Project:  A Portfolio Optimizer using an Exhaustive Search Engine

About ten years ago, I collaborated with Mike Chalek to develop a parser that analyzes the XML files TradeStation generates when saving strategy performance data. Trading a trend-following system often requires significant capital to manage a large portfolio effectively. However, many smaller traders operate with limited resources and opt to trade a subset of the full portfolio.

This approach introduces a critical challenge: determining which markets to include in the subset to produce the most efficient equity curve. For instance, suppose you have the capital to trade only four markets out of a possible portfolio of twenty. How do you decide which four to include? Do you choose the markets with the highest individual profits? Or do you select the ones that provide the best profit-to-drawdown ratio?

For smaller traders, the latter approach—prioritizing the profit-to-drawdown ratio—is typically the smarter choice. This metric accounts for both returns and risk, making it essential for those who need to manage capital conservatively. By focusing on risk-adjusted performance, you can achieve a more stable equity curve and better protect your account from significant drawdowns.

I enhanced Mike’s parser by integrating an exhaustive search engine capable of evaluating every combination of N markets taken n at a time. This approach allowed for a complete analysis of all possible subsets within a portfolio. However, as the size of the portfolio increased, the number of combinations grew exponentially, making the computations increasingly intensive. For example, in a portfolio of 20 markets, sampling 4 markets at a time results in 4,845 unique combinations to evaluate.

Calculating the number of combinations.

Using the formula above you get 4,845 combinations.  If you estimate each combination to take once second, then you are talking about 21 minutes.  N/2 will produce the most combinations.  Sampling 10 out of 20 will take 51.32 hours.

  • 1 out of 20: 20 combinations
  • 2 out of 20: 190 combinations
  • 3 out of 20: 1,140 combinations
  • 4 out of 20: 4,845 combinations
  • 5 out of 20: 15,504 combinations
  • 6 out of 20: 38,760 combinations
  • 7 out of 20: 77,520 combinations
  • 8 out of 20: 125,970 combinations
  • 9 out of 20: 167,960 combinations
  • 10 out of 20: 184,756 combinations
  • 11 out of 20: 167,960 combinations

The exhaustive method is not the best way to go when trying to find the optimal portfolios across a broad search space.  This is where a genetic optimizer comes in handy.  I played around with that too.  However, for this experiment I stuck with a portfolio of eleven markets.  I used the Andromeda-like strategy that I published in my latest installment of my Easing Into EasyLanguage series.

Here is the output of the project when sampling four markets out of a total of eleven.  All this was produced with Python and its libraries and ChatGPT.

Tabular Format

The best 4-Market portfolio out of 11 possibilities.

Graphic Format

ChatGPT, Python, Matplotlib, oh my!

Step 1 – Creating an XML Parser script

You can save your strategy performance report in RINA XML format.  The ability to save in this format seems to come and go, but my latest version of TradeStation provides this capability.  The XML files are ASCII files that contain every bar of data, market and strategy properties and trades.  However, they are extremely large as each piece of data has prefix and suffix tag.

<StrategyPerformance>
<Market>
<Name>SPY</Name>
<Bars>
<Bar>
<Date>2024-12-01</Date>
<Open>450.25</Open>
<High>455.30</High>
<Low>449.85</Low>
<Close>452.10</Close>
</Bar>
<Bar>
<Date>2024-12-02</Date>
<Open>452.15</Open>
<High>458.40</High>
<Low>451.50</Low>
<Close>457.75</Close>
</Bar>
</Bars>
</Market>
<Trades>
<Trade>
<Type>Buy</Type>
<Date>2024-12-01</Date>
<Price>450.50</Price>
<Quantity>100</Quantity>
</Trade>
<Trade>
<Type>Sell</Type>
<Date>2024-12-02</Date>
<Price>457.50</Price>
<Quantity>100</Quantity>
</Trade>
</Trades>
<PerformanceMetrics>
<NetProfit>700.00</NetProfit>
<Drawdown>50.00</Drawdown>
<ProfitFactor>2.5</ProfitFactor>
</PerformanceMetrics>
</StrategyPerformance>
Small example of XML file

​Imagine the size of the XML when working with one or even five-minute bars.  

Getting ChatGPT to create an XML parser for the specific TradeStation output

  • The first thing I did was save the performance, in XML format, from a workspace in TradeStation that used an Andromeda-like strategy on eleven different daily bar charts.
  • I asked Chat to analyze the XML file that I attached to a new chat.  I started a new chart for this project.  I discovered the chat session is also known as a “workflow”.  This term emphasizes:
    • Collaboration: We work as a team to tackle challenges.
    • Iteration: We revisit and improve upon earlier steps as needed.
    • Focus: Each session builds upon the previous ones to move closer to a defined goal.
  • Once it understood the mapping of the XML, I asked Chat to extract the bar data and output it to a csv file.  And it did it without a hitch.  When I say it did it, I mean it created a Python script that I loaded into my PyScripter IDE and executed.
  • I then asked for an output of the trade-by-trade report, and it did it without a hitch.  Notice these tasks do not require much in the way of reasoning.

Getting ChatGPT to combine the bar and trade data to produce a daily equity stream.

This is where Python and its number crunching libraries came in handy.  Chat pulled in the following libraries:

  • xml.etree
  • pandas
  • tkinter
  • datetime

I love Python, but the level of abstraction with its libraries can make you dizzy.  It is not important to fully understand the panda’s data frame to utilize it.   Heck, I didn’t really know how it mapped and extracted the data from the XML file.  I prompted chat with the following:

[My prompts are bold and italicized.]

With the bar data and the trade data and the bigpointvlaue, can you create an equity curve that shows a combination of open trade equity and closed trade equity?  Remember the top part of the xml file contains bar data and the lower part contains all the trade data.

It produced a script that hung up.  I informed chat that the script hung up and that the script wasn’t working.  It found the error and fixed it.  Think about what Chat was doing. It was able to align the data so that open trades produced open trade equity and closed out trades produced closed trade equity.  Well, initially it had a small problem.    It knew what Buy and Sell meant, and the math involved with calculating the two forms of equity, open and closed.  I didn’t inform Chat of any of this.  But the equity data did not look exactly right.  It looked like the closed trade equity was being calculated improperly.

Is the Script checking for LExit and SExit to calculate when a trade closes out?

Once it figured out that the equity stream must contain open and closed trade equity and learned the terms, LExit and SExit, a new script was created that nearly replicated the equity curve from the TradeStation report.  When Chat starts creating lengthy scripts it will open a side bar window call the “Canvas” and put the script in there.  This makes it easier to copy and paste the code.  I eventually noticed that the equity curve did not include commission and slippage charges.

Please extract the slippage and commission values and deduct this amount from each trade.

At this point the workflow remembered the mapping of the XML file and was able incorporate these two values into the trade processing.  I wanted the user to be able to select multiple XML files and have the script process these files and produce the three output files, bar_data, trade_data, and equity_data.    I did have to explain that the execution costs must be applied to all the entries and exits.

I would like the user to select multiple xml files and create the data and trade and equity files incorporating the system name and market name and their naming scheme.

A new library was imported, TKinter and a file open dialog was used for the user to select all the XML files they wanted to process.  These few chores required very little interaction between Chat and me.  I thought, wow this is going to be a breeze.  I moved onto the next phase of the project by asking Chat the following:

Can you create a script that will use Tkinter to open the equity files from the prior script and allow the user to choose N out of the total files selected to create all the combined equity curves using an exhaustive search method.

I knew this was a BIG ASK!  But it swallowed this big pill without a hitch.  Maybe us programmers will be replaced sooner than later.  I wanted to fine tune the script.

Can you keep track of maximum draw down for each combination and then sort the combinations by the profit to draw down ratio. For the best combination can you create a csv file with the daily returns so i can plot them in Excel.

On a quick scan of the results, I did not initially notice that the maximum draw-down metric was not right.  So, I pushed on the fine tuning.

This works great. In the output files can we delete the path name to the csv files. For the output I would just like to have the system name and the symbol for each combination.

Script created.

I had the following error message. NameError: name ‘all_combinations’ is not defined. I also asked Chat if it can you add “Date” to the header for the combination daily files?

Many times, Chat will just recreate the part of the code that needs to be modified.  This makes copying and pasting difficult, because you have to make sure you find the right location in the code to replace.  I always ask Chat to recreate the entire script with the latest changes.  

Hey Chat, the following error message was created:  system, symbol = filename.replace(“.csv”, “”).split(“_”) ValueError: too many values to unpack (expected 2)

The error was fixed, but I finally noticed the maximum draw down calculations were not right.

I don’t think the maximum draw down metric is being calculated properly. “Andromeda-Like_@SB, Andromeda-Like_@EC, Andromeda-Like_@S, Andromeda-Like_@JY”,
PL:  92141.8499999999,
DD:  92985.59999999992,
Ratio:  0.990926014350609,144.
The draw down metric is similar to the total profit metric.
Script created.

The draw down is still not calculated properly. I think there is an error in your reasoning. I calculate draw down by iterating through the equity curve and keeping track of peaks and subsequent troughs. The largest distance between the peaks and its associated trough is the max draw down.

Script created but looked a little primitive.  So, I asked.

Is this the most efficient method?

Well, I should have kept my mouth shut.  I felt like the Python libraries would be able to do this much quicker with list comprehension or with some other magic.   And Chat agreed with me.  Script created.

The draw down is still incorrect.

Script created.

It was still wrong and instead of going back and using the original draw down script I tried to nudge Chat into the right direction.

The equity curve will have multiple peaks and multiple troughs. You will need to keep track of all of the peaks and troughs and then find the maximum distance between the sets of peaks and troughs.

Script created and was still wrong.  This is where you start going down a rabbit hole.  Chat will produce debugging script to try and figure out where the error resides.  But this is very time consuming, so I kept pushing Chat to do it right.

Each peak must be tied to its respective trough. Each distance between each respective peak and drawdown can then be measured for the largest value.

Script is wrong.

This still not working. I don’t think you are calculating the difference between peaks and subsequent troughs properly.

Script is wrong.

This is still not working. We should be able to do this in a single pass. Iterate through the daily values and track the highest level at that point in the series and then the subsequent deepest trough. Store the first difference. Continue iterating looking for a new peak and the subsequent deepest trough. Compare this value to the prior and take the largest of the two. Store this value and continue iterating.

Script from original request is generated.  Finally got back to the starting point.  What a waste of time.  I will be more careful with my suggestions.  However, the script is still wrong – arrgh!  I decide to run the debugger on the draw down function and the code is right, but the data is wrong.

The problem lies in the equity series. It should contain the combined equity for the combinations. There should be a master date and each combination populates the master date. If there is a missing date in the combinations, then the master date should copy the prior combinations combined value.

Warning message:  se obj.ffill() or obj.bfill() instead.  df_aligned = df.reindex(master_date_index).fillna(method=”ffill”).fillna(0)

Chat created some deprecated code.   This was an easy fix, I just had to replace one line of code.  However, every iteration following this still had the same damn deprecated code.

Error or warning: FutureWarning: Series.getitem treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use ser.iloc[pos] peak = equity_series[0] # Initialize the first value as the peak

Script updated.

Can we drop the “_equity_curve” from the name of the system and symbol in the Perfomance metrics file. Will excel accept the combination names as a single column or will each symbol have its own column because of the comma. I would like for each combination in the Performance_Metrics file to occupy just one column.

Script created and this is what it should look like.  Notice it was remembering the wrong maximum draw down values I fed it earlier.  Don’t worry it was right in the script.

Combination Total Profit Max Drawdown Profit-to-Drawdown Ratio Combination Number
“Andromeda-Like_@SB, Andromeda-Like_@EC” 92141.85 92985.60 0.99 1
“Andromeda-Like_@S, Andromeda-Like_@JY” 82450.55 70530.45 1.17 2

Conclusion

I could have done the same thing ChatGPT did for me, but I wouldn’t have used the Numpy or Pandas libraries simply because I’m not familiar with them. These libraries make the exhaustive search manageable and incredibly efficient. They handle tasks much faster than pure Python alone.

To get ChatGPT to generate the code you need, being a programmer is essential. You’ll need to guide it through debugging, steer it in the right direction, and test the scripts it produces. It’s a back-and-forth process—running the script, identifying warnings or errors, and pointing out incorrect outputs. Sometimes, your programming insights and suggestions might inadvertently lead ChatGPT down a rabbit hole. Features that worked in earlier versions may stop working in subsequent iterations as modifications are applied to address earlier issues.

ChatGPT can also slow down at times, and its Canvas tool has a line limit, which can result in incomplete scripts. As a programmer, it’s easy to spot these issues—you’ll need to inform ChatGPT, and it will adjust by splitting the script into parts, some appearing in the Canvas and the rest in the chat window.

The collaboration between ChatGPT and me was powerful enough to replicate, in just one day, software that Mike Chalek and I spent weeks developing a decade ago. The original version had a cleaner GUI, but it was significantly slower compared to what we’ve achieved here.

If you’re a programmer, have Python installed with its libraries, and work with ChatGPT, the possibilities are endless. But there’s no magic—success requires thoughtful feedback and precise prompting.

Email me if you would like to have the Python scripts that accomplish the following tasks.  If you are not familiar with pandas or xml processing, the code, even being Python savvy, will look a little foreign.  No worries – it just works.

  1. XML Parser – creates data, trades and equity files in .csv format.
  2. TradeStationExhaustiveCombos – creates all the combos when sampling n out of N markets.
  3. The simple Tkinter GUI and Matplotlib graphing tool to plot the combos.

There is a total of three scripts.  Remember you will need to have Python, pandas, matplotlib already installed on your computer.  If you have any questions on how to install these just let me know.

Build your Technical/Trading Library for less than $150

Great books that are dirt cheap!

These books cover a wide range of technical analysis and trading topics.  I have read and/or utilized ideas from these books for developing trading ideas and in help writing my own books.  I wanted to see what I could get for less than $13 a book.  I was pleasantly surprised at what I could get at Thriftbooks.com for this price or lower.  If you go a little higher in price, you will get some really great deals too.  Yes, these books are old, and I am sure I have left out some great names, but the bang for the buck is ridiculous.

Note – I captured the Add to Cart button in the IMAGES – IT DOES NOT WORK.  I wanted to show the various editions of each book.  Go to www.thriftbooks.com and search for the title or author and you should be able to find these books just like I did.  Also check out amazon.com

The Gallacher, Taleb, McKay, Schwager, and Livermore (Lefevre) books are fun reads that give an honest view of trading in general.  Elders’ book just needs to be any trader’s library.

The Edwards and Magee, Sklarew and Darvas books are classics.

Sunny Harris’ book is great for beginners.

The Stridsman and Rotella books are concrete methods to systematic trading.

Kaufman and McCormick/Katz are great reference books.

The Conway book has great EasyLanguage examples.

You can learn a lot from Schwager’s first book.

Learn optimization from Robert Pardo and money management from Fred Gehm’s classic.

If you go up in price a little bit, you will get Ralph Vince, Larry Williams, John Carter, Rober Carver, Andreas Clenow, and Ernie Chan.  If you are just starting out, you must have a trading library, and this would be a great start for the first shelf.

 

Should you use a profit taking algorithm in your Trend Following system?

If letting profits run is key to the success of a trend following approach, is there a way to take profit without diminishing returns?

Most trend following approaches win less than 40% of the time.   So, the big profitable trades are what saves the day for this type of trading approach.  However, it is pure pain to simply sit there and watch a large profit erode, just because the criteria to exit the trade takes many days to be met.

Three methods to take a profit on a Trend Following algorithm

  1.  Simple profit objective – take a profit at a multiple of market risk.
  2.  Trail a stop (% of ATR) after a profit level (% of ATR) is achieved.
  3. Trail a stop (Donchian Channel) after a profit level (% of ATR) is achieved.

Use an input switch to determine which exit to incorporate

Inputs: initCapital(200000),rskAmt(.02),
useMoneyManagement(False),exitLen(13),
maxTradeLoss$(2500),
// the following allows the user to pick
// which exit to use
// 1: pure profit objective
// exit1ProfATRMult allows use to select
// amount of profit in terms of ATR
// 2: trailing stop 1 - the user can choose
// the treshhold amount in terms of ATR
// to be reached before trailing begins
// 3: trailing stop 2 - the user can chose
// the threshold amount in terms of ATR
// to be reached before tailing begins
whichExit(1),
exit1ProfATRMult(3),
exit2ThreshATRMult(2),exit2TrailATRMult(1),
exit3ThreshATRMult(2),exit3ChanDays(5);
Exit switch and the parameters needed for each switch.

The switch determines which exit to use later in the code.  Using inputs to allow the user to change via the interface also allows us to use an optimizer to search for the best combination of inputs.  I used MultiCharts Portfolio Trader to optimize across a basket of 21 diverse markets.  Here are the values I used for each exit switch.

MR = Market risk was defined as 2 X avgTrueRange(15).

  • Pure profit objective -Multiple from 2 to 10 in increments of 0.25.  Take profit at entryPrice + or – Profit Multiple X MR
  • Trailing stop using MR – Profit Thresh Multiple from 2 to 4 in increments of 0.1.  Trailing Stop Multiple from 1 to 4 in increments of 0.1.
  • Trailing stop using MR and Donchian Channel – Profit Thresh Multiple from 2 to 4 in increments of 0.1.  Donchian length from 3 to 10 days.

Complete strategy code incorporating exit switch.  This code is from Michael Covel’s 2005 Trend Following book (Covel, Michael. Trend Following: How Great Traders Make Millions in Up or Down Markets. FT Press, 2005.)  This strategy is highlighted in my latest installment in my Easing into EasyLanguage series – Trend Following edition.


vars:buyLevel(0),shortLevel(0),longExit(0),shortExit(0);

Inputs: initCapital(200000),rskAmt(.02),
useMoneyManagement(False),exitLen(13),
maxTradeLoss$(2500),whichExit(1),
exit1ProfATRMult(3),
exit2ThreshATRMult(2),exit2TrailATRMult(1),
exit3ThreshATRMult(2),exit3ChanDays(5);

Vars: marketRisk(0), workingCapital(0),
marketRisk1(0),marketRisk2(0),
numContracts1(0),numContracts2(0);

//Reinvest profits? - uncomment the first line and comment out the second
//workingCapital = Portfolio_Equity-Portfolio_OpenPositionProfit;
workingCapital = initCapital;


buyLevel = highest(High,89) + minMove/priceScale;
shortLevel = lowest(Low,89) - minMove/priceScale;
longExit = lowest(Low,exitLen) - minMove/priceScale;
shortExit = highest(High,exitLen) + minMove/priceScale;

marketRisk = avgTrueRange(15)*2*BigPointValue;
marketRisk1 =(buyLevel - longExit)*BigPointValue;
marketRisk2 =(shortExit - shortLevel)*BigPointValue;
marketRisk1 = minList(marketRisk,marketRisk1);
marketRisk2 = minList(marketRisk,marketRisk2);

numContracts1 = (workingCapital * rskAmt) /marketRisk1;
numContracts2 = (workingCapital * rskAmt) /marketRisk2;

if not(useMoneyManagement) then
begin
numContracts1 = 1;
numContracts2 =1;
end;

numContracts1 = maxList(numContracts1,intPortion(numContracts1)); {Round down to the nearest whole number}
numContracts2 = MaxList(numContracts2,intPortion(numContracts1));


if c < buyLevel then buy numContracts1 contracts next bar at buyLevel stop;
if c > shortLevel then Sellshort numContracts2 contracts next bar at shortLevel stop;

buytocover next bar at shortExit stop;
Sell next bar at longExit stop;

vars: marketRiskPoints(0);
marketRiskPoints = marketRisk/bigPointValue;
if marketPosition = 1 then
begin
if whichExit = 1 then
sell("Lxit-1") next bar at entryPrice + exit1ProfATRMult * marketRiskPoints limit;
if whichExit = 2 then
if maxcontractprofit > (exit2ThreshATRMult * marketRiskPoints ) * bigPointValue then
sell("Lxit-2") next bar at entryPrice + maxContractProfit/bigPointValue - exit2TrailATRMult*marketRiskPoints stop;
if whichExit = 3 then
if maxcontractprofit > (exit3ThreshATRMult * marketRiskPoints ) * bigPointValue then
sell("Lxit-3") next bar at lowest(l,exit3ChanDays) stop;
end;

if marketPosition = -1 then
begin
if whichExit = 1 then
buyToCover("Sxit-1") next bar at entryPrice - exit1ProfATRMult * marketRiskPoints limit;
if whichExit = 2 then
if maxcontractprofit > (exit2ThreshATRMult * marketRiskPoints ) * bigPointValue then
buyToCover("Sxit-2") next bar at entryPrice - maxContractProfit/bigPointValue + exit2TrailATRMult*marketRiskPoints stop;
if whichExit = 3 then
if maxcontractprofit > (exit3ThreshATRMult * marketRiskPoints ) * bigPointValue then
buyToCover("Sxit-3") next bar at highest(h,exit3ChanDays) stop;
end;

setStopLoss(maxTradeLoss$);

Here’s the fun code from the complete listing.

vars: marketRiskPoints(0);
marketRiskPoints = marketRisk/bigPointValue;
if marketPosition = 1 then
begin
if whichExit = 1 then
sell("Lxit-1") next bar at entryPrice + exit1ProfATRMult * marketRiskPoints limit;
if whichExit = 2 then
if maxContractProfit > (exit2ThreshATRMult * marketRiskPoints ) * bigPointValue then
sell("Lxit-2") next bar at entryPrice + maxContractProfit/bigPointValue - exit2TrailATRMult*marketRiskPoints stop;
if whichExit = 3 then
if maxContractProfit > (exit3ThreshATRMult * marketRiskPoints ) * bigPointValue then
sell("Lxit-3") next bar at lowest(l,exit3ChanDays) stop;
end;

if marketPosition = -1 then
begin
if whichExit = 1 then
buyToCover("Sxit-1") next bar at entryPrice - exit1ProfATRMult * marketRiskPoints limit;
if whichExit = 2 then
if maxContractProfit > (exit2ThreshATRMult * marketRiskPoints ) * bigPointValue then
buyToCover("Sxit-2") next bar at entryPrice - maxContractProfit/bigPointValue + exit2TrailATRMult*marketRiskPoints stop;
if whichExit = 3 then
if maxContractProfit > (exit3ThreshATRMult * marketRiskPoints ) * bigPointValue then
buyToCover("Sxit-3") next bar at highest(h,exit3ChanDays) stop;
end;

The first exit is rather simple – just get out on a limit order at a nice profit level.  The second and third exit mechanisms are a little more complicated.  The key variable in the code is the maxContractProfit keyword.  This value stores the highest level, from a long side perspective, reached during the life of the trade.  If max profit exceeds the exit2ThreshATRMult, then trail the apex by exit2TrailATRMult.  Let’s take a look at the math from a long side trade.

if maxContractProfit > (exit2ThreshATRMult * marketRiskPoints ) * bigPointValue

Since maxContractProfit is in dollar you must convert the exit2ThreshATRMult X marketRiskPoints into dollars as well.  If you review the full code listing you will see that I convert the dollar value, marketRisk, into points and store the value in marketRiskPoints.  The conversion to dollars is accomplished by multiplying the product by bigPointValue.

sell("Lxit-2") next bar at
entryPrice + maxContractProfit / bigPointValue - exit2TrailATRMult * marketRiskPoints stop;

I know this looks complicated, so let’s break it down.  Once I exceed a certain profit level, I calculate a trailing stop at the entryPrice plus the apex in price during the trade (maxContractProfit / bigPointValue) minus the exit2TrailATRMult X marketRiskPoints. If the price of the market keeps rising, so will the trailing stop.  That last statement is not necessarily true, since the trailing stop is based on market volatility in terms of the ATR.  If the market rises a slight amount, and the ATR increases more dramatically, then the trailing stop could actually move down.  This might be what you want.  Give the market more room in a noisier market.  What could you do to ratchet this stop?  Mind your dollars and your points in your calculations.

The third exit uses the same profit trigger, but simply installs an exit based on a shorter term Donchian channel.  This is a trailing stop too, but it utilizes a chart point to help define the exit price.

Results of the three exits

Exit 1 – Pure Profit Objective

Take a profit on a limit order once profit reaches a multiple of market risk aka 2 X ATR(15).

Pure profit object. Profit in terms of ATR or perceived market risk.

The profit objective that proved to be the best was using a multiple of 7.  A multiple of 10 basically negates the profit objective.   With this system several profit objective multiples seemed to work.

Exit – 2 – Profit Threshold and Trailing Stop in terms of ATR or market risk

Trail a stop a multiple of ATR after a multiple of ATR in profit is reached.

Trailing Stop using ATR
3-D view of parameters
3D view of parameter pairs

This strategy liked 3 multiples of ATR of profit before trailing and applying a multiple of 1.3 ATR as a stop.

Like I said in the video, watch out for 1.3 as you trailing amount multiple as it seems to be on a mountain ridge.

Exit – 3 – Profit Threshold in terms of ATR or market risk and a Donchain Channel trailing stop

Trail a stop using a Donchian Channel after a multiple of ATR in profit is reached.  Here was a profit level is reached, incorporate a tailing stop at the lowest low or the highest high of N days back.

Using Donchian Channel as trailing stop.
3-D view of parameters
3D view of parameters for Exit 3.

Conclusion

The core strategy is just an 89-day Donchian Channel for entry and a 13-Day Donchian Channel for exit.  The existing exit is a trailing exit and after I wrote this lengthy post, I started to think that a different strategy might be more appropriate.  However, as you can see from the contour charts, using a trailing stop that is closer than a 13-day Donchian might be more productive.   From this analysis you would be led to believe the ATR based profit and exit triggers (Exit #2) is superior.  But this may not be the case for all strategies.  I will leave this up to you to decide.  Here is the benchmark performance analysis with just the core logic.

Core logic results.

If you like this type of explanation and code, make sure you check at my latest book at amazon.com.  Easing into EasyLanguage – Trend Following Edition.

Buy in November, Sell in May Strategy Framework

Thanks to Jeff Swanson for the basis of this post

I like to post something educational at least once a month.  Sometimes, it’s difficult to come up with stuff to write about.  Jeff really got me thinking with his Buy November… post.  Check out his post “Riding the Market Waves:  How to Surf Seasonal Trends to Trading Success.”  Hopefully you have read his post and now have returned.  As you know, the gist of his post was to buy in November and sell in May.  Jeff was gracious enough to provide analysis, source and suggestions for improvement for this base strategy.

Why Change Jeff’s Code to a Framework?

I found Jeff’s post most intriguing, so the first think I start thinking about is how could I optimize the buy and sell months, a max loss, the three entry filters that he provided and in addition add a sell short option.  If you have read my books, you know I like to develop frameworks for further research when I program an algorithm or strategy.  Here is how I developed the framework:

  1. Optimize the entry month from January to December or 1 to 12.
  2. Optimize the exit month from January to December or 1 to 12.
  3. Optimize to go long or go short or 1 to 2 (to go short any number other than 1 really).
input: startMonth(11),endMonth(5),
longOrShort(1),


currentMonth = Month(Date of tomorrow);
If currentMonth = startMonth and mp = 0 and entriesThisMonth = 0 Then
begin
// a trade can only occur if canBuy is True - start month is active as
// long as the filtering allows it. Until the filter is in alignment
// keep looking for a trading during the ENTIRE startMonth
if longOrShort = 1 and canBuy then
entriesThisMonth = 1;
if longOrShort = 1 and canBuy then
buy("Buy Month") iShares contracts next bar at market;
if longOrShort <> 1 and canShort then
sellShort("Short Month") iShares contracts next bar at market;
if longOrShort = -1 and canShort then
entriesThisMonth = 1;
end;

if CurrentMonth = endMonth Then
begin
if longOrShort = 1 then
sell("L-xit Month") currentShares contracts next bar at market
else
buyToCover("S-xit Month") currentShares contracts next bar at market;
end;

if mp = 1 then
sell("l-xitMM") next bar at entryPrice - maxTradeRisk/bigPointValue stop;
if mp =-1 then
buyToCover("s-xitMM") next bar at entryPrice + maxTradeRisk/bigPointValue stop;
Snippet of the bones with extra flavor to enter and exit on certain months

You can see that I have provided the three inputs:

  1. startMonth
  2. endMonth
  3. longOrShort

I get the currentMonth by peeking at the date of tomorrow and passing this date to the month function.  If tomorrow is the first day of the month that I want to enter a long or short and the current market position (mp), and entriesThisMonth = 0, then a long or short position will be initiated.  If the filters I describe a little later allow it, I know that I will be executing a trade tomorrow, and I can go ahead assign a 1 to entries this month.  Why do I do this?  Just wait and you will see.   Long entries depend on the variable longOrShort being equal to 1 and the toggle canBuy set to True.  What is canBuy.  Just wait and you will see.  The sell short is similar, but conversely longOrShort needs to not equal 1.  In addition, canShort needs to be true too.

If the currentMonth = endMonth, then based on the market position a sell or a buy to cover will be executed.

How to add filters to Determine canBuy and canShort

inputs: 
useMACDFilter(1), MACDFast(9), MACDSlow(26), MACDAvgLen(9), MACDLevel(0),
useMAFilter(0), MALength(30),
useRSIFilter(0), RSILength(14), RSILevel(50)

RSIVal = rsi(close,RSILength);
MAVal = xAverage(close,MALength);
MACDVal = macd(close,MACDFast,MACDSlow);
MACDAvg = xAverage(MACDVal,MACDAvgLen);

if useMACDFilter = 1 then
begin
canBuy = MACDVal > MACDLevel;
canShort = MACDVal < MACDLevel;
end;

if useMAFilter = 1 then
begin
canBuy = close > MAVal and canBuy;
canShort = close < MAVal and canShort;
end;

if useRSIFilter = 1 then
begin
canBuy = RSIVal > RSILevel and canBuy;
canShort = RSIVal < RSILevel and canShort;
end;
Calculate Filter Components and then test them

You cannot optimize a True to False toggle, but you can optimize 0 for off and 1 for on.  Here the useFilterName inputs are initially set to 0 or off.  Each filter indicator has respective inputs so that the filters can be calculated with the user’s input.  If the filters are equal to one, then a test to turn canBuy and canShort to on or off is laid out in the code.  Each test depends on either the state of price compared to the indicator value, or the indicator’s relationship to a user defined level or value.

Will this code test all the combination of the filters?

Yes!  F1 is Filter 1 and F2 is Filter 2 and F3 is Filter 3.  By optimizing each filter from 0 to 1, you will span this search space.

  • F1 = On; F2 = Off; F3 = Off
  • F1 = On; F2 = On; F3 = Off
  • F1 = On; F2 = On; F3 = On
  • F1 = Off; F2 = On; F3 = Off
  • F1 = Off; F2 = On; F3 = On
  • F1 = Off; F2 = Off: F3 = On
  • F1 = On; F2 = Off; F3 = On

You will notice I initially set canBuy and canShort to True and then turn them off if an offending filter occurs.  Notice how I AND the results for Filter 2 and Filter 3 with canBuy or canShort.  Doing this allows me to cascade the filter combinations.  I do want to test when all filters are in alignment.  In other words, they must all be True to initiate a position.

Should the Filters be Active During the Entire Entry Month?

What if the first day of the month arrives and you can’t initiate a trade due to a conflict of one of the filters.  Should we allow a trade later in the entry month if the filters align properly?  If we are testing 25 years of history and allow for entry later on in the month, we could definitely generate as close to 25 trades as possible.   This line of code keeps the potential of a trade open for the entire month.


// only set entriesThisMonth to true
// when all the stars align - might enter a long
// trade on the last day of the month

if longOrShort = 1 and canBuy then
entriesThisMonth = 1;
Keep the entire start month active

Some Tricky Code

I wanted to allow a money management exit on a contract basis.  I had to devise some code that would not allow me to reenter the startMonth if I got stopped out prematurely in the startMonth (the same month as entry.)

if entriesThisMonth = 1 and monthOfTomorrow <> startMonth then
entriesThisMonth = 0;
This code resets entriesThisMonth

If a position is initiated, I know entriesThisMonth will be set to one.  If I enter into another month that is not the startMonth then entriesThisMonth is set to 0.  This prevents reentry in case we get stopped out in the same month we initially enter a position.  In other words, entriesThisMonth stays one until a new month is observed.  And we can’t enter when entriesThisMonth is equal to one.

Full Code

input: startMonth(11),endMonth(5),
longOrShort(1),
useMACDFilter(1),MACDFast(9),MACDSlow(26),MACDAvgLen(9),MACDLevel(0),
useMAFilter(0),MALength(30),
useRSIFilter(0),RSILength(14),RSILevel(50),
startAccountSize(100000),
marketRiskLen(30),
riskPerTrade(5000),
maxTradeRisk(5000);

vars: currentMonth(0),mp(0),iShares(0),entriesThisMonth(0),monthOfTomorrow(0),
RSIVal(0),MAVal(0),MACDVal(0),MACDAvg(0),canBuy(True),canShort(True);

mp = marketPosition;
iShares = riskPerTrade/bigPointValue/avgTrueRange(marketRiskLen);

RSIVal = rsi(close,RSILength);
MAVal = xAverage(close,MALength);
MACDVal = macd(close,MACDFast,MACDSlow);
MACDAvg = xAverage(MACDVal,MACDAvgLen);

canBuy = True;
canShort = True;

mp = marketPosition;

monthOfTomorrow = month(date of tomorrow);

if entriesThisMonth = 1 and monthOfTomorrow <> startMonth then
entriesThisMonth = 0;

if useMACDFilter = 1 then
begin
canBuy = MACDVal > MACDLevel;
canShort = MACDVal < MACDLevel;
end;

if useMAFilter = 1 then
begin
canBuy = close > MAVal and canBuy;
canShort = close < MAVal and canShort;
end;

if useRSIFilter = 1 then
begin
canBuy = RSIVal > RSILevel and canBuy;
canShort = RSIVal < RSILevel and canShort;
end;

currentMonth = Month(Date of tomorrow);
//print(d," ",currentMonth," ",startMonth," ",entriesThisMonth);
If currentMonth = startMonth and mp = 0 and entriesThisMonth = 0 Then
begin
// print(d," ",currentMonth," ",canBuy);
if longOrShort = 1 and canBuy then
entriesThisMonth = 1;
if longOrShort = 1 and canBuy then
buy("Buy Month") iShares contracts next bar at market;
if longOrShort <> 1 and canShort then
sellShort("Short Month") iShares contracts next bar at market;
if longOrShort = -1 and canShort then
entriesThisMonth = 1;
end;

if CurrentMonth = endMonth Then
begin
if longOrShort = 1 then
sell("L-xit Month") currentShares contracts next bar at market
else
buyToCover("S-xit Month") currentShares contracts next bar at market;
end;

if mp = 1 then
sell("l-xitMM") next bar at entryPrice - maxTradeRisk/bigPointValue stop;
if mp =-1 then
buyToCover("s-xitMM") next bar at entryPrice + maxTradeRisk/bigPointValue stop;
Complete Code Framework

Here is the best equity curve I uncovered when I optimized the startMonth from 1 to 12 and the endMonth from 1 to 12 and the maxTradeRisk per contract and the three entry filters.  Entering in November when the moving average filter aligns and exiting on the first day of August and risking $5,500 per contract produced this equity curve.

Enter November get out the beginning of August

The test returned what would basically be similar to a buy and hold scenario; the difference being you only hold the trade between seven and eight months of the year and risk only $5,500 per contract.  If you get stopped out, you wait until November to get back in – whenever the moving average filter allows.  Net profit to draw down ratio is north of 4.0.

Last Comment

If I optimize from 1 to 12 for the start month and 1 to 12 for end month, will this not cause an error?  What if the two values equal?  I mean I can’t enter and exit in the same month – a one-day trade?  You could make the code smarter, but it doesn’t matter.  As a user you will know better than to use the same number and the optimizer will test the combination with the same number, but the results will fall off the table.   In this case, error trapping doesn’t prevent a necessarily unwanted or dangerous scenario.

Prune Your Trend Following Algorithm

Multiple trading decisions based on “logic” may not add to the bottom line

In this post, I will present a trend following system that uses four exit techniques.  These techniques are based on experience and also logic.  The problem with using multiple exit techniques is that it is difficult to see the synergy that is generated from all the moving parts.  Pruning your algorithm may help cut down on invisible redundancy and opportunities to over curve fit.  The trading strategy I will be presenting will use a very popular entry technique overlaid with trade risk compression.

Entry logic

Long:

Criteria #1:  Penetration of the closing price above an 85 day (closing prices) and 1.5X standard deviation-based Bollinger Band.

Criteria #2:  The mid-band or moving average must be increasing for the past three consecutive days.

Criteria #3: The trade risk (1.5X standard deviation) must be less than 3 X average true range for the past twenty days and also must be less than $4,500.

Risk is initially defined by the standard deviation of the market but is then compared to $4,500. If the trade risk is less than $4,500, then a trade is entered. I am allowing the market movement to define risk, but I am putting a ceiling on it if necessary.

Short:

Criteria #1:  Penetration of the closing price below an 85 day (closing prices) and 1.5X standard deviation-based Bollinger Band.

Criteria #2:  The mid-band or moving average must be decreasing for the past three consecutive days.

Criteria #3:  Same as criteria #3 on the long side

Exit Logic

Exit #1:  Like any Bollinger Band strategy, the mid band or moving average is the initial exit point.  This exit must be included in this particular strategy, because it allows exits at profitable levels and works synergistically with the entry technique.

Exit #2:  Fixed $ stop loss ($3,000)

Exit #3:  The mid-band must be decreasing for three consecutive days and today’s close must be below the entry price.

Exit #4:  Todays true range must be greater than 3X average true range for the past twenty days, and today’s close is below yesterday’s, and yesterday’s close must be below the prior days.

Here is the logic of exits #2 through exit #4.  With longer term trend following system, risk can increase quickly during a trade and capping the maximum loss to $3,000 can help in extreme situations.  If the mid-band starts to move down for three consecutive days and the trade is underwater, then the trade probably should be aborted.  If you have a very wide bar and the market has closed twice against the trade, there is a good chance the trade should be aborted.

Short exits use the same logic but in reverse.  The close must close below the midband, or a $3,000 maximum loss, or three bars where each moving average is greater than the one before, or a wide bar and two consecutive up closes.

Here is the logic in PowerLanguage/EasyLanguage that includes the which exit seletor.

[LegacyColorValue = true]; 
Inputs: maxEntryRisk$(4500),maxNATRLossMult(3),maxTradeLoss$(3000),
indicLen(85),numStdDevs(1.5),highVolMult(3),whichExit(7);

Vars: upperBand(0), lowerBand(0),slopeUp(False),slopeDn(False),
largeAtr(0),sma(0),
initialRisk(0),tradeRisk(0),
longLoss(0),shortLoss(0),permString("");

upperBand = bollingerBand(close,indicLen,numStdDevs);
lowerBand = bollingerBand(close,indicLen,-numStdDevs);
largeATR = highVolMult*(AvgTrueRange(20));

sma = average(close,indicLen);

slopeUp = sma>sma[1] and sma[1]>sma[2] and sma[2]>sma[3];
slopeDn = sma<sma[1] and sma[1]<sma[2] and sma[2]<sma[3];

initialRisk = AvgTrueRange(20);
largeATR = highVolMult * initialRisk;
tradeRisk = (upperBand - sma);
// 3 objects in our permutations
// exit 1, exit 2, exit 3
// perm # exit #
// 1 1
// 2 1,2
// 3 1,3
// 4 2
// 5 2,3
// 6 3
// 7 1,2,3

if whichExit = 1 then permString = "1";
if whichExit = 2 then permString = "1,2";
if whichExit = 3 then permString = "1,3";
if whichExit = 4 then permString = "2";
if whichExit = 5 then permString = "2,3";
if whichExit = 6 then permString = "3";
if whichExit = 7 then permString = "1,2,3";



{Long Entry:}
If (MarketPosition = 0) and
Close crosses above upperBand and slopeUp and
(tradeRisk < initialRisk*maxNATRLossMult and tradeRisk<maxEntryRisk$/bigPointValue) then
begin
Buy ("LE") Next Bar at Market;
End;


{Short Entry:}

If (MarketPosition = 0) and slopeDn and
Close crosses below lowerBand and
(tradeRisk < initialRisk*maxNATRLossMult and tradeRisk<maxEntryRisk$/bigPointValue) then
begin
Sell Short ("SE") Next Bar at Market;
End;


{Long Exits:}

if marketPosition = 1 Then
Begin
longLoss = initialRisk * maxNATRLossMult;
longLoss = minList(longLoss,maxTradeLoss$/bigPointValue);

If Close < sma then
Sell ("LX Stop") Next Bar at Market;;

if inStr(permString,"1") > 0 then
sell("LX MaxL") next bar at entryPrice - longLoss stop;

if inStr(permString,"2") > 0 then
If sma < sma[1] and sma[1] < sma[2] and sma[2] < sma[3] and close < entryPrice then
Sell ("LX MA") Next Bar at Market;
if inStr(permString,"3") > 0 then
If TrueRange > largeATR and close < close[1] and close[1] < close[2] then
Sell ("LX ATR") Next Bar at Market;
end;

{Short Exit:}

If (MarketPosition = -1) Then
Begin

shortLoss = initialRisk * maxNATRLossMult;
shortLoss = minList(shortLoss,maxTradeLoss$/bigPointValue);
if Close > sma then
Buy to Cover ("SX Stop") Next Bar at Market;

if inStr(permString,"1") > 0 then
buyToCover("SX MaxL") next bar at entryPrice + shortLoss stop;

if inStr(permString,"2") > 0 then
If sma > sma[1] and sma[1] > sma[2] and sma[2] > sma[3] and close > entryPrice then
Buy to Cover ("SX MA") Next Bar at Market;
if inStr(permString,"3") > 0 then
If TrueRange > largeAtr and close > close[1] and close[1] > close[2] then
Buy to Cover ("SX ATR") Next Bar at Market;
end;
Trend following with exit selector

Please note that I modified the code from my original by forcing the close to cross above or below the Bollinger Bands.  There is a slight chance that one of the exits could get you out of a trade outside of the bands, and this could potentially cause and automatic re-entry in the same direction at the same price.  Forcing a crossing, makes sure the market is currently within the bands’ boundaries.

This code has an input that will allow the user to select which combination of exits to use.

Since we have three exits, and we want to evaluate all the combinations of each exit separately, taken two of the exits and finally all the exits, we will need to rely on a combinatorial table.    In long form, here are the combinations:

3 objects in our combinations of exit 1, exit 2, exit 3

  • one  – 1
  • two  – 1,2
  • three  –  1,3
  • four –  2
  • five  – 2,3
  • six –  3
  • seven  –  1,2,3

There are a total of seven different combinations. Given the small set, we can effectively hard-code this using string manipulation to create a combinatorial table. For larger sets, you may find my post on the Pattern Smasher beneficial. A robust programming language like Easy/PowerLanguage offers extensive libraries for string manipulation. The inStr string function, for instance, identifies the starting position of a substring within a larger string. When keyed to the whichExit input, I can dynamically recreate the various combinations using string values.

  1. if whichExit = 1 then permString = “1”
  2. if whichExit = 2 then permString= “1,2”
  3. if whichExit = 3 then permString = “1,2,3”
  4.  etc…

As I optimize from one to seven, permString will dynamically change its value, representing different rows in the table. For my exit logic, I simply check if the enumerated string value corresponding to each exit is present within the string.

	if inStr(permString,"1") > 0 then
sell("LX MaxL") next bar at entryPrice - longLoss stop;
if inStr(permString,"2") > 0 then
If sma < sma[1] and sma[1] < sma[2] and sma[2] < sma[3] and close < entryPrice then
Sell ("LX MA") Next Bar at Market;
if inStr(permString,"3") > 0 then
If TrueRange > largeATR and close < close[1] and close[1] < close[2] then
Sell ("LX ATR") Next Bar at Market;
Using inStr to see if the current whichExit input applies

When permString = “1,2,3” then all exits are used.  If permString = “1,2”, then only the first two exits are utilized.  Now all we need to do is optimize whichExit from 1 to 7.  Let’s see what happens:

Combination of all three exits

The best combination of exits was “3”.  Remember 3 is the permString  that = “1,3” – this combination includes the money management loss exit, and the wide bar against position exit.  It only slightly improved overall profitability instead of using all the exits – combo #7.  In reality, just using the max loss stop wouldn’t be a bad way to go either.  Occam uses his razor to shave away unnecessary complexities again!

If you like this code, you should check out the Summer Special at my digital store. I showcase over ten more trend-following algorithms with different entry and exit logic constructs.  These other algorithms are derived from the best Trend Following “Masters” of the twentieth century.  IMHO!

Here is a video you can watch that goes over the core of this trading strategy.

 

Is your Big Point Value right?

Thinking of using futures data from a vendor such as CSI, Pinnacle, Norgate…?

You would be surprised how many quants still use Excel or they own homegrown back-testing software.  Determining trade execution is simple.  On a stop order, all you need to do is see if today’s bar is higher than the prior bars calculated trade signal – right.  And if it is then a buy stop order is executed and the calculated signal price.  Store the entry price and when the exit occurs do the math to calculate the profit or loss.  Before you can do this, you must know two very important contract specs on the futures data you are testing:

  1. big point value – I will explain how to calculate this a little later
  2. minimum tick move

That’s it.  You don’t need to know anything beyond this to properly calculate trade signals and trade P and L.  As long as you store this information for each futures/commodity market you are testing, then you will be good to go.  Many testing platforms store more information such as contract size, trading hours, expiration date and several other contract specs.  You don’t need to know this information if you all you are interested is proper signal placement and accurate trade accounting.  Once you set the big point value and minimum tick move you are good to go, right?  Yes, if you stick with the same data vendor.  I kid you not.  Data vendors often quote the same exact market with different decimal places.  If you look up a sugar quote at the CME website, you will see it quoted like 0.1908.  If you look it up on TradeStation it is quoted as 19.08.  It’s the same size contract 112,000 pounds, but if you use the big point value at the CME, it will not produce accurate calculations on the TradeStation quote.  You can’t use the contract size of 112,000 pounds to help with the math either.  You have to delve into how each data vendor quotes their data and this will impact the big point move and minimum tick move.  Most vendors are gracious enough to provide a futures dictionary with these specs.  But it is wise to know how to do this by hand as well.

If you had to take a test, could you calculate the profit or loss from a trade in soybeans, in sugar or in euro currency?

Are you kidding me this is super simple.  Just program the strategy in TradeStation or Amibroker or Multicharts or TradingSimula-18.  I took the NFA Series 3 test more than two decades ago, and this type of question was on the test.  The following information was given:

  • contract size – 5000 bushels
  • minimum tick or move – 1/4th of a cent or penny
  • long entry price 8.32’2 and sell exit price 8.48’6 (per bushel)

The entry and exit price may look a little funny.  The entry price is eight dollars and 32 1/4 cents.   Many still quote soybeans in 1/8ths.  So, 2 X 1/8th = 1/4th.  For fun let’s calculate the notional value of the entry and exit prices.  The notional value of a soybean contract at the entry price is $8.3225 * 5000 = $41,612.50.  As you know futures are highly leveraged and you can control this quantity of soybeans for a small percentage of the notional value (margin.)  Now the exit price is $8.4875 (6*1/8th = 3/4th of a cent) and the notional value of the contract at exit is $8.4875 * 5000 = $42,437.5.  The result of the trade was $42,437.50 – $41,612.50 = $825.  Or you could simply multiple the difference between entry and exit by 5000.  This would $0.165 * 5000.  This makes perfect sense, but as a broker it was difficult to keep the contract size of a market and its minimum tick move in your memory.  Well, if you did it long enough it wasn’t that difficult.  You can reduce the math down to one easy to remember value and quickly do the math in your head.  The concept of the big point move allows for this.  If you download your data from Pinnacle or CSI the price of soybeans is usually quoted like this:  848.75.  This would be eight hundred and 48.75 cents.  The big point move is the amount of dollars required to lift (or drop) the first digit to the left of the decimal by one.  The first digit to the left of the decimal in beans is a penny or one cent.  A one cent move in beans is 5000 * $0.01 or $50.  Going back to our trade example 848.75 – 832.25 = 16.5 – multiply this by $50 you get $825.  You can also derive the minimum move dollar value too.  If the minimum move is 1/4th of a cent, then you can multiply 5000 * $.0025 and this yields $12.5.

Why is this important to know?

You should know this if you are trading the market, period.  Also, if you are a quant and are using some back testing software that requires you to set up the database outside the purview of the back-tester, then these values must be known, and must also be accurate.  Since TradeStation integrates its own data, you don’t have to worry about this.  But if you want to use a database from Pinnacle, BarChart, CSI or Norgate, then you have to take the time to set this up, right off the bat.  I feel like the leading data vendors, provide accurate data.  But there are differences from one vendor to another.  Not all data vendors use the same number of decimal places in their market quotes, so you must be able to determine the big point value and minimum move from the data.   You can do all the math to determine profit and loss from the big point value.  Here is a snapshot of a few markets in the TS-18 dataMasterPinnacle and the TS-18 dataMasterCSI text files. Like I said, all vendors will provide this information for you, so you can set your database properly.  Some back-testing platforms require more contract specs, but TS-18 just needs this information to carry out accurate calculations.  Here TS-18 wants to know the symbol, big point value, minimum tick move, and market name.

Pinnacle Data
AN,1000,0.01,Aus.Dol
ZL,600,0.01,BeanOil
BN,625,0.01,B.Pound
ZU,1000,0.01,Crude
ZC,50,0.25,Corn
CC,10,1,Cocoa
CL,1000,0.01,Crude
CN,1000,0.01,Can.Dol
CT,500,0.01,Cotton
FN,1250,0.005,EuroCur


CSI Data
AD,100000,0.0001,Aus.Dol
BO,600,0.01,BeanOil
BP,62500,0.0001,B.Pound
CL,1000,0.01,Crude
C2,50,0.25,Corn
C_,50,0.25,Corn
CC,10,1,Cocoa
CD,100000,0.0001,Can.Dol
CL,1000,0.01,Crude
CT,500,0.01,Cotton
CU,125000,0.00005,EuroCur
EC,125000,0.00005,EuroCur

Other than the symbol names the values for each symbol are very similar.  Most data vendors, including CSI quote the Euro currency like this:

1.08735 and a move to 1.08740 = 0.00005 *$125,000 = $6.25

But Pinnacle data quotes it like this:

108.735 and a move to 108.740 = 0.005 * $1,250 = $6.25

The size of the contract isn’t different here.  It is the quote that is different.  The big point value is tied to the contract size and the format of the quote.  This is why knowing the big point value is so important.  If you don’t set up the contract specifications correctly, then you will receive inaccurate results.

One more test in sugar – from the CME website

  • contract size – 112,000 pounds
  • minimum tick or move – $0.0001
  • long entry price $0.1910 and sell exit price $0.1980 (per pound)
  • big point value = $112,000
  • $0.0070 * 112,000 = $784

From TradeStation

  • contract size – 112,000 pounds
  • minimum tick or move – $0.01
  • long entry price $19.10 and sell exit price $19.80 (per pound??)
  • big point value = $1,120
  • $0.70 * 1,120 = $784

Most fractional quotes are delivered in decimal format and the also very important minimum tick move.

The interest rate futures have minimum tick moves ranging from 1/256ths to 1/32nds.  Most vendors will give you a quote like 120.03125 for the bonds.  If you test in TradersStudio or Excel or any other platform where the data is not integrated and are using CSI (or any other vendor), then you must force your calculated prices to fall on an even tick.  Assume you do a calculation to buy a bond future at 120.022365895.  If you don’t take the minimum tick move into considerations, you might be filled at this theoretical price.  In reality you should be filled on an even tick at 120.03125.  This is worse but realistic fill. You could create what you think is a really awesome strategy where you are shaving off the price on every trade – getting a better fill on every trade.

Thinking of purchasing and using back-testing software or Excel and data from a data vendor?

Become your own quant and do this.  You will learn a lot about algorithm testing and development.  But first things first.  Get your database set up according to your data vendor.  Once this chore is complete, testing becomes smooth sailing.  I provide the databases for Pinnacle and CSI with TS-18. Other software provides these as well, and a way to create your own databases.

Check out my interview in Traders’ Magazine [Germany]

INTERVIEW WITH THE GERMANY BASED TRADERS´ Magazine 05.2024 (May 2024 Issue)

traders-mag.com

Note from George – Original text was written in German.  The text below is translated without modification.

George Pruitt was Director of Research at Futures Truth for 30 years.

During this time he checked the performance of more than 1000 trading strategies that were commercially available. He had contact with many prominent people in the algorithmic trading industry. He has also advised domestic and foreign customers and published a total of eight books. We talk to George Pruitt about his experiences during more than three decades of trading system development.

Traders’ media Gmbh – Eisenbahnstraße 53
97084 Würzbug

info@traders-mag.com

TRADERS´: YOU BEGAN WORKING FOR FUTURES TRUTH IN 1989, WHILE YOU WERE STUDENT. HOW DID THAT HAPPEN?

Pruitt: I was in my final year of college and looking for an internship. I saw an ad for Futures Truth in the newspaper. They were looking for a part-time programmer. After the interview with founder John Hill and programmer John Fisher, I got the job.

TRADERS´: FUTURES TRUTH HAS BEEN AN INSTITUTION IN THE INDUSTRY FOR A LONG TIME. WHAT DID THE PROVIDERS OF TRADING STRATEGIES THINK ABOUT THE PERFORMANCE OF THEIR APPROACHES BEING SUBJECT TO EXTERNAL VALIDATION AND THEREFORE MADE TRANSPARENT?

Pruitt: Many system providers used the results published in the magazine as a springboard to become leaders in the heyday of the trading systems industry.

Other providers were not so happy about their results being made available to the public. Particularly in the early days of system trading, only a few traders had the computing power, data or software to check the claims about the strategies made in the providers’ advertisements. Sometimes the results shown were not accurate at all. Futures Truth used a rigorous backtesting methodology, using tick data when necessary to create authentic track records. These sometimes contradicted the advertising material of the providers, who were of course not that enthusiastic about them.

TRADERS: WHAT SURPRISED YOU THE MOST WHILE TESTING ALL THESE STRATEGIES OVER THE YEARS?

Pruitt: I was surprised that some very expensive trading systems were not worth the price the customer paid for them. In fact, price had very little to do with success. One reason for this was that many strategy providers were not traders in the true sense, but rather engineers or scientists.

TRADERS’: IN THE 1990’S YOU TESTIFIED TO YOUR FEDERAL GOVERNMENT ON THE SUBJECT OF ALGORITHMIC TRADING. WHAT WAS IT ABOUT THEN?

Pruitt: In this particular case, I was asked to essentially answer the following question: “Can an algo that has low trading success have periods of success – and could it in the future-be more successful at all?” Of course, any trading system, good or bad, can have profit and loss margins. However, in my experience at the time, an algo with a negative expected value usually didn’t last long after a winning streak.

TRADERS: YOU SAID “ACCORDING TO MY EXPERIENCE AT THE TIME”. HAS YOUR OPINION LATER CHANGED?

Pruitt: Back then, markets were relatively inefficient. One might expect a solid backtest to be pretty indicative of future performance. In the meantime, however, the markets have evolved. Don’t get me wrong: all we can lean on is the backtest, and it will always stay that way. But I don’t think it’s as meaningful today as it was in the 1990s.

TRADERS: DO YOU REMEMBER A SUPERIOR SYSTEM THAT GOT INTO TROUBLE?

Pruitt: One example was Keith Fitschen and his “aberration” system. It was a breakout system based on Bollinger bands with a period length of 80 days and two standard deviations. The exit was at the moving average (middle band). In my opinion, it was one of the most successful trading systems of all time. From 2010 to 2020, however, the strategy went through a difficult phase. Keith believed that markets have become much more efficient. With his “Paradigm Shift” system, he basically turned the trading rules around, although not exactly. This resulted in it having a really poor performance in the backtest, but succeeding after that. But only temporarily. The success of the reverse approach did not last.

The graphic shows a comparison of Keith Fitschen’s original aberration system and his practically reversed paradigm shift strategy, each applied to crude oil. For a while, it seemed like the market had undergone a paradigm shift. But then the new strategy also disappointed.

This is what the capital curve of a typical trend-following strategy looks like across a portfolio of different futures contracts. There were big movements in 2008, 2010 and 2014. After that, it became more difficult

TRADERS: YOU ARE AN EXPERT IN THE DEVELOPMENT OF TRADING SYSTEMS. WHAT ABOUT YOUR PERSONAL TRADING?

I was two years into working at Futures Truth. It took this long to complete the CompuTrac-M conversion. Futures Truth was started by John Hill who also owned Hill Financial Group and Commodity Research. At this time the managed futures business was booming and I was asked to sit for the Series 3 exam and to help start trading Fisher and Hills algorithms. John Hill, in my opinion, was the greatest market technician of his time. He had several books on bar patterns and he exposed me to discretionary trading. I couldn’t see the same things he did on a chart. However, Fisher had canned many of John’s concepts and programmed them into Fortran. I am a programmer and a computer scientist first and foremost and could see how the ideas, expressed by code, could work and they did for many years. I am an algorithmic trader and will always be one. I never traded futures for my personal accounts due to the conflict of interests. I spend most of my time trading stocks and working on my first love – programming.

TRADERS: WHAT WAS IT LIKE BACK THEN? YOUR APPROACH TO STOCK TRADING?

Pruitt: It was the dot-com era. We simply bought everything and sold it again a little later, which had a certain dynamism. In doing so, we also saw the signs of the times and held back a lot before the end of the bubble.

Shown is the capital curve of a short-term mean reversion strategy on the Nasdaq E-mini future. It buys pullbacks and trades in the direction of the long-term moving average. However, the drawdowns can be significant. For example, there was a 30 percent setback from trade 98 to 107.

TRADERS: YOU LED THE BACK-OFFICE TEAM OF A $180 MILLION HEDGE FUND IN THE 1990S AND DEVELOPED THE CORE ALGORITHMS OF ANOTHER HEDGE FUND IN THE 2000S. HOW DID YOU MANAGE THE GREAT SUCCESS AND LATER DISILLUSIONMENT WITH TREND-FOLLOWING STRATEGIES?

Pruitt: In the early 2000s, trend-following strategies became more and more popular. Back then, the managers of funds of funds didn’t even look at you if you were trading discretionarily, with options or anything else to follow trends. I, too, had included trend following in our programs and had some success with it. But at some point the zenith was passed. After the last major movements in raw materials and the stock market in the early 2010s, many hedge funds and commodity trading advisors (CTAs) had to give up. A two-percent fixed fee and a 20 percent performance fee are great when you’re making money, but it’s very hard when you’re not. The High Watermark became an obstacle that was difficult to overcome. At the same time, small hedge funds and CTAs in particular had to spend far too much money in relative terms to comply with the now stricter regulations.

TRADERS: NEVERTHELESS, TREND FOLLOWING IS STILL ONE OF THE MOST WELL-KNOWN TRADING STRATEGIES TODAY. WHY IS THAT?

Pruitt: For one thing, timing isn’t that important. And on the other hand, you do what the commandments of trading tell us: be selective when you enter, let the profits run, give the market room to its noise, and limit the losses. Most markets lend themselves well to trend following. And many of these systems have few parameters, so that over-optimization is hardly possible. This is also important.

TRADERS: WHAT DOES YOUR OWN TRADING APPROACH LOOK LIKE TODAY?

Pruitt: I trade a small portfolio of stocks on the daily chart. In doing so, I rely on both breakouts and mean reversion. I overlay levels on a 5-minute chart using Camarilla pivot points, for example, in order to systematically find entry levels. I derive the corresponding levels from the daily chart. The decisive factors here are indications that speak for a breakout or even a countercyclical trade. In these places, the risk can also be kept low. I also rely on the pyramiding of positions. However, my portfolio only contains stocks that I know and that I would hold for the long term.

TRADERS: IT IS SAID THAT AMONG DISCRETIONARY TRADERS ARE BOTH THE BEST AND THE WORST OF ALL. HOW MANY SUCCESSFUL DISCRETIONARY TRADERS THERE ARE IN THEIR EXPERIENCE?

Pruitt: Discretionary traders make a lot of money – but also lose it. For me, that’s not the way to go. I’ve met a lot of discretionary traders. But not many have been successful in the long run. Most eventually turn to algorithms – either as complete trading systems or as tools.

TRADERS: IT IS ALSO SAID THAT DISCRETIONARY TRADERS ALSO FOLLOW A SYSTEM. THE ONLY DIFFERENCE IS THAT THEY HAVE NOT YET SUFFICIENTLY SPECIFIED THEIR (UNCONSCIOUS) RULES. WOULD YOU AGREE WITH THAT?

Pruitt: Absolutely. We have a pattern in our head that we follow. You can’t just trade based on a gut feeling and be successful with it.

TRADERS: SYSTEMATIC TRADING HAS, BUT THERE ARE ALSO DOWNSIDES. WHAT ARE THEY?

Pruitt: It’s very challenging in a number of ways. Of course, if you’re winning non-stop with a system, it’s great. But what if ten losses occur in a row? At this point, at the latest, you start to doubt. To a certain extent, you have to enter the market with a blunt instrument and hope that your diligence in system development will pay off in the end. Remember, no matter what type of trader you are, your worst drawdown is yet to come. That’s why I believe that a multi-strategy approach works best. The hope, of course, is that diversification will improve earnings. In the end we were trading five different systems on almost the same portfolio.

TRADERS: THE MARKETS ARE CONSTANTLY CHANGING. IN 2001 YOU WROTE ABOUT IT, WHICH, FROM THEIR POINT OF VIEW, ARE THE BEST SYSTEMS FOR SHORT-TERM TRADING. HOW HAS THIS DEVELOPED OVER THE LAST TWO DECADES?

Pruitt: The best short-term swing systems in 2001 were based on breaking out of the opening range. The traders made a lot of money with this very simple approach. Toby Crabel wrote one of the best books about it. With the disappearance of the pits, the opening range was diluted. That said, I believe it still works for stocks and stock indices. Together with system developer Murray Ruggiero, we developed the Dynamic Open approach. The opening of the trading day is not based on time, but on a constant increase in volume. This also worked for a while, but unfortunately not permanently. Currently, the best short-term systems are based on mean reversion approaches.

TRADERS: CAN YOU GIVE US AN EXAMPLE?

Pruitt: You can use short-term Bollinger Bands, the Relative Strength Index, or anything else that shows a move away from the average. Buying pullbacks can be difficult because you don’t know where to end up in the downcycle.

 

TRADERS: SPEAKING OF MARKETS THAT CHANGE OVER TIME, HOW LONG DO YOU THINK IS THE IDEAL PERIOD FOR BACKTESTING?

Pruitt: The markets have undergone a change in strategy. It used to be that the more data, the better. But now I think that old data is affecting the validity of the results. A test period of ten to 15 years is more than sufficient these days.

TRADERS: ONE OF THE CHALLENGES OF BACKTESTING IS TO HAVE ENOUGH HIGH-QUALITY DATA IN THE FIRST PLACE. WHAT DO YOU WORK WITH? IS IT POSSIBLE TO ACHIEVE GOOD RESULTS EVEN WITH FREE DATA?

TO ACHIEVE THE BEST POSSIBLE RESULTS?

Pruitt: You can work with free data. This means that a positive expected value can be derived from it. But I would never trade it. Yahoo Finance and Quandl offer some free data, and good daily data is also relatively cheap. I’m using Pinnacle Data for commodities. They are very reasonably priced and very good. The same goes for CSI Data. I also test with Trade Station and their data. The integration of data, testing and trading in one package like TradeStation or MultiCharts is the main reason why I wrote my book series on EasyLanguage. Despite its limitations, it is probably the best programming language for beginners.

This capital curve looks too good to be true. That’s why caution is advised when it comes to data mining. However, it can also be the birth of a trading idea – used consciously – by optimizing all possible entry points and holding periods. Every once in a while, you’ll find something that works and can withstand rigorous testing.

TRADERS ́: WHAT ABOUT THE OLD IDEA OF DEVELOPING THE WORST POSSIBLE TRADING STRATEGY AND THEN JUST TRADING THE OPPOSITE?

Pruitt: That can work. You need to keep an eye on the average trade metric. If it’s high (big avgerage loss), why not? Often, bad systems are the ones that can’t cover their trading costs, and that’s a function of trade frequency. So a bad system that trades too much will always be a bad system.

TRADERS ́: WHAT INDICATORS WERE YOUR FAVORITES OVER THE YEARS?

Pruitt: I like Bollinger Bands, Keltner Channels, Moving Averages, and the MACD. Through Murray Ruggiero, I was introduced to John Ehlers’ indicators such as the Laguerre RSI and the Dominant Cycle. RSI and stochastic are good for the entry point, but not necessarily for making the entry decision.

TRADERS: DAY TRADING STRATEGIES ARE VERY POPULAR. HOWEVER, MANY PEOPLE UNDERESTIMATE HOW DIFFICULT IT IS TO EARN MONEY IN THE LONG RUN. WHAT ARE THE BIGGEST CHALLENGES?

Pruitt: Day trading can be successful, but you can’t trade too many times a day, and you can’t trade every day. The algorithms need appropriate filter rules. However, it’s hard for many traders to skip a day if they’re day traders. You have to wait for a setup. Waiting is perhaps the hardest part.

TRADERS: DO YOU HAVE ANY TIPS FOR DAY TRADERS THAT CAN BE USED TO IMPROVE RESULTS?

Pruitt: Identify support and resistance levels and trade around those levels. Be selective about which trades you make and don’t trade more than three times a day. In addition, filters should be developed to avoid bad trades in the first place.

TRADERS: WHAT ARE THE ANOMALIES IN RETURNS? THAT STILL EXIST TODAY?

Pruitt: There are still patterns in the market. They are a reflection of human nature. And there are persistent anomalies that can be detected with data mining tools. One example is the turn-of-the-month strategy. For day traders, there are specific times on the trading day that are suitable for long or short trades.

And the aforementioned open range breakout, which I still believe in, as well as the trade in the opposite direction in case the initial setup fails.

TRADERS: NO STRATEGY ALWAYS YIELDS PROFITS. THAT’S WHY SYSTEMATIC TRADERS NEED DIFFERENT APPROACHES THAT COMPLEMENT EACH OTHER. CAN YOU SHOW US A COMBINATION TO START WITH?

Pruitt: I would combine a trend-following approach with mean reversion and day trading. The trend-following portfolio should be the largest.

Mean Reversion only works in a handful of markets, and you should test this to see which markets are in this mode. When day trading, you should stick to markets with high volume and high volatility. As a rule, these are stocks and stock indices.

TRADERS: ONE CONTROVERSIAL ELEMENT OF STRATEGY DEVELOPMENT IS OPTIMIZATION. HOW MUCH DO YOU THINK SHOULD BE OPTIMIZED?

AND WITH WHAT APPROACH?

Pruitt: At the end of the day, all system developers are optimizing. A common question is whether you should use different parameters for different markets. When I was still working with managed futures, we didn’t allow for different parameters for different markets. We programmed concepts that were functions of the market, such as the Average True Range (ATR).

However, I believe that different parameters can work as long as you don’t overdo it, for example by optimizing individual parameters over too large a space. Data mining using genetic optimization can also be useful. On the other hand, I don’t believe in walk-forward optimization. This means that you are always behind the curve.

TRADERS: ONE METHOD OF OPTIMIZATION IS TO FILTER OUT THE LESS ATTRACTIVE SIGNALS WITHOUT COMPROMISING THE GOOD ONES. IS IT POSSIBLE TO IMPLEMENT THIS CONSCIENTIOUSLY?

Pruitt: Often, losing signals go hand in hand with a certain type of market behavior. In this case, filtering is the best approach to improve.

Let’s take a day trading system that trades every day but makes a loss overall. If you add that it only trades after days with a tight trading range, the performance of the system often increases.

TRADERS: YOU HAVE EXTENSIVE PROGRAMMING SKILLS IN DIFFERENT LANGUAGES. WHICH OF THEM ARE FOR YOU TODAY

MOST USEFUL?

Pruitt: Definitely Python and EasyLanguage. The latter is more or less identical to PowerLanguage from MultiCharts. Most trading strategies can be programmed in EasyLanguage to test and generate signals. Python allows you to test ideas outside of EasyLanguage, mainly because of the data limitations and access to academic libraries.

TRADERS: TODAY, ARTIFICIAL INTELLIGENCE (AI) IS ON EVERYONE’S LIPS. YOU HAVE ALREADY DEALT WITH SIMILAR IDEAS A FEW YEARS AGO.

IN WHICH AREAS CAN AI HELP?

Pruitt: We were already working with AI in the 1990s. She can help the trader develop the code and explain the huge library of indicators. Genetic optimization is helpful to some extent. As far as machine learning goes, I haven’t had any personal experience, but I’ve seen some disappointing results.

TRADERS: WITH YOUR KNOWLEDGE OF MORE THAN 1000 DIFFERENT ALGORITHMS, YOUR PROGRAMMING SKILLS AND ALL THE DATA AT YOUR DISPOSAL, DO YOU HAVE THE HOLY GRAIL FOUND?

Pruitt: In quantum circles, I was known as a “bubble brusher.” Often, developers and traders tested with table calculations and didn’t realize that they were cheating by unconsciously using future information. It’s easy to do. For example, you make the decision to trade at the open, but you accidentally use the daily close in your calculations for the signal. Most test platforms nowadays prevent such leaks, in the case of EasyLanguage, for example, through the “next bar” paradigm. However, if you have multiple signals that can be triggered on the same day, it is difficult to determine which one actually took place from a back testing perspective. In other words, during the back test if you need to know which long position was initiated from the various potential trade signals, the next bar paradigm makes it difficult to ascertain this.

TRADERS’: BASED ON YOUR DECADES OF EXPERIENCE, WHAT DO YOU RECOMMEND THAT ASPIRING TRADERS SHOULD START WITH?

Pruitt: John Hill’s most famous saying was, “Trading is the hardest way to make easy money!” Those of us who were able to act in the 1980s, 1990s, and a few years after were gifted. Arbitrage opportunities abounded at the time. John often said that trading bonds in those days was as easy as shooting fish in an aquarium. Today, on the other hand, it is very difficult to trade successfully. We have better instruments, but the market is more efficient. Countless market participants, access to data and fast computers have robbed us of easy profit opportunities. I would definitely recommend using an algorithm, and not trading too frequently. Don’t worry about every little movement in the market. Sooner or later, a bigger one will inevitably come. And be prepared to change your preconceived notions. Don’t try to force anything.

Infobox: Toby Crabel

Toby Crabel is a successful tennis player, US futures trader and fund manager at the hedge fund Crabel Capital Management, which he founded. In 1990, he published the book "Day Trading With Short Term Price Patterns and Opening Range Breakout", which is now considered a collector's item. On Amazon, more than 500 euros are called up, on the US site even over 1000 US dollars.

Source: Wikipedia

Infobox: Pivot-Punk Clique

Camarilla pivot points are a modified version of the classic pivot points. Introduced in 1989 by Nick Scott, a successful bond trader, they indicate potential intraday support and resistance levels. Similar to classic pivot points, they use the previous day's high, low, and close. However, Fibonacci numbers are used in the calculation of levels.

What: https://www.babypips.com

Multi-Agents and the Power of the Series Function

Jeff Swanson wrote a great post on multi-agent trading a few years ago.

Jeff created a simple mean reversion system and then created two derivatives that culminated in three systems (or three agents.)  Using Murray Ruggiero’s Equity Curve Feedback, he was able to poll which system was doing the best, synthetically, and execute the strategy that showed the best performance.   If memory serves, picking the highflyer turned out to be the way to go.  Jeff had just touched the surface of Murray’s tool. but it definitely did the job.  Murray contracted me to fix some problems with the ECF tool and I did, but the tool is just way too cumbersome, resource hungry and requires a somewhat higher level of EasyLanguage knowledge to be universally applicable.  I was doing similar research in the area of polling multiple strategies and picking the best, just like Jeff did, and just executing that one system, when I thought about this post.  Traders do this all the time.  They have multiple strategies in the pipeline and monitor the performance and if one is head and shoulders better than what they are currently trading, they will switch systems.  This was one of the side benefits of the ECF tool.

What is an agent

An agent is any trading system that produces a positive expectancy.  Using multiple agents in a polling process allows a trader to go with the strategy that is currently performing the best.  This sounds reasonable, but there are pitfalls.  You could always be behind the curve – picking the best system right before it has its draw down.  Agents can be similar, or they can be totally different types of systems.  I am going to follow in Jeff’s footsteps and create three agents with the same DNA.  Here is what I call the AgentSpawner strategy.

inputs: movAvgLen(200),numDownDays(3),numDaysInTrade(2),stopLoss(5000);


value1 = countIf(c < c[1],numDownDays);
if c > average(c,movAvgLen) and value1 = numDownDays then
buy next bar at open;
if barsSinceEntry = numDaysInTrade then
sell next bar at open;
if marketPosition = 1 then sell next bar at entryPrice - stopLoss/bigPointValue stop;
Use this template and optimize inputs to spawn new agents

This code trades in the direction of the longer-term moving average and waits for a pull back on N consecutive down closes.  I am using the neat function countIF. This function counts the number of times the conditional test occurs in the last N bars.  If I want to know the number of times I have had a down close in the last 3 days, I can use this function like this.

Value1 = countIF(c<c[1],3);

// this is what the function doues
// 1.) todays close < yesterdays close + 1
// 2.) yesterdays close < prior days close + 2
// 3.) day before yesterdays close < prior cays close + 3

// If value3 = 3 then I know I had three conscecutive down
// closes. If value3 is less than three then I did not.

If the close is greater than the longer-term moving average and I have N consecutive down closings, then I buy the next bar at the open.  I use a wide protective stop and get out after X bars since entry.  Remember EasyLanguage does not count the day of entry in its barsSinceEntry calculation.  I am not using the built-in setStopLoss as I don’t want to get stopped out on the day of entry.  In real trading, you may want to do this, but for testing purposes my tracking algorithm was not this sophisticated.  I spawned three agents with the following properties.

	Case 1: //Agent 1
movAvgLen = 200;
numDownDays = 2;
numDaysInTrade = 15;
stopLoss = 7500;
Case 2: //Agent 2
movAvgLen = 140;
numDownDays = 3;
numDaysInTrade = 9;
stopLoss = 2500;
Case 3: //Agent 3
movAvgLen = 160;
numDownDays = 3;
numDaysInTrade = 15;
stopLoss = 2500;

System Tracking Algorithm

This is why I love copy-paste programming.  This can be difficult if you don’t know your EasyLanguage or how TradeStation processes the bars of data.  Get educated by checking my books out at amazon.com – that is if you have not already.  This code is a very simplistic approach for keeping track of a system’s trades and its equity.

value4 = countIf(c<c[1],4);
value3 = countIf(c<c[1],3);
value2 = countIf(c<c[1],2);
//Agent #1 tracking algorithm
if sys1Signal<> 1 and c[1] > average(c[1],160) and value2[1] = 2 then
begin
sys1Signal = 1;
sys1BarCount = -1;
sys1TradePrice = open;
sys1LExit = open - 7500/bigPointValue;
end;
if sys1Signal = 1 then
begin
sys1BarCount+=1;
if low < sys1LExit and sys1BarCount > 0 then
begin
sys1TradePrice = sys1LExit;
sys1Signal = 0;
end;
if sys1BarCount = 16 and sys1Signal = 1 then
begin
sys1TradePrice = open;
sys1Signal = 0;
end;
end;
Yes, this looks a little hairy, but it really is simple stuff

I am pretending to be TradeStation here.  First, I need to test to see if Agent#1 entered into a long position.  If the close of yesterday is greater than the moving average, inclusive of yesterdays close, and there has been two consecutive down closes, then I know a trade should have been entered on todays open.  EasyLanguage’s next bar paardigm cannot be utilized here.  Remember I am not generating signals, I am just seeing if today’s (not tomorrows or the next bars) trading action triggered a signal and if so, I need to determine the entry/exit price.  I am gathering this information so I can feed it into a series function.  If a trade is triggered, I set four variables:

  1. sys1Signal – 1 for long, -1 for short, and 0 for flat.
  2. sys1BarCount – set to a -1 because I immediately increment.
  3. sys1TradePrice – at what price did I enter or exit
  4. sys1LExit – set this to our stop loss level

If I am theoretically long, remember we are just tracking here, then I need to test, starting with the following day, if the low of the day is below our stop loss level and if it is I need to reset two variables:

  1. sys1TradePrice – where did I get out
  2. sys1Signal – set to 0 for a flat position

If not stopped out, then I start counting the number of bars sys1Signal is equal to 1.  If sys1BarCount = 16, then I get out at the open by resetting the following variables:

  1. sys1TradePrice = open
  2. sys1Signal = 0

If you look back at the properties for Agent#1 you will see I get out after 15 days, not 16.  Here is where the next bar paradigm can make it confusing.  The AgentSpawner strategy says to sell next bar at open when barsSinceEntry = 15.  The next bar after 15 is 16, so we store the open of the 16th bar as our trade price.

Now copy and paste the code into a nice editor such as NotePad++ or NotePad and replace the string sys1 with sys2.  Copy the code from NotePad++ into your EasyLanguage editor.  Now back to NotePad++ and replace sys2 with sys3.  Copy that code into the EL edition too.  Now all you need to do is change the different properties for each agent and you will have three tracking modules.

The Power of the EasyLanguage Series Function

The vanilla version of EasyLanguage has object-oriented nuances that you may not see right off the bat.  In my opinion, a series function is like a class.  Before I get started, let me explain what I mean by series.  All EasyLanguage function are of three types.

  1. simple – like a Bollinger band calculation
  2. series – like we are talking about here
  3. auto-detect – the interpreter/compiler decides

The series function has a memory for the variables that are used within the function.  Take a look at this.

input: funcID(string),seed(numericSimple);

vars: count(0);
if barNumber = 1 then // on first bar seed count
count = seed;
count = count+1;
print(d," ",funcID," ", count);
SeriesFunctionTest = count;
Count is class-like member

On the first bar of the function call – remember it will be called on each bar in the chart, the function variable count is assigned seed. Seed will be ignored on subsequent bars.   What makes this magical is that no matter how many times you call the function on the same bar it remembers the internal variables on somewhat of a hierarchical basis (each call remembers its own stuff.)  It like a class in that it gets instantiated on the very first call.  Meaning if you call it three times on the first bar of the data, you will have three distinct internal variable memories.  Take a look at my sandbox function driver and its output.

result = SeriesFunctionTest("Call #1",50);
result = SeriesFunctionTest("Call #2",5);
result = SeriesFunctionTest("Call #3",100);

//outPut

1170407.00 Call #1 51.00 //first bar 51 = seed + count + 1
1170407.00 Call #2 6.00 //first bar 6 = seed + count + 1
1170407.00 Call #3 101.00 //first bar 101 = seed + count + 1

1170410.00 Call #1 52.00 // second bar it remembered count was 51
1170410.00 Call #2 7.00 // second bar it remembered count was 6
1170410.00 Call #3 102.00 // second bar it remembered count was 101

1170411.00 Call #1 53.00 // you have a unique function values that
1170411.00 Call #2 8.00 // were instantiated on the first bar
1170411.00 Call #3 103.00 // of the test.

1170412.00 Call #1 54.00
1170412.00 Call #2 9.00
1170412.00 Call #3 104.00
Series functions rock - but they are resource hungry

Why is this important?

I have created a PLSimulator function that keeps track of the three agent’s performance.  I need the profit or loss to stick with each function and then also add or subtract from it.  This is a neat function.  Remember if you like this stuff buy my books at Amazon.com.

//ProfitLoss Simulator
Inputs: signal(numericseries),tradePrice(numericSimple),orderType(numericSimple),useOte(Truefalse);
Vars:dmode(0),LEPrice(-99999),LXPrice(-99999),SEPrice(-99999),SXPrice(-99999);
vars: GProfit(0),OpenProfit(0);
vars: modTradePrice(0);

vars: printOutTrades(True);

modTradePrice = tradePrice;

if orderType = 1 then // stop order
begin
if signal = 1 or (signal = 0 and signal[1] = - 1) then
modTradePrice = maxList(open,modTradePrice);
if signal = -1 or (signal = 0 and signal[1] = 1) then
modTradePrice = minList(open,modTradePrice);
end;

if orderType = 2 then // limit order
begin
if signal = 1 or (signal = 0 and signal[1] = - 1) then
modTradePrice = minList(open,modTradePrice);
if signal = -1 or (signal = 0 and signal[1] = 1) then
modTradePrice = maxList(open,modTradePrice);
end;
if orderType = 3 then // market order
begin
modTradePrice = open;
end;

If Signal[0]=1 And (Signal[1]=-1 Or Signal[1]=0) Then
begin
LEPrice=modTradePrice;
SXPrice = -999999;
condition1 = false;
If Signal[1]=-1 Then
begin
SXPrice=modTradePrice;
GProfit=(SEPrice-SXPrice)+GProfit;
condition1 = True;
End;
if not(condition1) then
if printOutTrades then Print(d," L:Entry ",LEPrice)
else
if printOutTrades then Print(d," L:Entry ",LEPrice," ",(SEPrice-SXPrice)*bigPointValue:8:2," ",GProfit*bigPointValue:9:2);
End;
{('***********************************************}
If Signal[0]=-1 And (Signal[1]=1 Or Signal[1]=0) Then
begin
SEPrice=modTradePrice;
LXPrice = 999999;
condition1 = false;
If Signal[1]=1 Then
begin
condition1 = True;
LXPrice=modTradePrice;
GProfit=(LXPrice-LEPrice)+GProfit;
End;
if not(condition1) then
if printOutTrades then Print(d," S:Entry ",SEPrice)
else
if printOutTrades then Print(d," L:Exit ",LXPrice," ",(LXPrice-LEPrice)*bigPointValue:8:2," ",GProfit*bigPointValue:9:2);

End;
If Signal[0]=0 And Signal[1]=-1 Then
begin
SXPrice = modTradePrice;
GProfit=(SEPrice-SXPrice)+GProfit;
if printOutTrades then Print(d," S:Exit ",SXPrice," ",(SEPrice-SXPrice)*bigPointValue:8:2," ",GProfit*bigPointValue:9:2);

end;
If Signal[0]=0 And Signal[1]=1 Then
begin
LXPrice = modTradePrice;
GProfit=(LXPrice-LEPrice)+GProfit;
if printOutTrades then Print(d," L:Exit ",LXPrice," ",(LXPrice-LEPrice)*bigPointValue:8:2," ",GProfit*bigPointValue:9:2);

end;

If Signal[1]=1 And useOte=True Then
begin
OpenProfit=(Close[1]-LEPrice);
End;
If Signal[1]=-1 and useOte=True Then
begin
OpenProfit=(SEPrice-Close[1]);
End;
If Signal[1]=0 Or useOte=False Then
begin
OpenProfit=0;
End;

PLSimulator=(GProfit+OpenProfit)*bigpointvalue;
Simulate profit and loss and more importantly keep track of it

Feed tracker algorithm data into the function

Your information must be properly assigned to get this to work.  First, I show how to get the information into the function.  The function does all the work and returns the equity.  I then determine the best agent by looking at the ROC over the past thirty days of equity for each agent and pick the very best.  I then trade the very best.  This is a very quick application of the function.  I will have a more sophisticated function, something akin to Murray’s ECF but with much less overhead and more strategy templates.

sys1Equity = PLSimulator(sys1Signal,sys1TradePrice,1,True);
sys2Equity = PLSimulator(sys2Signal,sys2TradePrice,1,True);
sys3Equity = PLSimulator(sys3Signal,sys3TradePrice,1,True);


vars: multiAgent(0);

value1 = maxList(sys1Equity-sys1Equity[30],sys2Equity-sys2Equity[30],sys3Equity-sys3Equity[30]);
multiAgent = 1;
if sys2Equity-sys2Equity[30] = value1 then multiAgent = 2;
if sys3Equity-sys3Equity[30] = value1 then multiAgent = 3;


{print(d," ",sys1Equity-sys1Equity[30]," ",sys1Equity);
print(d," ",sys2Equity-sys2Equity[30]," ",sys2Equity);
print(d," ",sys3Equity-sys3Equity[30]," ",sys2Equity);}
print(d," MultiAgent ",multiAgent);

//system parameters
vars: movAvgLen(200),numDownDays(3),numDaysInTrade(2),stopLoss(5000);
Switch ( multiAgent )
Begin
Case 1:
movAvgLen = 200;
numDownDays = 2;
numDaysInTrade = 15;
stopLoss = 7500;
Case 2:
movAvgLen = 140;
numDownDays = 3;
numDaysInTrade = 9;
stopLoss = 2500;
Case 3:
movAvgLen = 160;
numDownDays = 3;
numDaysInTrade = 15;
stopLoss = 2500;
End;
// Actual system execution
value1 = countIf(c < c[1],numDownDays);

if multiAgent <> multiAgent[1] then print(d," ---->multiagent trans ");

if c > average(c,movAvgLen) and value1 = numDownDays then
begin
if multiAgent = 1 then buy("Sys1") next bar at open;
if multiAgent = 2 then buy("Sys2") next bar at open;
if multiAgent = 3 then buy("Sys3") next bar at open;
end;
if barsSinceEntry >= numDaysInTrade then
sell next bar at open;
if marketPosition = 1 then sell next bar at entryPrice - stopLoss/bigPointValue stop;
Cool usage of a switch-case and agent determination

If this seems over your head…

Get one of my books are check out Jeff Swanson’s course.  EasyLanguage has so many little nuggets that can help you define your algorithm into an actionable strategy.  You will never know how your strategy will work until your program it (properly) and back test it.  And then potentially improve it with optimization.

Multi-Agent Results

 

 

Trading Before and After Holidays

Morgan Stanley Research Idea from 2018

In May of 2018 Morgan Stanley presented a report from their research department.  The report was written by Tony Small and Matthew Hornbach.  The information presented validated the idea of seasonality in the markets.  The writers/researchers gathered all the data going back to 1984 on the ten-year futures and all the federal holidays going back over the same span of time.  They then analyzed either going long or short M days prior to the holiday and exiting N days before the holidays.  They also did the same thing after the holiday – entering M days after and exiting N days after.  The boundary was set to five days before and after.  This report was sent to me by one of my clients.  The conclusion of the article was that there seemed to be a seasonality component to the days prior and after a major holiday.  And that it seemed this component did indeed provide a technical advantage (on the surface.)  Here is their conclusion.

“Given the risk of data mining, investors should accept this analysis with some degree of caution,” Small and Hornbach said. “There is no guarantee that historical seasonality and past price performance will continue in the future.”

How would one test this idea on their own?

Tools needed:

  1. Python with NumPy installed.
  2. An AI chatbot such as ChatGPT.
  3. Access to TY futures data going back to 1984 (actual non-Panama adjusted data would be preferable.)
  4. A testing platform that allows future leak such as George’s own TradingSimula-18.  Excel would work too but would require a bunch of work.

Some programming knowledge.

  1. Create output for all of the federal holidays going back to January 1984.  This list must also include Good Fridays.  This list should have two fields – the date and the name of the holiday.
  2. TS-18 came in handy because I read the file that was created from step 1 and whenever I came across or spanned a holiday date, I created a loop that calculated the ROC for the prior five days and also for the five days that followed.   I created a nomenclature to quickly recognize the ROC span.
    • B54 – ROC between 5 days before and 4 days before – 1 day
    • B53 – ROC between 5 days before and 3 days before – 2 days
    • B52 – ROC between 5 days before and 2 days before – 3 days
    • B51 – ROC between 5 days before and 1 days before – 4 days
    • B43 – ROC between 4 days before and 3 days before – 1 day
    • B42 – ROC between 4 days before and 2 days before – 2 days
    • B41 – ROC between 4 days before and 1 days before – 3 days
    • B32 – ROC between 3 days before and 2 days before – 1 day
    • B31 – ROC between 3 days before and 1 days before – 2 days
    • B21 – ROC between 2 days before and 1 days before – 1 day
    • A12 – ROC between 1 days after and 2 days after – 1 day
    • A13 – ROC between 1 days after and 3 days after – 2 days
    • A14 – ROC between 1 days after and 4 days after – 3 days
    • A15 – ROC between 1 days after and 5 days after – 4 days
    • A23 – ROC between 2 days after and 3 days after – 1 day
    • A24 – ROC between 2 days after and 4 days after – 2 days
    • A25 – ROC between 2 days after and 5 days after – 3 days
    • A34 – ROC between 3 days after and 4 days after – 1 day
    • A35 – ROC between 3 days after and 5 days after – 2 days
    • A45 – ROC between 4 days after and 5 days after – 1 day
  3. This process created a file that looked like this:
    • Output of ROC with nested for loops
  4. With this data I summed up each ROC calculation (B54 thru A45).  From this you could pick an entry and exit day combination.  The best results were wrapped around Memorial Day.  Here was where I ran into a small but highly aggravating scenario.  Most of the holidays had similar numbers, but there were a few that had some really strange values.  I knew I couldn’t use regular adjusted futures data, because many times these values can go below zero and when doing division with these price adjustments, the values are simply not accurate.  So, I used continuous non adjusted data, but ran into a problem with the rollover gap.  Some of the holidays occurred really close to rollover dates.  The calculations that bridged a rollover was not accurate.  I then remembered Pinnacle data also includes Ratio Adjusted Data or RAD.  I applied the looping algorithm to this data, and it SEEMED to fix the problem.
  5. I was able to pick a B(n,m) or A(m,n) for each market.  If the magnitude of ROC was sufficiently negative, you could use the dates to short the market.

ChatGPT and Python and Numpy to the rescue

I asked ChatGPT to provide python code to generate a list of all federal holidays from 1984.

from pandas.tseries.holiday import USFederalHolidayCalendar
from datetime import datetime,timedelta
import numpy as np
# Create a calendar for U.S. federal holidays
cal = USFederalHolidayCalendar()

# Get the holidays for the year 2014
holidays_2014 = cal.holidays(start='1984-01-01', end='2024-12-31').to_pydatetime()

federalHolidays = []
date_list = holidays_2014.astype(datetime).tolist()
for n in range(len(date_list)):
dateString = f'{date_list[n]:%Y%m%d}'
federalHolidays.append(int(dateString))
Create a list of federal holidays

This worked great, but I had to convert the NumPy datetime construct to a string.  I like to keep things simple.  You will see this in the code.  I started to get to work on the second part of the task when I discovered there were no dates for GOOD FRIDAYS – argh!  ChatGPT to rescue once again.

def good_friday_dates(start_year, end_year):
good_friday_dates = []
for year in range(start_year, end_year + 1):
# Calculate the date of Easter Sunday for the given year
easter_sunday = calculate_easter_date(year)

# Good Friday is two days before Easter Sunday
good_friday = easter_sunday - timedelta(days=2)

# Add the Good Friday date to the list
good_friday_dates.append(good_friday)

return good_friday_dates

def calculate_easter_date(year):
# Algorithm to calculate the date of Easter Sunday
a = year % 19
b = year // 100
c = year % 100
d = b // 4
e = b % 4
f = (b + 8) // 25
g = (b - f + 1) // 3
h = (19 * a + b - d - g + 15) % 30
i = c // 4
k = c % 4
l = (32 + 2 * e + 2 * i - h - k) % 7
m = (a + 11 * h + 22 * l) // 451
month = (h + l - 7 * m + 114) // 31
day = ((h + l - 7 * m + 114) % 31) + 1
easter_date = datetime(year, month, day)
return easter_date

# Generate Good Friday dates from 1984 to 2024
good_friday_dates_1984_to_2024 = good_friday_dates(1984, 2024)
Hope this code is right for Good Fridays

Good Friday, as Easter, is all over the map.  Tomorrow is Good Friday.  The code looks pretty gross, so I just accepted it.  It may not be 100% accurate, but if ChatGPT says so….  Once I had a list of all holidays, I was able to start cooking (MLK was added in 1986 and Juneteenth in 2021.)  Since Juneteenth is relatively new it did not offer a statistically significant number of observations.

Future Leak comes in Handy.

Every quant needs some software that will look through data and do some calculations.  I did this project with EasyLanguage, but it was a little difficult.  I had to do a two-pass process (well I did the same with Python too.)  I first created an indicator that imported the holiday dates and then outputted the close (or open of next bar) for close[5], close[4], close[3], close[2], close[1] and then waited 1, 2, 3, 4 and 5 bars after the date and printed out those respective closing prices.  I did not do this step in EasyLanguage.  I actually create EasyLanguage as my output.  Here is what it looked like:

holidayName[1]="MemorialDay";
fiveBeforeArr[1]=1010518;
fourBeforeArr[1]=1010521;
threeBeforeArr[1]=1010522;
twoBeforeArr[1]=1010523;
oneBeforeArr[1]=1010524;
oneAfterArr[1]=1010529;
twoAfterArr[1]=1010530;
threeAfterArr[1]=1010531;
fourAfterArr[1]=1010601;
fiveAfterArr[1]=1010604;
holidayName[2]="July4th";
fiveBeforeArr[2]=1010626;
fourBeforeArr[2]=1010627;
threeBeforeArr[2]=1010628;
twoBeforeArr[2]=1010629;
oneBeforeArr[2]=1010702;
oneAfterArr[2]=1010705;
twoAfterArr[2]=1010706;
threeAfterArr[2]=1010709;
fourAfterArr[2]=1010710;
fiveAfterArr[2]=1010711;
holidayName[3]="LaborDay";
fiveBeforeArr[3]=1010824;
fourBeforeArr[3]=1010827;
threeBeforeArr[3]=1010828;
twoBeforeArr[3]=1010829;
oneBeforeArr[3]=1010830;
oneAfterArr[3]=1010904;
twoAfterArr[3]=1010905;
threeAfterArr[3]=1010906;
fourAfterArr[3]=1010907;
fiveAfterArr[3]=1010910;
All key dates are stored in arrays.

Future leak allows you to peak into the future and this comes in super handy when doing research like this.  Here is some python code in TS-18 that does all these calculations in one sweep.


# The date of the holiday and the holiday name are stored in lists
# Each Holiday has its own date and 20 ROC calculations
# These calculations are stored in lists as well
tempDate = int(extraData[extraCnt][0]) #first field in the csv file
whichHoliday = extraData[extraCnt][1] #second field in the csv file
if date[D] == tempDate or (date[D] > tempDate and date[D1] < tempDate):
foundJ = 99
for j in range(12):
if whichHoliday == listOfHolidayClass[j].holidayName:
foundJ = j
listOfHolidayClass[foundJ].holidayDates.append(tempDate);
for j in range(-5,-1): #Before Holiday
for k in range(j+1,0):
denom = close[D+j]
listOfHolidayClass[foundJ].rocList.append((close[D+k] - close[D+j])/denom)
for j in range(0,4): #After Holiday - here is the future leak
for k in range(j+1,5):
denom = close[D+j]
listOfHolidayClass[foundJ].rocList.append((close[D+k] - close[D+j])/denom)
extraCnt = extraCnt + 1
Python code using future leak to calculate the 20 ROC from before and after holiday

I then printed this out to a csv file and imported into Excel (see above) and found out the best combination of days for each holiday.  Now that I had the B(n,m) or the A(m,n) values, I need a process to tell TS-18 which day to buy/short and exit.  Here is some code that, again uses future leak, to print out the five dates before the holiday and the five dates after the holiday.

tempDate = int(extraData[extraCnt][0])
whichHoliday = extraData[extraCnt][1]
if date[D] == tempDate or (date[D] > tempDate and date[D1] < tempDate):
for j in range(12):
if whichHoliday == listOfHolidayClass[j].holidayName:
foundJ = j
print(date[D],end=',')
print(whichHoliday,end=',')
for j in range(-5,6):
print(date[D+j],end=',')
print("",end = '\n')
extraCnt = extraCnt + 1

# Here is what the file looked like
19840103,NewYear,19831223,19831227,19831228,19831229,19831230,19840103,19840104,19840105,19840106,19840109,19840110,
19840221,PresidentsDay,19840213,19840214,19840215,19840216,19840217,19840221,19840222,19840223,19840224,19840227,19840228,
19840423,GoodFriday,19840413,19840416,19840417,19840418,19840419,19840423,19840424,19840425,19840426,19840427,19840430,
19840529,MemorialDay,19840521,19840522,19840523,19840524,19840525,19840529,19840530,19840531,19840601,19840604,19840605,
19840705,July4th,19840627,19840628,19840629,19840702,19840703,19840705,19840706,19840709,19840710,19840711,19840712,
19840904,LaborDay,19840827,19840828,19840829,19840830,19840831,19840904,19840905,19840906,19840907,19840910,19840911,
19841008,ColumbusDay,19841001,19841002,19841003,19841004,19841005,19841008,19841009,19841010,19841011,19841012,19841015,
19841112,VeteransDay,19841105,19841106,19841107,19841108,19841109,19841112,19841113,19841114,19841115,19841116,19841119,
19841123,Thanksgiving,19841115,19841116,19841119,19841120,19841121,19841123,19841126,19841127,19841128,19841129,19841130,
Create the dates that occur in the date B4 and after holiday

Now read the data in and pick how many days before to b/s and before to exit or after to b/s and to exit

holidayBuyDates = []
holidayXitDates = []
gfCnt = 0

#structure of holidayDatesData list - all strings
# 0.) Holiday Date
# 1.) Holiday Name
# 2.) 5-Before
# 3.) 4-Before
# 4.) 3-Before
# 5.) 2-Before
# 6.) 1-Before
# 7.) 0-Before/After
# 8.) 1-After
# 9.) 2-After
# 10.) 3-After
# 11.) 4-After
# 12.) 5-After
#Below I pick 1 day after to buy and 4 days after to exit [8,11]
for j in range(numHolidayDates):
if holidayDatesData[j][1] == "MemorialDay":
holidayBuyDates.append(int(holidayDatesData[j][8]))
holidayXitDates.append(int(holidayDatesData[j][11]))



# Okay Let's put in some logic to create a long position
if not(long) and canTrade and date[D] == holidayBuyDates[gfCnt]:
price = close[D]
tradeName = "Hol-A1Buy"
posSize = 1
tradeTicket.append([buy,tradeName,posSize,price,mkt])
# Long Exits
if long and canTrade and date[D] == holidayXitDates[gfCnt]:
price = longExit
tradeName = "Hol-A4Xit"
tradeTicket.append([exitLong,tradeName,curShares,price,mkt])
gfCnt = gfCnt + 1
TradingSimual_-18 Code to execute buys and exits
No execution fees – executing on close of the day
Memorial Day Example -----------------------------------------
Sys Name : TS-18HolidayTest
Mkt Symb : TY Mkt Name : 10YNote
19840530 Hol-A1Buy 1 -35.90625 0.00 0.00
19840604 Hol-A4Xit 1 -34.12500 1731.25 1731.25
19850529 Hol-A1Buy 1 -18.43750 0.00 0.00
19850603 Hol-A4Xit 1 -16.96875 1418.75 3150.00
19860528 Hol-A1Buy 1 -0.40625 0.00 0.00
19860602 Hol-A4Xit 1 -2.87500 -2518.75 631.25
19870527 Hol-A1Buy 1 -1.09375 0.00 0.00
19870601 Hol-A4Xit 1 -0.28125 762.50 1393.75
19880601 Hol-A1Buy 1 -1.87500 0.00 0.00
19880606 Hol-A4Xit 1 -0.56250 1262.50 2656.25
19890531 Hol-A1Buy 1 2.68750 0.00 0.00
19890605 Hol-A4Xit 1 4.46875 1731.25 4387.50
19900530 Hol-A1Buy 1 2.50000 0.00 0.00
19900604 Hol-A4Xit 1 3.68750 1137.50 5525.00
19910529 Hol-A1Buy 1 6.59375 0.00 0.00
19910603 Hol-A4Xit 1 6.65625 12.50 5537.50
19920527 Hol-A1Buy 1 13.71875 0.00 0.00
19920601 Hol-A4Xit 1 14.50000 731.25 6268.75
19930602 Hol-A1Buy 1 28.21875 0.00 0.00
19930607 Hol-A4Xit 1 27.96875 -300.00 5968.75
19940601 Hol-A1Buy 1 24.62500 0.00 0.00
19940606 Hol-A4Xit 1 25.93750 1262.50 7231.25
19950531 Hol-A1Buy 1 33.34375 0.00 0.00
19950605 Hol-A4Xit 1 34.59375 1200.00 8431.25
19960529 Hol-A1Buy 1 32.09375 0.00 0.00
19960603 Hol-A4Xit 1 30.93750 -1206.25 7225.00
19970528 Hol-A1Buy 1 32.93750 0.00 0.00
19970602 Hol-A4Xit 1 33.75000 762.50 7987.50
19980527 Hol-A1Buy 1 40.00000 0.00 0.00
19980601 Hol-A4Xit 1 40.31250 262.50 8250.00
19990602 Hol-A1Buy 1 37.62500 0.00 0.00
19990607 Hol-A4Xit 1 37.81250 137.50 8387.50
20000531 Hol-A1Buy 1 36.10938 0.00 0.00
20000605 Hol-A4Xit 1 37.43750 1278.12 9665.62
20010530 Hol-A1Buy 1 42.92188 0.00 0.00
20010604 Hol-A4Xit 1 44.06250 1090.62 10756.25
20020529 Hol-A1Buy 1 50.14062 0.00 0.00
20020603 Hol-A4Xit 1 50.68750 496.88 11253.12
20030528 Hol-A1Buy 1 68.06250 0.00 0.00
20030602 Hol-A4Xit 1 68.34375 231.25 11484.38
20040602 Hol-A1Buy 1 64.43750 0.00 0.00
20040607 Hol-A4Xit 1 63.78125 -706.25 10778.12
20050601 Hol-A1Buy 1 72.32812 0.00 0.00
20050606 Hol-A4Xit 1 72.29688 -81.25 10696.88
20060531 Hol-A1Buy 1 65.46875 0.00 0.00
20060605 Hol-A4Xit 1 66.07812 559.38 11256.25
20070530 Hol-A1Buy 1 66.57812 0.00 0.00
20070604 Hol-A4Xit 1 66.06250 -565.62 10690.62
20080528 Hol-A1Buy 1 77.21875 0.00 0.00
20080602 Hol-A4Xit 1 76.10938 -1159.38 9531.25
20090527 Hol-A1Buy 1 87.12500 0.00 0.00
20090601 Hol-A4Xit 1 87.14062 -34.38 9496.88
20100602 Hol-A1Buy 1 95.48438 0.00 0.00
20100607 Hol-A4Xit 1 96.18750 653.12 10150.00
20110601 Hol-A1Buy 1 102.23438 0.00 0.00
20110606 Hol-A4Xit 1 103.12500 840.62 10990.62
20120530 Hol-A1Buy 1 115.32812 0.00 0.00
20120604 Hol-A4Xit 1 117.37500 1996.88 12987.50
20130529 Hol-A1Buy 1 115.51562 0.00 0.00
20130603 Hol-A4Xit 1 115.59375 28.12 13015.62
20140528 Hol-A1Buy 1 116.31250 0.00 0.00
20140602 Hol-A4Xit 1 116.56250 200.00 13215.62
20150527 Hol-A1Buy 1 121.20312 0.00 0.00
20150601 Hol-A4Xit 1 121.42188 168.75 13384.38
20160601 Hol-A1Buy 1 125.07812 0.00 0.00
20160606 Hol-A4Xit 1 126.50000 1371.88 14756.25
20170531 Hol-A1Buy 1 124.17188 0.00 0.00
20170605 Hol-A4Xit 1 124.65625 434.38 15190.62
20180530 Hol-A1Buy 1 120.35938 0.00 0.00
20180604 Hol-A4Xit 1 119.21875 -1190.62 14000.00
20190529 Hol-A1Buy 1 124.76562 0.00 0.00
20190603 Hol-A4Xit 1 125.76562 950.00 14950.00
20200527 Hol-A1Buy 1 137.43750 0.00 0.00
20200601 Hol-A4Xit 1 137.84375 356.25 15306.25
20210602 Hol-A1Buy 1 133.03125 0.00 0.00
20210607 Hol-A4Xit 1 133.42188 340.62 15646.88
20220601 Hol-A1Buy 1 122.48438 0.00 0.00
20220606 Hol-A4Xit 1 121.70312 -831.25 14815.62
20230531 Hol-A1Buy 1 115.40625 0.00 0.00
20230605 Hol-A4Xit 1 115.10938 -346.88 14468.75

Here are the results for the Morgan Stanley analysis in 2018

Best combos for entry and exit for each holiday.

My analysis covered the same period plus all of 2018, 2019, 2020, 2021, 2022, and 2023.  My combos were very similar in most cases, but the last few years moved the needle a little bit.  However, the results still showed a technical edge.

How to Fix the Fixed Fractional Position Size

The Fixed Fractional position sizing scheme is the most popular, so why does it need fixed?

Problems solved with Fixed Fractional:

  1. Efficient usage of trading capital
  2. Trade size normalization between different futures contracts
  3. Trade size normalization across different market environments

These are very good reasons why you should use positions sizing.  Problem #2 doesn’t apply if you are only trading one market.  This sounds idyllic, right?  It solves these two problems, but it introduces a rather bothersome side effect – huge draw down.  Well, huge draw down in absolute terms.  Draw downs when using a fixed fractional approach are proportional to the prior run up.  If you make a ton of money on a prior trade, then your position sizing reflects that big blip in the equity curve.  So, if you have a large loser following a large winner, the draw down will be a function of the run up.  In most cases, a winning trading system using fixed fractional position sizing will scale profit up as well as draw down.  A professional money manager will look at the profit to draw down ratio instead of the absolute draw down value.  The efficient use of capital will reflect a smaller position size after a draw down, so that is good right?  It is unless you believe in a Martingale betting algorithm – double up on losses and halve winners.  Are we just stuck with large “absolute” draw downs when using this size scheme?

Possible solutions to fixing Fixed Fractional (FF)

The first thing you can do is risk less than the industry standard 2% per trade.  Using 1% will cause equity to grow at a slower rate and also reduce the inevitable draw down.  But this doesn’t really solve the problem as we are giving up the upside.  And that might be okay with you.  Profit will increase and you are using an algorithm for size normalization.  In this blog I am going to propose a trading equity adjustment feature while using FF.  What if we act like money managers, and you should even if you are trading your own personal money, and at the end of the year or month we take a little off the table (theoretically – we are not removing funds from the account just from the position sizing calculation) that is if there is any extra on the table.  This way we are getting the benefit of FF while removing a portion of the compounding effect, which reduces our allocation for the next time interval.  How do you program such a thing?  Well first off let’s code up the FF scheme.

positionSize = round((tradingCapital * riskPerTrade) / (avgTrueRange(30)*bigPointValue),0);

Nothing new here.  Simply multiply tradingCapital by the riskPerTrade (1 or 2%) and then divide by a formula that defines current and inherent market risk.  This is where you can become very creative.  You could risk the distance between entry and exit if you know those values ahead of time or you can use a value in terms of the current market.  Here I have chosen the 30-day average true range.  This value gives a value that predicts the market movement into the future.  However, this value only gives the expected market movement for a short period of time into the future.  You could us a multiplier since you will probably remain in a trade for more than a few days – that is if you are trying to capture the trend.  In my experiment I just use one as my multiplier.

Capture and store the prior year/month NetProfit

When I come up with a trading idea I usually just jump in and program it.  I don’t usually take time to see if Easy Language already provides a solution for my problem.   Many hours have been used to reinvent the wheel, which isn’t always a bad thing.  So, I try to take time and search the functions to see if the wheel already exists.  This time it looks like I need to create the wheel.  I will show the code first and then explain afterward.

inputs: useAllocateYearly(True),useAllocateMonthly(False),initCapital(100000),removePerProfit(0.50),riskPerTrade(0.01);
vars: tradingCapital(0),prevNetProfit(0),tradingCapitalAdjustment(0);
vars: oLRSlope(0),oLRAngle(0),oLRIntercept(0), oLRValueRaw(0),mp(0);
arrays: yearProfit[250](0),snapShotNetProfit[250](0);vars: ypIndex(0);


once
begin
tradingCapital = initCapital;
end;

if useAllocateYearly then
begin
value1 = year(d);
value2 = year(d[1]);
end;

if useAllocateMonthly then //remember make sure your array is 12XNumYears
begin
value1 = month(d);
value2 = month(d[1]);
end;

if useAllocateYearly or useAllocateMonthly then
begin
if value1 <> value2 then
begin
if ypIndex > 0 then
yearProfit[ypIndex] = prevNetProfit - snapShotNetProfit[ypIndex-1]
else
yearProfit[ypIndex] = prevNetProfit;

snapShotNetProfit[ypIndex] = prevNetProfit;
tradingCapitalAdjustment = yearProfit[ypIndex];
if yearProfit[ypIndex] > 0 then
tradingCapitalAdjustment = yearProfit[ypIndex] * (1-removePerProfit);
tradingCapital = tradingCapital + tradingCapitalAdjustment;
print(d,",",netProfit,",",yearProfit[ypIndex],",",tradingCapitalAdjustment,",",tradingCapital);
ypIndex +=1;
end;
end
else
tradingCapital = initCapital + netProfit;
Capture either the prior years or months net profit

I wanted to test the idea of profit retention on a monthly and yearly basis to see if it made a difference.  I also wanted to just use the vanilla version of FF.  The use of Arrays may not be necessary, but I didn’t know ahead of time.  When you program on the fly, which is also called “ad hoc” programming you create first and then refine later.  Many times, the “ad hoc” version turns out to be the best approach but may not be the most efficient.  Like writing a book, many times your code needs revisions.  When applying a study or strategy that uses dates to a chart, you don’t know exactly when the data starts so you always need to assume you are starting in the middle of a year.   If you are storing yearly data into an array, make sure you dimension you array sufficiently.  You will need 12X the number of years as the size you need to dimension your array if you want to store monthly data.

 


//250 element array will contain more than 20 years of monthly data
//You could increase these if you like just to be safe
arrays: yearProfit[250](0),snapShotNetProfit[250](0);
//Remember you dimension you arrray variable first and then
//Pass it a value that you want to initiate all the values
//in the array to equal
vars: ypIndex(0);
Dimension and Initiate Your Arrays

The first thing we need to do is capture the beginning of the year or month.  We can do this by using the year and month function.  If the current month or year value is not the same as the prior day’s month or year value, then we know we crossed the respective timeline boundary.  We are using two arrays, snapShotNetProfit and yearProfit (to save time I use this array to store monthlty values as well, when that option is chosen) and a single array index ypIndex.  If we have crossed the time boundary, the first thing we need to do is capture the EasyLanguage function NetProfit’s value.  NetProfit keeps track of the cumulative closed out trade profits and losses. Going forward in this description I am going to refer to a yearly reallocation.  If it’s the first year, the ypIndex will be zero, and in turn the first year’s profit will be the same as netProfit.  We store netProfit in the yearProfit array at the ypIndex location.  Since we are in a new year, we take a snapshot of netProfit and store it in the snapShotNetProfit array at the same ypIndex location.  You will notice I use the variable prevNetProfit in the code for netProfit.  Here is where the devil is in the details.  Since we are comparing today’s year value with yesterday’s year value and when they are different, we are already inside the new year, so we need to know yesterday’s netProfit.   Before you say it, you can’t pass netProfit a variable for prior values; you know like netProfit(1) or netProfit[1] – this is a function that has no historic values, but you can record the prior day’s value by using our own prevNetProfit  variable.  Now we need to calculate the tradingCapitalAdjustment.  The first thing we do is assign the variable the value held in  yearProfit[ypIndex].  We then test the yearProfit[ypIndex] value to see if it is positive.  If it is, then we multiply it by (1-removePerProfit).  If you want to take 75% of the prior year’s profit off the table, then you would multiply the prior year’s profit by 25%.  Let’s say you make $10,000 and you want to remove $7,500, then all you do is multiply $10,000 by 25%.  If the prior year’s netProfit is a loss, then this value flows directly through the code to the position sizing calculation (auto deallocation on a losing year).   If not, the adjusted profit portion of funds are deallocated in the position sizing equation.

The next time we encounter a new year, then we know this is the second year in our data stream, so we need to subtract last year’s snapshot of netProfit (or prevNetProfit) from the current netProfit.  This will give us the change in the yearly net profit.  We stuff this information into the yearProfit array.  The snapShotNetProfit is stuffed with the current prevNetProfit.  ypIndex is incremented every time we encounter a new yearNotice how I increment the ypIndex – it is incremented after all the calculations in the new year.  The tradingCapitalAdjustment is then calculated with the latest information.

Here is a table of how the tradingCapital and profit retention adjustment are calculated.  A yearly profit adjustment only takes place after a profitable year.  A losing year passes without adjustment.

All tests were carried out on the trend friendly crude oil futures with no execution costs from 2006 thru 2/28/2204.

See how money is removed from the allocation model after winning years.

Here are some optimization tests with 75% profit retention on yearly and monthly intervals.

Yearly First-

Yearly retention with no stop loss or break-even levels.

Now Monthly-

Monthly retention with no stop loss or break-even levels.

What if we didn’t reallocate on any specific interval?

Huge drawdowns with very little change in total profit.

Add some trade management into the mix.

Here we optimize a protective stop and a break-even level to see if we can juice the results.  All the trade management is on a position basis.

200K with No Reallocating with $14k and $8.5 stop/break-even levels [ranked by Profit Factor]
No position sizing and no reallocation with $5K and $10K stop/break-even levels

200K and monthly reallocating with $7K and $5.5K stop/break-even levels [BEST PROFIT FACTOR]
200K and monthly reallocating with $7K and $8.5K stop/break-even levels [2ND BEST PROFIT FACTOR]

Are we really using our capital in the most efficient manner?

If we retain profit, should we remove some of the loss form the position sizing engine as well.  All the tests I performed retained profit from the position size calculations.  I let the loss go full bore into the calculation.  This is a very risk averse approach.  Why don’t I retain 25% of losses and deduct that amount from the yearly loss and feed that into the position sizing engine.  This will be less risk averse – let’s see what it does.

Not as good.  But I could spend a week working different permutations and optimization sessions.

Are you wondering what Trend Following System I was using as the foundation of this strategy?  I used EasyLanguage’s Linear Regression function to create buy and short levels.  Here is the very simple code.

Value1 = LinearReg (Close, 60, 1, oLRSlope, oLRAngle, oLRIntercept, oLRValueRaw);

Value2 = oLRSlope;

Value3 = oLRAngle;

Value4 = oLRIntercept;

Value5 = oLRValueRaw;


//Basically I am buying/shorting on the change of the linear regression slope
//Also I have a volatility filter but it really isn't used
If value2 >=0 and value2[1] < 0 and avgTrueRange(30)*bigPointValue < 10000 then
buy positionSize contracts next bar at market;
If value2 <=0 and value2[1] > 0 and avgTrueRange(30)*bigPointValue < 10000 then
sellShort positionSize contracts next bar at market;

mp = marketPosition;

//I also have incorporated a 3XATR(30) disaster stop
if mp = 1 and c <= entryPrice - 3 * avgTrueRange(30) then sell next bar at market;
if mp = -1 and c >= entryPrice + 3 * avgTrueRange(30) then buyToCover next bar at market

If you want to see some more Trend Following models and their codes in EasyLanguage and TS-18 Python check out my TrendFollowing Guide and Kit.