Building, backtesting and deploying Autonomous Trader using mt5se

For an introduction to mt5se, see: https://paulo-al-castro.github.io/mt5se/

In [1]:
import mt5se as se
import pandas as pd
connected=se.connect()
if connected:
    print('Ok!! It is connected to se exchange!!')
else:
    print('Something went wrong! It is NOT connected to se!!')
Ok!! It is connected to se exchange!!

Let's create a Simple Algorithmic Trader

In [2]:
import mt5se as se

class MultiAssetTrader(se.Trader):
    def trade(self,dbars):
        assets=dbars.keys()
        orders=[]
        for asset in assets:
            bars=dbars[asset]
            curr_shares=se.get_shares(asset)
            money=se.get_balance()/len(assets) # divide o saldo em dinheiro igualmente entre os ativos
            # number of shares that you can buy of asset 
            price=se.get_last(bars)
            free_shares=se.get_affor_shares(asset,price,money)
            rsi=se.tech.rsi(bars)
            if rsi>=70 and free_shares>0: 
                order=se.buyOrder(asset,free_shares)
            elif  rsi<70 and curr_shares>0:
                order=se.sellOrder(asset,curr_shares)
            else:
                order=None
            if order!=None:
                orders.append(order)
        return orders  


trader=MultiAssetTrader() # also available in se.sampleTraders.MultiAssetTrader()
print(trader)
 
<__main__.MultiAssetTrader object at 0x0000017E1C5A1EC8>

Setup and check a backtest!

In [3]:
# sets Backtest options 
prestart=se.date(2018,12,10)
start=se.date(2019,1,10)
end=se.date(2019,2,27)
capital=1000000
results_file='data_equity_file.csv'
verbose=False 
assets=['GOOG','AAPL']
# Use True if you want debug information for your Trader 
#sets the backtest setup
period=se.DAILY 
 # it may be se.INTRADAY (one minute interval)
bts=se.backtest.set(assets,prestart,start,end,period,capital,results_file,verbose)
if se.backtest.checkBTS(bts): # check if the backtest setup is ok!
    print('Backtest Setup is Ok!')
else:
    print('Backtest Setup is NOT Ok!')
Backtest Setup is Ok!

Run the Backtest

In [4]:
# Running the backtest
df= se.backtest.run(trader,bts)   
# run calls the Trader. setup and trade (once for each bar)

