Multi-agent modeling and simulation of a stock market

The stock market represents complex systems where multiple agents interact. The complexity of the environment in the financial markets in general has encouraged the use of modeling by multi-agent platforms and particularly in the case of the stock market. In this paper, an agent-based simulation model is proposed to study the behavior of the volume of market transactions. The model is based on the case of a single asset and three types of investor agents. Each investor can be a zero intelligent trader, fundamentalist trader or traders using historical information in the decision making process. The goal of the study is to simulate the behavior of a stock market according to the different considered endogenous and exogenous variables. Mohamed Amine Souissi (Morocco), Khalid Bensaid (Morocco), Rachid Ellaia (Morocco)


INTRODUCTION
The aim of this work is to use multi-agent modeling to simulate the behavior of a stock market. This will make it possible to study the evolution of liquidity in such an environment according to the different endogenous and exogenous variables of the model.
We recall that a stock market is a place of purchase and sale of financial products such as equities, bonds, derivatives, and any type of financial security. The term "financial markets" refers to several types of contracts relating to the different categories of financial products: bond markets, equity markets, money markets, derivatives markets, foreign exchange markets, commodity markets, insurance markets and other. For more information about the stock market, we can see Alexander et al. (2001), Campbell et al. (1997) and Lahrichi (2013).
In the financial markets, there are several types of players or agents (investors, banks, central bank...). There is a great interaction between these different market players via sales, purchases and exchanges of securities and financial products. In addition, the interaction between the different actors can also be indirect, because any decision or behavior of an agent impacts others. The agents of the market interact permanently with the external entourage, different news can thus influence and impact them (see, for example, Challet et al., 2005). The modeling of financial markets requires a good understanding of the characteristics of market agents, their behavior, their access to information, their interactions and their sensitivity to various exogenous factors. We can see, for instance, Palmer et al. (1997) and Lux et al. (1998).
In recent years, the approach of agent-based economic and financial analysis has advanced, providing a tool for understanding the complex phenomena observed in economic systems.
Agent-based models offer the opportunity to model behavioral problems and to study in this way the effect of agents' behavior on the market; prices are the result of the interaction of market players. They also offer the opportunity to study the dynamic, heterogeneous and adaptive behavior of market players and its impact on market dynamics. We can refer to LeBaron et al. (1999) and Levy et al. (2000) and , they emphasize the utility of representing traders as agents and studying how macro features emerge from agent interactions.
In this work we are interested in simulating a simplified stock exchange with three types of investors and one type of asset, in order to analyze the evolution of the exchange volumes in the market according to the types of investors.
We begin by a literature review and we will introduce our methodology using a modeling and multiagent simulation with a focus on Netlogo, and we propose a model to simulate a stock market using Netlogo, and give some numerical results.

