Calculate MAE/MFE 30 Bars after A Signal
A very astute reader of this blog brought a snippet of code that looks like EasyLanguage and sort of behaves like it, but not exactly. This code was presented on the exceptional blog of Quant Trader posted by Kahler Philipp. He used some of the ideas from Dave Bergstrom.
Equilla Programming Language
The theory behind the code is quite interesting and I haven’t gotten into it thoroughly, but will do so in the next few days. The code was derived from Trade-Signal’s Equilla Programming Language. I looked at the website and it seems to leans heavily on an EasyLanguage like syntax, but unlike EZLang allows you to incorporate indicators right in the Strategy. It also allows you, and I might be wrong, to move forward in time from a point in the past quite easily. The code basically was fed a signal (+1,0,-1) and based on this value progressively moved forward one bar at a time (over a certain time period) and calculated the MAE and MFE (max. adverse/favorable excursion for each bar. The cumulative MAE/MFE were then stored in a BIN for each bar. At the end of the data, a chart of the ratio between the MAE and MFE was plotted.
I tried to replicate the code to the best of my ability by going back in time and recording a trading signal and then moving Back to The Future thirty bars, in this case, to calculated and store the MAE/MFE in the BINS.
Simple Moving Average Cross Over Test
After 100 bars, I looked back 30 bars to determine if the price was either greater than or less than the 21 day moving average. Let’s assume the close was greater than the 21 day moving average 30 days ago, I then kept going backward until this was not the case. In other words I found the bar that crossed the moving average. It could have been 5 or 18 or whatever bars further back. I stored that close and then started moving forward calculating the MAE/MFE by keeping track of the Highest Close and Lowest Close made during 30 bar holding period. You will see the calculation in the code. Every time I got a signal I accumulated the results of the calculations for each bar in the walk forward period. At the end of the chart or test I divided each bars MFE by its MAE and plotted the results. A table was also created in the Print Log. This code is barely beta, so let me know if you see any apparent errors in logic or calculations.
Here is an output at the end of a test on Crude Oil
Using Arrays for Bins
When newcomers start to program EasyLanguage and encounter arrays it sometimes scares them away. They are really easy and in many cases necessary to complete a project. In this code I used two 40 element or bins arrays MFE and MAE. I only use the first 30 of the bins to store my information. You can change this to 30 if you like, and when you start using a fixed array it is best to define them with the exact number you need, so that TradeStation will tell you if you step out of bounds (assign value to a bin outside the length of the array). To learn more about arrays just search my blog. The cool thing about arrays is you control what data goes in and what you do with that data afterwards. Anyways play with the code, and I will be back with a more thorough explanation of the theory behind it.