BBScript Program Sample: (Momentum Indicator with Exponential Moving Average)
The following is an example of a BBScript code sample to plot the 12 period momentum indicator along with a 12 period exponential moving average of that indicator in the same indicator chart.
You need to be subscribed to BollingerOnBollingerBands in order to run BBScript. If you haven't subscribed yet, get your 30 day FREE trial here
Once you've subscribed, go to the Chart section
and make sure that the "Advanced" tab is selected.
Open the "BBScript" editor by clicking on its tab option in the main chart menu. The editor will open. To enable the script to run, make sure the "Run Script" checkbox is checked. On the left panel text area, insert the script you want to run. Click the "Run" button to run the script and plot its output. The script will run on the currently selected symbol and sample rate. In the case below, it is the ticker AAPL's DAILY chart. If you wish to run the script on another symbol or sample rate, just change those settings from their corresponding input fields and drop down menus.
data("x") //data object
//create momentum indicator and its ema
period1 = 12 //mtm period
period2 = 12 //ema period
momentum = close(x) - close(x)[-period1] //mtm formula
plot1 = plot(momentum,"Momentum","histogram","ff0000") //mtm plot
ema_mtm = ema(momentum,period2) //ema of mtm
plot2 = plot(ema_mtm, "EMA", "line", "0000ff") //plot of ema
chart(plot1,plot2) //display mtm and ema in indicator chart
The BBScript editor is displayed below. It consists of the following components:
- Run Script Checkbox: (top left side) The script will run when this checkbox is checked.
- New Button: This will create a blank new script and open it in the current workspace. The script name must be provided in the Script name field.
- Browse Button: This button will open the file manager where all the user's scripts are displayed.
- Delete Button: This button will delete the current script from the backend server and remove it from the workspace.
- Script Name: The script name must be unique and can only contain up to 40 alpha-numeric characters, spaces or the underscore character (_). A script can't be saved or run until a valid name is provided.
- Save Button: (top left side) To save your script to our server, click this button. A valid script name must be provided. If it is disabled, then your script is already saved.
- Input Script Panel: (left side) Insert your code in this area. Comments are highlighted in green and errors are underlined in red. As you type your script, autocomplete functions and their description is displayed on the output screen.
- Search Manual Text Field: (top right side) enter a keyword to lookup in the BBScript manual.
- Output Panel: (right side) Displays the system notifications, variable printouts, error messages, manual or API lookups.
- Duration Indicator: (bottom right side) Displays the duration of the script execution.
BBScript is cloud based. It runs in a browser and all scripts are stored safely on our servers. You can access the scripts anytime you are on line and logged onto BollingerOnBollingerBands. You must have an Adobe Flash Player installed on your computer and be connected to the Internet to create, edit, save and execute your scripts from our web interface.
In the BBScript file manager, scripts can be loaded into the workspace, renamed or deleted by clicking the corresponding buttons ( load, rename or delete) next to a script's name in the same row.
- Loading a Script: To load a script into the editor's workspace so you can edit or run it, click the "Load" button next to its name. Before the action is completed, you will be asked to save the currently displayed unsaved workspace script before it is removed from the workspace.
- Deleting a Script: To delete a script, simply click the "Delete" button. You will be prompted to confirm your delete action before it is executed.
- Renaming a Script: Clicking the "Rename" button will allow you to edit the script's name as shown below. Once you have finished entering the new name (with the name parameters outlined in Script Name above), click the "Apply" button next to it and the changes will be applied. If the name is invalid, an error message will appear.
The resulting indicator is shown below the price chart. Notice the labels for the momentum and its exponential moving average. Also notice that the momentum indicator is plotted in histogram format (positive green vertical lines and red negative vertical lines). The exponential moving average is plotted in blue in a linear format. The created indicator behaves the same way as the built in indicators. If you hover the cursor above it you can determine the value at a specific point. You can zoom in and out as well as drag the chart to the right or left. As data get streamed the script will rerun and the indicator chart is updated automatically.
The BBScript Backtester is a new feature of BBScript. It was purposely designed to be simple and easy to use without sacrificing power. Its goal is to let you quickly try out your trading and analysis ideas and then automate them if needed. We have prewritten all the Bollinger Band indicators and many other technical indicators for you. In addition, we have written many example scripts that you can use as is or employ as templates for your own ideas. There are many system creation, optimization and backtesting platforms; usually they are quite complicated and very expensive. Ours is a free addition to your existing BB subscription and is designed to be easy to use. Like any powerful tool, it'll take a little while to get used to it, but once you are up to speed I think that you'll appreciate just how unique and useful the BBScript Backtester is. Enjoy!
- You can test 6 different types of systems.
- Mode one: Always in the market, no pyramiding
- Wait for first signal and enter the market long or short.
- Ignore subsequent signals of the same type.
- Reverse to opposite state on next signal of opposite type.
- Repeat step b and c.
Example, if buy first, buy 1(long entry), sell 2 (long exit and short entry), buy 2, sell 2...
Example, if sell first, sell 1, buy 2, sell 2, buy 2...
- Mode two: Always in the market, with pyramiding
- Wait for first signal and enter the market long or short.
- On subsequent signals of the same type add a position.
- Reverse to opposite state on next signal of opposite type. If long, sell net position plus one more unit; If short, cover net position plus one more unit
- Repeat step b and c.
Example, if long first, buy 1, sell 2, sell 1(an add-on), buy 3 (2 long exits, one short entry), sell 2...
- Mode three: Discrete Trades without stops, no pyramiding
- For long trades, wait for the first long entry signal, march forward until matching exit is found, record trade and look for next long entry signal.
- For short trades, wait for the first short entry signal, march forward until matching exit is found, record trade and look for next short entry signal.
- Mode four: Discrete Trades without stops, with pyramiding
- For long trades, march forward until matching exit is found, record trade. Return to long entry and look for next long entry signal.
- For short trades, wait for the first short entry signal, march forward until matching exit is found, record trade. Return to short entry and look for next short entry signal.
- Mode five: Discrete Trades with stops, no pyramiding
- For long trades, wait for the first long entry signal, march forward until matching exit or stop is found, record trade and look for next long entry signal.
- For short trades,wait for the first short entry signal, march forward until matching exit or stop is found, record trade and look for next short entry signal.
- Mode six: Discrete Trades with stops and pyramiding
- For long trades, wait for the first long entry signal, march forward until matching exit or stop is found, record trade. Return to long entry and look for next long entry signal.
- For short trades,wait for the first short entry signal, march forward until matching exit or stop is found, record trade. Return to short entry and look for next short entry signal.
- An optional stop type can be specified when testing systems with stops (mode five and mode six).
There are 3 types of stops: Chandelier Stop, Parabolic Stop and Bollinger Stop.
- Your signals will be numbered as pairs and can be plotted on the chart. Click on any entry or exit and its complement will be instantly highlighted.
- Besides normal system statistics, you can see at a glance how your approach is doing over time by using equity curve plot, which can be constructed by adding daily trade gains or compounding daily trade gains.
Please refer to documentation
for more information.
The following is an example of a BBScript code sample to construct a simple Bollinger Band System, discrete trades with stops and no pyramiding backtester and equity curve plot.
// Written by John Bollinger April 2014
// use the data from the chart
// Bollinger Bands using builtin functions
middleBB = bbands(x, 20, 2, "middle")
lowerBB = bbands(x, 20, 2, "lower")
// back in the lower BBands = buy
entry = xover(close(x), lowerBB)
// tag the middle BBand = sell
exit = -xover(close(x), middleBB)
// group buy and sell signals in one array
signals = entry + exit
// back test type 4 = discrete trades, use stops, no pyramiding
back_type = 4
// stop type = Chandelier
stop_type = 0
// run the back test
bt = backtest(x, signals, back_type, stop_type)
// prepare price chart with signals
plot1 = plot(close(x), "signals", "line", "00000000", bt)
// show chart with signals
// calculate equity curve without compounding
equity_curve_calc = 0
// get equity-curve array using the back-tester object
eqCurve = equitycurve(bt, equity_curve_calc)
// create equity-curve plot
plot2 = plot(eqCurve, "EQ Curve", "line", "0000ff")
// display equity-curve chart
To use BBScript Backtester, go to Advanced Chart section, click BBScript button in the main chart menu.
Copy the "Simple System and Backtester" example code or write your own BBScript code in Script Input panel and run it. Please make sure the "Run Script" checkbox is check. System notifications will be shown in the Output Panel.
After successful code execution, click the "Backtester Report" button (top ride side of BBScript Editor), a detailed Backtester Report will show up. Click "Pop Up" button to open the report in a separate window.
The Report includes Trade Summary and Trades History. Below are statistical terms used in Backtester report and their definitions.
In Trade Summary section:
- Total Trades: total number of closed trades
- Total Wins: total number of winning closed trades
- Total Losses: total number of losing closed trades
- Winning %: percentage of winning closed trades in the total number of closed trades
- Total Average Win %: average percent gain per winning closed trade (short and long)
- Total Average Loss %: average percent loss per losing closed trade (short and long)
- Profit Factor: total absolute points won/total absolute points lost, closed trades only
- Overall Average Gain %: average percent gain per closed trade (longs, shorts, winning and losing trades)
- Overall Cumulative Gain %: total percentage compounded gains, closed trades only
In Trades section - Long:
- Number of Longs: total number of closed long trades
- Wins: number of winning closed long trades
- Losses: number of losing closed long trades
- Average Win %: average percent gain per winning closed long trade
- Average Loss %: average percent loss per losing closed long trade
- Cumulative Gain %: total compounded percentage gains from long trades, closed trades only
In Trades section - Short:
- Number of Shorts: total number of closed short trades
- Wins: number of winning closed short trades
- Losses: number of losing closed short trades
- Average Win %: average percent gain per winning closed short trade
- Average Loss %: average percent loss per losing closed short trade
- Cumulative Gain %: total compounded percentage gains from short trades, closed trades only
The following is an example of Backtester Report.
Total Trades: 22
Total Wins: 16 / Total Losses: 6
Winning %: 72.72
Total Average Win %: 5.03
Total Average Loss %: -7.93
Profit Factor: 1.25
Overall Average Gain %: 1.5
Overall Cumulative Gain %: 33.18
Number of Longs: 22
Average Win %: 5.03
Average Loss %: -7.93
Cumulative Gain %: 33.18
1: Enter: 2010-08-25 242.5500, Exit: 2010-09-01 250.3300, + 3.2076 %
2: Enter: 2011-03-17 334.6400, Exit: 2011-03-25 351.5400, + 5.0502 %
3: Enter: 2011-05-17 336.1400, Exit: 2011-05-31 347.8300, + 3.4777 %
4: Enter: 2011-06-13 326.6000, Exit: 2011-06-27 332.0400, + 1.6656 %
5: Enter: 2011-06-21 325.3000, Exit: 2011-08-08 353.2100, + 8.5798 %
6: Enter: 2011-11-15 388.8300, Exit: 2011-11-25 363.5700, - 6.4964 %
7: Enter: 2011-11-18 374.9400, Exit: 2011-12-01 387.9300, + 3.4646 %
8: Enter: 2011-11-22 376.5100, Exit: 2011-12-20 395.9500, + 5.1632 %
9: Enter: 2012-04-17 609.7000, Exit: 2012-05-09 569.1800, - 6.6459 %
10: Enter: 2012-05-18 530.3800, Exit: 2012-05-23 570.5600, + 7.5757 %
11: Enter: 2012-10-09 635.8500, Exit: 2012-10-31 595.4500, - 6.3537 %
12: Enter: 2012-10-22 634.0300, Exit: 2012-11-02 576.8000, - 9.0264 %
13: Enter: 2012-11-05 584.6200, Exit: 2012-11-08 537.2600, - 8.1010 %
14: Enter: 2012-11-09 547.0600, Exit: 2012-11-23 571.5000, + 4.4675 %
15: Enter: 2013-01-16 506.0900, Exit: 2013-01-24 450.5000, - 10.9842 %
16: Enter: 2013-01-28 449.8300, Exit: 2013-02-11 479.9000, + 6.6847 %
17: Enter: 2013-03-05 431.1400, Exit: 2013-03-15 443.8500, + 2.9480 %
18: Enter: 2013-04-22 399.3600, Exit: 2013-04-29 430.1200, + 7.7023 %
19: Enter: 2013-06-27 393.7800, Exit: 2013-07-03 420.8000, + 6.8617 %
20: Enter: 2013-09-18 464.6800, Exit: 2013-09-23 490.8300, + 5.6275 %
21: Enter: 2014-01-06 543.9300, Exit: 2014-01-15 557.3600, + 2.4691 %
22: Enter: 2014-01-31 500.6000, Exit: 2014-02-10 528.9900, + 5.6712 %
Number of Shorts: 0
Average Win %: 0
Average Loss %: 0
Cumulative Gain %: 0
Signals and equity curve
You can see trade signals on the chart after your BBScript run successfully. Green up arrows are buys (long entries, short exits) and Red down arrows are sells (long exits, short entries). Click on any arrow, its complement will be highlighted. Entry and Exit Date along with trade gain will also show up. Equity curve is shown below the price chart.