Strategy testing system
Test before you invest


 

 
  Home Downloads Licensing Contact

Bollinger Bands

The code below can be used to create Bollinger Bands.  To use, copy the code into a module and create a macro having the following command line:

     mymodule.BollingerBands periods, type, amount

where

  mymodule = Name of the module the code was copied to.
  periods = Number of periods used in the calculation.
  type = Type of channel used for bands (1=percent, 2=standard deviations)
  amount = Amount channels are offset from mean in either percentage points or standard deviations.
 

Sub BollingerBands(pintPeriods, pintType, pintAmount)
    Dim vw     'View object
    Dim ps     'PriceSeries
    Dim ds     'DataSeries to hold formula
    Dim ds1    'DataSeries for upper band
    Dim ds2    'DataSeries for lower band
    Dim cpl    'Compiler
    Dim txt    'String

    'Get a reference to the current view
    Set vw = Views.CurrentView

    'Create data series for upper band using the SMA DSFunction
    Set cpl = DSCompiler
    txt = "SMA(?.1," & pintPeriods & ", " & pintType & ", " & pintAmount & ")"
    Set ds1 = cpl.Compile(txt)
    ds1.ChartName = "PriceChart"
    ds1.Name = "Upper Band"
    ds1.Keep = True
    vw.DSCollection.Add ds1

    'Create data series for lower band
    txt = "SMA(?.1," & pintPeriods & ", " & pintType & ", " & -1 * pintAmount & ")"
    Set ds2 = cpl.Compile(txt)
    ds2.ChartName = "PriceChart"
    ds2.Name = "Lower Band"
    ds2.Keep = True
    vw.DSCollection.Add ds2

    'Refresh the charts
    vw.Refresh

    'Clean up
    Set ds1 = Nothing
    Set ds2 = Nothing
    Set vw = Nothing
End Sub

 

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.