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 and “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) – and 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. 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.This 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.

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.

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.

 

Picture 2.5. Transaction requests