"How to fetch and backtest crypto data using fastquant"
# uncomment to install in colab
# !pip3 install fastquant --update
# or pip install git+https://www.github.com/enzoampil/fastquant.git@history
from fastquant import get_crypto_data
crypto = get_crypto_data("BTC/USDT",
"2018-12-01",
"2019-12-31",
time_resolution='1d'
)
crypto.tail()
open | high | low | close | volume | |
---|---|---|---|---|---|
dt | |||||
2019-12-27 | 7202.00 | 7275.86 | 7076.42 | 7254.74 | 33642.701861 |
2019-12-28 | 7254.77 | 7365.01 | 7238.67 | 7316.14 | 26848.982199 |
2019-12-29 | 7315.36 | 7528.45 | 7288.00 | 7388.24 | 31387.106085 |
2019-12-30 | 7388.43 | 7408.24 | 7220.00 | 7246.00 | 29605.911782 |
2019-12-31 | 7246.00 | 7320.00 | 7145.01 | 7195.23 | 25954.453533 |
from fastquant import backtest
results = backtest('smac',
crypto,
fast_period=[7,14,21,28],
slow_period=[30,45,60,75],
plot=False,
verbose=False
)
===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 30 Final Portfolio Value: 167957.05730000004 Final PnL: 67957.06 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 45 Final Portfolio Value: 200109.894525 Final PnL: 100109.89 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 60 Final Portfolio Value: 189298.80590000006 Final PnL: 89298.81 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 75 Final Portfolio Value: 258316.23405000006 Final PnL: 158316.23 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 14 slow_period : 30 Final Portfolio Value: 161429.22347500004 Final PnL: 61429.22 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 14 slow_period : 45 Final Portfolio Value: 166675.70495000004 Final PnL: 66675.7 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 14 slow_period : 60 Final Portfolio Value: 149527.12537499995 Final PnL: 49527.13 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 14 slow_period : 75 Final Portfolio Value: 229555.53917499998 Final PnL: 129555.54 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 21 slow_period : 30 Final Portfolio Value: 119204.3985 Final PnL: 19204.4 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 21 slow_period : 45 Final Portfolio Value: 162617.28744999995 Final PnL: 62617.29 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 21 slow_period : 60 Final Portfolio Value: 185407.30802499995 Final PnL: 85407.31 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 21 slow_period : 75 Final Portfolio Value: 218637.07270000002 Final PnL: 118637.07 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 28 slow_period : 30 Final Portfolio Value: 99122.65879999999 Final PnL: -877.34 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 28 slow_period : 45 Final Portfolio Value: 200118.49420000007 Final PnL: 100118.49 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 28 slow_period : 60 Final Portfolio Value: 253832.4204 Final PnL: 153832.42 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 28 slow_period : 75 Final Portfolio Value: 215884.7391 Final PnL: 115884.74 Time used (seconds): 1.2722818851470947 Optimal parameters: {'init_cash': 100000, 'buy_prop': 1, 'sell_prop': 1, 'commission': 0.0075, 'execution_type': 'close', 'channel': None, 'symbol': None, 'fast_period': 7, 'slow_period': 75} Optimal metrics: {'rtot': 0.9490143617322465, 'ravg': 0.002396500913465269, 'rnorm': 0.8292722866407841, 'rnorm100': 82.92722866407841, 'sharperatio': 0.9873670567519415, 'pnl': 158316.23, 'final_value': 258316.23405000006}
results.head()
strat_id | init_cash | buy_prop | sell_prop | commission | execution_type | channel | symbol | fast_period | slow_period | rtot | ravg | rnorm | rnorm100 | sharperatio | pnl | final_value | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 3 | 100000 | 1 | 1 | 0.0075 | close | None | None | 7 | 75 | 0.949014 | 0.002397 | 0.829272 | 82.927229 | 0.987367 | 158316.23 | 258316.234050 |
1 | 14 | 100000 | 1 | 1 | 0.0075 | close | None | None | 28 | 60 | 0.931504 | 0.002352 | 0.809002 | 80.900205 | 0.986999 | 153832.42 | 253832.420400 |
2 | 7 | 100000 | 1 | 1 | 0.0075 | close | None | None | 14 | 75 | 0.830975 | 0.002098 | 0.696898 | 69.689847 | 0.984563 | 129555.54 | 229555.539175 |
3 | 11 | 100000 | 1 | 1 | 0.0075 | close | None | None | 21 | 75 | 0.782243 | 0.001975 | 0.645083 | 64.508323 | 0.983142 | 118637.07 | 218637.072700 |
4 | 15 | 100000 | 1 | 1 | 0.0075 | close | None | None | 28 | 75 | 0.769574 | 0.001943 | 0.631874 | 63.187426 | 0.982741 | 115884.74 | 215884.739100 |
That's a 258% maximum profit using only SMAC because bitcoin was bullish all time long!
#get best parameters on top row
fast_best, slow_best = results.iloc[0][["fast_period","slow_period"]]
fast_best, slow_best
(7, 75)
import matplotlib as pl
pl.style.use("default")
pl.rcParams["figure.figsize"] = (9,5)
results = backtest('smac',
crypto,
fast_period=fast_best,
slow_period=slow_best,
plot=True,
verbose=False
)
===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 75 Final Portfolio Value: 258316.23405000006 Final PnL: 158316.23 Time used (seconds): 0.10248279571533203 Optimal parameters: {'init_cash': 100000, 'buy_prop': 1, 'sell_prop': 1, 'commission': 0.0075, 'execution_type': 'close', 'channel': None, 'symbol': None, 'fast_period': 7, 'slow_period': 75} Optimal metrics: {'rtot': 0.9490143617322465, 'ravg': 0.002396500913465269, 'rnorm': 0.8292722866407841, 'rnorm100': 82.92722866407841, 'sharperatio': 0.9873670567519415, 'pnl': 158316.23, 'final_value': 258316.23405000006}
This is done by setting return_history
=True.
results, history = backtest('smac',
crypto,
fast_period=fast_best,
slow_period=slow_best,
plot=False,
verbose=False,
return_history=True
)
===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 75 Final Portfolio Value: 258316.23405000006 Final PnL: 158316.23 Time used (seconds): 0.10444140434265137 Optimal parameters: {'init_cash': 100000, 'buy_prop': 1, 'sell_prop': 1, 'commission': 0.0075, 'execution_type': 'close', 'channel': None, 'symbol': None, 'fast_period': 7, 'slow_period': 75} Optimal metrics: {'rtot': 0.9490143617322465, 'ravg': 0.002396500913465269, 'rnorm': 0.8292722866407841, 'rnorm100': 82.92722866407841, 'sharperatio': 0.9873670567519415, 'pnl': 158316.23, 'final_value': 258316.23405000006}
results
strat_id | init_cash | buy_prop | sell_prop | commission | execution_type | channel | symbol | fast_period | slow_period | rtot | ravg | rnorm | rnorm100 | sharperatio | pnl | final_value | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 100000 | 1 | 1 | 0.0075 | close | None | None | 7 | 75 | 0.949014 | 0.002397 | 0.829272 | 82.927229 | 0.987367 | 158316.23 | 258316.23405 |
history.keys()
dict_keys(['orders', 'periodic'])
orders = history['orders']
orders
strat_id | strat_name | dt | type | price | size | value | commission | pnl | |
---|---|---|---|---|---|---|---|---|---|
0 | 0 | fast_period7_slow_period75 | 2019-02-15 | buy | 3590.56 | 27 | 96945.12 | 727.088400 | 0.00 |
1 | 0 | fast_period7_slow_period75 | 2019-08-21 | sell | 10142.57 | -27 | 96945.12 | 2053.870425 | 176904.27 |
2 | 0 | fast_period7_slow_period75 | 2019-11-02 | buy | 9231.61 | 29 | 267716.69 | 2007.875175 | 0.00 |
3 | 0 | fast_period7_slow_period75 | 2019-11-12 | sell | 8821.94 | -29 | 267716.69 | 1918.771950 | -11880.43 |
The final value in results
can be calculated from the commission
and pnl
(profit & loss) of all the closed (bought and sold) transactions in history:
r = results.squeeze()
r.final_value
258316.23405000006
r.init_cash + orders.pnl.sum() - orders.commission.sum()
258316.23405000003
results, history = backtest('smac',
crypto,
fast_period=[7,14,21],
slow_period=[30,45,60],
plot=False,
verbose=False,
return_history=True
)
===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 30 Final Portfolio Value: 167957.05730000004 Final PnL: 67957.06 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 45 Final Portfolio Value: 200109.894525 Final PnL: 100109.89 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 60 Final Portfolio Value: 189298.80590000006 Final PnL: 89298.81 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 14 slow_period : 30 Final Portfolio Value: 161429.22347500004 Final PnL: 61429.22 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 14 slow_period : 45 Final Portfolio Value: 166675.70495000004 Final PnL: 66675.7 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 14 slow_period : 60 Final Portfolio Value: 149527.12537499995 Final PnL: 49527.13 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 21 slow_period : 30 Final Portfolio Value: 119204.3985 Final PnL: 19204.4 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 21 slow_period : 45 Final Portfolio Value: 162617.28744999995 Final PnL: 62617.29 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 21 slow_period : 60 Final Portfolio Value: 185407.30802499995 Final PnL: 85407.31 Time used (seconds): 0.7071244716644287 Optimal parameters: {'init_cash': 100000, 'buy_prop': 1, 'sell_prop': 1, 'commission': 0.0075, 'execution_type': 'close', 'channel': None, 'symbol': None, 'fast_period': 7, 'slow_period': 45} Optimal metrics: {'rtot': 0.6936965022801388, 'ravg': 0.0017517588441417647, 'rnorm': 0.5549497480208785, 'rnorm100': 55.494974802087846, 'sharperatio': 0.9800219547779011, 'pnl': 100109.89, 'final_value': 200109.894525}
orders = history['orders']
orders.strat_id.unique(), orders.strat_name.unique()
(array([0, 1, 2, 3, 4, 5, 6, 7, 8]), array(['fast_period7_slow_period30', 'fast_period7_slow_period45', 'fast_period7_slow_period60', 'fast_period14_slow_period30', 'fast_period14_slow_period45', 'fast_period14_slow_period60', 'fast_period21_slow_period30', 'fast_period21_slow_period45', 'fast_period21_slow_period60'], dtype=object))
key = 'strat_id'
periodic = history['periodic']
g = periodic.set_index('dt').groupby(key)
axs = g.portfolio_value.plot(legend=key)
axs[0].set_ylabel('Returns')
axs[0].legend(g.groups, title=key, bbox_to_anchor=(1.01, 1), loc='upper left')
<matplotlib.legend.Legend at 0x7fd996d8cd90>
from fastquant import backtest
strats= {
'smac': {
'fast_period': 7,
'slow_period': 60
},
'rsi': {
'rsi_upper': 70,
'rsi_lower': 30
}
}
results, history = backtest('multi',
crypto,
strats=strats,
plot=False,
verbose=False,
return_history=True
)
===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 60 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 70 rsi_lower : 30 Final Portfolio Value: 96154.84370000003 Final PnL: -3845.16 Final Portfolio Value: 96154.84370000003 Final PnL: -3845.16 Time used (seconds): 0.12853026390075684 Optimal parameters: {'init_cash': 100000, 'buy_prop': 1, 'sell_prop': 1, 'smac.commission': 0.0075, 'execution_type': 'close', 'smac.channel': None, 'smac.symbol': None, 'smac.fast_period': 7, 'smac.slow_period': 60, 'rsi.commission': 0.0075, 'rsi.channel': None, 'rsi.symbol': None, 'rsi.rsi_period': 14, 'rsi.rsi_upper': 70, 'rsi.rsi_lower': 30} Optimal metrics: {'rtot': -0.039210338727095576, 'ravg': -9.901600688660499e-05, 'rnorm': -0.024643304876387637, 'rnorm100': -2.464330487638764, 'sharperatio': -0.12300657942849803, 'pnl': -3845.16, 'final_value': 96154.84370000003}
results
strat_id | init_cash | buy_prop | sell_prop | smac.commission | execution_type | smac.channel | smac.symbol | smac.fast_period | smac.slow_period | ... | rsi.rsi_period | rsi.rsi_upper | rsi.rsi_lower | rtot | ravg | rnorm | rnorm100 | sharperatio | pnl | final_value | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 100000 | 1 | 1 | 0.0075 | close | None | None | 7 | 60 | ... | 14 | 70 | 30 | -0.03921 | -0.000099 | -0.024643 | -2.46433 | -0.123007 | -3845.16 | 96154.8437 |
1 rows × 23 columns
orders = history['orders']
orders.strat_id.unique(), orders.strat_name.unique()
(array([0]), array(['smac.fast_period7_slow_period60', 'rsi.rsi_upper70_rsi_lower30'], dtype=object))
from fastquant import backtest
strats= {
'smac': {
'fast_period': [7,14],
'slow_period': [30,60]
},
'rsi': {
'rsi_upper': [70,80],
'rsi_lower': [20,30]
}
}
results, history = backtest('multi',
crypto,
strats=strats,
plot=False,
verbose=False,
return_history=True
)
===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 30 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 70 rsi_lower : 20 Final Portfolio Value: 101479.475225 Final PnL: 1479.48 Final Portfolio Value: 101479.475225 Final PnL: 1479.48 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 30 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 70 rsi_lower : 30 Final Portfolio Value: 92990.43297500002 Final PnL: -7009.57 Final Portfolio Value: 92990.43297500002 Final PnL: -7009.57 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 30 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 80 rsi_lower : 20 Final Portfolio Value: 165538.20930000002 Final PnL: 65538.21 Final Portfolio Value: 165538.20930000002 Final PnL: 65538.21 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 30 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 80 rsi_lower : 30 Final Portfolio Value: 151316.80185000005 Final PnL: 51316.8 Final Portfolio Value: 151316.80185000005 Final PnL: 51316.8 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 60 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 70 rsi_lower : 20 Final Portfolio Value: 107714.30350000001 Final PnL: 7714.3 Final Portfolio Value: 107714.30350000001 Final PnL: 7714.3 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 60 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 70 rsi_lower : 30 Final Portfolio Value: 96154.84370000003 Final PnL: -3845.16 Final Portfolio Value: 96154.84370000003 Final PnL: -3845.16 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 60 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 80 rsi_lower : 20 Final Portfolio Value: 134756.84680000003 Final PnL: 34756.85 Final Portfolio Value: 134756.84680000003 Final PnL: 34756.85 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 7 slow_period : 60 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 80 rsi_lower : 30 Final Portfolio Value: 120430.34112500004 Final PnL: 20430.34 Final Portfolio Value: 120430.34112500004 Final PnL: 20430.34 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 14 slow_period : 30 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 70 rsi_lower : 20 Final Portfolio Value: 92345.06152500001 Final PnL: -7654.94 Final Portfolio Value: 92345.06152500001 Final PnL: -7654.94 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 14 slow_period : 30 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 70 rsi_lower : 30 Final Portfolio Value: 83860.13070000001 Final PnL: -16139.87 Final Portfolio Value: 83860.13070000001 Final PnL: -16139.87 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 14 slow_period : 30 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 80 rsi_lower : 20 Final Portfolio Value: 127624.16555000003 Final PnL: 27624.17 Final Portfolio Value: 127624.16555000003 Final PnL: 27624.17 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 14 slow_period : 30 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 80 rsi_lower : 30 Final Portfolio Value: 115264.11515000004 Final PnL: 15264.12 Final Portfolio Value: 115264.11515000004 Final PnL: 15264.12 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 14 slow_period : 60 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 70 rsi_lower : 20 Final Portfolio Value: 90231.50885000001 Final PnL: -9768.49 Final Portfolio Value: 90231.50885000001 Final PnL: -9768.49 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 14 slow_period : 60 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 70 rsi_lower : 30 Final Portfolio Value: 84980.43325000003 Final PnL: -15019.57 Final Portfolio Value: 84980.43325000003 Final PnL: -15019.57 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 14 slow_period : 60 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 80 rsi_lower : 20 Final Portfolio Value: 111348.934 Final PnL: 11348.93 Final Portfolio Value: 111348.934 Final PnL: 11348.93 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== fast_period : 14 slow_period : 60 ===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== rsi_period : 14 rsi_upper : 80 rsi_lower : 30 Final Portfolio Value: 104417.4315 Final PnL: 4417.43 Final Portfolio Value: 104417.4315 Final PnL: 4417.43 Time used (seconds): 2.0397427082061768 Optimal parameters: {'init_cash': 100000, 'buy_prop': 1, 'sell_prop': 1, 'smac.commission': 0.0075, 'execution_type': 'close', 'smac.channel': None, 'smac.symbol': None, 'smac.fast_period': 7, 'smac.slow_period': 30, 'rsi.commission': 0.0075, 'rsi.channel': None, 'rsi.symbol': None, 'rsi.rsi_period': 14, 'rsi.rsi_upper': 80, 'rsi.rsi_lower': 20} Optimal metrics: {'rtot': 0.5040318540855331, 'ravg': 0.0012728077123372048, 'rnorm': 0.37815761213727767, 'rnorm100': 37.81576121372777, 'sharperatio': 1.834688207984273, 'pnl': 65538.21, 'final_value': 165538.20930000002}
orders = history['orders']
orders.strat_id.unique(), orders.strat_name.unique()
(array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]), array(['smac.fast_period7_slow_period30', 'rsi.rsi_upper70_rsi_lower20', 'rsi.rsi_upper70_rsi_lower30', 'rsi.rsi_upper80_rsi_lower20', 'rsi.rsi_upper80_rsi_lower30', 'smac.fast_period7_slow_period60', 'smac.fast_period14_slow_period30', 'smac.fast_period14_slow_period60'], dtype=object))
import numpy as np
#add a column which is a proxy buy/sell indicator for custom strategy
crypto["custom"] = crypto.close.pct_change()
results, history = backtest('custom',
crypto,
upper_limit=0.05,
lower_limit=-0.05,
plot=False,
verbose=False,
return_history=True
)
===Global level arguments=== init_cash : 100000 buy_prop : 1 sell_prop : 1 commission : 0.0075 ===Strategy level arguments=== Upper limit: 0.05 Lower limit: -0.05 Final Portfolio Value: 165576.88775000008 Final PnL: 65576.89 Time used (seconds): 0.12016487121582031 Optimal parameters: {'init_cash': 100000, 'buy_prop': 1, 'sell_prop': 1, 'commission': 0.0075, 'execution_type': 'close', 'channel': None, 'symbol': None, 'upper_limit': 0.05, 'lower_limit': -0.05, 'custom_column': 'custom'} Optimal metrics: {'rtot': 0.5042654794956734, 'ravg': 0.0012733976754941247, 'rnorm': 0.3783625190470573, 'rnorm100': 37.83625190470573, 'sharperatio': 0.7416913074113402, 'pnl': 65576.89, 'final_value': 165576.88775000008}
orders = history['orders']
orders.strat_id.unique(), orders.strat_name.unique()
(array([0]), array(['upper_limit0.05_lower_limit-0.05'], dtype=object))