Optimizing Automated Trading Systems
Optimizing Automated Trading Systems
Abstract. In 2016, more than the 80% of transactions in the Forex market
(where the world’s currencies trade) have been directed by robots. The design of
profitable automatic trading systems is becoming a challenging process. This
requires a strong synergy of economists and computer scientists. Our aim is to
provide an optimization framework for trading systems that starting from a
generic strategy, enhances its performances by exploiting mathematical con-
straints. Moreover, the growth of new markets requires suitable solutions inte-
grating computer science tools with economic analysis. In this work, we mainly
refer to an emerging market known as Binary Options. Starting from basic
strategies used every day in the stock markets by professional traders, we show
how optimization issues enhance the outcoming performances. Tests on the
optimized algorithms are conducted on both historical and real time data.
1 Introduction
Over each asset there are different ways to invest, here we consider the Binary
Options [9, 16]. This is a very young and particular market where who invests does not
buy or sell a contract of an asset, but rather bets on the fluctuations that will occur. The
bets can be CALL or PUT: within a fixed time, CALL (PUT) means that one bets on the
price to increase (decrease). After the bet is placed, one has to wait the fixed amount of
time in order to achieve a gain or a loss. The success of a Binary Option is thus based
on a YES/NO proposition, hence binary. If the bet is successful then the gain is given
by a certain percentage called payout; otherwise the entire investment is lost. Actually,
also nil trades can occur when the price remains unchanged. The payout is not fixed,
but might vary during the price fluctuations. It depends on various factors: the current
volume of trading; the time of the day; the type of betting (1 min, 3 min, 5 min,
30 min). However, the payout variations are closely related to the broker by which one
operates. Reasonable values for the payout are on average around 70%–80%. There are
plenty of strategies [4, 6, 12] to try to predict fluctuations, that have been studied and
experimented. Some are based on the Fundamental Analysis [7], that measures the
quality and the potential growth of a company/asset. However, from the computer
science point of view, more interesting is the Technical Analysis [15, 17] that aims to
predict fluctuations on the basis of historical behaviors. The main rule that governs the
markets is that to some extent history repeats. Starting from a well-known strategy (by
economists), we show how algorithmic optimization can be effective.
In this section, we introduce some essential financial resources that we need in order to
describe the trading strategy. The technical analysis is mainly based on bar charts, here
we consider the so-called Japanese candles [14], see Fig. 1.
Given a generic asset A, candles are formed observing the fluctuation of A within a
fixed time period t = [t1, t2]. A candle is defined by four prices: opening (price at time
t1), closure (price at time t2), maximum (maximum price reached in the interval) and
minimum (minimum price reached in the interval). If a candle satisfies the condition
opening < closure (closure < opening) then it is named a bull (bear) candle. We
represent a bull (bear) candle in white (black), a nil candle has no body. A sequence of
white (black) candles is named a bullish (bearish) trend. Figure 1 (right) shows a bar
256 A. Bigiotti and A. Navarra
chart as a sequence of candles. In the example we suppose the time period defining
candle i + 1 has not yet terminated. All candles before i + 1 compose the set X. In
order to obtain forecasts on candle i + 1 we may refer to n candles Xi, suffix of X, for
some constant n. Each candle xi in Xi is identified by a vector with the four prices that
define the candle itself: opening, closure, maximum, minimum. These prices are used
by particular functions, named indicators and oscillators, to forecast fluctuations of the
chosen asset A.
3 Trading Systems
In general, strategies differ between those that follow the current trend (bearish or
bullish), or those that operate counter the current trend. Both approaches make use of
indicators and oscillators. Leaving aside the financial aspects and focusing on the
mathematical ones, we can see indicators and oscillators as particular functions with
several variables defined on the price fluctuations. Indicators aim is to predict the future
trend of the prices. Oscillators are an important category of indicators. Their aim is to
detect points of excess of bearish or bullish, or the weakening of the current trend. They
are named oscillator because their value oscillates between a fixed range. Generally, the
domain of these functions is a price in the set Xi, instead the codomain of these
functions is closely related to the particular indicator or oscillator. In this section, we
need to introduce an indicator and an oscillator that will be used in the strategy we
design for Binary Options. The known strategy presented, is based on the Bollinger
Bands (BB, see [5]) indicator, and on the Relative Strength Index (RSI, see [13])
oscillator.
Bollinger Bands (BB). This indicator provides the calculation of a simple moving
average (SMA) and of two standard deviations r+ and r− from the SMA. We formalize
the indicator as a function: BBfp;wg ðXi Þ :¼ fmai ; ri þ ; ri g, where: i is the index of the
last candle of a bar chart one refers to; p = |Xi| is the period for the moving average
calculation, generally 20 candles; w is a factor for the calculation of the standard
deviations, generally 2. The domain of the indicator is the set of closure prices cj of the
candles xi in Xi, the codomain is a vector fmai ; ri þ ; ri g, where: mai is the moving
average relative to the i-th candle; ri þ :¼ mai þ ðw ri Þ; ri :¼ mai ðw ri Þ, and ri
is the square root of the fraction between the squared differences of the closing prices of
candles j and i, with j = i, …, i − p, and p −1. The idea behind BB is: if the closure
price of the last candle goes over ri þ or under ri , then the market will get in a tense
situation, from which it should reverse the current trend.
Relative Strength Index (RSI). This oscillator provides the calculation of two
moving averages over the differences between the opening and the closure prices of a
given set of bearish candles Xb, and bullish candles Xw. We could formalize the
oscillator as a function RSIfp;os;obg ðXw [ Xb Þ ¼ si , where: i is the index of the last
candle of a bar chart one refers to; Xw and Xb are two sets of the same cardinality of
white and black candles, respectively, before candle xi+1; p = |Xb| = |Xw|: is the period
for the moving average calculation, generally 14 candles; os is the so-called over-sold
Optimizing Automated Trading Systems 257
limit, that is a bound for considering the current trend dictated by ‘too many’ selling
operations; ob is the so-called over-bought limit, that is a bound for considering the
current trend dictated by ‘too many’ buying operations. The domain of the oscillator is
the difference between opening and closure prices of the candles in the sets Xw and Xb,
the codomain is a value si ranging in [0, 100]. Value si is the result of the calculus
100 − (100/(1 + RS)), and RS : ¼ mi þ =mi ; where: mi þ is the moving average of the
absolute value of the difference from opening and closure prices of the last p white
candles, xj in Xw; mi is the moving average of the difference from opening and closure
prices of the last p black candles, xj in Xb. Finally, we need to fix the over-sold and
over-bought limits, generally 30 and 70, respectively. The idea behind RSI is: if the
oscillator breaks over (under) the over-bought (over-sold) limit, then the market should
reverse the current trend.
The If control at Line 8 checks if the price of the closure of a candle (xi) is over the
upper standard deviation (ri þ ), that is a tense situation. The If control in Line 11 checks
if the price of the closure of a candle (xi) is under the lower standard deviation (ri ),
that is another tense situation. Lines 9 and 12 are devoted to a confirmation for the PUT
or the CALL signals, respectively. The If control in Line 9 checks if the value (si) of the
RSI is over the over-bought limit (ob), this is used as a confirmation of the PUT signal
sent by the engine. Similarly, as a confirmation of the CALL signal, there is the If
control in Line 12. Lines 10 and 13 are designed to send an order; result will contain
the outcome of the operation, after the expire time te.
5 Experimental Results
In this section, we experimentally show how our optimization can enhance the per-
formance of the BB-RSI strategy over extended historical data. Also, we provide the
effectiveness of our results, showing the performance of our optimized strategy on a
real time trade system, currently operating. Tests on historical data are made on an in
intel core i7-5500U @ 2.4 GHz with 4 GB DDR3, Windows 10 Education x64.
260 A. Bigiotti and A. Navarra
The software used is Meta Trader 4 [2]; Algorithm 1 was implemented in Meta
Quotes Language 4 (MQL4, see [1]). We execute O(BB, RSI) on the last five years H,
considering the asset A = EUR/USD (the price of euros in US dollars) and t = 1 min for
the candles formation. The variables involved by the strategy vary in the following
discrete intervals: p in [10, 50], w in [0.3, 2.6], p′ in [5, 20], os in [25, 35], ob in [65, 75].
The interpretation of the data is based on an initial balance of 1000 $, an average
payout p~a of 75%, and an investment of 100 $ for each bet. The optimized configu-
rations obtained by the first step of our approach provide the restricted ranges for the
involved variables shown in Table 1. The outcome is shown in Table 2 along with the
number of trades performed. The best setting is thus c1 where NP is maximized and
MDD is minimized, even though W% is slightly smaller than that achieved in c2. In
Fig. 2. Balance evolution in the period 2 January–15 June 2018 of the BB-RSI strategy under
configuration c1, starting with 1000 $ and bets of 100 $ each.
Optimizing Automated Trading Systems 261
Fig. 2 we show the effectiveness of our approach by reporting the balance evolution of
a real-time trading system currently operating on the basis of c1.
We have shown how optimization issues are effective for the setup of a trading system for
Binary Options. It might be reasonable to think about other markets and/or other assets
for which multi-criteria optimization is required. Other approaches and methodologies
could be applied to further enhance the behavior of a strategy or to provide completely
new strategies. For instance, we are thinking about applying martingale approaches [3]
that could be effective in Binary Options due to the relation of such a market with
gambling. Other emerging markets require deep investigations. An example is about
crypto-currencies [8], even though the lack of historical data might be critical.
Acknowledgments. Special thanks go to Simone Ciancone for his expertise and useful dis-
cussions. The work has been supported in part by the GNCS-INdAM project 2018 “Anti-Social
Networks”.
References
1. mql4 documentation. https://docs.mql4.com
2. mt4. https://www.metatrader4.com/en
3. Le Gall, J.: Brownian Motion, Martingales, and Stochastic Calculus. Graduate Texts in
Mathematics. Springer, New York (2016)
4. Aldridge, I.: High-Frequency Trading: A practical Guide to Algorithmic Strategies and
Trading System. Wiley Trading Series. Wiley, New York (2013)
5. Bollinger, J.: Bollinger on Bollinger Bands. McGraw-Hill Education, New York (2001)
6. Bonenkamp, U.: Combining Technical and Fundamental Trading Strategies. Gabler Verlag,
Wiesbaden (2010)
7. Bulkowsk, T.: Fundamental Analysis and Position Trading: Evolution of a Trader. Wiley,
New York (2013)
8. Clarkson, M.: Cryptocurrency: Best Strategies for Investing and Profiting from Cryptocur-
rency. CreateSpace Independent Publishing Platform (2018)
9. Coulling, A.: Binary Options Unmasked. Anna Coulling (2015)
10. Guy, D.: Forex Trading Money Management System. Roulette Trader (2016)
11. Haynes, R., Roberts, J.: Automated Trading in Futures Markets Update (2017)
12. Laamb, D.: Automated Trading Strategies by Example. Philip Lim (2017)
13. Perchanok, K., Hrytsyuk, I.: The Encyclopedia of the Indicator RSI (Relative Strength
Index). CreateSpace Independent Publishing Platform (2011)
14. Person, J.: Candlestick and Pivot Point Trading Triggers. Wiley Trading Series. Wiley, New
York (2018)
15. Pring, M.: Study Guide for Technical Analysis Explained. McGraw-Hill, New York (2002)
16. Schwager, J.: A Complete Guide to the Future Markets. Wiley Trading Series. Wiley, New
York (2017)
17. Stevens, L.: Essential Technical Analysis: Tools and Techniques to Spot Market Trends.
Wiley Trading Series. Wiley, New York (2002)