You are on page 1of 23

2.3.

System Architecture

2.3.1. Application, packages and components

On picture 2.1 you can see how different components and packages are
interconnected. This is a UML diagram, big rectangles are packages, small rectangles
inside them are modules. Puncture lines show how packages and modules are
connected.

Picture 2.1. Connections between various packages and


components inside the application
Main component in this diagram is the application itself (top-left rectangle). It
depends on 8 other packages:

 GLScene (http://www.glscene.org) – A set of components for Delphi (or

and Engine) that works with graphics, sound, physics and user input. T is used
in game and simulation development and other projects that require 3D
Graphics.

 Strange Components (http://www.sector-37.com) – A set of


components that extend GLScene, that I created for real-time strategy game.
Later it was enhanced and used and other projects.

 RemObjects Pascal Script (http://www.remobjects.com) – one of the

most popular scripting systems for Delphi. It helps make the system flexible
and convenient.

 FastMM (http://fastmm.sf.net) – a memory manager that allows to

allocate and free memory more effectively. It also detects memory leaks and
access violations.

 ExtLib (my version of this component - http://www.sector-


37.com/files/third_party/StrangeExtLib2.2.rar) – only the Property
inspector component is used from this package. It allows to view and change
properties of any object in real time.

 Application Specific – all those components that were created specially for the

«Financial Market Simulation» project, but were separated from the main
application to be able to use them in future projects.

This is a classical example of component-based architecture. Also in the FMS


package we see that groups of components are located in hierarchical order. First
come the FMSTypesAndConstants и FMSUtils modules, which contain types,
functions, constants that are common for every modules of this system. Then comes
mudule FMSWorld, which contains classes that describe the virtual world which is a
placeholder for all other objects. On this level nothing is known about the actual
inhabitants of the world (for more information see the TFMSWorld class
description). On the third level you can see the FMSStandardRoles module, which
defines shares, share market, roles, strategies and plans. On the next level you can see
FMSMarkets, FMSTraders, FMSTraders, FMSSimulatedActivityClasses and
FMSEventDrivenWorld. They can be considered to be add-ons to the system because
they extend standard classes to introduce trader and market-specific behavior. On the
last level you can find the FMSStatistics module, which processes various statistical
data that comes from the system. It allows building certain queries faster. So here you
see that certain parts of the system are also strictly hierarchical (layer-based
architecture).

2.3.2. Virtual World

On picture 2.2 you can see how different classes are interconnected inside the
FMSWorld module. Main class in the diagram is TFMSWorld, all other classes
represent various world settings, classificators and time.

Virtual world (TFMSWorld) controls time flow, main settings, classificators


and news generation. Time flows discreetly with a certain period. On every “tick” the
world sends an “update” signal to every object inside it. What objects do with this
time and how they update themselves does not concern the world.

Time (TFMSWorldTime) – class specially created to control the time-flow and


to cache most frequently requested time parameters.

Participant (TFMSParticipant) – is an inhabitant of the virtual world, a


company or a trader for example.

UpdateAble object (TFMSUpdateAble) – is something more general than


TFMSParticipant. This can be any object that needs periodic updates. For example, a
class that calculates share statistics that can be used by a group of traders.
Picture. 2.2. Main classes of FMSWorld unit

Role (TFMSParticipantRole) – a class that is similar to Participant. The


difference is that one participant may play different roles in the real world. For
example a trader can also own his own business and act more like a company. Also he
like any other peson is a consumer. A share market has at least two roles: it connects
different traders and is a company that hires staff and pays taxes.

Classificator (TFMSClassificator) – is a means to divide different market


participants into groups. For every group there is different news. That way it is easy
to simulate and analyze bull and bear trends.
2.3.3. Standard system classses

The standard world (unit FMSStandardRoles.pas) declares several roles


(picture 2.3):

Standard role (TFMStandardParticipantRole) – an abstract class that serves


as an ancestor to all other more complex roles. It allows to use strategies and roles
and also controls schedule of update signals via a special class Working Hours
(TFMSWorkingHours).

Picture 2.3. Main classes of FMSStandardRoles.pas unit

Trader (TFMTrader) – Buys and sells market shares via his broker, his
actions are based upon his current active strategy.
Broker (TFMSBroker) – Acts as a link between a market and a trader. Broker
receives buy/sell requests from traders, processes them, and either makes the deal
using its own shares, either redirects them to the market.

Company (TFMCompany) – Hires staff, produces goods and/or services,


emits shares and pays dividends to shareholders. Company stores information about
its economic value, incomes and expenses.

Share market (TFMSMarket) – Receives transaction requests from brokers,


finds matching requests and commits transactions.

Unit FMSStandardRoles.pas contains other useful classes:

Strategy (TFMSStrategy) – abstract class that serves as an ancestor to all other


participant-specific strategies. Market participant uses a strategy to imitate
intelligence. That way all participant’s algorithms are divided into two categories:
common (participant level) and participant specific (strategy level). Each individual
participant can evaluate the effectiveness of his strategy and choose one that allows
him to earl the most money in the current market situation. It is clear that traders,
brokers and companies have different groups of strategies. A strategy also has a
separate method to process messages of active plans that are linked to it
(DoProcessActiveLinkedPlan).

Plan (TFMSPlan) – abstract class that serves as an ancestor to other more


complex plans. All participants can use plans to schedule tasks that need to be done
some time in the future, depending on a certain condition. Plans are often linked with
strategies to control the effectiveness of plans.

Transaction order (TFMSShareTransactionRequest) – traders and brokers


use this class to express their interest in buying or selling shares. Sender of this
request has to specify name of the share, operation type, number of shares and
preferably the strategy that was used to issue this transaction request. Each order has
a certain period of lifetime after which it is returned to the sender with a tag that it
was not processed (or partially processed).
Transaction (TFMSShareTransactiont) – This is a process when a number of
shares change its owner (are sold from one trader or broker to another). Market
creates a transaction only when it finds opposite transaction requests that have
matching or overlapping prices. So a transaction can be complete at a better price
than user originally requested.

2.3.4. Trader Strategies

Standard trader strategy (TFMSStandardTraderStrategy) – this class


predefines several methods and common properties that can be used by derived
strategies and have any unique algorithms. Other three strategies contain original
procedures that imitate traders’ decision making (picture 2.4). They are the main
object for analysis of this work.

Market maker trader strategy (TFMSMarketMakerStrategy) – traders that


use this strategy, tend to buy and sell the same share type at the same moment of time
(price at which they sell is higher that than the price at which they buy). So they earn
their money on the spread the same way banks do when they exchange currencies.
Their main purpose in the system is to maintain market liquidity, so there is always
someone selling and buying each and every share type on the market. They also
regulate share prices. If they sell / buy a lot of shares at the given price, they increase
the spread, making next exchange requests less profitable for all other traders. An
vice versa, if they are not selling / buying anything at the current price, they lower the
spread, thus earning less money from each individual transaction, but increasing total
transactions count altogether.
Picture 2.4. Standard trader strategies

Newbie trader strategy (TFMSTraderRandomBuyStrategy) – trader randomly


selects a share type and sells or buys it, thus creating “noise” in the system.Thais
strategy can be used to imitate amateur traders and as a template for more advanced
strategies. When choosing a price at which he wants to buy / sell shares, newbie
trader has a choice: either to agree to the current market bid or ask price (which is
probably of market maker’s) or to choose a price for himself somewhere within the
spread. In the first case his order will be most likely completed, but at a rather high
price. In the second case no one can guarantee that his order will succeed. He has to
hope that there will be other people like him that will choose to “fight the market
price”. If he is lucky, his transaction request will be completed at a better price that
the market offered. His actual choice will depend on a parameter that controls his
tolerance to risk.

Fundamental trader strategy (TFMSTraderFundamentalStrategy) – trader


believes that there should be some «fundamental» price, around which the share price
changes. This fundamental price reflects the real value of the company and the
“should be” share price, not the market price, which can be artificially lowered (or
made higher) by insiders and false news reports. So this trader will buy shares when
the market price is lower than this fundamental price and sell shares when the market
price is higher that the fundamental price. For the sake of clarity traders calculate this
price as the price of company’s net assets divided by the number of emitted shares.
Theoretically more complex algorithms can be used to calculate this price, for
example taking into account latest news about this company or the long-term market
trend.

2.3.5. Requests and transactions

Transactions play a crucial role in the system. In order to succeed, there need
to be two transaction requests (orders) on this share with the same price. Of course,
one request should be a sell order and the other – a buy order.
1. Trader
sends a
request to a 2. Broker processes his request
broker and either completes it himself,
or sends it to the share market

5. Trader analyzes this


information and makes
3. Market finds
conclusions based on his
matching requests
strategy
and commits
transactions

4. Broker receives a
reply from the market
and redirects it to the
trader

Picture 2.5. Transaction requests

Transaction requests can be sent from any market participant, but the most
popular route is shown on picture 2.5:

1. Trader uses his currently selected strategy to think, makes a decision and sends
a transaction request to the broker.

2. Broker processes his request and either rejects it (not enough money on the
account), or completes it himself, or sends it to the share market.
3. Market finds matching requests and commits transactions. Each order has a
certain life time. If it hasn’t been completed during this time, it is returned to
the sender with a “failed” tag.
4. Broker receives a reply from the market and redirects it to the trader
5. Trader analyzes this information and makes conclusions based on his strategy.
As you can see, all economic parameters including share prices and trade
volumes are calculated without using special statistical formulas, mathematical
models or random number generators. They are based only on the information about
the transactions that agents make on the virtual market.
Part III. Experiments with the virtual market

3.1. A brief description of the program

You can find detailed manual inside the package of the program. In this section
I will only describe the basics. On picture 3.1 you can see a screenshot of the
program. It displays all types of editors and unities that are available to the end-user.

Picture 3.1. Graphic user interface of the program

Here are brief instructions on how to use this application:

1) Launch the program.


2) Open the scenario editor via the "Editors" menu.
3) Choose one of the demo scenarios from the list box.
4) Customize its parameters if you want to.
5) Press the "Apply" button to compile the script and make it active.
6) Launch the simulation by pressing the “Start” button in the "Controls" menu.
7) Open the Array analyzer via the "Editors" menu. Open one of the demo scripts
from the list box inside it.
8) Press the "Apply" button, to compile the script and make it active. Wait till the
chart begins drawing lines. Hey will show different economic parameters
depending on which script you selected. You can also open the “Statistical
information” panel inside the Array analyzer.
9) In the same manner you can open and tweak other panels that can help analyze
statistical data that comes from the virtual market.

