Transcript
Page 1: FiQuant Market Microstructure Simulator: Strategy Definition Language

Market Microstructure Simulator: Strategy DefinitionLanguage

Anton Kolotaev

Chair of Quantitative Finance, Ecole Centrale Paris

[email protected]

March 5, 2014

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 1 / 63

Page 2: FiQuant Market Microstructure Simulator: Strategy Definition Language

Overview

1 IntroductionHistoryDesignDSL

2 Strategy Definition LanguageFunctionsTypesClassesPackages

3 StrategiesPosition strategiesSide strategiesPrice strategiesAdaptive strategies

4 Simulator components

5 GUIs: Veusz and Web

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 2 / 63

Page 3: FiQuant Market Microstructure Simulator: Strategy Definition Language

Evolution of the simulator I

1 Initial C++ version was developed in 2009-2011 by Riadh Zaatour. Inthis version a user implements strategy logic in C++. Though thisversion was quite easy to learn and understand, it had problems withflexibility and scalability.

2 In order to improve its extensibility and performance the simulatorwas rewritten using C++ template metaprogramming techniques byAnton Kolotaev in 2012. Python bindings to it were implementedusing Boost.Python. Unfortunately the price for providing highextensibility with no overhead was quite high: in order to use it aproficiency in C++ template metaprogramming was required.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 3 / 63

Page 4: FiQuant Market Microstructure Simulator: Strategy Definition Language

Evolution of the simulator II

1 In order to make the simulator easy to start work with, a Pythonversion with a Web interface was developed in 2013. Karol Podkanskiimplemented number of trading strategies and indicators during hisinternship at summer 2013. Though this version gave a lot of insightson how a good modular design for market simulation software shouldbe implemented, it showed that a lot of syntax noise appears instrategy description and the development becomes very error-pronebecause of the dynamic typing nature of Python.

2 In October 2013 a decision to introduce a strategy definition languageand a compiler for it was taken.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 4 / 63

Page 5: FiQuant Market Microstructure Simulator: Strategy Definition Language

Design goals

1 Flexibility. A simulation library must have a very modular design inorder to provide a high level of flexibility to the user. Thisrequirement comes from the original purpose of a simulation as a testbed for experiments with different models and parameters.

2 Used-friendliness. Since a typical simulation model is composed ofmany hundreds and thousands blocks, it is very important to providea simple way for user to tell how a behaviour wanted differs from thedefault one. Simulator API should be friendly to modern IDEs

3 Performance. A user should be allowed to choose between smallstart-up time and high simulation speed.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 5 / 63

Page 6: FiQuant Market Microstructure Simulator: Strategy Definition Language

Modular Design

Representing a simulation model as a network of modules communicatingby messages is a widely accepted practice for DES systems (e.g.Omnet++ or ns-2 to simulate telecommunication networks).Module may have parameters that are used to adjust their behaviour.Modules are organized into hierarchy in order to facilitate construction oflarge-scale simulation.We will distinguish two sorts of modules:

1 Simple modules provide functionality which is considered elementary(and there is no reason to reuse part of it to implement othermodules).

2 Compound modules combine together other modules in some wayand define their parameters based on its own parameters. Compoundmodule behaviour is just a composition of its constituting modulesbehaviours.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 6 / 63

Page 7: FiQuant Market Microstructure Simulator: Strategy Definition Language

Modular design sample: Crossing Averages Strategy

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 7 / 63

Page 8: FiQuant Market Microstructure Simulator: Strategy Definition Language

Motivation for an external DSL

1 Syntax. We may design an external DSL so its syntax describes verywell domain specific abstractions.

2 Error checking. A DSL compiler can detect incorrect parametervalues as soon as possible thus shortening simulation developmentcycle and facilitating refactorings.

3 Multiple target languages. A DSL can be compiled into differentlanguages: e.g. into Python to provide fast start-up and into C++ tohave highly optimized fast simulations. A new target languageintroduced, only simple modules need to be re-written into it;compound modules will be ported automatically.

4 IDE support. Modern IDEs like Eclipse, Intellij IDEA allow writingplug-ins that would provide smart syntax highlighting,auto-completion and check errors on-the-fly for user-defined DSLs.

5 High-level optimizations are easier to implement.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 8 / 63

Page 9: FiQuant Market Microstructure Simulator: Strategy Definition Language

Scala as a language to implement the DSL compiler

1 As a ML-family member, very suitable for sophisticated symbolprocessing tasks like compilation: an internal DSL to parse texts,algebraic data types, pattern matching, a powerful collections library,mixin inheritance via traits.

