forked from 51bitquant/howtrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portfolio_backtesting.py
68 lines (58 loc) · 1.61 KB
/
portfolio_backtesting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from howtrader.app.cta_strategy.backtesting import BacktestingEngine, OptimizationSetting
from strategies.atr_rsi_strategy import AtrRsiStrategy
from strategies.boll_channel_strategy import BollChannelStrategy
from howtrader.trader.object import Interval
from datetime import datetime
def run_backtesting(strategy_class, setting, vt_symbol, interval, start, end, rate, slippage, size, pricetick, capital):
engine = BacktestingEngine()
engine.set_parameters(
vt_symbol=vt_symbol,
interval=interval,
start=start,
end=end,
rate=rate,
slippage=slippage,
size=size,
pricetick=pricetick,
capital=capital
)
engine.add_strategy(strategy_class, setting)
engine.load_data()
engine.run_backtesting()
df = engine.calculate_result()
return df
def show_portafolio(df):
engine = BacktestingEngine()
engine.calculate_statistics(df)
engine.show_chart(df)
df1 = run_backtesting(
strategy_class=AtrRsiStrategy,
setting={},
vt_symbol="BTCUSDT.BINANCE",
interval=Interval.MINUTE,
start=datetime(2020, 1, 1),
end=datetime(2021, 1, 1),
rate=4/10000,
slippage=0.2,
size=300,
pricetick=0.2,
capital=1_000_000,
)
df2 = run_backtesting(
strategy_class=BollChannelStrategy,
setting={'fixed_size': 16},
vt_symbol="BTCUSDT.BINANCE",
interval=Interval.MINUTE,
start=datetime(2020, 1, 1),
end=datetime(2021, 1, 1),
rate=4/10000,
slippage=1,
size=10,
pricetick=1,
capital=1000000,
)
print(df1)
print(df2)
dfp = df1 + df2
dfp =dfp.dropna()
show_portafolio(dfp)