3.2. Criteria by which we could analyze the system


We created an application that can imitate the work of a real share market.
Now we need to determine how close the model is to the real world. The best criteria
would be a practical experiment where we make virtual market completely identical
to the real market (as it was on a certain date in the past), input all major events that
influenced the market, then we launch the simulation and compare the results. In my
opinion, parameters of the real market would be pretty close to the virtual market.
But the real problem is that it is virtually impossible for one man to organize such an
experiment. He would have to input information about all the companies on the
market, all major news that happened before and during the simulation and try to
guess the ratio of different trader types. I believe this data could be gathered and
inputted in the system, but it is too much work for a single person. I will cover this
subject more closely when I get to the subject of application’s commercial
perspectives. Meanwhile I will focus on other ways to analyze its work.

3.2.1. Statistical criteria

In this paper I analyzed several statistical parameters of the system:

1) The ratio of share type, broker and trader numbers on the market.
2) Visual similarity of the trend.
3) Share price patterns: head and shoulders, topping patterns, etc.

4) Distribution of share prices.


5) Volume of trade.
6) Number of transactions per day.
7) Spread.
8) Reaction to positive and negative new.

During the coarse of my research I will compare virtual and real markets based
on these criteria and make a conclusion whether the virtual market is adequate.

Picture 3.2. Average, maximum and close price of UCOS’s shares


