ESPL: Power Programmer, Part I


This article is a review of some of the features and the power of ESPL; the programming language provided with Ensign Windows.  I should mention that I have been programming for over 20 years, and as such, this article is written by a programmer, for programmers.  Before I chase all the non-programmers away, let me point out that, like all aspects of Ensign Windows, ESPL can be used in many ways.  It is very possible to write useful tools in just a few simple lines of ESPL. So if you want to learn just enough ESPL, to be able to whip off a simple 5 line indicator, or a new way to color bars, ESPL can do it.  On the other hand, if you want to write a series of complex computations, requiring several charts and timeframes, remembering prior computations and generating multiple forms of output; well then, ESPL can do that too.

Before getting started, I’ll mention that my outline for this article could result in a small novel, rather than an article.  Therefore the plan is to break it into pieces, with subsequent parts in future issues of this newsletter.

Operating Environment

Before writing the first line of ESPL, there are a few concepts that are foundational in ESPL, that should be understood.  The first of these is that Ensign uses, at most, one ESPL file (aka script or program) at a time.  However, that script may contain the code for any number of studies.  In addition, the physical file that is specified to the ESPL compiler may reference other files, whose content are read by the compiler.  This allows your script to be extremely large, while still allowing the programmer to segment and manage the code.

The ESPL Editor window, in addition to providing a text editor, controls when the code is running (the Run button) and when it is not (the editing check box). Clicking in the editor window stops all ESPL code from running.  Clicking the Run button will compile the code, and if error free, allow the code to run.  Notice that ESPL is compiled, enabling your code to run much faster than otherwise.  It is important to note that the compiler only allows for backward referencing.  That is, any reference to a variable or procedure, must occur after that variable or procedure has already been declared.

There are several features in Ensign Windows that can make use of ESPL code.  These include:

  • Studies

An ESPL study is applied to a chart, in the same way that regular studies are applied to charts. Multiple instances of an ESPL study may be applied to one chart.  Similarly, instances of an ESPL study may be applied to multiple charts.  How the ESPL code determines which instance of the study is being run, is discussed later in this article.

  • Drawing Tools

Just like studies, an Ensign drawing tool can be implemented in ESPL.  Multiple instances of the drawing tool may be applied to one or more charts.

  • Color Bar

Similar to studies and drawing tools, color bar studies can be implemented in ESPL.  Several instances of a color bar study may be applied to multiple charts; where, only one instance is applied to each chart.

  • Timer

Ensign has a timer that can invoke an ESPL script on a regular time interval; configurable in second increments.

  • Scheduler

The Ensign scheduler can invoke an ESPL script at a configured time of day.  The scheduler is unique in it’s use of ESPL, in that, it is the only place that the ESPL code file may be specified.  In all other cases, the currently loaded ESPL file (the last file opened in the ESPL editor) is used.

  • Run and numbered buttons

These buttons appear in the ESPL editor, and in the menu bar of the main window, after the editor has been opened.  Clicking any of these buttons, 0 (also called Run) through 9, can invoke an ESPL script.

  • Keyboard

The invocation of ESPL can be accomplished using hot keys Alt-0 through Alt-9.  And, the invocation of ESPL can be tied to the Shift function keys, F2 through F12.

Before I proceed, let me give you a moment to ponder the massive flexibility of this list.

ESPL uses a built-in global variable, called ESPL (called Who in old versions of Ensign Windows) , to determine which of these invocation methods has been used.  Each study, button, function key, etc. will invoke the ESPL code, with a different ESPL value.  (Refer to the ESPL documentation of the ESPL variable for specific values).

Some of the invocation methods, such as studies, may invoke the ESPL script for every tick or at the close of each bar.  A check box in the study’s properties window selects this setting.  Of course, studies that are called on every tick, will require more processing power, than studies that are called only at the end of the bar.  To be accurate, the study is actually called on the first tick of the new bar, rather than the last tick of the finished bar.

When ESPL code for a study, drawing tool or color bar study is called (invoked), there are some other global variables that define the context in which the code runs.  An Ensign chart exists in a window, and so, the global variable Window identifies the window (and thus the chart) that owns the study; for which the script is being called.  Chart information, by default, is obtained for the current window.  It is possible for the code to change the Window variable, and thus obtain information about other windows/charts.  There is also the concept of the current study; which is the study for which the code was invoked.  Methods that access study information, take a parameter that specifies the study.  The current study is referred to by the value zero.

The global built-in variables BarBegin and BarEnd are basically indices into the array of bars displayed in a chart.  BarBegin is the left most (or oldest) bar, and BarEnd is the right most (or newest) bar.  When Ensign needs the ESPL study to process all of the bars in the chart, then these variables are not equal, and encompass the range of bars in the chart.  Subsequent calls to the ESPL script, made for new bars in the chart, are called with these two variables being equal, and identifying the newest bar.  Thus the code can identify an initialization invocation by comparing these values for non-equality.

For reasons of both size and subject matter, this is a good place to stop. Next time, we will look at some of the key aspects of a programming language, that are incorporated in ESPL.

Read more» Part II
Read more» Part III

Article by Paul Levin


Last modified 10/20/08 1:57 PM