The Easylanguage function: A thing of beauty

Apr 17, 2023 | EasyLanguage, TradeStation

The Easylanguage function: A thing of beauty

The concept of Scope when it comes to a programming language can get very confusing very quickly. If you are new to programming and you think Scope is a mouthwash, no worries here. I will give a full explanation of Scope in terms of programming with the help of ChatGPT. Its response is italicized.

I recently chatted with ChatGPT about Scope in terms of the Python programing language.

In Python, the scope of a variable refers to the region of the program where that variable can be accessed or referenced. There are two types of scopes in Python: global scope and local scope.

Here’s an example that demonstrates the concept of scope in Python:

# global variable
x = 10

def my_func():
    # local variable
    y = 5
    print("x inside my_func:", x)  # x is accessed from the global scope
    print("y inside my_func:", y)  # y is accessed from the local scope

my_func()

print("x outside my_func:", x)  # x is accessed from the global scope
print("y outside my_func:")  # y is not defined in the global scope, so this will raise a NameError
Example of Scope as demonstrated by Chat-GPT

In this example, we define a global variable x with a value of 10.

We then define a function my_func that has a local variable y with a value of 5. Inside the function, we print the values of x and y. The variable x is accessed from the global scopewhich means it can be referenced from anywhere in the program.

The variable y, on the other hand, is only accessible within the function, which is the local scope. Outside the function, we print the value of x again, which is still accessible from the global scope. However, when we try to print the value of y, we get a NameError because y is not defined in the global scope.

Okay that was pretty formal. In simpler terms, a variable declared inside a function can only be seen inside that function, unless it is declared globally. Another thing about Scope, is that the variables have amnesia. Once you step out of the function the variable forgets what it was, and you can’t refer to its prior value upon return to the function.

Unless you are an EasyLanguage function. Scope is still constrained by an EasyLanguage function, because it is a separate piece of code held within the library of Analysis Techniques. Most of the time you can’t see what’s in the function unless you open it with the ELEditor. However, the variables that are defined inside the function do not suffer from amnesia. If you need to refer to a prior value of a locally declared variable, you can. This type of function is what EasyLanguage calls a Serial function. The only downside to this function is it slows processing down quite a bit.

Okay. To make a long story short I wanted to show the magic of EasyLanguage function that I have been working with on a project. This project includes some of Ehlers’ cycle analysis functions. The one I am going to discuss today is the HighRoof function – don’t worry I am not going to go into detail of what this function does. If you want to know just GOOGLE it or ask ChatGPT. I developed a strategy that used the function on the last 25 days of closing price data. I then turned around and fed the output of the first pass of the HighRoof function right back into the HighRoof function. Something similar to embedding functions.

doubleSmooth = average(average(c,20),20);

Sort of like a double smoothed moving average. After I did this, I started thinking does the function remember the data from its respective call? The first pass used closing price data, so its variables and their history should be in terms of price data. The second pass used the cyclical movements data that was output by the initial call to the HighRoof function. Everything turned out fine, the function remembered the correct data. Or seemed like it did. This is how you learn about any programming language – pull out your SandBox and do some testing. First off, here is my conversion of Ehlers’ HighRoof function in EasyLanguage.

//Ehlers HiRoof
 Inputs: dataSeries(numericseries),cutPeriod(Numeric);
 
 Vars:   a1(0), b1(0), c1(0), c2(0), c3(0), Filt(0), Filt2(0),
         alpha1(0),oneMinusAlpha1(0), highPass(0),myhp(0),degrees(0);
 Vars: numTimesCalled(0);   

//Highpass filter cyclic components whose periods are shorter than 48 bars

numTimesCalled = numTimesCalled + 1;

print(d," numTimesCalled ",numTimesCalled," highPass[1] ",highPass[1]," highPass[2] ",highPass[2]," highPass[3] ",highPass[3]);
degrees = .707*360 / CutPeriod;

alpha1 = (Cosine(degrees) + Sine(degrees) - 1) / Cosine(degrees);

oneMinusAlpha1 = 1-alpha1;
 
highPass = square(oneMinusAlpha1/2)*(dataSeries-2*dataSeries[1]+dataSeries[2]) +
  2*(oneMinusAlpha1)*highPass[1]-square(oneMinusAlpha1)*highPass[2];
 
 
 