from 25.05.2005 to 19.09.2006

3.2.3. Real market parameters

I needed to compare the virtual and the real market. So first I decided to
analyze the Russian stock market. I compared average, maximum, minimum, open,
close prices and trader volumes for companies RAO EES, UCOS and Kamaz during
different time periods: from one month to three years. Companies are very different
which in some way guaranteed that I will be able to find criteria that are similar to all
of them. Share prices of RAO EES were very stable all the time, UCOS’s and
Kamaz’s shares were in some periods quite unstable.

It’s quite obvious that such parameters as average, dispersion and covariation
were completely different in these cases. The only parameter that was almost the
same is share price distribution (picture 3.2). It has a noticeable peak in the middle
that can be closer to one or the other edge. The peak can also lean to the right side, so
this side can appear edgier then the left side. I also noticed that the less elements in
the input array, the lower the peak (picture 3.3).

Picture 3.3. Distribution of trade volume of «KAMAZ»


from 19.02.2007 to 25.05.2007

Note: in this work when I speak about share price or volume distribution, I
actually mean the distribution of changes. So for every input array X which has N
elements I create a new array Y that has (N-1) elements. In this new array for every
element index “i” (0 < i < n – 1) I put values (x[i + 1] – x[i]). And after that I find the
distribution of the new array Y. This is a standard way to analyze stock and FOREX
markets. Sometimes every element of the resulting array is also run through a log()
function, but that does not change how the array visually looks, only the values. So I
won’t be doing it in this work.

