User-named variables

Top  Previous  Next

 

User-named variables work like Mechanicas generic memory variable, MEMORY[n], in that they provide storage for numerical values. They offer the same convenience as grid-column aliases, and are just as easy to create. Grid column aliases can only be used in a SIG file.

 

Understanding user-named variables

The values assigned to user-named variables declared in a SIG file are accessible from any page (or pages) within the Signal Rules. For instance, the value of a user-named variable which resides on the Trade Entry page can be read and used by both the Resources page and the Trade in Progress & Exit page. But before you attempt this, it is important to understand (a) the order in which Mechanica processes pages, and (b) the manner in which the value of a variable persists across pages, and through time. This topic is covered in “Understanding persistence in variables,” later in this section.

Likewise, the value of user-named variables declared in a SIZ file are accessible from either page of the Position Sizing Rules. For instance, the value of a user-named variable which resides on the Initial Size page can be read and used above the categories or within a category on the Resize page. (We havent covered the concept of Categories before, but they are discussed in the Position Sizing / Resizing section later in this help document.)

Again, when assigning a value to a variable located on one page, and accessing that value from another page, it is important to thoroughly understand the order in which Mechanica processes these pages, and the persistence of the variable.

ThumbTack white

Because running Signal Rules files and running Position Sizing Rules files are completely separate processes, you cannot declare a variable in a SIG file and expect to access its value later on from a SIZ file (or vice-versa).

However, there are four (4) Mechanica Basic keywords that have been designed to pass data values from SIG code to SIZ code. They are:

SIZING[n]

SYSTEM

SORTLIST[n]

USERDEFINEDRISK

 

Well discuss two of them here. (SORTLIST[n] and SYSTEM are discussed in detail in the Initial Position Sizing Lesson.)

SIZING[n] is a variable that allows you to assign (and store) numerical values while your system is processing trades. These values are automatically passed on to the Position Sizing Rules for use in sizing trades. SIZING[n] is legal on any Signals Rule page. Please also note: If you are going to pass a value in a SIZING[n] variable (by assigning a value to SIZING[n]), be sure that to only reference yesterday's information in the Signal Rules code.

USERDEFINEDRISK is designed as a substitute for Mechanicas internal risk definition, and the value you assign this variable flows through to all Sizing keywords containing the word RISK. This includes not only initial sizing keywords, but resizing keywords as well. (Well cover this concept in detail later in this section, and in the Resizing section.)

To create and apply user-named variables

1Creating a user-named variable is simple. All you have to do is “declare” it, so that Mechanica knows what it is, and how to treat it. You do this by typing a statement of the form: VAR = varname   (shown below):

VAR = MID_POINT        'var declaration

 

2Now that the variable has been declared, you can assign a value to it. (This is called “initializing” the variable):

VAR = MID_POINT           'var declaration

MID_POINT = (HIGH - LOW) / 2        'assignment

 

3If you want to reference previous days value of a user-named variable, you must first assign the user-named variable to a column, and then reference the desired offset value from another column (this also holds true for MEMORY as well):

VAR = MID_POINT           'var declaration

MID_POINT = (HIGH - LOW) / 2   'assignment

COL1 = MID_POINT        

COL2 = COL1[1]         

4Or, you can create another user-named var to reference the desired offset:

VAR = MID_POINT           'var declaration

MID_POINT = (HIGH - LOW) / 2   'assignment

COL1 = MID_POINT

'

VAR = MID_POINT_YESTERDAY        'var declaration

MID_POINT_YESTERDAY = COL1[1]         'assignment

5But, you cannot simply add an offset to a named variable as you can with a Column definition, or with a grid-column alias:

VAR = MID_POINT           'var declaration

MID_POINT = (HIGH - LOW) / 2   'assignment

COL1 = MID_POINT[1]               'ILLEGAL STATEMENT

 

Like MEMORY, user-named variables retain their value when the current page is exited (their value “persists” across pages). This means that a value assigned on the Resources page can be read from the Trade Entry page, or the TIP & Exit page. Once set to a value, that value will not change until the variable is written to again (assigned another value), or until a new symbol (instrument) is loaded in which case all are reset to zero.

So, user-named variables are just like MEMORY in every regard except that (a) you can give them a descriptive label (hence their name!), and (b) user-named variables must be declared before you can use them.

ThumbTack white

1.Just to clarify any confusion, you cannot employ a named variable (or any other variable type), in a SIG file and expect to reference it later from a SIZ file, or vice-versa. It should be clear by now that Mechanica just doesnt work that way!
2.Mechanica has another variable type called TEMPMEM[n]. With TEMPMEM, values are lost when the current page is exited (the values do not persist across pages). In this regard, Mechanica helps you manage TEMPMEM, whereas MEMORY and user-named variables are completely user-managed.

 

Understanding persistence in variables

