NetLogo banner

NetLogo Publications
Contact Us

Modeling Commons

Beginners Interactive NetLogo Dictionary (BIND)
NetLogo Dictionary

User Manuals:
Farsi / Persian


NetLogo User Community Models

(back to the NetLogo User Community Models)

[screen shot]

If clicking does not initiate a download, try right clicking or control clicking and choosing "Save" or "Download".

Try It in NetLogo Web


The model explores the mechanism of price formation in a stock market. The model is relatively simple, yet generates all the emergent properties of real stock markets. It also shows under what circumstances one can get pathological price behaviour such as monotonic moves to zero or infinity, or permanent oscillations between two price levels.


There is one asset (a stock) in the market. The model is non-spatial. Agents (investors) do not interact with each other directly but with the stock price, which is determined by the total demand for the stock, i.e., they interact indirectly in their aggregate. So net aggregate demand determines the price movement in each time period (tick), and this price movement then determines the investors' demand in the next time period. The price formation mechanism is thus highly recursive.

### Investor types

There is a variable number of investors. These investors are of two types, followers and contrarians, with two diametrically opposite strategies. If the stock’s price moved up in the last time period, followers want to buy while contrarians wish to sell, and vice versa for a downward move in price. There are therefore always buyers and sellers, but with differing demands of the quantity desired to transact. There is no leverage, but short selling is allowed.

Sales of the stock generate cash for an investor and purchases require cash. An investor's wealth is the sum of the value of their shareholding and their cash balance.

### Demand function
In every time period there will be demand from both followers and contrarians. The magnitude of their demand represents "how big do I want to bet" and is determined by a demand function. It is always a positive number or zero. The sign of this demand, i.e. buy or sell, is then determined separately by the previous price move.

For each investor, their demand function contains three components:

**Risk demand.**
The value of stock demanded is proportional to the moving average (m.a.) of returns over the last _m_ returns. (Investors have a memory of the last _m_ time periods.)
So risk demand = (_k_<sub>4</sub>) (_r_) (_s_) (m.a.) where:
_k_<sub>4</sub> is an empirical constant,
_r_ is a scale factor: _r_ = 1 if m.a. >=0 and _r_ = 2.5 if m.a. <0, since studies have shown that investors hate losses ~2.5x more than they love gains.
_s_ is the sign. If the m.a. is positive, investors know there is a a higher probability of future negative returns, so the risk appetite of contrarians will increase and that of followers will decrease. So if m.a. is +/-, the sign for contrarians will be +/- and for followers will be -/+.

The value of the risk demand is the same for all investors in one time period. Individual investors are then randomly assigned an individual risk demand in the range [0, risk demand].

**Herd demand.**
The value of stock demanded is proportional to the aggregate stock demanded by other investors _of the same type_. It is assumed that followers and contrarians have different propensities to herd, and are given two different empirical constants.

So herd demand = (_k_<sub>2</sub>) (total demand of followers) for followers, and
... herd demand = - (_k_<sub>3</sub>) (total demand of contrarians) for contrarians
Note that the herd demand can only be of positive sign.

This value of herd demand is therefore different for each type of investor in a time period. Individual investors of each type, i.e. followers and contrarians, are then randomly assigned an individual herd demand in the range [0, herd demand - followers] and [0, herd demand - contrarians] respectively.

**Wealth demand.**
The value of stock demanded is proportional to the investor's wealth.
So wealth demand = (_k_<sub>1</sub>) (wealth), where _k_<sub>1</sub> is an empirical constant.

This demand component is the same for all investors. Note "wealth demand" will be of the same sign as wealth, which we assume can be negative as well as positive. This value of wealth demand is the same for all investors in one time period. Individual investors are then randomly assigned an individual wealth demand in the range [0, wealth demand].

Investors' wealth is dependent on how many shares they buy or sell in each time period. The value of their shares will then change with the new share price in the next period, in turn changing their wealth.

The total demand for each investor is then the sum of these three demands. Its sign must always be positive as it is the "size of desired bet", and its sign will simply be determined by the direction of the previous price move. Therefore, if the magnitude of total demand is less than zero, it is set to zero.

### Price, price return, value and volume traded
The net share value demanded is
_D_ = (demand from followers + demand from contrarians)
where the two demands are appropriately signed.

The change in stock price is then proportional to this net demand. So, at time _t_ the price is
_P_<sub>_t_</sub> = _P_<sub>_t_-1</sub> + (_k_<sub>5</sub>) (_D_) where _k_<sub>5</sub> is an empirical constant.

Note that changing the number of investors is not neutral - a larger number of investors will tend to result in larger net demands _D_, which will cause larger price changes.

The stock price return will be
_R_<sub>_t_</sub> = (_P_<sub>t-1</sub> / _P_<sub>t</sub>) - 1 (log returns are not strictly necessary here).

Followers and contrarians will usually wish to transact with different amounts of demand, so to clear the market the total share value traded is
_T_<sub>_t_</sub> = _min_ (|demand from followers|, |demand from contrarians|)

Note that all the model's relationships are linear, while in the real world everything is non-linear!


The input items in the interface tab are largely self-explanatory, consisting of the number of investors, the fraction of investors who are contrarians, the persistence of investors' memory (_m_ time periods) and the empirical parameters _k_<sub>1</sub>..._k_<sub>5</sub>.
The command buttons are the usual ones. "Go once" is useful for examining the causes of a price change from tick to tick and for debugging. The _"go slowly"_ button uses one tick every 0.5 seconds, to slow things down.