2 Good balance between functional and imperative programmingfeatures – easier to find software engineers

3 The most mainstream of functional programming languages thatresults in wide community, excellent library and tool support, matureIDEs.

4 Very portable since runs on JVM.

5 Statically typed

6 Right choice to develop a Web server.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 9 / 63

Page 10: FiQuant Market Microstructure Simulator: Strategy Definition Language

Functions

Conceptually, a ”function” declaration defines a term with a constructorprovided (like case classes in Scala).Types for input arguments are inferred automatically from their initializersand for the ”return” value - from the function body.Functions represent compound modules of a simulation model.

All methods can be considered as extension methods of their firstargument.x∼>Lagged(dt) is equivalent to .observable.Lagged(x, dt).

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 10 / 63

Page 11: FiQuant Market Microstructure Simulator: Strategy Definition Language

Intrinsic Functions

Intrinsic functions import simple modules from a target language into theDSL. ”Return” types for intrinsic functions must be specified explicitly.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 11 / 63

Page 12: FiQuant Market Microstructure Simulator: Strategy Definition Language

Type System I

Types correspond to interfaces without methods from mainstreamlanguages. They are used for error checking and for function overloading.

1 Simple types may derive from other types or be aliases

2 Tuple and function types

3 Top (Any) and bottom (Nothing) types.

4 Lists (List[T])

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 12 / 63

Page 13: FiQuant Market Microstructure Simulator: Strategy Definition Language

Type System II

Types may be generic.Functions are contravariant in the input type and covariant in the outputtype: CanCast(D,B) ∧ CanCast(R,T) ⇒ CanCast(B=>R,D=>T).All other types are covariant.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 13 / 63

Page 14: FiQuant Market Microstructure Simulator: Strategy Definition Language

Classes I

Classes are syntax sugar for a type declaration, constructor function andmember accessors

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 14 / 63

Page 15: FiQuant Market Microstructure Simulator: Strategy Definition Language

Classes II

Previous definition is de-sugared at typing stage into

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 15 / 63

Page 16: FiQuant Market Microstructure Simulator: Strategy Definition Language

Class Inheritance

Classes derive fields and methods from base classes.Methods are treated as ”virtual” to stimulate code re-use

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 16 / 63

Page 17: FiQuant Market Microstructure Simulator: Strategy Definition Language

Packages and Attributes

Packages are used to group functions and types. They can be nested.Attributes are inherited from enclosing package. Anonymous packages areused to assign same attributes to a group of functions without introducinga new name scope.

In this sample .A.B.f will have attributes X == "Xa", Y == "Y" and.A.B.g and .A.B.h will have attributes X == "Xb", Y == "Y"

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 17 / 63

Page 18: FiQuant Market Microstructure Simulator: Strategy Definition Language

Example: Relative Strength Index

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 18 / 63

Page 19: FiQuant Market Microstructure Simulator: Strategy Definition Language

Relative Strength Index Strategy

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 19 / 63

Page 20: FiQuant Market Microstructure Simulator: Strategy Definition Language

Bollinger Bands Strategy

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 20 / 63

Page 21: FiQuant Market Microstructure Simulator: Strategy Definition Language

Side Strategies

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 21 / 63

Page 22: FiQuant Market Microstructure Simulator: Strategy Definition Language

Signal Strategy I

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 22 / 63

Page 23: FiQuant Market Microstructure Simulator: Strategy Definition Language

Signal Strategy II

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 23 / 63

Page 24: FiQuant Market Microstructure Simulator: Strategy Definition Language

Trend Follower Strategy I

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 24 / 63

Page 25: FiQuant Market Microstructure Simulator: Strategy Definition Language

Trend Follower Strategy II

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 25 / 63

Page 26: FiQuant Market Microstructure Simulator: Strategy Definition Language

Crossing Averages Strategy I

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 26 / 63

Page 27: FiQuant Market Microstructure Simulator: Strategy Definition Language

Crossing Averages Strategy II

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 27 / 63

Page 28: FiQuant Market Microstructure Simulator: Strategy Definition Language

Fundamental Value Strategy I

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 28 / 63

Page 29: FiQuant Market Microstructure Simulator: Strategy Definition Language

Fundamental Value Strategy II

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 29 / 63

Page 30: FiQuant Market Microstructure Simulator: Strategy Definition Language

Mean Reversion Strategy I

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 30 / 63

