Strategy testing system
Test before you invest


 

 
  Home Downloads Licensing Contact

 

Using Scripts

This exercise introduces TekView's scripting interface and covers how to create script for the popular TRIX indicator and insert it into an graph.

Begin with opening the Script Editor by selecting Tools / Scripting from the menu.  At the top of the form are two pull-down boxes.  The one on the left displays modules.  The one on the right displays procedures in the selected module.  The user can create as many modules as they wish but there must always be one "Global" module.  Procedures in all other modules can access the variables and procedures of the Global modules but not of each other.  Script is entered in the text section of the form and when debugged and saved can be executed in three ways; by issuing a command from the Command Line on the Script Editor, by inserting with a DS Function or by creating a macro that calls the procedure. Each method is demonstrated in this exercise

First, create a new subroutine called SayHi in the Global module by selecting File / New / Subroutine from the menu.  Type in MsgBox "Hi" as shown above.  Type the name of the subroutine in the command line and press the execute button and a message box displays Hi.  This simple example shows how to execute a procedure from the command line.

Next, create a new module named MyIndicator by Selecting File / New / Module from the menu.  Then create a new subroutine named CallSayHi and enter the subroutine as shown below.

 

Notice the module containing procedure must be identified in the command since it is not in the Global module (procedures in the Global module can be called without designating the module).  However, you can not call MyIndicator.CallSayHi from another module.

Next, enter and execute the code below and notice how the Debug Window opens and displays the printed message.   The Debug Window,  the message box object and the Command Line are valuable when trying to debug script.

 

Now copy the following script into the MyIndicator module.  The code uses the TekView objects exposed to the script engine to create and return a DataSeries object.  This data series can be used by TekView like any other data series when added to DSCollection.  Pay particular notice to how the formula string is constructed and other properties of the data series set.  See TekView Help for a reference to objects, properties and methods.

Function TRIX(pPeriods)
    Dim vw 'Current view
    Dim dp 'Data point
    Dim ds 'Data series   
    Dim ps 'Chart price series
    Dim pr 'Price object
    Dim pv 'Price value
    Dim e 'Smoothing exponent
    Dim ema1 '1st smoothed value
    Dim ema2 '2nd smoothed value
    Dim ema3 '3rd smoothed value
    Dim val
    Dim i

    'Calculate smoothing exponent
    If pPeriods <= 1 Then
        e = 1
    Else
        e = 2 / (pPeriods - 1)
    End If

    Set vw = Views.CurrentView
    Set ps = vw.PSCollection.ChartSeries
    Set ds = vw.DSCollection.CreateDataSeries
    For Each pr in ps.Prices
        i = i + 1
        pv = pr.ClosePrice
        ema1 = ema1 * (1-e) + pv * e
        ema2 = ema2 * (1-e) + ema1 * e
        val = ema3
        ema3 = ema3 * (1-e) + ema2 * e
        Set dp = vw.DSCollection.CreateDataPoint
        If i < pPeriods * 3 Or val = 0 Then
            dp.Value = 0
            dp.Excluded = True
        Else
            dp.Value = (ema3 - val) / val * 100
        End If
        ds.DataPoints.Add dp
    Next
    ds.Formula = "=MyIndicator.TRIX(" & pPeriods & ")"
    ds.Name = "TRIX(" & pPeriods & ")"
    ds.Origin = mmOriginZero
    ds.Mean = DSAvg(ds)
    ds.StdDeviation = DSStdDev(ds)
    ds.Keep = True
    Set TRIX = ds

End Function

One way of inserting this indicator into the current chart view is with the Scripted DSFunction.  Simply type the function call into the CommandString property preceded with an equal sign (e.g., "=MyIndicator.TRIX(20)" without the quotation marks).  Then drag the function onto an indicator chart.

Another way is to write a subroutine that can be executed by a macro that adds the data series by code as in the following example.

Sub InsertTRIX(pPeriods)

    Dim vw 'View
    Dim ds 'DataSeries to hold formula
    Dim idx 'New chart index
    Dim txt 'String

    Set vw = Views.CurrentView
    idx = vw.CreateIndicatorChart()
    Set ds = TRIX(pPeriods)
    ds.ChartName = "IndicatorChart"
    ds.ChartIndex = idx
    vw.DSCollection.Add ds
    ds.Keep = True
    vw.Refresh

End Sub 

Then create a new macro from the menu by selecting File / New / Macro.  Give the macro a friendly name and then in the Command property enter "MyIndicator.InsertTRIX 20" without the quotation marks.  Notice that unlike a DSFunction the command is not preceded by an equal sign and the command is in the form of a subroutine call rather than a function (i.e., arguments aren't surrounded by parentheses).

 

Back