[TradeStation/EasyLanguage] Optimize indicators and strategies – avoid calling series functions

 

Optimize slow series function

Everyone who has been coding with TradeStation (EasyLanguage) for a while, knows that there are two kinds of functions:  Simple and Series (If you don’t yet know, I STRICTLY URGE YOU to read  Wiki about Functions to understand the idea how functions work in TS).

So, when your script (indicator, strategy, or whatever) calls functions, then you should know which kind of function you are calling and how you call that. In case you are using SERIES FUNCTION inside conditions, like:

inputs:  Use_Average(false);

if (Use_Average) then  begin
my_value= XAverage(close, 14);
... do something ...
end;

then the XAverage (as it’s series function) is being called whether check condition is true or false ( because it is not simple function). Although in such case, the returned value is not assigned to left my_value variable, however it is still executed in background.

So, that case  series function needs correct coding which you  SHOULDN’T ignore.  In WIKI, it is recommended to use such approach to avoid any confusion:

my_value= XAverage(close, 14);
if (Use_Average) then  begin
... do something ...
end;

Also, don’t forget – it’s even worse to use SERIES functions in methods, I highly don’t recommend that (as, if you execute that method 2 times in a cycle, it TS can’t differentiate between the previous and current cycle).

My Approach

Last period, after I become annoyed with the slowness of large scripts (that use Series functions) I started thinking about how to make the scripts faster. Obviously we can’t avoid series function being called on every bar (despite of condition is true/false), however, we can make that function to calculate nothing (thus, saving the calculation energy).
What I got so far, is a “trick” like this:

if (Use_Average) then  begin
my_value= XAverage( close,  iff(NOT Use_Average, 1, 14)  ) ;
... do something ...
end;

!!! Note1: DON’T DO THIS ONTO SERIES VARIABLES! Read more at https://pastebin.com/raw/PuxbHUmm
!!! Note2: Use `IFF` only on static variables (like LENGTH, etc..). Don’t use `IFF` onto argument, that outputs SERIES type value (like CLOSE, SERIES VARIABLE ETC..), otherwise, `IFF` will convert their previous SERIES values.
Average( iff( condition, 0, close) // <------ DONT !
Average( iff( condition, 0, my_variable), ... // <------ DONT !

Thus, you see that the function instead of 14 cycles, will only execute 1 cycle of it’s calculation. I think, in complex functions, it should have some noticeable performance/speed-up.  Unfortunately, I was unable to find a better method, because EasyLanguage developers haven’t created other optimized way avoid unnecessary calculations (and most of us {coders} know, that TradeStation is a dead end to user-requests, at least what I’ve seen during years. So, whatever Developers do, we can only follow that rules).

If anyone knows a better solution/approach, kindly comment it.  (btw, you might also read: Optimising function calculations )

Leave a Reply

Your email address will not be published. Required fields are marked *