"Bollinger band active strategy."

The applet requires Java 5 or higher. Java must be enabled in your browser settings. Mac users must have Mac OS X 10.4 or higher. Windows and Linux users may obtain the latest Java from Oracle's Java site.

Here you can find an introduction to the Bollinger band active strategy application.

WHAT IS IT?

The project “bollinger_band_active_strategy” defines the behaviour of two kind of agents: Random Agents ,that act randomly and generates a random market; and Bollinger’s Agents that apply to market prices, a strategy based on Bollinger Bands conditions. We try to study the effect of Bollinger agents on market prices analysing the signifiance of their strategy in terms of profit or loss.

HOW IT WORKS

Random Agents create the market through an auction mechanism, where prices are ordered in order to obtain bid and ask prices. Bollinger Agents participate to the price formation if one of the two condition is satisfied: 1) the price goes under the lower band and then crosses it; 2) the price goes above the upper band and next crosses it.

HOW TO USE IT

Set NRANDOMAGENTS and NBBAGENTS sliders to control the number of agents. Set the PASSLEVEL slider to fix the probability for each agent of avoiding the negotiation. Moreover the NMOVINGAVERAGE and BANDWIDTH sliders represent the parameters for the creation of the duct defined by the bands, typical of Bollinger’s analysis. Then click the button SETUP to create the world. Click GO to start the simulation.

THINGS TO NOTICE

Each strategy in our program, alternates the buy and sell conditions, considering that the starting point is always buy.

THINGS TO TRY

Try to modify the NRANDOMAGENTS and the NBBAGENTS sliders in order to evaluate the effectivenes of the Bollinger strategy compared to the ordinary strategy. Try to modify the NMOVINGAVERAGE and the BANDWIDTH sliders to understand how the choose of different parameters used for the Bollinger bands construction influence the Bollinger strategy, analysing also the variations related to the graph ABOVE/UNDER BANDS.

EXTENDING THE MODEL

You may introduce different strategies related to how the price fill in the bands, defining new overbought and oversold conditions. You can also insert more complicated market strategies and improve agents intelligence giving them some form of learning skills about investing in the market price.

CREDITS AND REFERENCES

Bollinger Bands official website (www.bollingerbands.com).
Pietro Terna. g1_CDA_basic_model

CODE

; Unlike most things you buy, both the buyer and seller set stock prices.
; The buyer states what price they will pay for the stock: this is the bid price.
; The seller also has a price: the ask price.

; If you have access to the proper online pricing systems, you can see the bid and
; ask prices. You will notice that the bid price and the ask price are never the same.
; The ask price is always a little higher than the bid price.

; What this means is if you are buying the stock you pay the ask price (the higher
; price) and if you are selling the stock you receive the bid price (the lower price).

breed [BBAgents BBAgent]
breed [randomAgents randomAgent]
BBAgents-own[overbought oversold stocks purchase sold out-of-market buy sell pass price cash]
randomAgents-own[out-of-market buy sell pass price cash stocks overbought oversold sold]
globals [adj logB logS exePrice number t n j meanBox days AR ovB ovS timeP SBoxP SBoxM SBoxLB SBoxUB varBox devSt A MA part partS dev UB LB BBpocket ordinaryPocket OP difference Obuy Osell under above]

to setup

clear-all
if seed != 0 [random-seed seed]
set exePrice 1000
set MA 0
set A 0
set AR []
set OP []
set dev 0
set devSt 0
set days []
set meanBox []
set varBox []
set SBoxP []
set SBoxM [] ;it is a vector useful for testing the correctness of the program, but useless for the outputs
set SBoxLB []
set SBoxUB []
set timeP 0
set number 0
set n 0
set j 0
set ovB 0
set ovS 0
set Osell 1
set logB []
set logS []
set difference 0
set t 0
set under 0
set above 0
reset-ticks

create-randomAgents nRandomAgents
create-BBAgents nBBAgents

let side sqrt (nRandomAgents + nBBAgents)

let step max-pxcor / side

[
set shape "person"
set out-of-market False
set size 2
set BBpocket 0
;set cash 0
;set stocks 0
]
[
set out-of-market False
set BBpocket 0

set shape "person"
set size 2
set BBpocket 0
set overbought False
set oversold False
set sold 1
set purchase 0
set ordinaryPocket 0
;set cash 0
;set stocks 0
]