Page 31: FiQuant Market Microstructure Simulator: Strategy Definition Language

Mean Reversion Strategy II

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 31 / 63

Page 32: FiQuant Market Microstructure Simulator: Strategy Definition Language

Pair Trading Strategy I

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 32 / 63

Page 33: FiQuant Market Microstructure Simulator: Strategy Definition Language

Pair Trading Strategy II

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 33 / 63

Page 34: FiQuant Market Microstructure Simulator: Strategy Definition Language

Liquidity Provider Strategy

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 34 / 63

Page 35: FiQuant Market Microstructure Simulator: Strategy Definition Language

Market Data Strategy I

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 35 / 63

Page 36: FiQuant Market Microstructure Simulator: Strategy Definition Language

Market Data Strategy II

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 36 / 63

Page 37: FiQuant Market Microstructure Simulator: Strategy Definition Language

Market Maker Strategy I

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 37 / 63

Page 38: FiQuant Market Microstructure Simulator: Strategy Definition Language

Market Maker Strategy II

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 38 / 63

Page 39: FiQuant Market Microstructure Simulator: Strategy Definition Language

Trade-If-Profitable Strategy I

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 39 / 63

Page 40: FiQuant Market Microstructure Simulator: Strategy Definition Language

Trade-If-Profitable Strategy II

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 40 / 63

Page 41: FiQuant Market Microstructure Simulator: Strategy Definition Language

Choose-the-best strategy

Backtests aggregated strategies and allows to run only to that one whohas the best performance. By default, first derivative of a moving averageof ’cleared’ trader’s balance is used to evaluate the efficiency.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 41 / 63

Page 42: FiQuant Market Microstructure Simulator: Strategy Definition Language

Multiarmed Bandit Strategy I

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 42 / 63

Page 43: FiQuant Market Microstructure Simulator: Strategy Definition Language

Multiarmed Bandit Strategy II

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 43 / 63

Page 44: FiQuant Market Microstructure Simulator: Strategy Definition Language

Arbitrage Strategy

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 44 / 63

Page 45: FiQuant Market Microstructure Simulator: Strategy Definition Language

Simulator components

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 45 / 63

Page 46: FiQuant Market Microstructure Simulator: Strategy Definition Language

Scheduler

Main class for every discrete event simulation system.

Maintains a set of actions to fulfill in future and launches themaccording their action times: from older ones to newer.

Interface:

Event scheduling:

schedule(actionTime, handler)

scheduleAfter(dt, handler)

Simulation control:

workTill(limitTime)

advance(dt)

reset()

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 46 / 63

Page 47: FiQuant Market Microstructure Simulator: Strategy Definition Language

Events

event.Event. Base class for multicast events.

event.Conditional. Multicast event class with conditional eventssupport. Allows effective notification mechanism for collections ofevent handler of form event.GreaterThan(x, listener) andevent.LessThan(x, listener). Internally it keeps two dequeuesof event handlers sorted by their trigger values and notifies only thosehandlers whose trigger conditions are fullfilled.

event.GreaterThan(x, listener). Fires listener only if thevalue observed is greater than x.

event.LessThan(x, listener). Fires listener only if the valueobserved is less than x.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 47 / 63

Page 48: FiQuant Market Microstructure Simulator: Strategy Definition Language

Order book

Represents a single asset traded in some market (Same asset tradedin different markets would be represented by different order books)

Matches incoming orders

Stores unfulfilled limit orders in two order queues (Asks for sell ordersand Bids for buy orders)

Corrects limit order price with respect to tick size

Imposes order processing fee

Supports queries about order book structure

Notifies listeners about trades and price changes

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 48 / 63

Page 49: FiQuant Market Microstructure Simulator: Strategy Definition Language

Order book for a remote trader

Models a trader connected to a market by a communication channelwith non-negligible latency

Introduces delay in information propagation from a trader to an orderbook and vice versa (so a trader has outdated information aboutmarket and orders are sent to the market with a certain delay)

Assures correct order of messages: older messages always come earlierthan newer ones

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 49 / 63

Page 50: FiQuant Market Microstructure Simulator: Strategy Definition Language

Basic orders

Orders supported internally by an order book:

Market(side, volume)

Limit(side, price, volume)

Cancel(limitOrder)

Limit and market orders notifies their listeners about all trades they takepart in. Factory functions are usually used in order to create orders.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 50 / 63

Page 51: FiQuant Market Microstructure Simulator: Strategy Definition Language

Meta orders I