EhlersHighRoof=highPass;
Ehlers High Roof Function

This function requires just two inputs – the data (with a history) and a simple length or cut period. The first input is of type numericSeries and the second input is of type numericSimple. You will see the following line of code

 print(d," numTimesCalled ",numTimesCalled," highPass[1] ",highPass[1]," highPass[2] ",highPass[2]," highPass[3] ",highPass[3]);

This code prints out the last three historic values of the HighPass variable for each function call. I am calling the function twice for each bar of data in the Crude Oil futures continuous contract.

1230206.00 numTimesCalled  494.00 highPass[1]   -0.78 highPass[2]   -0.51 highPass[3]   -0.60
1230206.00 numTimesCalled  494.00 highPass[1]   -0.05 highPass[2]   -0.02 highPass[3]   -0.06
1230207.00 numTimesCalled  495.00 highPass[1]   -0.38 highPass[2]   -0.78 highPass[3]   -0.51
1230207.00 numTimesCalled  495.00 highPass[1]    0.04 highPass[2]   -0.05 highPass[3]   -0.02
1230208.00 numTimesCalled  496.00 highPass[1]    0.31 highPass[2]   -0.38 highPass[3]   -0.78
1230208.00 numTimesCalled  496.00 highPass[1]    0.16 highPass[2]    0.04 highPass[3]   -0.05
1230209.00 numTimesCalled  497.00 highPass[1]    0.49 highPass[2]    0.31 highPass[3]   -0.38
1230209.00 numTimesCalled  497.00 highPass[1]    0.15 highPass[2]    0.16 highPass[3]    0.04
1230210.00 numTimesCalled  498.00 highPass[1]    0.30 highPass[2]    0.49 highPass[3]    0.31
1230210.00 numTimesCalled  498.00 highPass[1]    0.07 highPass[2]    0.15 highPass[3]    0.16
1230213.00 numTimesCalled  499.00 highPass[1]    0.52 highPass[2]    0.30 highPass[3]    0.49
1230213.00 numTimesCalled  499.00 highPass[1]    0.08 highPass[2]    0.07 highPass[3]    0.15
1230214.00 numTimesCalled  500.00 highPass[1]    0.44 highPass[2]    0.52 highPass[3]    0.30
1230214.00 numTimesCalled  500.00 highPass[1]    0.04 highPass[2]    0.08 highPass[3]    0.07
Output of calling HighRoof twice per bar

Starting at the top of the output you will see that on 1230206 the function was called twice with two different sets of data. As you can see the output of the first two lines is of a different magnitude. The first line is approximately an order or magnitude of 10 of the second line. If you go to lines 3 and 4 you will see the highPass[1] of lines 1 and 2 moves to highPass[2] and then onto highPass[3]. I think what happens internally is for every call on per bar basis, the variables for each function call are pushed into a queue in memory. The queue continues to grow for whatever length is necessary and then either maintained or truncated at some later time.

Why Is This So Cool?

In many languages the encapsulation of data with the function requires additional programming. The EasyLanguage function could be seen as an “object” like in object-oriented programming. You just don’t know you are doing it. EasyLanguage takes care of a lot of the behind-the-scenes data management. To do the same thing in Python you would need to create a class of Ehlers Roof that maintain historic data in class members and the calculations would be accomplished by a class method. In the case of calling the function twice, you would instantiate two classes from the template and each class would act independent of each other.

Here is my SandBox for Indicator


Value1 = EhlersHighRoof(close,25);
plot1(Value1,"EhlersHiRoof");
Value2 = EhlersHighRoof(value1,25);
plot2(Value2,"EhlersHiRoof2");
Sandbox Playground for Ehlers Function

 

One last nugget of information. If you are going to be working with trigonometric functions such as Cosine, Sine or Tangent, make sure your arguments are in degrees not radians. In Python, you must use radians.

Article written by George Pruitt

Quantified Models YouTube Channel

We have several videos available on our YouTube channel that you may find very useful in developing trading systems.

We hope this information has been useful to you.

Subscribe to our Newsletter

Join our mailing list to receive the latest news and updates from Quantified Models team.

Subscribe to our Newsletter

You have Successfully Subscribed!

Skip to content