< All Topics

EasyLanguage and Strategy Coding Good Practices

Over the years there are some good practices that I have adopted that help avoid some of the pitfalls, bugs or logic issues when writing strategies in EasyLanguage. I detail several below:

Use The Dictionary

TradeStation includes a pretty comprehensive dictionary in the TradeStation Development Environment that describes all the EasyLanguage keywords and functions.

Do Not Use Built-in Variables Value1-99 and Condition1-99

Never use EasyLanguage’s built-in variables Value1-99 or Condition1-99.  From a programming professional point-of-view, these should have never been included in Easylanague.  There are two primary reasons to avoid using these keywords:

Confusion and errors

I ran into situations where a strategy used Value1 and then included a piece of code another strategy that also used Value1.  The result was a very confusing mess trying to determine why the strategy results were incorrect, only to find that the cuprit was Value1 being used for two different reasons.

Lack of meaning

When I am reading someone elese’s code, Value1 has no meaning. What is Value1? What is the purpose of the calculation? But creating and using your own descriptive variable names, such as ‘BreakoutPrice’ or ‘DayCounter’ gives context and meaning to the variable’s usage. SO much better than Value1!

Document Your Code

Always document your code.  Why are you performing a calculation?  Why did you include the logic in your code?  What was the source of your idea?

Describe, describe, describe.  If not for your own sanity when you return to the code months later and wonder why you did something, but also for others if you share the code in the Strategy Factory Club.

Qualify Buy, SellShort, Sell and BuyToCover orders with MarketPosition IF statements

Entry Orders

Assuming that we are following the Strategy Factory Club protocol and only trading N contracts without any pyramiding/scaling in or out methods, then we would only ever have one long or short position active at any time. In a perfect world, we could simply write the following code for long or short market entries as:

if ( RSI(Close,2) < 20 ) then buy next bar at market;
if ( RSI(Close,2) > 80 ) then sellshort next bar at market;

But TradeStation is far from a perfect world. There are some bug situations, such as this one, that gives incorrect results when we don’t tell TradeStation to only execute the order if a position in the same direction does not exist.  This can easily be avoided by adding MarketPosition qualifiers:

if ( RSI(Close,2) < 20 and MarketPosition <> 1 ) then buy next bar at market;
if ( RSI(Close,2) > 80 and MarketPosition <> -1 ) then sellshort next bar at market;

Exit Orders

In a perfect world, we could simply write the following code to exit any short or long positions:

sell next bar at market;
buytocover next bar at market;

I have found that, for exiting positions, that the above code will sometimes issue extraneous real-time orders that both enter and exit a position immediately. This causes unnecessary commission costs.

Unless you have good reason to use the above unqualified orders, this can be avoided by adding a simple qualification:

if ( MarketPosition = 1 ) then sell next bar at market;
if ( MarketPosition = -1 ) then buytocover next bar at market;

Don’t Use Funcitons Over And Over Again

In the following example, RSI is called twice:

if ( RSI(Close,2) < 20 and MarketPosition <> 1 ) then buy next bar at market;
if ( RSI(Close,2) > 80 and MarketPosition <> -1 ) then sellshort next bar at market;

This causes TradeStation to redundantly call the same RSI function for the same results. This causes unnecessary strain on TS (especially when running many strategies real-time) and creates more processing time (which is especially important when running strategies through MultiWalk that will compound those calculations hundreds and thousands of times!)

Instead, call functions ONE TIME by assigning the result to a variable and then using that variable in your code:

RSIValue = RSI( Close, 2 );

if ( RSIValue < 20 and MarketPosition <> 1 ) then buy next bar at market;
if ( RSIValue > 80 and MarketPosition <> -1 ) then sellshort next bar at market;

Avoid Two Critical TradeStation Bugs

There are two crtical TradeStation bugs that we need to avoid in our strategies. It has been years and they still remain unresolved. Kevin and I have both created different workarounds for these bugs. For an explanation and the workarounds, see the followoing article:

https://multiwalk.net/knowledge-base/overcoming-two-tradestation-strategy-bugs

Table of Contents