Unicorn Trading
TradeStation EasyLanguage Functions


| Home | ProSizer | EasyLanguage | Quotes |

These functions are intended for use with TradeStation, but may be adapted easily to other languages. All function names begin with an underscore character. It's a good idea to do this for all user-developed code so that they appear grouped together when TradeStation lists them, which alleviates the need to hunt around for your own work.

EasyLanguage Reference Manual

ELreference2000i.pdf (right-click)
Right-click and "save as" on this link to download the PDF (Adobe Acrobat Reader) version of the Easy Language reference manual for TradeStation 2000i.

The links to EL source code below will display text files. To import them properly into PowerEditor, I suggest you right-click on the link, Save As a text file, load it into WordPad (not NotePad), and then copy+paste it into PowerEditor. If you copy+paste it directly from your browser, it will work too, but you'll lose the blank lines.


OPTIMIZATION AIDS

_SystemQuality
Allows you to optimize your strategies for other things besides the canned optimization results offered by TradeStation. All you do is put a call to _SystemQuality at the end of your strategy signal code, and begin an optimization (set it up to do less than 65535 trials because this is the maximum number of rows Excel can hold). _SystemQuality will execute on every bar, but it will output data only on the last bar, summarizing the system's performance. _SystemQuality outputs a line of comma-delimited data showing all your strategy's input parameters, and expectancy score. When the optimization completes, import the file into Excel, sort by the last column in descending order, and you can see the parameters that gave the highest score.
 
The expectancy score is, in my opinion, the only truly objective way to evaluate the performance of a trading strategy. Sharpe Ratio doesn't do it. Follow the link to learn more.
 
_SystemHistory
You don't want this one executing during an optimization! If you put a call to _SystemHistory in your strategy's signal code, it will execute on every bar, but it will output data to a file only when a position is closed. You pass it your current stop and your measure of volatility (like 20-day ATR) at each bar. _SystemHistory outputs the profit or loss, initial stop, market volatility at entry, maximum adverse excursion (MAE) and maximum favorable excursion (MFE). This data can then be imported into software tools to create a position sizing strategy.
 
_ProSizerQty
This is a companion EL function to ProSizer, my Excel tool for finding the best position sizing model for your trading strategy. Once you have settled on a model, simply call this function to get the quantity of contracts to trade on the next order. See the ProSizer documentation for information on how all the position sizing models work.

EXPONENTIAL MOVING VALUES

Everyone is familiar with indicators where all bars in some lookback interval are given equal weight. These include anything using statistical measures, such as mean, standard deviation, regression slope, etc. These are great for analyzing static collections of data, but not for time series data!

All these indicators suffer from being affected by activity N bars ago, when what happened N bars ago has no relevance to what is happening now. One way around it is to use as many "exponential moving" indicators as possible in place of those that weight all bars equally.

Advantages to using exponential moving values over their traditional counterparts:

_xAverage - Exponential Moving Average
Yes, TradeStation already includes an xAverage function. However, you cannot pass into it variable lookback lengths that change with each bar, as you may wish to do if you have some adaptive technique based on market cycle analysis. This function lets you pass a different length each time.
 
_T3Average - T3 Moving Average
Bob Fulks contributed this function to the Omega list in 1997 (original article is here.) The T3 Average is essentially a low-pass filter, as are the traditional moving average and exponential moving average. The T3 Average, however, exhibits a steeper rolloff, resulting in better filtering of high-frequency noise while better preserving the low-frequency components of a time series. While it doesn't come near achieving the performance of the "gold standard," Mark Jurik's JMA, this version of T3 works reasonably well.
 
T3 Average can be used as a drop-in replacement for TradeStation's Average or xAverage functions. The function available here corrects two minor problems I found in the original algorithm:
  • The original version lags twice as much as other moving averages (linear or exponential) for a given Length parameter. My version does not.
  • Also, I analyzed the frequency response and discovered that the default damping coefficient is too high, resulting in amplification at low frequencies.
     
 
_xStdDev - Exponential Moving Standard Deviation (EMSD)
This started out as a nice 1-line formula, exceedingly simple and quick compared to the "real" standard deviation, except it seemed to work best only when the data mean was near zero. I've fixed it now; the calculation is a 2-liner, but it's still simple and fast, requiring no loops like the traditional standard deviation.
 
Have a look at this Excel spreadsheet showing the difference between the traditional standard deviation and the EMSD. The spreadsheet shows a graph of normally-distributed random data with an "outlier" value stuck in. The traditional standard deviation reacts instantly, but the effect persists until the outlier rolls out of the lookback range. The EMSD follows the traditional one pretty well until the outlier appears. EMSD also reacts quickly but begins settling down right away. Because more weight is given to the most recent activity, the EMSD appears noisier and spikier than the traditional version, but at least it minimizes the effects of old data, which is especially important for long lookback lengths.
 
_T3xStdDev - T3 Exponential Moving Standard Deviation
This function adapts the T3 Moving Average algorithm to standard deviations. It uses _xStdDev above to create a smoother standard deviation with a lag equivalent to either the traditional or exponential standard deviation.
 
_xLinRegSlope - Exponential Moving Linear Regression Slope
This is the traditional slope calculation, with the summations in the formula substituted by exponential moving sums. Because an exponential moving average approximates a "true" equally-weighted average, and the equally-weighted average is simply the sum of data values divided by N values, then the sum may be approximated by the exponential moving average multiplied by N. That's what this function does. (Updated 4/24/2004)

OTHER INTERESTING BITS

_LinRegSlopeSFC - Linear Regression Slope Super Fast Calc
If you want an equally-weighted linear regression slope, use this function rather than the ones supplied with TradeStation. The output of _LinRegSlopeSFC matches the traditional linear regression slope perfectly, and it doesn't use any loops except for once during initialization.
 
_FractalDim - Fractal Dimension
The fractal dimension D is related to the Hurst exponent H by D = 2 - H, or H = 2 - D. Either one can be used to evaluate the fractal nature of a time series such as a market. In general, a market's dimension lies somewhere between 1 and 2. The more a market trends, the closer the dimension gets to 1. A plot of fractal dimension looks remarkably similar (although inverted) to a plot of other indicators like ADX or dominant cycle length.
Sevcik, Carlos, A Proccedure to Estimate the Fractal Dimension of Waveforms", Complexity International 5, 1998.
 
_heapsort_a - HeapSort
If you need to sort large arrays, this sort function is significantly faster than the builtin SortUp function in TradeStation. It's fairly short and simple (yet not easy to understand). It sorts an array on the order of N log N iterations, whereas the builtin sort needs on the order of N2 iterations, which is much slower when your array is bigger than a few hundred elements.
 
_filter2pole - 2-pole lowpass/highpass IIR filters
This function implements a Butterworth, Critically-Damped, or Bessel 2-pole IIR filters, both low-pass and high-pass. Click here for step-by-step calculations for these filters, as well as test results showing frequency response curves and temporal responses to a step function, for all filters.

| Home | ProSizer | EasyLanguage | Quotes |

Copyright © 2004 by Unicorn Research Corporation
All rights reserved.