Distribution chart looks almost the same for prices as well as trade volumes, so
I will be paying a lot of attention to it during this research. Note, that I will only
analyze its appearance, so my opinion will be subjective.

3.2.2. Comparing virtual and real markets

At first we need to determine the initial parameters of the system. You can
view them in table 3.1 (is market data is based on the New-York and Thailand stock
markets.

In a virtual market each trader can own 0 to 2 share portfolios, each one costs 2
to 4 thousand dollars. Also everybody can have 2 to 7 thousand dollars on their
broker’s account. Also a trader has 500 to 5000 dollars on his own account, which
can be used in extreme circumstances.

The choice of initial parameters of a virtual market makes sure that the virtual
market is a reduced copy of an average real market. This in some way guarantees that
virtual market will behave similar to the real market during the experiments.

Table 3.1

Comparing virtual and real markets

Parameter name Read market Virtual market


Number of markets in the world 30..200 (around 50) 1
Number of share types per market 500...2000 (around 1000) 10
Brokers per market 20...1500 (around 100) 5
Traders (per broker) 10-50 k. (around 15 k.) 100
Traders (total) around 150 k. 500
Transactions (total per day) 118 k...8 b. 1 000
Transactions (for 1 share / day) 1-100 k. (around 1 k.) 100
Transactions (for 1 trader / day) 0.1-10 (average 2) 2
On each market there are at least 5 market-maker traders
(TFMSTraderMarketMakerStrategy strategy). I described this strategy in detail when
I covered the subject of architecture of the system and its main classes. I remind you
that their main purpose for the system is to maintain market liquidity and price
balancing.

3.3. Experiments with the virtual market

3.3.2. Fundamental Traders and their influence


on share price changes

Experiment is based on the «FundamentalTraders.fms» scenario. Its goal is to


determine how fundamental traders’ actions can influence share price changes.

The participants of this experiment are: 2 market market-makers, 50 newbie


traders, 450 fundamental traders. I described the “TFMSTraderFundamentalStrategy”
strategy in detail, in the “System Architecture” part of this work. I’ll remind you that
the main goal of fundamental traders in the system is to make the share price follow
some predefined “want-to-be” price. There are several events in this scenario: at first
the company increases its assets, then it decreases them very quickly, and in the end
of this 3-month experiment it again increases them. Traders’s fundamental price is
direct linked with companies’ assets, so we can expect that the market price will be
influenced by traders’ expectations and somehow be linked to the fundamental price
too.
Picture 3.8. Minimal, maximum and average price changes in the
«FundamentalTraders.fms» scenario

On picture 3.8 we can see how minimal, maximum and average price changed.
At first the price rises, then falls at about the same rate. In the end it rises again. So in
general the price behaves just like we expected, i.e. it follows the fundamental price.
But the changes in market price were less than changes in fundamental price. Market
price turned out to be more predictable and stable than the real value of companies.
This may be how things really are in real markets too, despite the common belief that
company values tend to be constant and prices jump up and down because of major
traders’ games. Also this fact can have a simpler explanation: market-makers don’t do
their job too good and can’t update share prices fast enough. Also the virtual market
consists of about 15% newbie traders that don’t care at what price they will buy or
sell shares.
Picture 3.9. Last transaction price and fundamental price change
in the «FundamentalTraders.fms» scenario

On picture 3.9 you can see both last transaction price and fundamental price
change (placed on the same chart to make the spread more clear). You can see how
the spread dramatically decreases after fundamental price became less than market
price. As soon as fundamental price became more than the market price, spread again
increased. This phenomenon is caused by the absence of short orders in the system.
In the real market tradercan and most likely will loan shares from the broker,
promising to buy them back if he is sure that their price will fall. He sells these shares
at a current high price, waits some time and buys them back at a lower price thus
making profit. As you can see on the real market traders can influence market price
changes even if the price is going down.

Currently, the developed system does not support short orders, so the only way
traders can influence drop of share price is to just stop buying it. As you can see, this
mechanism is not working too well: share price is lowering not as fast is it was rising.
I believe this is the fault of market-maker traders; their algorithm of “thinking” is not
flexible enough. Я это считаю недостатком, прежде всего, алгоритма работы
маркет-мейкеров.

Picture 3.10. Distribution of share prices in the


«FundamentalTraders.fms» scenario

Share price distribution (picture 3.10) is very close to distributions on the real
markets. Only the average price distribution has a rather low peak value. Not sure
what could have caused it. Also the last transaction price has a very high peak. It may
have been caused by high spread value or a very high number of transactions (around
1000). It’s a well known fact – the longer the initial array, the higher peak on it’s
distribution. Almost flat distribution of fundamental price is an expected result,
because it is generated from company’s assets, which change based on a simple
algorithm that uses the standard Delphi random number generator.
Picture 3.11. Change of trade volume
in the «FundamentalTraders.fms» scenario

On the trade volume chart (picture 3.11) everything is like expected. It starts at
a rather high level, because the fundamental price begins to grow quickly and all
traders start buying them at once. This trend slows down and close to the middle you
can see a peak – all fundamental traders try to get rid of overpriced share at once.
Then the trade volume is quite low, because no one wants to invest in this share.
Closer to the end, the share again becomes attractive and at this point we can see the
biggest peak of trade volume – all traders want to invest in it. The chart of
distribution of trade volume (picture3.12) is ideal – just like on real markets, so I
don’t have any comments to add about it.
Picture 3.12. Distribution of trade volume
in the «FundamentalTraders.fms» scenario

The most interesting part in this experiment to me is that the fundamental price
could have been any value. We could take short-term assets and debt into account like
traders do during fundamental analysis, or we could just randomly guess it. The main
conclusion of this experiment is that if all traders believe that company costs some
amount of money – it’s shares eventually will cost that amount. So the real market
price of shares always follows traders’ expectations with a certain time gap.

So this comes in some contradiction with efficient market theory. According to


this theory, the market price always reflects the economic state of a company. It is the
“real” price of a company in the current time period. But in this model the market
price may of may not reflect the price of a company – it all depends on traders, their
belief (disbelieve) and trust (mistrust) in this company.

Also you can notice that the spread is too big compared to real market
(pictures 3.7, 3.8). I believe it is due to the small relative number of market makers or
a fault of their algorithm of thinking. In any way, this problem theoretically can be
fixed during further development of the application. It does not prove that the system
is inadequate, but rather not tuned correctly.

You might also like