Mt5se: An Open Source Framework For Building Autonomous Trading Robots
Mt5se: An Open Source Framework For Building Autonomous Trading Robots
Abstract—Autonomous trading robots have been studied in This kind of simulation using real-time data feed from real
artificial intelligence area for quite some time. Many AI tech- markets is very important to identify possible overfitting
arXiv:2101.08169v3 [cs.AI] 28 Jun 2022
niques have been tested for building autonomous agents able in financial machine learning models, as discussed in sec-
to trade financial assets. These initiatives include traditional
neural networks, fuzzy logic, reinforcement learning but also tion II-B1. The framework provides access to several stock
more recent approaches like deep neural networks and deep exchanges (NYSE,Nasdaq, London SE, Tokyo SE, Brazil’s B3
reinforcement learning. Many developers claim to be successful and others) through MetaTrader 5 platform [3]. Besides stock
in creating robots with great performance when simulating exchanges, it also allows trading with Foreign Exchanges pairs
execution with historical price series, so called backtesting. including cryptocurrencies like Bitcoin and Ethereum, through
However, when these robots are used in real markets frequently
they present poor performance in terms of risks and return. the so called Contract For Differences instrument [4]. Further-
In this paper, we propose an open source framework (mt5se) more, it is possible to use the same autonomous trader with
that helps the development, backtesting, live testing and real minor changes in real operations. It is possible to create Trad-
operation of autonomous traders. We built and tested several ing Robots using Neural networks, Random Forests, Support
traders using mt5se. The results indicate that it may help the Vector Machines, Genetic Algorithms, Bayesian Networks,
development of better traders. Furthermore, we discuss the
simple architecture that is used in many studies and propose an Reinforcement Learning, Deep Learning and other techniques
alternative multiagent architecture. Such architecture separates using the wide range of available libraries in python. The
two main concerns for portfolio manager (PM) : price prediction mt5se framework is an evolution of a previous framework
and capital allocation. More than achieve a high accuracy, a PM called mt5b3 [5], which is now deprecated.
should increase profits when it is right and reduce loss when it
is wrong. Furthermore, price prediction is highly dependent of
asset’s nature and history, while capital allocation is dependent
only on analyst’s prediction performance and assets’ correlation.
Finally, we discuss some promising technologies in the area.
A. Organization of the Text
I. I NTRODUCTION
An autonomous trader must be able of selecting, buying and We assume the reader is already familiar with Finance The-
selling financial assets in order to fulfill it’s investor require- ory, including Modern Portfolio Theory [6], Efficient Market
ments about risk and return within the investor’s horizon of Hypothesis [7], Capital Asset Pricing Model (CAPM) and
investment. This period may range from very some seconds Market micro-structure. If that is not the case, a very short
or fractions of seconds (high frequency trading [1], to longer introduction to these concepts are available in section 2 of [8].
periods as several days or even years. It is an autonomous The remainder of this paper is organized as follows: section II
agent that perceives and acts in a very particular environment: discuss the main aspects of modelling autonomous analysts,
financial markets, which brings several complex features that traders and portfolio managers, including some unique charac-
may make it very challenging for autonomous agents. It is teristics of financial markets that make them hard challenges
important to note that there are also ethical and legal implica- for artificial intelligence based agents. We present and analyze
tions that should be taken in consideration in the process of briefly some available frameworks for building autonomous
building automated trading robots. We recommend Wellman traders and compare them with our proposed framework,
and Rajan’s paper [2] about ethical issues in Autonomous called mt5se, which we present minutely in section IV. We
Trading Agents. also provide several examples of autonomous traders and
In this paper, we propose an open source framework portfolio managers and their source code in section V. We
framework for building and testing trading agents, called discuss some open problems in the development of high
mt5se. We present some examples of trading agents devel- performance autonomous traders and we also present some AI
oped using such architecture and the respective results. This related technologies that may contribute to the finance field in
framework allows the development of autonomous traders on section VI. Finally, we conclude the paper with some possible
python language, and the execution of real-time simulation. extensions and suggestion for future work in section VII.
II. AUTONOMOUS A NALYSTS , T RADERS AND P ORTFOLIO given time frame (1 minute, 1 hour or 1 day ). Your Market
M ANAGERS data provider may also allow access to the order books or
The spectrum of used AI techniques in finance field is even to semi-structured information, like FIX messages. In
wide and it includes since reinforcement Learning [9], [10], this case, you may try to recognize patterns that identify some
multiagent systems [11], [12] complex networks [13], genetic traders. For instance, human GUI traders often trade in round
algorithms [14], random forests [15] to more recent approaches lots, and you can use this fact to estimate what percentage of
like deep reinforcement learning [16]. Regardless of the picked the volume is coming from them at a given point in time.
AI technology, there are some aspects that are always present Analytics data is usually provided by human experts. It is
and we observe that quite often AI researchers disregards a kind of derivative data, based on an original source, which
significant aspects of finance theory, as for instance: the could be fundamental, market, alternative, or even a collection
correlation among assets, different investor profiles and risk of other analytics. It is not available from an original source,
control. Dealing with such issues, it is fundamental to build and it was processed in a particular way (hopefully not biased).
better autonomous agents for finance, however it is also really Independent analysts, investment banks and research firms
challenging. sell valuable information that results from deep analyses of
companies’ business models, activities, competition and so on.
A. Types of Financial Data Alternative data may refer to satellite image or video
feeds include monitoring of tankers, tunnel traffic activity,
The data that should be considered by an autonomous
or parking lot occupancy. Before an Oil company reports
analyst is far more diverse and complex than historical prices
increased earnings, before its market price shot up, before
or volumes. It can be splitted in four different types: technical,
analysts wrote their commentary of their latest filings, before
fundamentalist, analytical and alternative data [17]. Figure 1
all of that, there were movements of tankers and drillers and
presents some examples for each type.
pipeline traffic. They happened months before those activities
were reflected in the other data types. What truly characterizes
alternative data is that it is primary information, by that we
mean the information has not made it to the other sources.
There are some problematic aspects of alternative data. It
may bring high cost and privacy concerns, in fact it could
even bring legal concerns. Capture those data may be very
expensive, and the observed company may object, not to
mention bystanders. Furthermore, it is usually hard to process.
Fig. 1. Types of Financial Data However, Alternative data offers the opportunity to work
with unique datasets. Data that is hard to store, manipulate,
Fundamental data encompasses information that can be and operate may be very rewarding, because perhaps your
found in regulatory filings. It is mostly accounting data, competitors did not try to use it for logistic reasons, gave up
reported quarterly or yearly. A particular aspect of this data midway, or processed it incorrectly. Therefore, you may have
is that it is reported with some temporal lapse. You must a truly unexploited field to mining.
confirm exactly when each data point was released, so that
your analysis uses that information only after it was publicly B. Autonomous Analysts
available. A common error is to assume that this data was Investment analysis can be seen as the process of assessment
published at the end of the reporting period, which is almost and selection of investments options in terms of risk and return
never the case. Another problem is the use of back-filled in order to provide advice to the investor or manager. Analysts
or reinstated data. Back-filling means that missing data is are responsible for transforming raw data into information that
assigned some value, even if those values were unknown at can guide investment decisions. The small but relevant differ-
that time. A “reinstated value” is a corrected value that amends ence from traders is that analyst are independent from investor
an incorrect initial release. The problem is, the corrected preferences, investment policies or the amount of capital to be
values were not known on that first release date. The use of managed. Analysts are dependent of the data related to their
such data gives a unrealistic advantage to models, that will not target assets. It is important to observe that some data may be
be present in real operation. In another words, we could say very relevant for some asset, while it is irrelevant or almost
it also leads to overfitting. Nevertheless, it is common to find to another asset. For instance, oil prices can be very relevant
studies that use the final released value and assign it to the to airline companies, but perhaps they are not that relevant
time of the first release, or even to the last day in the reporting to bank stocks. Trading analysis for cryptocurrency prices are
period. most based on historical prices and volumes or some times in
Technical or Market data includes all trading activity that signal obtained from news using natural language processing
takes place in an exchange, including open, high, low and methods (sentiment analysis). In comprehensive survey about
closes prices and their historical series. It is common to use cryptocurrency with 126 research papers, no one used oil
the term bar to refer to open, high, low and close prices in a price to predict cryptocurrency prices [18]. We do not argue
that using oil prices would be a good idea, our point is that
autonomous analysts for different assets may require different
kind of information besides historical prices and volume.
Meanwhile, autonomous trading is the process of submitting
buy or sell orders by an autonomous software agent software
in order to achieve financial goals. It is quite common that
autonomous trading agent deals with just one asset [11].
However, it is well know in finance field that an investor must
consider the relationship among the investments to build a
portfolio that will best meet the investor’s objectives [19]. It
seems that developers of autonomous traders often disregard
two important aspects: investor preferences and the relation-
ship among assets. We address these issues in section II-D
and we use the term autonomous portfolio managers (APM)
to differentiate autonomous agents that deal with such issues
from those who do not, what we call simply autonomous Fig. 2. Single Agent Architecture for Autonomous Traders
traders. Some authors do not make such distinction. We discuss
increasing order of complexity: autonomous analysts ( II-B),
autonomous traders (section II-C) and autonomous portfolio from each other. Likely it is the main reason that leads such
managers (section II-D). studies to focus on creating AT that deals with just one asset
1) Overfitting: The problem of creating models with great at the time. However, it misses important issues regarding
performance in some known data set, but with bad per- dependence among assets.
formance in new data is well known in Machine Learning As stated before, it is important to reason about depen-
field and it is usually called overfitting. Nevertheless, this dencies among assets and investor preferences about risk
problem seems to be unknown or at least disregarded by many and return. For instance, an investor may be more averse
practitioners in autonomous analysis and trading. However, the to risk than others, but lower risk portfolio can be obtained
dangers of failing to avoid overfitting are even more severe by incorporation negatively correlated assets [19]. We address
in the financial field for two reasons. Overfitting is more autonomous traders that care about these important issues in
likely to happen in finance than in traditional machine learning next section.
problems, like face recognition, because of the low ratio
signal-noise and the fact that markets are not IID (independent D. Autonomous Portfolio Managers
and identically distributed). Furthermore, overfitting in finance The portfolio theory proposed by Markowitz [6] become the
leads to over optimistic expectations about performance [20]. de facto standard [19]. Under the portfolio theory assumptions,
For a deeper discussion about the problem, we suggest [8], a single asset or portfolio of assets is efficient if no other
[17] asset or portfolio of assets offers higher expected return with
the same (or lower) risk or lower risk with the same (or
C. Autonomous Traders higher) expected return. It is possible to achieve lower risk
Autonomous traders need to deal with two main concerns: portfolios by including negatively correlated assets in it. In
price prediction and capital allocation, which is also called fact, it is possible to derive portfolios that have lower risk
bet sizing [17]. More than just achieve a high accuracy, an than the assets that compose it. This ability to reduce and
autonomous trader should increase profits when it is right manage risk is the essence of diversification. Many optimiza-
and reduce loss when it is wrong. Furthermore, price (or tion techniques may be used, such as Linear programming,
trend) prediction is highly dependent of asset’s nature and Convex optimization, Quadratic programming, Meta-heuristic
history, while capital allocation is dependent only on investor methods, Genetic algorithm and others. Despite all the work
preferences about risk and return and assets’ correlation. done, portfolio optimization is still an open problem and the
Many studies in autonomous traders (AT) adopt implicitly or available methods have some drawbacks. For instance, it is
explicitly a mono agent architecture that encapsulates most well known that if the mean return vector and the return
of the complexity in a single component that defines orders covariance matrix for the target assets are known, then the
given some information, see Figure 2. It is also common to Markowitz problem has a closed-form solution. However in
define one auxiliary module that collects data from external practice that is never the case, and therefore they are estimated
sources that may be relevant to the trading strategy, that we from historical data. According to some authors [21], it may
refer as Data Collector. Another module to dispatch the defined turn the Markowitz theory impracticable in real portfolio
orders to the market is also common. Despite the simplicity management applications. We suggest [17], chapter 16, for
advantage, such architecture has an important drawback. The a deeper discussion about that.
trading strategy may become very complex as the number of However, it is interesting to observe that autonomous trader
assets increases, specially if the assets’ nature differ very much developers quite often disregard that fact. According to the
portfolio theory, any investor would prefer the efficient port-
folio, for a given level of risk or return. However, different
investor may have different levels of maximum acceptable
risk or minimum acceptable return, therefore they would
prefer different portfolios. Autonomous traders should explore
diversification and be aware of investor preferences, in order to
do a better in trading in the investor’s behalf. As stated before,
We use the term autonomous portfolio manager to refer to an
autonomous trader that uses these facts in its decision process,
despite the fact that other authors use the term, autonomous
trader, for both cases.
Autonomous portfolio managers should also reason about
investor preferences. Naturally, it include preferences about
risk and return, but we have no doubt that ethical, envi-
ronmental and social concerns will have to be incorporated
in autonomous traders in the near future, just like investors Fig. 3. Multi-agent Architecture for Autonomous Portfolio Manager
are demanding these concerns from human investment man-
agers [22].
data high dimensionality and investor preferences awareness,
Investor preferences about return and risk may be expressed
an APM should be able to explain its decisions, just like a
as a maximum acceptable risk or minimum acceptable re-
human portfolio managers are used to. In fact, the ability to
turn [11], but it could also be expressed in terms of a list
provide explanations for its decision, explainability, is consid-
of possible assets and percentage limits for some classes
ered to be essential to create artificial intelligence applications
of assets. For instance, Graham’s book ’Intelligent Investor’
that are trusted by users [24]. The interest in creating AI
suggests an investor should never have less than 25% or
models that can explain their decisions is not new, but it has
more than 75% of her portfolio in common stocks and, at
revived in the last years with machine learning models that are
the same time, never more than 75% or less than 25% in
hard to interpret [25]. Some authors argue the use of inherently
bonds [23]. In terms of autonomous portfolio managers, an
interpretable models rather than searching for methods that
investment policy statement (IPS) may be defined as a class
can interpret the so called black box models [26]. Considering
that implements a method to check if the current portfolio is
these challenges, we proposed a multiagent architecture for
adherent to IPS and other to check a set of intended orders
autonomous portfolio managers that divides these challenges
would make the new portfolio violate the IPS.
among some autonomous agents that working together make
When an APM deals with many assets, it is not hard to
the tasks expected from an APM. Our proposed architecture
realize that each asset will demand different data streams in
aims to be inherently interpretable by providing analyses for
order to be analyzed and it may require a significant aunt
each asset, configurable investment policies and optimization
of data volume for analysing all assets. One should also
algorithm that try to achieve the best portfolio for a given
note that investor information is also part of data needs to
objective function. It facilitates reusing well known approaches
be considered to define the trading strategy. That may bring
to create analysts [27] and optimize portfolios, as discussed
the so called curse of dimensionality. As the number of
in section II-D. At same time, it reduces the problem of
dimensions because the amount of data needed to support
high dimensionality, because each analyst deals only with
result (in a significant way) will likely grow exponentially
data related to its asset independently from other analysts or
with dimensionality. In another hand, it is well known that
investor preferences.
diversification (a large number of target assets) may provide a
better risk-reward relation in portfolios, by reducing the risks. The APM multiagent architecture is presented in figure 3.
So, APMs are required to deal with a large number of assets. It is composed by analyst agents and one allocator agent. An
The challenges discussed here: Curse of dimensionality, di- analyst provides a return distribution (analysis) for a given
versification and investor preferences awareness make the task asset using several concurrent models. Using such analyses,
of creating effective autonomous portfolio managers very hard. the allocator may calculate target prices, expected returns and
It may be even harder if one decides to use the single agent optimize the portfolio to maximize an objective function, the
architecture. For these reasons, we propose an multiagent Sharpe ratio, for instance. The APM architecture also counts
architecture, which is described in section II-E. with some additional software components that are based in
traditional (non AI based) algorithms. These are shown as
E. Multi-agent Architecture for Autonomous Portfolio Man- rectangles in figure 3, while autonomous agents are shown
agers as rounded rectangles. These components are described next.
There are many challenges for real autonomous portfolio • Data Collector: it collects relevant data about all target
managers (APM). Besides the challenges discussed in sec- assets that may be used by the analysts and their models.
tion II-D: risk mitigation by diversification, dealing with the It parses and integrates information from different data
sources in a format that can be used by the analysts. useful, but it does not provide a comprehensive evaluation
• Analyst: Autonomous agents that provide an analysis of the manager’s performance. It is absolutely necessary to
about the asset, ideally a return distribution. Naturally, control for the portfolio risk [19]. However, it is quite common
it is dependent of the asset, but it is independent of to observe in evaluation of autonomous trader without a proper
investor’s preferences and capital. Ensemble several mod- control for the risk of the portfolio. We argue that there is no
els to create an analyst is a good idea, since it can good reason to evaluate autonomous traders (or autonomous
provide better predictions but also help to understand portfolio managers) using different methods than those used
the reason behind its predictions. The analysts estimate for human professionals. Despite the fact that there is not a
return distributions for each asset using their best effort single universally accepted method for evaluating portfolio
and available information. performance adjusted to risk, there are several techniques
• Allocator: It uses the analyses provided by the analysts that are used in practice, among them Sharpe’s ratio [29].
and according with investor profile it searches for the best It is widely used despite some criticism about it. For in-
allocation (or re-allocation) of resources among the target stance, high outlier returns could increase the value of the
assets. It does not need to know more about the assets Sharpe ratio’s denominator (standard deviation) more than the
itself, since it works with the analyses, therefore given value of the numerator (return in excess), that way the ratio
the analyses, it is asset independent. would be lower thereby lowering the value of the ratio and
• Investment Policy Statement (IPS) Checker: The set for positively skewed return [30]. Some alternative methods
of orders are checked to ensure that they respect the are also available, as for instance: Sortino ratio [31] and
investment policy statement. Orders can be ignored or Modigliani index [32]. Furthermore, it is important to measure
changed by the IPS checker to warrant its goal. performance for a proper track record length [33]. There is a
• Order Dispatcher: Once the orders coherent with IPS solid literature about evaluation of portfolio performance [19],
are defined, these orders need to be dispatched to some but it goes beyond the scope of this paper to cover it here.
Market (real or simulated) to be completely or partially
executed or even not executed according to the market
conditions. That is the role of the Order Dispatcher. III. C OMPARATIVE A NALYSIS OF F RAMEWORKS FOR
AUTONOMOUS T RADERS
The multiagent architecture may be seen as a very sim-
plified structure of a hedge fund, which could be described
in three parts: analysts team, portfolio manager and traders. In table I, we present a comparative analysis of some
The analysts make analysis of the target assets and pass it to selected systems with similar propose of mt5se. Such analysis
the manager. The manager checks the portfolio composition is based on some features that facilitate the development and
and makes new positions based on their risk control strategy test of autonomous trading strategies. We do not intend to
and the analysts’ analysis. Ultimately the positions updates judge the overall quality of the cited systems, but just identify
are executed by traders. Naturally, it is an overly simplified differences (positive and negative) with the mt5se framework
explanation about a hedge fund work process. proposed here. It is relevant to note that mt5se relies on
MT5 [3] to connect to stock markets. The MT4 [34] is not
F. Performance Measure for Autonomous Analysts and APMs just an earlier version of MetaTrader 5, but an alternative tool
Autonomous Analysts as described earlier are concerned focused on Forex markets. While, MetaTrader 5 allows trading
with asset price prediction. Therefore it is easy to note that in Forex or stock markets, however it comes with the cost of
their performance may be measured as traditional machine higher complexity and two accounting systems: netting and
learning models for classification (in case price is discretized) hedging systems. Therefore, MT4 is still wildly used for Forex
or regression (continuous price). Concepts like accuracy, operations. The other system, called AgEx [35], is an open
mean-square error, cross-validation and other common asso- source financial market simulation tool that uses FIPA ACL
ciated to machine learning evaluation are all relevant, but language and a market ontology created specifically to be used
one should be special careful to avoid overfitting since it for trader agents.
is more dangerous and likely in finance [17]. Furthermore,
one should consider taking into account the cost of errors. If Feature MT4 AgEx MT5 mt5se
Real Operation support Yes No Yes Yes
an Autonomous Analyst (AA) predicts a high return and the Stock operation support No Yes Yes Yes
return is even higher than expected, it may be considered an Forex operation support Yes No Yes Yes
error, but certainly if the real return were negative it would Cryptocurrency operation support Yes No Yes Yes
be a much worse error. For a deeper discussion about cost of Open source No Yes No Yes
Python support No No No Yes
errors in autonomous trading see [28]. On the other hand, in Object Oriented No Yes Yes Yes
order to address evaluation of autonomous traders or APM, Backtest support Yes Yes Yes Yes
we need to review evaluation of human portfolio managers. TABLE I
There are many possible procedures to evaluate portfolio C OMPARISON AMONG S ELECTED S YSTEMS
manager’s performance beyond the simple historical return
comparison with other managers. Such comparison may be
IV. B UILDING AUTONOMOUS T RADERS USING MT 5 SE B. Testing Autonomous Traders
In this section, we present how to build and test autonomous Once you have built an autonomous trader, you need to
traders using the mt5se framework. It allows access to price verify if it is suitable for operation. The basic form of testing
data (open, close, high, low) and book data (bid, ask) and an autonomous trader is often called backtest. It is a kind of
order placement in simulated or real financial markets. It was evaluation for trading robots. It is basically a trading robot
designed to operate in any financial market accessible through executing with historical price series , and its performance
Metatrader platform, that uses the so called netting accounting is computed. In backtesting, time is discretized according
system. It was tested in simulated and real operation connected with bars and mt5se controls the information access to the
just to the Brazilian stock exchange (B3) and Nasdaq Ex- Trader according with the simulated time. As simulation time
change, but in principle, it could used to trade in any stock advances, the function ’trade’ is called and receives the new
exchange with Brokers that provide access to MetaTrader bar info and decides which orders to send. In order to backtest
platform. There are several Brokerage company that provide one strategy, one just need to create a Trader, establish the test
such access for Nasdaq for instance, TradeView [36] and in parameters and execute it. These parameters define trading
B3, XP [37], Clear [38], among others. guidelines (target assets, available capital and horizon of
The framework mt5se may be used to create simple trading investment) and some system parameters that define operation
robots based on algorithms, but it also supports the creation and log register. In listing 3, we present an example of backtest
of traders based on Artificial Intelligence techniques, such as definition and execution.
decision trees [39], neural networks [40], Bayesian dynamic 1 # trading data options
networks [41], reinforcement learning [42] and so on. 2 capital=100000
results_file=’data_equity_file.csv’
In listing 1, we present a very simple autonomous trader 34 assets=[’PETR4’,’VALE3’,’ITUB4’]
built using mt5se. It is able to trade with any number of target 5
assets, but it does that randomly. It select aleatory a number of 6 #backtest options
prestart=se.date(2019,12,10)
shares between and 1 and 1000, and then also aleatory picks 78 start=se.date(2019,1,10)
buying or selling that number of shares at market price for 9 end=se.date(2019,2,27)
each asset, at each moment. 10 # Use True if you want debug information for your
Trader
1 import numpy.random as rand 11 verbose=False
2 class RandomTrader(se.Trader): 12 #sets the backtest setup
3 def trade(self,dbars): 13 period=se.DAILY
4 orders=[] 14 # it may be se.INTRADAY (one minute interval)
5 assets=list(dbars.keys()) 15
6 for asset in assets: 16 bts=se.backtest.set(assets,prestart,start,end,period
7 if rand.randint(2)==1: ,capital,results_file,verbose)
8 order=se.buyOrder(asset,100) 17 #create trader instance
9 else: 18 trader=RandomTrader()
10 order=se.sellOrder(asset,100) 19 # Running the backtest
11 orders.append(order) 20 df= se.backtest.run(trader,bts)
12 return orders
Listing 3. Backtesting an Autonomous Trader
Listing 1. Random Autonomous Trader Example
If we execute the listing 3, we backtest the trader presented
in listing 1. As we said before, the random trader does make
A. Installation and Further information about mt5se intelligent decisions, just picks randomly numbers of shares
The framework mt5se may be download from github or and sells or buys them. Naturally, its performance may vary
PyPI platforms. In its repository [43], there are also available widely. In order to evaluate the trader’s performance, you may
tutorials presenting mt5se API and how to create simple use the function se.backtest.evaluate that generates a report,
autonomous Trader, and also Traders based on AI techniques. see listing 4. In figure 4, we present two very different results
Some jupyter notebooks with several examples are also avail- for the Random Trader using the same assets and trading
able. In order to install mt5se in a Python, you may just use period. In execution (a), the performance is very good, where
one of the ways presented in listing 2 trader achieves profit (13.18% return) in less than two months
1 # this package is required by mt5se
(January 10 to February 27, 2019), however another execution
2 pip install MetaTrader5 (b) using the same random trader and period achieved a
3 # installing mt5se package negative return of 21.49%.
4 pip install mt5se
5 1
6 # within a jupyter notebook, you may use: 2 #Run the trader according setup and get the results
7 #import sys 3 df=se.operations.run(trader,ops)
8 #!{sys.executable} -m pip install Metatrader5 4 #evaluate the results
9 #!{sys.executable} -m pip install mt5se 5 se.backtest.evaluate(df)
6 #Alternatively, you can evaluate using the generated
Listing 2. Installing mt5se file
7 #se.backtest.evaluateFile(fileName)
8 #fileName is the name of file generated by the 17 else:
backtest 18 order=None
19 if order!=None:
Listing 4. Evaluating performance an Autonomous Trader 20 orders.append(order)
21 return orders
We need to note that it is hard to perform meaningful eval-
uations using backtest. There are many pitfalls to avoid and Listing 5. Autonomous Trader suitable for real operation
it may be easier to get trading robots with great performance In listing 5, we present the complete code of an autonomous
in backtest, but that perform really badly in real operation. trader called MultiAssetTrader, ready for real operations. It is
For a deeper discussion about trading strategies evaluation, based on a simple interpretation of the Relative Strength Index
we suggest [8], [17] and [33]. (RSI), and splits equally the available capital among assets.
1 #trading data
2 # target assets (three of the main assets in B3)
3 assets=[’PETR4’,’VALE3’,’ITUB4’]
4 # available capital
5 capital=100000
6
7
8 # Options for ending time of operation
9 # endTime=se.now(minOffset=1) # it will run for
one minute!
10 # endTime=se.now(hourOffset=1,minOffset=30) # the
trader will run for 1:30h after started!
11 endTime=se.operations.sessionEnd() # it will run by
the end of session!
12
Fig. 4. Two different results for the same Trader and setup
13 #if market not open, keep waiting
14 waitForOpen=True
After achieving good performance in backtesting properly 15 #get information and decise every minute
conceived and executed, the next phase would be to operate 16 timeFrame=se.INTRADAY
in real market with limited portfolio. As mentioned before 17 18 #Connect to B3 using default account in MT5
you can use mt5se traders in real environments, but you may 19 se.connect()
have to perform some minor changes in a trader so it can 20
operate in real mode and you need to define the operational 21 22 # System information
parameters, which are a little different from backtest parame- 23 # gives information during executing
ters. In listing 6, we show how to establish the parameters for 24 verbose=True
# operations register file
trader’s operation, create a trader instance from a class called 25
26 data_file=’data_equity_file.csv’
MultiAssetTrader and run it according with its setup. Some 27 # seconds to wait between trade calls
brokers provide dedicated servers and accounts for simulated 28 delay=1
# number of bars to take in each decision
operations, often called demo accounts. For instance, XP Inc is 29
30 mem=10
one of those brokers in B3 stock exchange that provide demo 31 # setup operation (ops)
accounts. Despite being simulated account, under the point of 32 ops=se.operations.set(assets,capital,\
endTime, mem,timeFrame,data_file,\
view of an autonomous trader they are just like real accounts 33 34 verbose,delay,waitForOpen)
and can use exactly the same code, as presented in listings 6 35
and 5. 36 # Create an instance of the trader
37 trader=MultiAssetTrader()
1 class MultiAssetTrader(se.Trader): 38
2 def trade(self,dbars): 39
3 assets=dbars.keys() 40 #Run the trader according setup
4 orders=[] 41 se.operations.run(trader,ops)
5 for asset in assets:
6 bars=dbars[asset] Listing 6. Setup and Running an Autonomous Trader for real operation
7 curr_shares=se.get_shares(asset)
8 money=se.get_balance()/len(assets) #
divide o saldo em dinheiro igualmente entre os C. Autonomous traders based on AI
ativos In this section, we are going to present some AI powered
9 # number of shares that you can buy of
asset trading robots, based on Machine learning and Artificial Intel-
10 price=se.get_last(bars) ligence algorithms. In listing 7, we present an example based
11 free_shares=se.get_affor_shares(asset, on Random Forests that encompass the whole decision pro-
price,money)
12 rsi=se.tech.rsi(bars) cess, taking into account data about target assets and investor
13 if rsi>=70 and free_shares>0: information. It is based on Random Forest classifier. Another
14 order=se.buyOrder(asset,free_shares) option would be split those concerns in different models or
15 elif rsi<70 and curr_shares>0:
16 order=se.sellOrder(asset,curr_shares agents, as discussed in II-E. We present an autonomous trader
) based on the first alternative, using Random Forests [44] and
deals with investor preferences in a very simplistic way, by curr_shares)
dividing the capital equally among the assets and using the 60 else:
61 order=None
same model in listing 7 62 if order!=None:
1 ## Defines the Simple AI Trader 63 orders.append(order)
2 from sklearn.ensemble import RandomForestClassifier 64 return orders
65
3 from sklearn.preprocessing import KBinsDiscretizer
4
66 # creates instance of the Simple AI Trading
5 class RandomForestTrader(se.Trader): 67 trader=RandomForestTrader()
6
7 def setup(self,dbars): Listing 7. Example of Autonomous Trader based on AI (Random Forest)
8 assets=list(dbars.keys())
9 if len(assets)!=1: D. Building AI-based Autonomous Traders using historical
10 print(’Error, this trader is supposed to
deal with just one asset’)
data
11 return None It is quite common to use historical data to build au-
12 bars=dbars[assets[0]]
13 # Data preparation
tonomous analysts or traders using historical data to build
14 timeFrame=10 Machine Learning models. The training process may take
15 horizon=1 # it projects the closing price significant amount of computer processing time. The data
for next bar
16 attr_list=[’open’,’close’,’MA’]
preparation, which includes gathering, cleaning and selecting
17 features, and also creating new features based on available
18 #getting bars info information, is fundamental and often it requires a lot of effort
19 bars=se.get_bars(’PETR4’,timeFrame*5)
20 # creating a new feature
from practitioners and researches. In this section, we provide
21 bars[’MA’]=se.tech.ma(bars[’close’]) some source code examples of Data preparation, ML models
22 creation using mt5se and Sci-kit-learn [45]. In listing 8, we
23 # you may use get_XY providing all info
24 target=’close’
present a code snippet that get information about one asset,
25 X,y=se.ai_utils.get_XY(bars,attr_list,target and creates a supervised dataset with independent features (X)
,timeFrame,horizon) and a target feature (Y), which is the future price of the given
26
27 discretizer = KBinsDiscretizer(n_bins=3,
asset in a given time horizon. Then, we create an ML model
encode=’ordinal’, strategy=’uniform’) to predict Y given the independent features in a specific time
28 # creates the discrete target frame in listing 9. The data preparation transform a time series
29 dy=discretizer.fit_transform(y)
30
into a suitable dataset to use with sci-kit learn framework, but
31 #clf = tree.DecisionTreeClassifier() that could be used in several other frameworks. The figure 5
32 clf = RandomForestClassifier(n_estimators illustrates such transformation.
=10)
33 clf = clf.fit(X, dy)
34 self.clf=clf
35
36 def trade(self,dbars):
37 assets=dbars.keys()
38 orders=[]
39 timeFrame=10
40 horizon=1
41 attr_list=[’open’,’close’,’MA’]
42 money=se.get_balance()/len(assets) #
shares the balance equally among the assets
43 for asset in assets:
44 bars=dbars[asset]
45 curr_shares=se.get_shares(asset)
46 price=se.get_last(bars)
47 free_shares=se.get_affor_shares(
asset,price,money)
48 # get new information (bars),
transform it in X Fig. 5. Data Preparation Example: From bars to X and Y features with time
49 bars=dbars[asset] frame (tF)=2, horizon(h)=1 and [x1, x4] selected as relevant features. X and
50 bars[’MA’]=se.tech.ma(bars[’close’]) Y are numpy arrays
51 X=se.ai_utils.get_X(bars,attr_list,
timeFrame,horizon)
52 # predict the result, using the 1 # Data preparation
latest info 2 timeFrame=10
53 p=self.clf.predict([X[-1]]) 3 horizon=1 # it projects the closing price for next
54 if p==2: bar
55 #buy it 4 attr_list=[’open’,’close’,’MA’]
56 order=se.buyOrder(asset, 5
free_shares) 6 #getting bars info
57 elif p==0: 7 bars=se.get_bars(’PETR4’,timeFrame)
58 #sell it 8 # creating a new feature
59 order=se.sellOrder(asset, 9 bars[’MA’]=se.tech.ma(bars[’close’])
10 21 joblib.dump(clf, ’model.joblib’)
11 # getting X (independent features)
12 X=se.ai_utils.get_X(bars,attr_list,timeFrame,horizon Listing 10. Creating a ML model for deciding trader’s actions and saving it.
)
13 1 ## Loading a pre-trained model to create an AI based
14 #getting Y (dependent feature) Trader
15 y=se.ai_utils.get_Y(bars,target,timeFrame,horizon) 2 from sklearn.ensemble import RandomForestClassifier
16 3 from sklearn.preprocessing import KBinsDiscretizer
17 # Alternatively, you may use get_XY providing all 4 import joblib
info 5
18 X,y=se.ai_utils.get_XY(bars,[’open’,’close’,’MA’], 6 class RandomForestTrader(se.Trader):
target,timeFrame,horizon) 7
19 8 def setup(self,dbars):
20 # Example of Discretization (it may be required by 9 fileName=’model.joblib’
the chosen ML technique 10 self.clf=joblib.load(fileName)
21 # Discretization 11
22 12 def trade(self,dbars):
23 from sklearn.preprocessing import KBinsDiscretizer 13 assets=dbars.keys()
24 14 orders=[]
25 discretizer = KBinsDiscretizer(n_bins=3, encode=’ 15 timeFrame=10 # it takes into account the
ordinal’, strategy=’uniform’) last 10 bars
26 16 horizon=1 # it projects the closing
27 dy=discretizer.fit_transform(y) # you make each price for the next bar
value assume a discrete value [0,1,2,....] 17 attr_list=[’open’,’close’,’MA’]
according to discretization strategy 18 money=se.get_balance()/len(assets) #
shares the balance equally among the assets
Listing 8. Example of Data preparation for Training ML models 19 for asset in assets:
20 bars=dbars[asset]
1 # Creating an AI model 21 curr_shares=se.get_shares(asset)
2 from sklearn.ensemble import RandomForestClassifier 22 price=se.get_last(bars)
3 clf = RandomForestClassifier() 23 free_shares=se.get_affor_shares(
4 asset,price,money)
5 # Training an AI based Trader 24 # get new information (bars),
6 clf = clf.fit(X, dy) transform it in X
7 25 bars=dbars[asset]
8 # Classifing instances 26 bars[’MA’]=se.tech.ma(bars[’close’])
9 p=clf.predict([X[-3]]) 27 X=se.ai_utils.get_X(bars,attr_list,
10 p=int(p) timeFrame,horizon)
11 labels=[’sell it’,’buy it’,’do nothing’] 28 # predict the result, using the
12 latest info
13 for i in range(-8,-15,-1): 29 p=self.clf.predict([X[-1]])
14 p=clf.predict([X[i]]) 30 if p==2:
15 l=int(p) 31 #buy it
16 print(’X[’,i,’]:’,’decision=’,labels[l], ’ 32 order=se.buyOrder(asset,
prediction=’,p) free_shares)
17 # Now let’s save the trained model 33 elif p==0:
18 import joblib 34 #sell it
19 35 order=se.sellOrder(asset,
20 print(type(clf)) curr_shares)
21 joblib.dump(clf, ’model.joblib’) 36 else:
37 order=None
Listing 9. Creating a ML model for deciding trader’s actions and saving it. 38 if order!=None:
39 orders.append(order)
1 # Creating an AI model 40 return orders
2 from sklearn.ensemble import RandomForestClassifier 41