let an 0
let bn 0
let x 0
let y 0
while [an < nRandomAgents + nBBAgents]
[
if x > (side - 1) * step
[set y y + step
set x 0       ]
[setxy x y]
set x x + step
set an an + 1

]
end

to go

[
if overbought [set color violet]
if oversold  [set color yellow]
if not overbought and not oversold [set color white]
]

[
ifelse out-of-market [set color white]

[ifelse random-float 1 < passLevel [set pass True][set pass False]
ifelse not pass
[ifelse random-float 1 < 0.5 [set buy  True set sell False]
[set sell True set buy  False] ]
[set buy False set sell False]

if pass       [set color gray]
if sell       [set color green]

;set price 501 + random 999
;set price random-normal 1000 100
set price exePrice + (random-normal 0 100)

]

]

;use these clearing operationa if a 'go cycle' is 'a day'
;commenting them logs are kept until the end of the simulation experiment
set logB []
set logS []

;tick
;show ticks
;NB in future implementation it would be interesting to make empty both the side of
;   the market at regular intervals of more than one cycle

[if not pass and not out-of-market
[
let tmp[]
set tmp lput price tmp
ask BBAgents[if overbought [set tmp lput price tmp]]
ask BBAgents[if oversold [set tmp lput price tmp]]
set tmp lput who tmp

;show who

if buy [set logB lput tmp logB]
set logB reverse sort-by [item 0 ?1 < item 0 ?2] logB
;show logB

if (not empty? logB and not empty? logS) and
item 0 (item 0 logB) >= item 0 (item 0 logS)

[set exePrice item 0 (item 0 logS)
let agB item 1 (item 0 logB)
let agS item 1 (item 0 logS)

;strategy based on random market prices: BUY
if Osell >= 1[set ordinaryPocket ordinaryPocket - exeprice
set OP lput ordinaryPocket OP
set Osell Osell - 1]]

;ask randomAgent agB [set stocks stocks + 1
;set cash cash - exePrice]
;ask randomAgent agS [set stocks stocks - 1
;set cash cash + exePrice]
set logB but-first logB
set logS but-first logS

;insert each price in a vector
if exePrice > 0 and j >= (nMovingAverage - 1) [set SBoxP lput exePrice SBoxP
set timeP timeP + 1
set days lput exeprice days]

;set the wallet for the buy and hold strategy
;if exeprice != 0 and length days >= (nForFirstPrice + 1) [ set firstPrice  item nForFirstPrice  days
;set wallet (- firstPrice + exeprice)]

;Moving Average calculus
set part exeprice / (nMovingAverage - 1)
if part != 0 [set meanBox lput part meanBox
set number number + 1
set A A + part
set j j + 1]

if number = nMovingAverage - 1 [set MA A
set number number - 1
set A A - (item 0 meanBox)
set meanBox but-first meanBox]

;insert each calculated mean in a vector
if MA != 0 and timeP >= (nMovingAverage - 1) [set SBoxM lput MA SBoxM
]

;standard deviation calculus
if MA != 0 [set partS (exeprice - MA) ^ 2]
if exeprice != 0 [set dev dev + partS
set n n + 1
set varBox lput partS varBox]
if n = nMovingAverage - 1 [set devSt sqrt(dev) / (nMovingAverage - 2)
set n n - 1
set dev dev - (item 0 varBox)
set varBox but-first varBox]

;Bands formation
if MA != 0 and devSt != 0 [set UB  MA + (Bandwidth * devSt)
set LB  MA - (Bandwidth * devSt)]

if timeP >= (nMovingAverage - 1) and LB != 0 and UB != 0 [set SBoxLB lput LB SBoxLB
set SBoxUB lput UB SBoxUB
]

]
;show exePrice

if sell [set logS lput tmp logS]
set logS sort-by [item 0 ?1 < item 0 ?2] logS
;show logS

if (not empty? logB and not empty? logS) and
item 0 (item 0 logB) >= item 0 (item 0 logS)
[set exePrice item 0 (item 0 logB)
let agB item 1 (item 0 logB)
let agS item 1 (item 0 logS)

;strategy based on random market prices: SELL
if Obuy >= 1 [set ordinaryPocket ordinaryPocket + exeprice
set OP lput ordinaryPocket OP
set Osell Osell + 1

;ask turtle agB [set stocks stocks + 1
;set cash cash - exePrice]
;ask turtle agS [set stocks stocks - 1
;set cash cash + exePrice]
set logB but-first logB
set logS but-first logS

;insert each price in a vector
if exePrice != 0 and j >= nMovingAverage [set SBoxP lput exePrice SBoxP
set timeP timeP + 1
set days lput exeprice days]

;Moving Average calculus
set part exeprice / (nMovingAverage - 1)
if part != 0 [set meanBox lput part meanBox
set number number + 1
set A A + part
set j j + 1]

if number = nMovingAverage - 1 [set MA A set number number - 1
set A A - (item 0 meanBox)
set meanBox but-first meanBox]

;insert each calculated mean in a vector
if MA != 0 and timeP >= (nMovingAverage - 1) [set SBoxM lput MA SBoxM
]

;standard deviation calculus
if MA != 0 [set partS (exeprice - MA) ^ 2]

if exeprice != 0 [set dev dev + partS
set n n + 1
set varBox lput partS varBox]

if n = nMovingAverage - 1 [set devSt sqrt(dev) / (nMovingAverage - 2)
set n n - 1
set dev dev - (item 0 varBox)
set varBox but-first varBox]

;bands formation
if MA  != 0 and devSt != 0 [set UB  MA + (Bandwidth * devSt)
set LB  MA -  (Bandwidth * devSt)]

;insert band values in a vector
if timeP >= (nMovingAverage - 1) and UB != 0 and LB != 0 [set SBoxLB lput LB SBoxLB
set SBoxUB lput UB SBoxUB
]

;show MA

]
;show exePrice
set under 0
set above 0
]

;if exePrice <= 300 [stop set adj True]

;[set t t + 1 ]
;if t = 2 [set exeprice 300 + random-normal 0 100 set out-of-market False set t 0 ]

BBStrat
if length SBoxP >= 2 and length SBoxLB >= 2 and length SBoxUB >= 2
[set SBoxP but-first SBoxP
set SBoxLB but-first SBoxLB
set SBoxUB but-first SBoxUB]
check_strategies
graph
]
;show SBoxUB
;show UB
;show SBoxP
;show SBoxLB
;show LB

