The applet requires Java 1.4.1 or higher. It will not run on Windows 95 or Mac OS 8 or 9. Mac users must have OS X 10.2.6 or higher and use a browser that supports Java 1.4. (Safari works, IE does not. Mac OS X comes with Safari. Open Safari and set it as your default web browser under Safari/Preferences/General.) On other operating systems, you may obtain the latest Java plugin from Sun's Java site.
powered by NetLogo
Alessandro De Simone's report
NN basis in Python
view/download model file: stock_market_with_boundendly_rational_agents.nlogo
This model is a simulation of a single stock market with boundedly rational agents competing in the market trying to increase their cash following a personal strategy based on Artificial Neural Networks (ANN).
In the model each agent having enough cash or stocks will perform an offer of buying or selling that will be reported in the corrispondent logbook until prices will allow the transaction.
Each agent will be equipped with n-strat strategies as Artificial Neural Network (ANN) that, given the last mem value of the price exePrice forecast the new price. The agents will decide if to buy or sell and the corresponding price on the basis of the forecast; after the realization of the transaction, an handicap score will be assigned to the strategies according to the difference between the forecast and the effective price. The agents will follow the strategy with the lowest handicap trying to maximize their performances. The ANNs have been trained on a real time series using a Python code.
Click the SETUP button to set up the traders.
Click the GO button to run the simulation.
The number of trader can be changed with the slider n-traders, while the initial number of stocks and cash of the agents can be modified with the input form initial-cash and initial-stocks.
The log is cleaned after a tick-for-day number of ticks.
When the GO button has been pushed the simulation starts and the agents begin to exchange stocks and cash: the price is an emergent feature of the interaction between the past prices and the strategies. It is worth stressing that some agents will decide not to act if they are in economic troubles (not enough cash or no stocks), so that after a period all the agents will be in the pass state and there will not be any transaction.
Does the number of traders changes the behaviour of the market? What does it happens if the number of strategies for trader is increased? Which is the initial number of stocks and cash necessary to avoid a sudden death of the market at the beginning of the simulation?
The model presents a wide range of improvements: first of all even if in this simulation the attention is focused on the absolute value of the price, also the value of the return can be considered. The way agents make the offer, considering the last price and the forecast one, can be further discussed in order to find the most realistic and coherent with the market rule.
; n-traders number of traders ; n-strat number of strategies per traders ; mem is the memory, the number of inputs of the nn ; ch is the number of data of the hidden layer ; n-nns is the number of neural networks globals [n-nns mem ci ch co logB logS exePrice history day] breed[nns nn] ;neural networks to be used by traders breed[traders trader] ;agents - traders id goes from n-nns to (n-traders+n-nns-1) nns-own[wi wo forecast handicap] traders-own[nnset out-of-market buy sell pass cash stocks forecastt] to setup clear-all set n-nns 90 set history [10.2 10.3 10.4 10.2 10.5] set exePrice last history set logB  set logS  set mem 5 ;global variables setting, memory set ci (mem + 1) ;input nn set ch 4 ;hidden layer set co 1 ; number of output create-nns n-nns foreach n-values (n-nns) [?] [ ; reads neural networks from files ask nn ? [ set handicap 0 set hidden? true let path word word "nn/wi" ? ".out" set wi readM path ci ch set path word word "nn/wo" ? ".out" set wo readM path ch co ] ] create-traders n-traders ask traders [ set nnset  repeat (n-strat) [ set nnset lput (random n-nns) nnset ; associates to each agent a strategy: some agents can be stubborn with some strategies identical ] set out-of-market False set shape "person" set size 2 set stocks initial-stocks set cash initial-cash ] let side sqrt n-traders let step max-pxcor / side let an n-nns let x 0 let y 0 while [an < (n-traders + n-nns)] [ if x > (side - 1) * step [set y y + step set x 0 ] ask trader an [setxy x y] set x x + step set an an + 1 ] end to-report readvalue [fname] ;read the first value of a file file-open fname let value file-read file-close report value end to-report readM [fname r c] ;read a matrix stored in a file file-open fname let line  let m  repeat (r)[ set line  repeat (c)[ set line lput file-read line ] set m lput line m ] file-close report m end to-report multmatr [vi m] ;multiplies a vector vi to a matrix m let vo  let j 0 repeat (length (item 0 m)) [ let somm 0 let i 0 repeat (length vi)[ set somm (somm + (item i vi) * (item j (item i m))) set i (i + 1) ] set vo lput somm vo set j (j + 1) ] report vo end to-report sigmoid [v] ;applies sigmoid to all the elements of a vector let vo  foreach v [set vo lput ((e ^ (?) - e ^ (- ?)) / ( e ^ (?) + e ^ (- ?) )) vo] report vo end to-report outputnn [inp0 wi0 wo0] ;calculates the output of an ann given the input let inpscaled  foreach inp0 [set inpscaled lput (? / (2 * (item 0 inp0))) inpscaled ] set inpscaled lput 1 inpscaled let temp  set temp sigmoid(multmatr inpscaled wi0 ) set temp item 0 (sigmoid(multmatr temp wo0)) report (temp * 2 * (item 0 inp0)) end to update-forecast ;updates the forecasts ask nns [ set forecast outputnn history wi wo ] ask traders [ set nnset sort-by [[handicap] of (nn ?1) < [handicap] of (nn ?2)] nnset set forecastt ([forecast] of nn (item 0 nnset)) ] end to go update-forecast ;logs are cleaned after tick-for-day ticks if (ticks mod tick-for-day) = 0 [ set logB  set logS  ] tick ask traders [ ifelse out-of-market [set color white] [ set pass False ifelse forecastt < exePrice [ set buy False set sell True if stocks < 1 [ set pass True let minPrice 0 ifelse empty? logB [set minPrice exePrice] [set minPrice item 0 (item 0 logB)] if cash < minPrice [set out-of-market True] ] ] [ set sell False set buy True let minPrice 0 ifelse empty? logB [set minPrice exePrice] [set minPrice item 0 (item 0 logB)] if cash < minPrice [ set pass True ;; the maximum value of logB is the minimum possible price if stocks < 1 [set out-of-market True] ] ] ] ;show exePrice ifelse pass [set color grey] [ if buy [set color red] if sell [set color green] ] let tmp if not pass and not out-of-market [ ifelse not empty? logS and not empty? logB [ if buy [set tmp lput ((forecastt + item 0 (item 0 logB)) / 2) tmp] ;;mean value choice if sell [set tmp lput ((forecastt + item 0 (item 0 logS)) / 2) tmp] ;set tmp lput forecastt tmp ;;forecast value choice ;if buy [set tmp lput (item 0 (item 0 logB) + 0.1) tmp] ;; best choice according to ticket size ;if sell [set tmp lput (item 0 (item 0 logS) - 0.1) tmp] ] [set tmp lput ((forecastt + exePrice ) / 2) tmp] ;without offering set tmp lput who tmp 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) ask trader agB [set stocks stocks + 1 set cash cash - exePrice] ask trader agS [set stocks stocks - 1 set cash cash + exePrice] set logB but-first logB set logS but-first logS set history but-first (history) set history lput exePrice history ask nns [ set handicap (handicap + abs ((forecast - exePrice) / 100)) set forecast outputnn history wi wo ] ask traders [ set nnset sort-by [[handicap] of (nn ?1) < [handicap] of (nn ?2)] nnset set forecastt ([forecast] of nn (item 0 nnset)) ] ] ] ;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) ask trader agB [set stocks stocks + 1 set cash cash - exePrice] ask trader agS [set stocks stocks - 1 set cash cash + exePrice] set logB but-first logB set logS but-first logS set history but-first (history) set history lput exePrice history ask nns [ set handicap (handicap + abs ((forecast - exePrice) / 100)) set forecast outputnn history wi wo ] ask traders [ set nnset sort-by [[handicap] of (nn ?1) < [handicap] of (nn ?2)] nnset set forecastt ([forecast] of nn (item 0 nnset)) ] ] ] ] ] graph if exePrice < 0 [ask traders [set out-of-market True]] end to graph set-current-plot "exePrice" plot exePrice end