powered by NetLogo
view/download model file: Stock_Market_Predictor.nlogo
Stock markets represent an emergent phenomenon which scientists have attempted to study, often with mixed results. The theories which try to define the actions of the markets use many assumptions, some of which are suspect (do market participants act rationally). Understanding these limitations, this model creates a fantasy stock market with 200 stocks.
Stocks are created with a random starting price and a random volatility number. The volatility number will determine how much the stock moves after any given interaction.
Every tick, each stock compares itself against every other stock. If the volatility (our proxy for risk) indicates the price is not favorable compared against any given stock, the comparing stock will be sold. Likewise, if the price is favorable, the comparing stock will be bought.
Hit clear, then setup to populate the stocks. Go-once will run the interaction algorithms one time (ie one tick). Go will run the algorithms continuously. A bad event will cause all stocks to be sold exactly once, and a good event causes all stocks to be bought exactly once. An IPO is an initial public offering. This will create a new stock to be added to the market. A Bankruptcy will cause a stock to be delisted from the market, signalling the end of its public trading.
In the current iteration of the model, you may notice that in 6 to 9 ticks, the stocks will all begin to follow a certain pattern. However, becareful of trying to predict the movements of any given stock. Unlike a true stock market, this market has no human irrationality, and will always act based on the given rules. As such, momentum trading and other day trading techniques may prove to be your undoing.
Inspect a couple stocks, and create a fantasy portfolio. Then, track this portfolio over time, and see what happens to money invested. Or, try trading stocks very actively and see how often you can pick winners.
After letting the model run for a bit, look at the effect of bad and good events. Espeically when observing the graph, allow the good or bad events to be macroeconomic events, acting over the course of time. Observing the chart, you'll see a trend that is similar to long-term growth in equity markets.
Individuals taking on the task of extending this model will be best served by incorporating more depth to individual stocks, such as fundamental ratios. Further, certain technical analyses may beautify the visual aspects of a model, and perhaps tease users into buying certain stocks.
breed [stocks stock]
stocks-own[
symbol
open-price
daily-high
daily-low
current-price
volatility
]
globals[
aStock
]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;SETUP;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup
create-stocks 200
[set color grey
set shape "circle"
set size 2 ]
set-prices
set-locations
set aStock one-of stocks
do-plots
end
to set-prices
ask stocks [
set current-price ((random 200.000) + 1)
set open-price current-price
set volatility ((random 200.000) + 1)
set daily-low current-price
set daily-high current-price
]
end
to set-locations
ask stocks [
set xcor min-pxcor + 1
set ycor current-price
set heading 90
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;RUNNING;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to go
ask stocks[
set daily-low current-price
set daily-high current-price
]
stock-interaction
tick
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;Stock interactions;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to stock-interaction
ask stocks[
ask other stocks
[ifelse
((volatility > ([volatility] of myself)) and (current-price > [current-price] of myself))
[sell-stock]
[buy-stock]
update-stats
]
]
end
to sell-stock
let n (current-price - (.001 * volatility) * current-price)
set current-price n
set color red
set heading 135
jump n
do-plots
end
to buy-stock
let n (current-price + (.001 * volatility) * current-price)
set current-price n
set color green
set heading 45
jump n
do-plots
end
;;;;;;;;;;;;;;;;;;;;;;;;
;;;;Stats updating;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;
to update-stats
if current-price < daily-low
[set daily-low current-price]
if current-price > daily-high
[set daily-high current-price]
end
;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;CLEAR;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;
to clear
ca
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;Market-movers;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
to bad-event
ask stocks[
sell-stock
]
do-plots
end
to good-event
ask stocks[
buy-stock]
do-plots
end
to IPO
create-stocks 1
[set color grey
set shape "circle"
set size 2
set current-price ((random 200.000) + 1)
set open-price current-price
set volatility ((random 200.000) + 1)
set daily-low current-price
set daily-high current-price
set xcor min-pxcor
set ycor current-price
set heading 90]
end
to Bankruptcy
ask one-of stocks
[die]
end
;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;Plotting;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;
to do-plots
set-current-plot "Stock Price"
set-current-plot-pen "Stock"
plot [current-price] of aStock
end