Tutorial 196 gives an example of “scope” in the context of a method.
A common guideline in good programming practice suggests that a method should ideally only utilize variables that are provided through its parameter list or those that are locally declared within the method itself. However, there are situations where adhering strictly to this principle might be overly cumbersome or impractical. In such cases, it becomes possible to reference variables that have been declared at a the EasyLanguage study level.
Both “types” of methods are demonstrated in this tutorial.
When a variable is declared within the primary section of a study, it can be directly accessed and modified throughout that study. Nevertheless, when a method is invoked, it operates within its designated scope by design. This means that a variable declared within a method is confined to that method’s scope. Even if a variable with an identical name exists elsewhere, it is treated as a distinct and separate entity, maintaining its own distinct scope within the method.
To demonstrate converting the functions to methods two functions are used:
- XAverage (a series function), and
- Average( a simple function)
The video, part 1 (see below) demonstrates the creation of two methods. The first uses a variable in the method that has ‘study’ scope, i.e. it is declared in the main study. The second uses a variable that is declared in the method and hence, whose scope is distinct to the method. The video also demonstrates a scenario when a variable is declared in the main study AND in a method.
The second video, part 2 (see below) demonstrates how to create a method that performs the same as the Average function. This requires the last Length number of prices to be passed into the method. In this example this is achieved by populating a vector and passing this vector into the method.
To learn more about methods you might also be interested in Tutorial 118 | Exploring EasyLanguage methods which explains how a method can return multiple outputs, amongst other things.
Tutorial 196 | Part 1
Tutorial 196 | Part 2
TO THE BEST OF MARKPLEX CORPORATION’S KNOWLEDGE, ALL OF THE INFORMATION ON THIS PAGE IS CORRECT, AND IT IS PROVIDED IN THE HOPE THAT IT WILL BE USEFUL. HOWEVER, MARKPLEX CORPORATION ASSUMES NO LIABILITY FOR ANY DAMAGES, DIRECT OR OTHERWISE, RESULTING FROM THE USE OF THIS INFORMATION AND/OR PROGRAM(S) DESCRIBED, AND NO WARRANTY IS MADE REGARDING ITS ACCURACY OR COMPLETENESS. USE OF THIS INFORMATION AND/OR PROGRAMS DESCRIBED IS AT YOUR OWN RISK.
ANY EASYLANGUAGE OR POWERLANGUAGE TRADING STRATEGIES, SIGNALS, STUDIES, INDICATORS, SHOWME STUDIES, PAINTBAR STUDIES, PROBABILITYMAP STUDIES, ACTIVITYBAR STUDIES, FUNCTIONS (AND PARTS THEREOF) AND ASSOCIATED TECHNIQUES REFERRED TO, INCLUDED IN OR ATTACHED TO THIS TUTORIAL OR PROGRAM DESCRIPTION ARE EXAMPLES ONLY, AND HAVE BEEN INCLUDED SOLELY FOR EDUCATIONAL PURPOSES. MARKPLEX CORPORATION. DOES NOT RECOMMEND THAT YOU USE ANY SUCH TRADING STRATEGIES, SIGNALS, STUDIES, INDICATORS, SHOWME STUDIES, PAINTBAR STUDIES, PROBABILITYMAP STUDIES, ACTIVITYBAR STUDIES, FUNCTIONS (OR ANY PARTS THEREOF) OR TECHNIQUES. THE USE OF ANY SUCH TRADING STRATEGIES, SIGNALS, STUDIES, INDICATORS, SHOWME STUDIES, PAINTBAR STUDIES, PROBABILITYMAP STUDIES, ACTIVITYBAR STUDIES, FUNCTIONS AND TECHNIQUES DOES NOT GUARANTEE THAT YOU WILL MAKE PROFITS, INCREASE PROFITS, OR MINIMIZE LOSSES.