Skip to content

Tutorial 196 | Scope in methods (plus convert a function to a method)

T196

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)
A series function is a specialized function that refers to a previous function value within its calculations. In addition, series functions update their value on every bar even if the function call is placed within a conditional structure that may not be true on a given bar. Because a series function automatically stores its own previous value and executes on every bar.
A simple function is the most common type of function that only stores its current value. Unlike series functions, simple functions do not store historical values of themselves and therefore do not require much memory to perform their calculations. However, a simple function can still refer to other historical bar prices (such a High, Volume, etc.). The simple function used in this demonstration is Average.

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

Free Gold Pass download of the tutorial 196 code

If you are a Gold Pass member you can download the tutorial 196 program for free. (Make sure that you are logged into Gold Pass): This content is for members only.

Download of the tutorial 196 code

This program is available for immediate download for $9.95 by clicking the ‘add to cart’ button below. You may pay using PayPal or a credit card.

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.