#print the results
print(df)
print(bts['capital'])
'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 1000000.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 4}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 1000000.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 5}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 1000000.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 5}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 1000000.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 6}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 1000000.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 6}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 1000000.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 7}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 1000000.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 7}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 1000000.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 8}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 1000000.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 8}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 1000000.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 9}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 1000000.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 9}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 490970.0, 'shares_GOOG': 467.0, 'shares_AAPL': 0.0, 'curr': 10}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 490970.0, 'shares_GOOG': 467.0, 'shares_AAPL': 0.0, 'curr': 10}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 250670.0, 'shares_GOOG': 692.0, 'shares_AAPL': 0.0, 'curr': 11}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 250670.0, 'shares_GOOG': 692.0, 'shares_AAPL': 0.0, 'curr': 11}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 988342.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 12}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 988342.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 12}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 988342.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 13}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 988342.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 13}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 988342.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 14}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 988342.0, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 14}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 496027.24000000005, 'shares_GOOG': 443.0, 'shares_AAPL': 0.0, 'curr': 15}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 496027.24000000005, 'shares_GOOG': 443.0, 'shares_AAPL': 0.0, 'curr': 15}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': -5393.28999999995, 'shares_GOOG': 666.0, 'shares_AAPL': 1487.0, 'curr': 16}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': -5393.28999999995, 'shares_GOOG': 666.0, 'shares_AAPL': 1487.0, 'curr': 16}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': -5393.28999999995, 'shares_GOOG': 666.0, 'shares_AAPL': 1487.0, 'curr': 17}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': -5393.28999999995, 'shares_GOOG': 666.0, 'shares_AAPL': 1487.0, 'curr': 17}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': -5393.28999999995, 'shares_GOOG': 666.0, 'shares_AAPL': 1487.0, 'curr': 18}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': -5393.28999999995, 'shares_GOOG': 666.0, 'shares_AAPL': 1487.0, 'curr': 18}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': -5393.28999999995, 'shares_GOOG': 666.0, 'shares_AAPL': 1487.0, 'curr': 19}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': -5393.28999999995, 'shares_GOOG': 666.0, 'shares_AAPL': 1487.0, 'curr': 19}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 247694.11000000004, 'shares_GOOG': 666.0, 'shares_AAPL': 0.0, 'curr': 20}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 247694.11000000004, 'shares_GOOG': 666.0, 'shares_AAPL': 0.0, 'curr': 20}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 123957.98000000004, 'shares_GOOG': 779.0, 'shares_AAPL': 0.0, 'curr': 21}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 123957.98000000004, 'shares_GOOG': 779.0, 'shares_AAPL': 0.0, 'curr': 21}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 61237.98000000004, 'shares_GOOG': 835.0, 'shares_AAPL': 0.0, 'curr': 22}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 61237.98000000004, 'shares_GOOG': 835.0, 'shares_AAPL': 0.0, 'curr': 22}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 31000.140000000036, 'shares_GOOG': 862.0, 'shares_AAPL': 0.0, 'curr': 23}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 31000.140000000036, 'shares_GOOG': 862.0, 'shares_AAPL': 0.0, 'curr': 23}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 997302.14, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 24}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 997302.14, 'shares_GOOG': 0.0, 'shares_AAPL': 0.0, 'curr': 24}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 496384.94, 'shares_GOOG': 0.0, 'shares_AAPL': 2940.0, 'curr': 25}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 496384.94, 'shares_GOOG': 0.0, 'shares_AAPL': 2940.0, 'curr': 25}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 247612.77999999997, 'shares_GOOG': 0.0, 'shares_AAPL': 4396.0, 'curr': 26}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 247612.77999999997, 'shares_GOOG': 0.0, 'shares_AAPL': 4396.0, 'curr': 26}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 123084.77999999997, 'shares_GOOG': 0.0, 'shares_AAPL': 5120.0, 'curr': 27}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 123084.77999999997, 'shares_GOOG': 0.0, 'shares_AAPL': 5120.0, 'curr': 27}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 62037.77999999997, 'shares_GOOG': 0.0, 'shares_AAPL': 5477.0, 'curr': 28}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 62037.77999999997, 'shares_GOOG': 0.0, 'shares_AAPL': 5477.0, 'curr': 28}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 30771.839999999967, 'shares_GOOG': 0.0, 'shares_AAPL': 5658.0, 'curr': 29}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 30771.839999999967, 'shares_GOOG': 0.0, 'shares_AAPL': 5658.0, 'curr': 29}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 15286.729999999967, 'shares_GOOG': 0.0, 'shares_AAPL': 5747.0, 'curr': 30}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 15286.729999999967, 'shares_GOOG': 0.0, 'shares_AAPL': 5747.0, 'curr': 30}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 7803.439999999967, 'shares_GOOG': 0.0, 'shares_AAPL': 5790.0, 'curr': 31}
Esta em backtest. bts= {'verbose': False, 'prestart': datetime.datetime(2018, 12, 10, 0, 0), 'start': datetime.datetime(2019, 1, 10, 0, 0), 'end': datetime.datetime(2019, 2, 27, 0, 0), 'type': 1, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL'], 'capital': 7803.439999999967, 'shares_GOOG': 0.0, 'shares_AAPL': 5790.0, 'curr': 31}
End of backtest with  32  bars,  saving equity file in  data_equity_file.csv
         date     balance      equity  load
0  2019-01-11  1000000.00  1000000.00   0.0
1  2019-01-14  1000000.00  1000000.00   0.0
2  2019-01-15  1000000.00  1000000.00   0.0
3  2019-01-16  1000000.00  1000000.00   0.0
4  2019-01-17  1000000.00  1000000.00   0.0
5  2019-01-18  1000000.00  1000000.00   0.0
6  2019-01-22  1000000.00  1000000.00   0.0
7  2019-01-23  1000000.00  1000000.00   0.0
8  2019-01-24  1000000.00  1000000.00   0.0
9  2019-01-25   490970.00  1000000.00   0.0
10 2019-01-28   250670.00   989726.00   0.0
11 2019-01-29   988342.00   988342.00   0.0
12 2019-01-30   988342.00   988342.00   0.0
13 2019-01-31   988342.00   988342.00   0.0
14 2019-02-01   496027.24   988342.00   0.0
15 2019-02-04    -5393.29   986428.24   0.0
16 2019-02-05    -5393.29  1015323.06   0.0
17 2019-02-06    -5393.29   996180.84   0.0
18 2019-02-07    -5393.29   980535.35   0.0
19 2019-02-08   247694.11   977064.01   0.0
20 2019-02-11   123957.98   976970.77   0.0
21 2019-02-12    61237.98   996437.98   0.0
22 2019-02-13    31000.14   996371.18   0.0
23 2019-02-14   997302.14   997302.14   0.0
24 2019-02-15   496384.94   997302.14   0.0
25 2019-02-19   247612.78   998713.34   0.0
26 2019-02-20   123084.78  1003724.78   0.0
27 2019-02-21    62037.78   998604.78   0.0
28 2019-02-22    30771.84  1008134.76   0.0
29 2019-02-25    15286.73  1015207.26   0.0
30 2019-02-26     7803.44  1015437.14   0.0
31 2019-02-27     3954.76  1020706.04   0.0
3954.759999999967

Evaluate the Backtest result

In [5]:
# evaluates the backtest results
se.backtest.evaluate(df)
 -----------------------   Backtest Report  ------------------------------- 

Total Return (%)=2.07 in 31 bars 
Average Bar Return (%)=0.07  
Std Deviation of returns (%) =0.8539

 ----------------------        End of Report     -------------------------------- 

Deploying a Trader for operation on real (or simulated ) Accounts

In [2]:
import mt5se as se

class MultiAssetTrader(se.Trader):  # It is the same trader!!! There is no need to change it from backtest to live operation!!!!
    def trade(self,dbars):
        assets=dbars.keys()
        orders=[]
        for asset in assets:
            bars=dbars[asset]
            curr_shares=se.get_shares(asset)
            money=se.get_balance()/len(assets) # divide o saldo em dinheiro igualmente entre os ativos
            # number of shares that you can buy of asset 
            price=se.get_last(bars)
            free_shares=se.get_affor_shares(asset,price,money)
            rsi=se.tech.rsi(bars)
            if rsi>=70 and free_shares>0: 
                order=se.buyOrder(asset,free_shares)
            elif  rsi<70 and curr_shares>0:
                order=se.sellOrder(asset,curr_shares)
            else:
                order=None
            if order!=None:
                orders.append(order)
        return orders  


trader=MultiAssetTrader() # also available in se.sampleTraders.MultiAssetTrader()
print(trader)
 
<__main__.MultiAssetTrader object at 0x000001D3A13B1D88>

Setup the operation!

Observe that there are some differences between setuping a backtest and a live operation, see below!

In [7]:
    #account data
login=None #'your account Number!' 
password=None # 'Guess what is it..'
# if login and password are not defined (i.e. None), it will use the default account defined in Metatrader
#trading data
assets=['GOOG','AAPL','TSLA']
endTime=se.now(minOffset=1)   # it will run for one minute!
#endTime=se.now(hourOffset=1,minOffset=30) # the trader will run for 1:30h after started!
 
capital=100000
data_file='data_equity_file.csv'
verbose=False
timeFrame=se.INTRADAY
delay=20 # seconds to wait between trade calls
waitForOpen=True
mem=10 
# number of bars to take into account


# define operation setup (ops)  (no se.backtest.set)
ops=se.operations.set(assets,capital,endTime,mem,timeFrame,data_file,verbose,delay,waitForOpen)
print(ops)
{'waitForOpen': True, 'verbose': False, 'delay': 20, 'mem': 10, 'end': datetime.datetime(2021, 4, 29, 14, 28, 51, 129404), 'type': 2, 'file': 'data_equity_file.csv', 'assets': ['GOOG', 'AAPL', 'TSLA'], 'capital': 100000.0}

Connect to the Stock Exchange thorugh your Broker and execute trader through given Account

In [9]:
#Connect to the Stock Exchange
#se.connect()

#executes the trader, according setup
se.operations.run(trader,ops)

Next, see our notebook about building AI based Autonomous Trader