Follow order interface from trader’s perspective (so they can be usedinstead of basic orders) but behave like a sequence of base orders from anorder book point of view.

Iceberg(volumeLimit, underlyingFactory) splits orders createdby underlyingFactory to pieces with volume less thanvolumeLimit and sends them one by one to an order book ensuringthat only one order at time is processed there

FloatingPrice(price, underlyingFactory) listens to anobservable price and when it changes, cancels its order on themarkets and resends it with the new price.

Peg(underlyingFactory) creates a limit-like order with givenvolume and the most attractive price, sends it to an order book and ifthe order book best price changes, cancels it and resends with abetter price. Implemented via FloatingPrice.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 51 / 63

Page 52: FiQuant Market Microstructure Simulator: Strategy Definition Language

Meta orders II

WithExpiry(lifetime, underlyingFactory) sends a limit-likeorder and after lifetime cancels it

ImmediateOrCancel(underlyingFactory) is like WithExpiry butwith lifetime equal to 0 making a limit order to act as a conditionalmarket order

StopLoss(lossFactor, underlyingFactory) order is initialisedby an underlying order and a maximal acceptable loss factor. It keepstrack of position and balance change induced by trades of theunderlying order and if losses from keeping the position exceed certainlimit (given by maximum lossFactor), the meta order clears itsposition.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 52 / 63

Page 53: FiQuant Market Microstructure Simulator: Strategy Definition Language

Traders

Single asset traders

send orders to order books

bookkeep their position and balance

run a number of trading strategies

notify listeners about trades done and orders sent

Single asset traders operate on a single or multiple markets. Multiple assettraders are about to be added.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 53 / 63

Page 54: FiQuant Market Microstructure Simulator: Strategy Definition Language

Observable

Traders and order books provide basic accessors to their current state butdon’t collect any statistics. In order to do it in an interoperable way anotion of observable value was introduced: it allows to read its currentvalue and notifies listeners about its change. Examples of observables are:

on traders: position, balance, market value of the portfolio, ’cleared’balance etc.

on order books: ask/mid/bid price, last trade price, price at volume,volume of orders with price better than given one etc.

OnEveryDt(dt, dataSource) evaluates dataSource every dt

moments of time.

History of an observable can be stored in a TimeSerie and rendered lateron a graph.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 54 / 63

Page 55: FiQuant Market Microstructure Simulator: Strategy Definition Language

Using Veusz

When developing a new strategy it is reasonable to test it using scriptsand visualize results by Veusz

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 55 / 63

Page 56: FiQuant Market Microstructure Simulator: Strategy Definition Language

Rendering graphs by Veusz

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 56 / 63

Page 57: FiQuant Market Microstructure Simulator: Strategy Definition Language

Web interface

Web interface allows to compose a market to simulate from existingobjects and set up their parameters

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 57 / 63

Page 58: FiQuant Market Microstructure Simulator: Strategy Definition Language

Time series

Timeseries field of a trader or an order book instructs what data should becollected and rendered on graphs

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 58 / 63

Page 59: FiQuant Market Microstructure Simulator: Strategy Definition Language

Rendering results

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 59 / 63

Page 60: FiQuant Market Microstructure Simulator: Strategy Definition Language

Node aliases

Object tree nodes can be assigned aliases that can be used later to refer tothe sub-tree (explicit by-value or by-reference cloning semantics is to be

implemented)

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 60 / 63

Page 61: FiQuant Market Microstructure Simulator: Strategy Definition Language

Workspaces

Every user (identified by browser cookies) may switch between multipleworkspaces. Workspaces can be forked, removed or created from a set of

predefined ones.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 61 / 63

Page 62: FiQuant Market Microstructure Simulator: Strategy Definition Language

Installation

OS supported: Linux, Mac OS X, Windows

Browsers supported: Chrome, Firefox, Safari, Opera

Scala 10.2 can be installed using SBT

Python 2.7

Python packages can be installed using pip or easyinstall:

Veusz (for graph plotting)Flask and Docutils (to run a Web-server)Blist (sorted collections used by ArbitrageTrader)Pandas (only needed for observable.Quotes at the moment)Numpy (only needed for strategy.MultiArmedBandit at themoment)

Source code downloadable from GitHub

Latest public version of the simulator can be downloaded from here.

Online documentation can be found here.

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 62 / 63

Page 63: FiQuant Market Microstructure Simulator: Strategy Definition Language

Thank you!

Anton Kolotaev (ECP) FiQuant Market Simulator March 5, 2014 63 / 63


Top Related