Professional Documents
Culture Documents
Portfolio Modeling For An Algorithmic Trading
Portfolio Modeling For An Algorithmic Trading
Portfolio Modeling For An Algorithmic Trading
2018
Ruiz-Cruz, Riemann
Ruiz-Cruz, Riemann (2018). Portfolio modeling for an algorithmic trading based on control theory,
IFAC-PapersOnLine, 51(13): 390-395. https://doi.org/10.1016/j.ifacol.2018.07.310.
Este documento obtenido del Repositorio Institucional del Instituto Tecnológico y de Estudios Superiores de
Occidente se pone a disposición general bajo los términos y condiciones de la siguiente licencia:
http://quijote.biblio.iteso.mx/licencias/CC-BY-NC-2.5-MX.pdf
Abstract: In the present paper, a mathematical model for a portfolio is proposed. This model
is valid for operations of buying and selling shares of an asset in constant periods of time,
additionally, it has a state space form which can be used to design a control law using control
theory. The designed control law can be interpreted as a trading signal to reach a portfolio value
desired. The mathematical model and control law proposed are validated by means simulations
using real daily prices of Mexican stock exchange.
In a financial market, it is normal to carry out stock Assuming that it is not permitted to ask for a loan to invest
purchase and sale operations, driven by the interest of and neither short sales, the number of shares available to
increasing the value of the portfolio. A purchase transac- buy can be bounded as follows:
tion means to exchange money for contracts that hold m(k)
us as owners of the shares, and correspondingly a sale 0 ≤ nam(k) ≤ f loor , (13)
(1 + rcom )p(k)
operation means to change the shares for their value in
and the number of shares available to sale is bounded by:
money. Unfortunately, to operate in a financial market,
there are intermediaries who charge commissions for the 0 ≤ nam(k) ≤ na(k) . (14)
use of platforms, information and, investment advice. That
commission usually varies depending on the amount or 2.2 Generalizing the model
type of stocks or assets being traded. In this paper, rcom
is used as the percentage of the intermediary charges for From (11) and (12), it can be seen that the commission
the operations that are carry out. (rcom ) appears in a buy or sale operation. So, this commi-
ssion can be considered as an uncontrollable known per-
In order to simplify the development of the model proposed turbation in the system. Then, the buy and sell operation
in this paper, it is considered that there is an amount m to can be rewritten as follows:
be invested in a single type of asset through the purchase
of na shares. Then, if is adquired a number na of shares Buy:
at a price p, the amount that has to be paid (real cost) is
defined as: m(k+1) = m(k) − p(k) nam(k) − rcom p(k) nam(k) ,
real cost = (1 + rcom )p × na . (4) na(k+1) = na(k) + nam(k) , (15)
Sell: T
na1(k) na2(k) . . . naq(k) is a shares number vector,
T
M(k) = m1(k) m2(k) . . . mq(k) is a vector with the
m(k+1) = m(k) + p(k) nam(k) − rcom p(k) nam(k) ,
amount to invest in each asset, U(k) = u1(k) u2(k) . . . uq(k)
na(k+1) = na(k) − nam(k) . (16) is the control signal vector, or trading signal vector, for
each asset.
So far, two operation conditions have been considered
separately. A unique model can be constructed by adding
a variable that indicates whether the stock is a buy or sell, 3. TRADING ALGORITHM BASED ON CONTROL
αcv , which is defined as: THEORY
1 , Buy
αcv = . (17)
−1 , Sell In the section 2, the procedure to obtain a generalized
model for a portfolio composed of an amount to invest
Then, the equations (15) and (16) can be expresed as an m(k) in a na(k) shares is presented. Based on the model
unique model for both operations. proposed, a trading algorithm is designed aplying control
theory.
m(k+1) = m(k) − p(k) αcv nam(k) − rcom p(k) nam(k) ,
na(k+1) = na(k) + αcv nam(k) . (18)
3.1 Trading algorithm design
It can be seen that the proposed dynamic model is directly
influenced by buying and selling operations (αcv value) and From (20), the vp at time k + 1 can be calculated as:
the number of shares that are required to operate (nam(k) vp(k+1) = p(k+1) na(k+1) + m(k+1) , (22)
value). Then, if u(k) = αcv nam(k) is defined, the system can
explicitly have a control signal that alters their behavior. then using na(k+1) and m(k+1) from (20), the vp(k+1) is
This control signal can be seen as a trading signal where obtained as
the sign and magnitude of u(k) corresponds to a decision
of purchase or sale and, the number of shares respectively. vp(k+1) = p(k+1) (na(k) + u(k) ) + m(k) − p(k) u(k)
−rcom (p(k) ku(k) k),
m(k+1) = m(k) − p(k) u(k) − rcom p(k) ku(k) k,
vp(k+1) = p(k+1) na(k) + m(k) + (p(k+1) − p(k) )u(k)
na(k+1) = na(k) + u(k) . (19)
−rcom (p(k) ku(k) k). (23)
Including the portfolio value vp , the complete model pro-
posed is defined as: Considering that the future price p(k+1) can be obtained
from the actual price p(k) adding an increment ∆p(k) ,
vp(k) = p(k) na(k) + m(k) ,
p(k+1) = p(k) + ∆p(k) , (24)
m(k+1) = m(k) − p(k) u(k) − rcom p(k) ku(k) k,
then,
na(k+1) = na(k) + u(k) . (20)
vp(k+1) = (p(k) + ∆p(k) )na(k) + m(k) + ∆p(k) u(k)
The model (20) has the form of a time variant state space
model as follows: −rcom p(k) ku(k) k,
vp(k+1) = p(k) na(k) + ∆p(k) na(k) + m(k) + ∆p(k) u(k)
y(k) = C(k) x(k) ,
−rcom p(k) ku(k) k,
x(k+1) = Ax(k) + B(k) u(k) − g(u(k) ),
T vp(k+1) = vp(k) + ∆p(k) na(k) + ∆p(k) u(k)
where y(k) = vp(k) , x(k) = m(k) na(k) , C(k) = −rcom p(k) ku(k) k. (25)
T
1 p(k) , A = I2×2 , B(k) = −p(k) 1 , and g(u(k) ) =
rcom p(k) ku(k) k. In (25), it can be seen that the future portfolio value
vp(k+1) depends directly on the actual portfolio value vp(k) ,
In general the model (20) can be rewritten for a portfolio
the share price p(k) , the price increment ∆p(k) , the current
that considers more than one asset to invest as follows:
shares number na(k) and u(k) value.
Vp(k) = P(k) Na(k) + M(k) , Usually, the objective of an investment is to have a
portfolio that reaches a target value (vpref ) or desired
M(k+1) = M(k) − P(k) U(k) − rcom P(k) kU(k) k,
profit, then we define an error variable (ep ) to make
Na(k+1) = Na(k) + U(k) , (21) decisions. Then, the error variable is defined as
where q is the assets number where the algorithm can
T ep(k) = vpref − vp(k) , (26)
operate, Vp(k) = vp1(k) vp2(k) . . . vpq(k) is a vec-
where vpref
is a constant desired portfolio value constant.
tor that includes the portfolio value for each asset,
Evaluating the ep(k) at the time k + 1, the error dynamics
P(k) = diag p1(k) p2(k) . . . pq(k) is a diagonal ma-
can be obtained as:
trix with the price at time k for each asset, Na(k) =
ep(k+1) = vpref − vp(k+1) , where it can be seen that V(k) decreases because the term
(Ke2 − 1)e2p(k) is always negative, and ∆V(k) is bounded
ep(k+1) = vpref − vp(k) − ∆p(k) na(k) − ∆p(k) u(k) 2
by γ1 = 2Ke ep(k) rcom p(k) α1 + rcom p2(k) α12 . Then, when
+rcom p(k) ku(k) k, 2
ep(k) → 0, γ1 → rcom p2(k) α12 .
ep(k+1) = ep(k) − ∆p(k) na(k) − ∆p(k) u(k)
+rcom p(k) ku(k) k. (27) 4. SIMULATION RESULTS
Then, the u(k) signal is designed to manipulate the beha- In order to test the behavior of the proposed trading
vior of the error variable ep(k) . The desired dynamic beha- algorithm, simulations with real stock shares were im-
vior to reach the expected value vpref is ep(k+1) = Ke ep(k) , plemented. Software used for simulation was MATLAB 1
then the control signal u(k) is defined as: and, the stock prices were obtained from Yahoo Fi-
−1 nance 2 . Simulation conditions for all tests scenarios are
u(k) = ∆p(k) ep(k) − ∆p(k) na(k) − Ke ep(k) , (28) the following:
where, ∆p(k) is known and measurable, ∆p(k) 6= 0 and
kKe k < 1 to ensure convergence. If ∆p(k) = 0, means that • Amount to invest: m(0) = 50000 MXN (Mexican
there has been no change in the price of the stock. Due that pesos)
it is not necessary to apply some operation then, u(k) = 0. • Operation commision: rcom = 0.29%
• Control gain: Ke = 0.8
Substituting (28) in (27), the error dynamics in closed-loop • Simulation period: 249 days (05/23/2016-05/23/2017)
is obtained as follows • Portfolio value desired: vpref = 1.1m(0) = 55000 MXN
ep(k+1) = Ke ep(k) + rcom p(k) ku(k) k. (29) • Shares to test: GRUMAB, AXTELCPO, AMXL and
BOLSAA from mexican stock exchange, which are
It can be seen that closed-loop dynamics is not asymptoti- chosen randomly in order to test the algorithm.
cally stable, because the perturbation term rcom p(k) ku(k) k • For the simulations it is considered that the buying
depends on the magnitude of the control law. The per- and selling operations are executed instantly.
turbation is bounded due to rcom is a small percentage of Fig. 1-4 will show: 1) stock value variations during the
operation cost p(k) ku(k) k. Considering (13) and (14), the given period, 2) portfolio value calculation based on the
control signal u(k) is bounded as follows: control algorithm, stock price value and commissions
m(k) payed for each operation and, 3) control signal behavior.
−na(k) ≤ u(k) ≤ f loor . (30) Additionally, figures will show the number of days it took
(1 + rcom )p(k)
each scenario to reach the desired portfolio value.
3.2 Stability analysis
BMV:GRUMAB
300
In order to simplify the stability analysis,
lets define
the
m(k) 250
control signal limits as umax = f loor (1+rcom )p(k) and
price
umin = −na(k) , then control signal limits (30) can be 200
0 50 100 150 200 250
rewrritten as 15
umin ≤ u(k) ≤ umax . (31)
10
Assumming, kKe k < 1, rcom << 1, ∆p(k) 6= 0, and there 5 real yield
desired yield
is α1 > 0 such that max(kumin k, kumax k) < α1 , then 0
50 100 150 200
50
ep(k+1) = Ke ep(k) + rcom p(k) ku(k) k,
ep(k+1) ≤ Ke ep(k) + rcom p(k) α1 . (32) 0
-50
Let define a candidate Lyapunov function V(k) = e2(k) , and 0 50 100 150 200 250
5 30
price
0 20
0 50 100 150 200 250 0 50 100 150 200 250
15 15
10 10
0 0
-2000 -500
0 50 100 150 200 250 0 50 100 150 200 250
operations.
10
BMV:AMXL
15
price
6
10
0 50 100 150 200 250
15
4
10
REFERENCES
Bendtsen, M. and Pena, J.M. (2016). Gated bayesian
networks for algorithmic trading. International Journal
of Approximate Reasoning, 69, 58 – 80.
BUSCH, D. (2016). MiFID II: regulating high frequency
trading, other forms of algorithmic trading and direct
electronic market access. Law and Financial Markets
Review, 10, 72–82. doi:10.1080/17521440.2016.1200333.