Random Walk Simulation Code
Run this code in NetLogo in order to replicate the model.
globals [
; the stock price
pt
; stock price in next period
pt+1
; stock price in previous period
pt-1
; true fundamental value of stock
fundamental-value
; list of past prices
price-list
; population of fundamentalists
pop-fund
; population of technical optimists
pop-tech-op
; population of technical pessimists
pop-tech-pess
total-pop
; number of buy orders
D
; number of sell orders
S
; random element in price equation
gamma
; coefficient representing the speed of price adjustment
a
; TEST
good-news?
bad-news?
severity
partner-profit
partner-philosophy
shares-outstanding
volatility
]
; different types of traders as breeds
breed [ fundamentalists fundamentalist ]
breed [ technical-optimists technical-optimist ]
breed [ technical-pessimists technical-pessimist ]
turtles-own [
stock-holdings
trader-pt+1 ; what the trader expects the price to be next period
initial-value ; initial value of portfolio
portfolio-value ; current value of portfolio
money-in ; all money spent buying stocks
money-out ; all money recieved selling stocks
profit ; current portfolio value + money out - money in
gain-loss ; profit / portfolio-value
good-news-reaction
bad-news-reaction
philosophy
]
fundamentalists-own [
F ; true fundamental value
f ; parameter of speed with
; which the fundamentalist believes
;the price will return to fundamental value
alpha ; random variable accounting for perception errors
]
technical-optimists-own [
c ; reaction coefficient of the trader to price changes
beta ; random noise and uncontrollable aspects of technical trading
trend
]
technical-pessimists-own [
c ; reaction coefficient of the trader to price changes
beta ; random noise and uncontrollable aspects of technical trading
trend
]
to setup
clear-all
set pop-fund 100
set pop-tech-pess 100
set pop-tech-op 100
set good-news? false
set bad-news? false
setup-price
setup-breeds
reset-ticks
end
to setup-price
set pt 80
set pt-1 80
set pt+1 80
;set price-list [ 71.98 72 ]
set fundamental-value 80
set volatility 0
;type "pt-1: " print pt-1
;type "pt: " print pt
;type "fundamental-value: " print fundamental-value
end
to setup-breeds
set total-pop pop-fund + pop-tech-pess + pop-tech-op
create-fundamentalists pop-fund
ask fundamentalists [
set philosophy "fundamentalist"
setxy random-xcor random-ycor
set color blue
set shape "circle"
set F fundamental-value
set alpha random-normal 0 5
set f random-float 0.5 ; random parameter for the speed of price adjustment
set stock-holdings 100
set money-in money-in + stock-holdings * pt
set good-news-reaction random-float 1
set bad-news-reaction random-float 1
set initial-value stock-holdings * pt
set portfolio-value initial-value
; type "pt " print pt
if testing? = true
[type "f " print f
type "F " print F
type "alpha " print alpha]
set trader-pt+1 pt + (f * (fundamental-value - pt)) + alpha
if testing? = true [type "expected price of fundamentalist " print who print trader-pt+1]
]
create-technical-optimists pop-tech-op
ask technical-optimists [
set philosophy "technical-optimist"
setxy random-xcor random-ycor
set color green
set shape "square"
set c random-float 1
set beta random-normal 0 5
set trader-pt+1 pt + (c * (pt - pt-1)) + beta
set stock-holdings 100
set initial-value stock-holdings * pt
set portfolio-value initial-value
set money-in money-in + stock-holdings * pt
set good-news-reaction random-float 1
set bad-news-reaction random-float 1
if testing? = true
[type "expected price of technical optimist " print who print trader-pt+1]
]
create-technical-pessimists pop-tech-pess
ask technical-pessimists [
set philosophy "technical-pessimist"
setxy random-xcor random-ycor
set color red
set shape "triangle"
set c random-float 1
set beta random-normal 0 5
if testing? = true [type "beta " print beta]
set trader-pt+1 pt + c * (pt-1 - pt) + beta
set stock-holdings 100
set initial-value stock-holdings * pt
set portfolio-value initial-value
set money-in money-in + stock-holdings * pt
set good-news-reaction random-float 1
set bad-news-reaction random-float 1
if testing? = true [type "expected price of technical pessimist " print who print trader-pt+1]
]
end
to decide-buy-sell
ask fundamentalists [
if trader-pt+1 - pt > 0
[place-buy-order]
if trader-pt+1 - pt < 0
[place-sell-order]
]
ask technical-optimists [
if trader-pt+1 - pt > 0
[place-buy-order]
if trader-pt+1 - pt < 0
[place-sell-order]
]
ask technical-pessimists [
if trader-pt+1 - pt > 0
[place-buy-order]
if trader-pt+1 - pt < 0
[place-sell-order]
]
end
to go
; if abs(pt+1 - pt) < 0.5 [stop]
if ticks mod 50 = 0
[set fundamental-value fundamental-value + random-normal 0 3 ;+ random-float 1 ; this keeps the overall trend in fundamental value positive
ask technical-optimists
[set c random-float 1
set beta random-normal 0 5]
ask technical-pessimists
[set c random-float 1
set beta random-normal 0 5]
ask fundamentalists
[set f random-float 1
set gamma random-normal 0 5]
decide-philosophy]
external-shock
decide-buy-sell
if D + S != 0 [set-price]
set pt-1 pt ; update prices
set pt pt+1
calculate-expected-price
ask turtles [move-randomly]
tick
end
to move-randomly
set heading (random 360)
fd 1
end
to place-buy-order
let ordered? false
if (trader-pt+1 - pt) / pt >= 0.1 ; if the trader expects the price to rise 10% or more next period buy 8 shares
[ set stock-holdings stock-holdings + 8
set D D + 8
set money-in money-in + 8 * pt
calculate-profit
set color green
if testing? = true [type "buying 8 shares " print who]
set ordered? true
]
if (trader-pt+1 - pt) / pt > 0.05 and (trader-pt+1 - pt) / pt < 0.1 and ordered? = false ; if the trader expects price to rise between 5% and 9.99% next period buy 5 shares
[ set stock-holdings stock-holdings + 5
set D D + 5
set money-in money-in + 5 * pt
calculate-profit
set color green
if testing? = true [type "buying 5 shares " print who]
set ordered? true
]
if (trader-pt+1 - pt) / pt >= 0.025 and (trader-pt+1 - pt) / pt <= 0.05 and ordered? = false ; if trader expects price to rise between 2.5% and 5% next period buy 2 shares
[ set stock-holdings stock-holdings + 2
set D D + 2
set money-in money-in + 2 * pt
calculate-profit
set color green
set ordered? true
if testing? = true [type "buying 2 shares " print who]
]
if (trader-pt+1 - pt) / pt < 0.025 and ordered? = false ; if trader expects price to rise between 0.001% and 2.5% next period buy one share
[set stock-holdings stock-holdings + 1
set D D + 1
set money-in money-in + pt
calculate-profit
if testing? = true [type "buying 1 share " print who]
set color green
]
end
to place-sell-order
let ordered? false
if (trader-pt+1 - pt) / pt > -0.025 and (trader-pt+1 - pt) / pt < 0 and stock-holdings >= 1 ; if trader expects price to fall between 0.001% and 2.5% next period sell one share
[set stock-holdings stock-holdings - 1
set S S + 1
set money-out money-out + pt
calculate-profit
set color red
if testing? = true [type "selling 1 share " print who
type stock-holdings print " shares left"]
set ordered? true
]
if (trader-pt+1 - pt) / pt <= -0.025 and (trader-pt+1 - pt) / pt >= -0.05 and ordered? = false ; if trader expects price to fall between 2.5% and 5% next period sell 2 shares
[ifelse stock-holdings >= 2
[set stock-holdings stock-holdings - 2
set S S + 2
set money-out money-out + 2 * pt
calculate-profit
set color red
if testing? = true [type "selling 2 shares " print who
type stock-holdings print " shares left"]
set ordered? true
]
[set S S + stock-holdings
set money-out money-out + stock-holdings * pt
calculate-profit
if testing? = true [type "selling all " type stock-holdings type " shares" print who
type stock-holdings print " shares left"]
set stock-holdings 0
set ordered? true
set color red]
]
if (trader-pt+1 - pt) / pt < -0.05 and (trader-pt+1 - pt) / pt > -0.1 and ordered? = false ; if the trader expects price to fall between 5% and 9.99% next period sell 5 shares
[ifelse stock-holdings >= 5
[set stock-holdings stock-holdings - 5
set S S + 5
set money-out money-out + 5 * pt
calculate-profit
if testing? = true [type "selling 5 shares " print who
type stock-holdings print " shares left"]
set ordered? true
]
[set S S + stock-holdings
set money-out money-out + stock-holdings * pt
calculate-profit
set ordered? true
if testing? = true [type "selling all " type stock-holdings type " shares" print who
type stock-holdings print " shares left"]
set stock-holdings 0]]
if (trader-pt+1 - pt) / pt <= -0.1 and ordered? = false ; if the trader expects the price to fall 10% or more next period sell all shares
[ifelse stock-holdings >= 8
[set stock-holdings stock-holdings - 8
set S S + 8
set money-out money-out + 8 * pt
calculate-profit
if testing? = true [type "selling 8 shares " print who
type stock-holdings print " shares left"]
]
[set S S + stock-holdings
set money-out money-out + stock-holdings * pt
calculate-profit
if testing? = true [type "selling all " type stock-holdings type " shares" print who
type stock-holdings print " shares left"]
set stock-holdings 0]]
end
to set-price
set gamma random-normal 0 1 ; draws gamma from random normal distribution. 0.25 standard deviation can be positive or negative
set a random-float 1 ; sets alpha between 0 and 1
if testing? = true [type "D: " print D
type "S: " print S
type "gamma " print gamma
type "a " print a]
set pt+1 pt + a * ((D - S) / (D + S)) + gamma ; sets price based on number of buy and sell orders
if pt+1 < 0
[set pt+1 0.0001]
if testing? = true [type "price " print pt+1]
set volatility D - S
set D 0
set S 0
;set price-list lput pt+1 price-list
end
to calculate-expected-price
ask fundamentalists [
;type "f " print f
;type "funadamental-value " print fundamental-value
;type "alpha " print alpha
ifelse good-news? = false and bad-news? = false
[set trader-pt+1 pt + f * (fundamental-value - pt) + alpha]
[ifelse good-news? = true
[set trader-pt+1 pt + f * (fundamental-value - pt) + alpha + (severity * good-news-reaction)] ; adds in random shock mean 100 st dev 30
[set trader-pt+1 pt + f * (fundamental-value - pt) + alpha - (severity * bad-news-reaction)]]
if testing? = true [type "expected price " print who print trader-pt+1]
; type "profit of " type who print " " print profit
ifelse portfolio-value > 0
[set gain-loss profit / portfolio-value]
[set gain-loss 0]
; type "gain loss " print gain-loss
]
ask technical-optimists [
ifelse good-news? = false and bad-news? = false
[set trader-pt+1 pt + c * (pt - pt-1) + beta]
[ifelse good-news? = true
[set trader-pt+1 pt + c * (pt - pt-1) + beta + (severity * good-news-reaction)] ; adds in random shock mean 100 st dev 30
[set trader-pt+1 pt + c * (pt - pt-1) + beta - (severity * bad-news-reaction)]]
if testing? = true [type "expected price " print who print trader-pt+1]
; type "profit of " print who print " " print profit
ifelse portfolio-value > 0
[set gain-loss profit / portfolio-value]
[set gain-loss 0]
;type "gain loss " print gain-loss
]
ask technical-pessimists [
ifelse good-news? = false and bad-news? = false
[set trader-pt+1 pt + c * (pt-1 - pt) + beta]
[ifelse good-news? = true
[set trader-pt+1 pt + c * (pt-1 - pt) + beta + (severity * good-news-reaction)] ; adds in random shock mean 100 st dev 30
[set trader-pt+1 pt + c * (pt-1 - pt) + beta - (severity * bad-news-reaction)]]
if testing? = true [type "expected price " print who print trader-pt+1]
; type "profit of " print who print " " print profit
ifelse portfolio-value > 0
[set gain-loss profit / portfolio-value]
[set gain-loss 0]
; type "gain loss " print gain-loss
]
end
to external-shock
let newstype? true
set good-news? false
set bad-news? false
if random-float 1 > 0.5
[set newstype? false] ; 0 means bad news 1 means good news
if random-float 1 > 0.9
[ifelse newstype? = true
[set good-news? true
set severity random-float 100]
[set bad-news? true
set severity random-float 100]]
end
to calculate-profit
set portfolio-value stock-holdings * pt
;type "stock holdings of " print who print stock-holdings
;type "price " print pt
;type "portfolio value of " print who print portfolio-value
set profit portfolio-value + money-out - money-in
;type "profit of " print who print profit
end
to decide-philosophy
ask turtles [
let partner one-of other turtles
ask partner [
set partner-profit profit
;type "partner profit " print partner-profit
set partner-philosophy philosophy
;type "partner philosophy " print partner-philosophy
]
if partner-profit > profit [
let change-prob random-float 1
if change-prob > 0.6 [
if partner-philosophy = "fundamentalist"
[set breed fundamentalists
set f random-float 0.5
set alpha random-normal 0 0.25]
if partner-philosophy = "technical-optimist"
[set breed technical-optimists
set c random-float 0.5
set beta random-normal 0 0.25]
if partner-philosophy = "technical-pessimist"
[set breed technical-pessimists
set c random-float 0.5
set beta random-normal 0 0.25]
;type who print "changed breed to " print partner-philosophy
]
]
]
end