;evaluate_strategies
tick
end

;Bollinger Bands strategy in BBpocket only for BBAgents
to BBStrat
if length SBoxLB >= 2 and length SBoxP >= 2 and
item 0 SBoxP < item 0 SBoxLB and
item 1 SBoxP > item 1 SBoxLB and
sold >= 1 and buy [set BBpocket BBpocket - item 1 SBoxP
set overbought True
set oversold False
set ovB ovB - 1
set AR lput BBpocket AR
set purchase purchase + 1 set sold sold - 1]

if length SBoxUB >= 2 and length SBoxP >= 2 and
item 0 SBoxP > item 0 SBoxUB and
item 1 SBoxP < item 1 SBoxUB and
purchase >= 1 and sell [set BBpocket BBpocket + item 1 SBoxP
set oversold True
set overbought False
set ovS ovS + 1
set AR lput BBpocket AR
set purchase purchase - 1 set sold sold + 1]

if length SBoxLB >= 2 and length SBoxP >= 2 and
item 0 SBoxP > item 0 SBoxLB and
item 1 SBoxP > item 1 SBoxLB and
item 0 SBoxP < item 0 SBoxUB and
item 1 SBoxP < item 1 SBoxUB [ set overbought False set oversold False]

if length SBoxLB >= 2 and length SBoxP >= 2 and
item 0 SBoxP < item 0 SBoxLB and
item 1 SBoxP < item 1 SBoxLB [ set overbought False set oversold False]

if length SBoxLB >= 2 and length SBoxP >= 2 and
item 0 SBoxP > item 0 SBoxUB and
item 1 SBoxP > item 1 SBoxUB [ set overbought False set oversold False]]

;show ovB
;show ovS
end

;Calculate the differences in the two strategies(buy and sell in market conditions, or according to Bollinger Bands signals) in both LONG and SHORT positions
to check_strategies
set difference BBpocket - ordinaryPocket

end

;end

to graph

set-current-plot "exePrice"
plot exePrice

set-current-plot "BollingerBands strategy"
plot BBpocket

set-current-plot "Ordinary Strategy"
plot ordinaryPocket
set-current-plot "Difference in the two strategies"
plot difference

end