Often, the best way to grasp a concept is to write a small test file and examine its output in the Grid. So thats what well do to demonstrate how the value of a variable can persist across pages, and through time. Although a user-named variable is shown, the same concept of persistence holds true for MEMORY (but not for TEMPMEM).

This also happens to be a good demonstration of the sequence in which Mechanica processes the pages of a SIG file.

Weve created a user-named variable called MYVAR, and will employ it as a “flag” to keep track of trade status. MYVAR is declared on the Resources page (refer to SIG code below), and its value assigned to COL1.

Long and short entries occur on specific, separate dates. After a trade fires off, MYVAR is assigned a value of either +1 for a long trade, or 1 for a short trade; that value is subsequently displayed in COL2.

Each trade exits on the close of the 5th day. Upon exit, the value of MYVAR is reset to zero, or “cleared.”  Note that the value of MYVAR is read and displayed in two places on the TIP & Exits page; first, at the top of the page prior to the exit statements, and then again, immediately following the exit statements.

Lets run this SIG file and see what happens. You can do this yourself in Mechanica with Test_01.sig (found in ..\Signal Rules), and the symbol BP_B_REV, or you can follow along on this page. (Note that weve dispensed with the graphical representation of the various tabs of the Rules Editor, based on the assumption that, at this point, an all-text layout below should be sufficiently clear.)

Resources

SYSTEM = 5

VAR = MYVAR

COL1 = MYVAR

 

Trade Entry

IF DATE = 19890106 THEN BUYOPEN ; MYVAR = 1   long entry

IF DATE = 19890117 THEN SELLOPEN ; MYVAR = -1 'short entry

COL2 = MYVAR

 

Trade in Progress & Exit

COL3 = MYVAR

IF DAYSINTRADE = 5 THEN SELLCLOSE ; MYVAR = 0  'long exit

IF DAYSINTRADE = 5 THEN BUYCLOSE ; MYVAR = 0   'short exit

COL4 = MYVAR

 

Column-aliases are employed in the Grid to designate the pages where the value of MYVAR has been sampled and subsequently displayed (image below). Keep in mind that the value of MYVAR has been sampled twice on the TIP & Exit page. (It was sampled once before, and once after the exit statements.)

The value of the user-named variable MYVAR (created on the Resources page), is automatically “initialized” to zero, since no other value was specified. Remember, the Resources page is Mechanicas first stop each trading day. So even though the system enters a long trade on 19890106...keep in mind that, when Mechanica visits the Resources page that day, the system is still flat, and MYVAR = 0 (see Col 1 for that date, in the graphic below).

IF DATE = 19890106 THEN BUYOPEN ; MYVAR = 1   'long entry

 

When processing is handed over to the Trade Entry page on that same date, a long trade fires off, and MYVAR is set equal to 1. Next, processing passes to the TIP & Exit page, and the value MYVAR = 1 persists (see Col 3 and Col 4 below).

 

Starting with the next trading date (19890109), and reading across the Grid by rows, the value MYVAR = 1 persists on every page, every day, except on the Trade Entry page, where the value is zero (highlighting added). Why? Because when a trade is in progress, processing passes back and forth every day from the Resources page to the TIP & Exit page, and the Trade Entry page is skipped...until after an exit occurs.  So a zero is displayed on those days (even though MYVAR has not literally been reset to zero).

IF DAYSINTRADE = 5 THEN SELLCLOSE ; MYVAR = 0  'long exit

 

As instructed, the system exits the trade on the close of the 5th day (19890112). Notice on this day that the value of MYVAR which sits at the top of the TIP & Exit page, prior to the exit statements, is still equal to 1 (see Col 3, above). After the exit statement executes, the value of MYVAR is reset to zero (Col 4, highlighted), where it remains until another trade is entered.

Three trading days later, on 19890117, the system enters a short trade as instructed, and the process repeats, with the exception that MYVAR assumes the value of 1 when a short trade fires off.

IF DATE = 19890117 THEN SELLOPEN ; MYVAR = -1 'short entry

 

Both trades are shown below, back-to-back.

You can repeat this exercise using MEMORY[n] ...where n = 1-100, and the results will be identical to using MYVAR (a user-named variable). If you want to try it, then in TEST_01.sig, comment out VAR = MYVAR on the Resources page, and replace every remaining occurrence of MYVAR with MEMORY[1]. Then rerun the SIG file in Mechanica.

However, if you substitute TEMPMEM[n] instead (in the same manner), you will see that it only retains its value on the current page (Trade Entry, in this case), and loses its value as soon as the Trade Entry page is exited. Results for the example long trade are shown below.

       

ThumbTack white

1.The previous example demonstrated the use of a variable as a “flag” to keep track of trade status. In case you were wondering, Mechanica Basic has keywords dedicated to this task, such as LONG and SHORT.
2.Mechanica does not allow you to employ a user-named variable as a grid-column alias.