Strategy testing system
Test before you invest


 

 
  Home Downloads Licensing Contact

TRIX Indicator

The code below can be used to insert the TRIX (triple-smoothed exponential moving average) indicator.  To use, copy the code into a module and create a macro having the following command line.  Note the coded in bold font that may need to be changed to a module name other than MyIndicator.

    mymodule.TRIX(periods)

where

  mymodule = Name of the module the code was copied to.
  periods = Number of periods used in the calculation.
 


Sub TRIX(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 = fnTRIX(pPeriods)
    ds.ChartName = "IndicatorChart"
    ds.ChartIndex = idx
    vw.DSCollection.Add ds
    ds.Keep = True
    vw.Refresh

End Sub
 

Function fnTRIX(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.fnTRIX(" & pPeriods & ")"
    ds.Name = "TRIX(" & pPeriods & ")"
    ds.Origin = mmOriginZero
    ds.Mean = DSAvg(ds)
    ds.StdDeviation = DSStdDev(ds)
    ds.Keep = True
    Set fnTRIX = ds

End Function

 

 

Back

DISCLAIMER All examples are intended for illustrative purposes only .  No representations or warranties of any kind are made about their effectiveness in making trading decisions.