The prime output is the share price graph. The value of stock demanded (in dollars) by followers and contrarians, and their total, is also shown. The next plot shows the percentage return in each period, together with the moving average of the previous _m_ periods, i.e. it shows if the share has generally gone up or down in investors' recent memory, and by how much. The distribution of these returns is also shown. Market returns are not random and their distribution is not normal. The volatility of price movements over the past 36 periods is calculated and displayed. Trade, in both dollars and number of shares, is also plotted, as is the dollar wealth of followers, contrarians and their total. The Demand/Wealth graph is discussed in NETLOGO FEATURES.

This data can then be extracted with BehaviorSpace and analysed to see if it accords with the known emergent phenomena of markets, which include:

* persistence of returns
* volatility clustering
* low autocorrelation
* excess kurtosis
* volume correlation with price volatility


A market consisting of only these two types of investors is intrinsically unstable. This is exacerbated by the fact that as an investor's wealth grows, they take larger buying or selling positions.

There are two bounding unstable price behaviors:
* If the demand of followers is greater than contrarians (e.g., there are no contrarians) and the price, for example, declines, then it will continue declining to zero. Conversely if the price moves up it will continue to infinity.
* If the demand of contrarians is greater than that of followers, for either initial price movement, the price subsequently will become cyclical, oscillating indefinitely between two values.

Most combinations of parameter settings eventually lead to these bounding behaviors.

Also, in the case of monotonic increases or decreases in price, the wealth of followers will become infinite, while price oscillations will see the wealth of contrarians rise to infinity, as these investors will make the correct decision in every time period.

These behaviors are legitimate and would happen in a real market as modeled. They are not a code flaw. They demonstrate the intrinsic instability of markets. Market crashes, for example, often occur without any obvious proximate cause - they are simply emergent behavior. (And, inevitably, if the price goes to zero, many variables tend to blow up.)


Best explored with the _"go once"_ or _"go slowly"_ buttons. Try adjusting the parameters under various settings. How sensitive is the stability of price behavior to these parameters? Which parameters counteract each other and which reinforce each other? If there are exactly equal numbers of followers and contrarians, what does the stock price do? See the extreme situations where there are only followers or contrarians, which breaks the market. Do the returns look random or is there clustering? Can you see fat tails and kurtosis in the return distributions? Does the price volatility correlate with anything?


Possible embellishments of the model are limitless. Additional investor categories such as long-term and short-term players and insider traders could be added. Individual investors could be allowed to use leverage. Also, risk appetite (as determined by the moving average) has been the same for each investor type. Ideally, each investor should use the moving average of their individual return history. Additional assets, as well as transaction costs could be incorporated. Most importantly, investors could be given a range of different and possibly competing strategies, including adaptive ones - which is closest to the actual behaviour in real markets. (This was the original Santa Fe Artificial Stock Market Model.)
Note that complicating the model does not change the market's emergent properties materially. It can however be useful in examining the sensitivity to these additional parameters.


Because the model is non-spatial, the main plot has been transformed into a graph. This graph shows 5 dimensions for each agent on the same plot using shape, coordinates, size and color intensity to indicate agent type, wealth, demand, herd influence and risk aversion, respectively.

The histogram is of a global variable, not a turtle property. In this case, 'histogram' operates on a list. So the variable's values were accumulated in a list, with the list being extended by one element at each tick. Lists were also required to accumulate histories for the moving average of the last _m_ returns and 36-period price volatility.

They were also used for the dynamic scaling of some graphs. Smart scaling is implemented on the 'Returns distribution', 'Price volatility' and 'Trade' graphs via plot update commands.

A zero axis is drawn on the Demand and Period return & MA graphs. The Price volatility graph only begins drawing after 36 periods, using the plot-pen-up command in the plot update commands.


The seminal model in this genre was the original Santa Fe Artificial Stock Market Model:
Arthur, W. B., Holland, J. H., LeBaron, B., Palmer, R., & Taylor, P. (1996). _Asset pricing under endogenous expectation in an artificial stock market._ (No. 96-12-093).

A later retrospection on it was:
LeBaron, B. (2002). _Building the Santa Fe artificial stock market._ Physica A, 1-20.

Various improvements were suggested in the book:
Ehrentreich, N. (2007). _Agent-based modeling: The Santa Fe Institute artificial stock market model revisited._ (Vol. 602). Springer Science & Business Media.

There has been surprisingly little highly-cited research since then. Two studies are:
Šperka, R., & Spišák, M. (2013). _Transaction costs influence on the stability of financial market: agent-based simulation._ Journal of Business Economics and Management, 14(sup1), S1-S12.
Oldham, M. (2017). _Introducing a multi-asset stock market to test the power of investor networks._ Journal of Artificial Societies and Social Simulation, 20(4).


If you mention this model or the NetLogo software in a publication, we ask that you include the citations below.

For the model itself:
* Busetti, F. R., (2021). NetLogo Bulls & Bears model. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.

Please cite the NetLogo software as:
* Wilensky, U. (1999). NetLogo. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.


![CC BY-NC-SA 3.0](

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

(back to the NetLogo User Community Models)