LITERATURE REVIEW
Several authors have focused on agent-based modeling and simulation to simulate the behavior of the financial market and the interactions between different agents. Indeed, it is clearly difficult to model and solve in an analytical way this type of problem. In economics, Kim and Markowitz were interested in studying the destabilizing potential of dynamic hedging strategies via Monte Carlo simulations of a relatively complex model (see Stigler, 1964).
Recently, modeling and in particular multi-agent modeling has become a very popular practice for the simulation of artificial markets (see Kim & Markowitz, 1989;Stauffer, 2001).
Financial markets represent complex systems where several agents interact constantly. Several classical analytical models have been developed to study and explain the behavior of financial markets, however, the complexity of the financial environment has encouraged modeling by multiagent platforms. Authors claim that multi-agent modeling provides a better understanding of how observable properties in real markets emerge from interactions between agents.
Multi-agent modeling is a new discipline used to model complex systems in different domains. Multi-agent modeling requires the creation of a system comprising individual agents and their environment (Macal et al., 2010). The interest of this practice is to be able to study the impact of variations at the micro level on the macro level. can be applied to any system that can be broken down into a set of autonomous entities. There are applications in many fields such as the social sciences, ecology, biology, economics, finance and other.
These approaches attempt to model financial markets as systems competitive and autonomous interactive agents and focus on their learning dynamics, this can be found in Tesfatsion ( , 2002 and LeBaron (2000).
Several works have emerged using multi-agent modeling as the "Santa Fe Artificial Stock" model developed in the 1990s at Santa Fe Institute For the simulation of a multi-agent model, there are several platforms such as: Netlogo, Agent Sheet, Ascape, Repast, Mason, Anylogic, Flame, Swarm, Starlogo … In this work, we opted for Netlogo. Indeed, several models of financial markets were simulated using the Netlogo platform, such as the works of Gonçales in 2003 and 2005 "Artificial Financial Market". On the other hand, several works have developed financial market conceptions with heterogeneous agents, the decisions of one or all of the agents are influenced by the decisions of their neighbors, while another party acts in a rational manner. We can see, for example, Caldana et al. (2006) who simulated with Netlogo a financial market with a single type of title and three different types of agents differentiated according to their type of behavior (imitator, fundamentalist and obstinate).
In this work, we are interested by the case of stock market with an order book and involving several types of traders (zero intelligent traders, fundamentalists and historicals). Before describing our proposed model, we will present in the following our methodology and we recall briefly some notions of multi-agent modeling.

METHODOLOGY
The objective of our work is to simulate the behavior of the stock market according to the behavior of investors: their knowledge of the market and their intelligence, which determine their type and the degree of their influence by endogenous variables such as macroeconomic liquidity, deficit banking and exogenous variables like risk aversion, or investment capacity. For same cases studies, we give numerical results using multi-agent modeling which it is the most suitable for a complex system such as a stock exchange market (see Tesfatsion, , 2002LeBaron, 2000). Analytic models do not generally allow for taking into account several factors especially the imitation weight in the decision.
As already specified for our simulation, we used Netlogo, a multi-agent programming language and a modeling environment, for the simulation of natural and social phenomena. It is a free, mature, stable and fast software with a history of 19 years of development (since 1999), used also for financial markets simulation (see Caldana et al., 2006).
In order to use multi agents modeling, we have to model different elements such as: • initial system statutes; • behavioral rules of agents (interactions, learning, adaptation...); • rules for the development of the environment; • different properties and characteristics of the system.
In the next subsection, we will present our proposed multi-agent model and the methodology we used in order to simulate the stock market and specifically the investor behavior. We will begin by defining the considered agents and then we present the complex considered system in our case.

Proposed model for stock market
We propose a multi-agent model for simulating the investor behavior. It allows agents of the "investors" model to place orders at the stock exchange level in order to buy or sell quantities of financial security. These agents interact with each other and make decisions based on a number of management rules, which depend on two variable types: • exogenous variables: • macroeconomic liquidity (deficit banking system...); • transparency of the market; • economic conditions (GDP, mass of listed companies...); • endogenous variables: • risk aversion; • knowledge of the market; • investment capacity.
The decisions of each agent are also influenced by the decisions of his neighbors. The schematic diagram of our system is presented in Figure 1.
In an attempt to simulate a stock exchange system based on an order book, we consider a group of 1,000 individual investors, where each investor can be a zero intelligent trader, fundamentalist trader or trader using historical information in the decision-making process, and owns the following properties: • initial amount of cash (uniformly distributed); • initial portfolio (number of held shares); • weight of relative decision (imitation weight in the decision).
We consider also that the order book system allows to confront demand and supply and adjust the price in an asynchronous way.
The decision-making process of each type of traders can be described as follows: • for the zero intelligent traders: decide randomly the side of market and the amount to trade; • for the fundamentalist trader: decide randomly the side of market and decide an amount around the fundamental value of the asset (fundamental value simulated using a random walk); • for the trader using the historical information: the decision is made based on a comparison between the average of the short-term price and average of the long-term price to detect the trend.
The relative behavior impacts the intention of each agent by influencing the choice of the side of market regarding a group reference intention composed of the mean of the intentions of all neighbors.
We use Netlogo in order to implement the proposed agent-based model. The agents are modelled as turtles on a landscape. Each agent has an initial wealth (cash + portfolio).
Each period (day), every agent decides a side of market and amount to trade regarding his type and his individual characteristics.

Figure 1. Schematic diagram of the buying/selling decision-making by the agent (investor)
Systematic: • macroeconomic liquidity (deficit banking system…); • transparency of the market; • economic conditions (GDP, mass of listed companies…); • stock market indices.
• Risk aversion The proposed system confronts the supply and demand and adjusts the asset price in an asynchronous way.
Each period, the wealth of the agents is updated according to the occurred cash flows and investment decision. Bankruptcy occurs when, at the end of a period, the wealth of an agent is equal to zero, it would reset all variables to zero, and the bankrupt agent is out of the economy.

Program description, input and output and variables
In this subsection, we describe the model implemented on Netlogo platform, we specify the input parameters, the global variables, agent variables, the procedures used and also the outputs.

Input parameters:
User in Netlogo interface sets these global parameters: • agents-number is used to set the number of the agents; • initial-wealth is used to set the maximum of the uniformly distributed wealth; • initial-portfolio is used to set the maximum of the uniformly distributed shares; • max-talking-price is used to set the maximum of talking price for the zero intelligent traders; • max-decision is used to set the maximum of amount to trade for the zero intelligent traders; • weight is used to set the weight of the relative behavior of agents; • F-precision is used to set the precision of the fundamental value for the fundamentalist traders; • Z is used to set the percentage of the zero intelligent traders; • Ex is used to set the percentage of the fundamentalist traders (En: the percentage of the traders using the historical information is determined from Z and Ex).

Global variables:
• totAsk: total demand; • totBid: total supply; • clock: clock; • price: current market price; • p-1: last price; • price-vector: all prices in a list; • rent-vector: all returns in a list; • rent: variation of rate of the value; • random-walk-vector: the historical prices of the fundamental value in a list; • random-walk: used to create a random walk; • mct: the average short-term price; • mlt: the average long-term price; • activate: used as an activation threshold in the decision-making process of the historical traders.

Agent variables:
Traders represent the agents of the model. Each trader has a number of variables used at the model: • talking-price: talking price of the trader; • talk: talk or not; • decision: quantity to trade of the trader; • intention: seller or buyer?

Procedures:
• to set up: used to initialize all the agents and global variables according to the input parameters. Traders are distributed by type according to the input parameters; • to go: • step 1: call procedure do-random-walk; • step 2: call procedures make-decision and execute-orders for each agent; • step 3: call procedure bankruptcy; • step 4: call procedure cancel-orders for talking agents; • step 5: reset and tick; • to do-random-walk: creates the fundamental value of the asset using a random walk based on a normal distribution (0.3); • to make-decision: decides the intention and the amount to trade for each trader regarding his type (randomly for the zero intelligent traders, around the fundamental value for the fundamentalists and using the comparison between the average short-term price and average long-term price for the traders using historical information); • to execute-orders: confronts demand and supply and adjusts the price in asynchronous way; • to cancel-orders: sets the decision, intention, talk and decision to zero; • to bankrupt: if the agent wealth is inferior to zero, he is going bankrupt.

Outputs
Several indicators reflect the evolution of the market: • price level: chart of the price curve; • volume: the traded volume chart; • bid/ask: supply and demand chart; • standard deviation of prices: chart of the standard deviation of prices to measure the volatility.

RESULTS
In this section, we will present four study cases to analyze the evolution of stock market indicators (volatility, volume, price level, bankruptcy).
The evolution of stock market indicators (volatility, volume, price level, bankruptcy) in this case is described in Figure 3.

Start Setup
Repeat Bankruptcy Cancel order Execute order Report results Make decision: • randomly for the zero intelligent traders; • around the fundamental value for the fundamentalists; • by comparison between the average short-term price and average longterm price for the traders using historical information. The evolution of stock market indicators (volatility, volume, price level, bankruptcy) in this case is described in Figure 4.
The evolution of stock market indicators (volatility, volume, price level, bankrupcy) in this case is described in Figure 5.
The evolution of stock market indicators (volatility, volume, price level, bankruptcy) in this case is described in Figure 6.

DISCUSSION
We notice that a system composed from 100% of zero intelligent traders leads to the chaos and high level of volatility of 44, and 30% of traders go bankrupt after 600 periods. The volume is decreasing consequently.
In the case where there are 10% of zero intelligent traders, they still influence the level of volatility In the last case, we are interested in simulating the impact of the relative behavior (50%) on the market stability, we notice that the imitation mechanism leads to high volatility levels (STD = 36 instead of 30) compared to a simulation without this mechanism. However, the high level of volatility didn't impact the percentage of bankrupt agent in this case.

CONCLUSION
The proposed model allows to study the behavior of the agents in the simulation of a stock market with order book. We focused on the type of behavior of the traders and its influence on the decision-making. The different simulations allowed us to obtain interesting results. We notice that the stability of the market is strongly impacted by the distribution of traders types and the introduction of imitation mechanism.
Indeed, the existence of zero intelligent traders (speculative traders) is one of the factors that can lead to the market instability and high volatility levels. The introduction of the imitation mechanism also confirms the negative impact of herding behavior on the market stability, as well as the bankruptcy rate. As a perspective of this work, we project to study the sensitivity of some stock market indicators according to the various parameters of the model, and to study the impact of the introduction of other types of traders on the stability of the stock market. Otherwise, the developed model can be generalized for the simulation of other components of the financial market and other kinds of financing. On the other hand, we can use this approach to modelling and simulate behavior of an Islamic stock market.