forked from szy1900/Event_driven_framework_for_backtesting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbacktest.py
144 lines (126 loc) · 5.35 KB
/
backtest.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: utf-8 -*-
#
# backtest.py
from __future__ import print_function
import datetime
import pprint
import queue
import time
from equity_plot import plot_performance
class Backtest(object):
"""
Back_test class. The main class that capsule every thing
"""
def __init__(
self, csv_dir, symbol_list, initial_capital,
heartbeat, start_date, data_handler_cls,
execution_handler_cls, portfolio_cls, strategy_cls
):
self.csv_dir = csv_dir
self.symbol_list = symbol_list
self.initial_capital = initial_capital
self.heartbeat = heartbeat
self.start_date = start_date
self.data_handler_cls = data_handler_cls
self.execution_handler_cls = execution_handler_cls
self.portfolio_cls = portfolio_cls
self.strategy_cls = strategy_cls
self.events = queue.Queue()
self.signals = 0
self.orders = 0
self.fills = 0
self.num_strats = 1
self._generate_trading_instances()
# self.strat_params_list=strat_params_list
def _generate_trading_instances(self):
"""
Generate all the instances associated with the trading: data handler, strategy and execution_handler instance
"""
print(
"Creating DataHandler,Strategy,Portfolio and ExecutionHandler/n"
)
# print("strategy parameter list:%s..." % strategy_params_dict)
self.data_handler = self.data_handler_cls(self.events, self.csv_dir,
self.symbol_list)
self.strategy = self.strategy_cls(self.data_handler, self.events) # Create the instance of strategy
self.portfolio = self.portfolio_cls(self.data_handler, self.events, self.start_date,
self.initial_capital) # create instance of portfolio
self.execution_handler = self.execution_handler_cls(self.events)
def _run_backtest(self):
"""
执行回测
"""
i = 0
while True:
i += 1
print(i)
if self.data_handler.continue_backtest == True:
self.data_handler.update_bars() # Trigger a market event
else:
break
while True:
try:
event = self.events.get(False) ##Get an event from the Queue
except queue.Empty:
break
else:
if event is not None:
if event.type == 'MARKET':
self.strategy.calculate_signals(event) ## Trigger a Signal event #
self.portfolio.update_timeindex()
elif event.type == 'SIGNAL':
self.signals += 1
self.portfolio.update_signal(
event) # Transfer Signal Event to order Event and trigger an order event
elif event.type == 'ORDER':
self.orders += 1
self.execution_handler.execute_order(event)
elif event.type == 'FILL': # finish the order by updating the position. This is quite naive, further extention is required.
self.fills += 1
self.portfolio.update_fill(event)
time.sleep(self.heartbeat)
def _output_performance(self):
self.portfolio.create_equity_curve_dateframe() # get equity curve object
print("Creating summary stats...")
stats = self.portfolio.output_summary_stats()
print("Creating equity curve...")
print(self.portfolio.equity_curve.tail(10))
pprint.pprint(stats)
print("Signals: %s" % self.signals)
print("Orders: %s" % self.orders)
print("Fills: %s" % self.fills)
self.portfolio.equity_curve.to_csv('equity.csv')
# self.execution_handler.execution_records.set_index('date_time',inplace =True)
self.execution_handler.execution_records.to_csv('Execution_summary.csv')
def run_trading(self):
"""
模拟回测以及输出业绩结果的过程
"""
self._run_backtest()
self._output_performance()
my_plot = plot_performance(self.portfolio.equity_curve,
self.data_handler.symbol_data[self.symbol_list[0]],
self.execution_handler.execution_records )
my_plot.plot_equity_curve()
my_plot.plot_stock_curve()
my_plot.show_all_plot()
# out=open("opt.csv","w")
# spl=len(self.strat_params_list)
# for i,sp in enumerate(self.strat_params_list):
# print("Strategy %s out of %s..." %(i+1,spl))
# self._generate_trading_instances(sp)
# self._run_backtest()
# stats=self._output_performance()
# pprint.pprint(stats)
#
# tot_ret=float(stats[0][1].replace("%",""))
# sharpe=float(stats[1][1])
# max_dd=float(stats[2][1].replace("%",""))
# dd_dur=int(stats[3][1])
#
# out.write(
# "%s,%s,%s,%s,%s,%s,%s\n" %
# sp["ols_window"],sp["zscore_high"],sp["zscore_low"],
# tot_ret,sharpe,max_dd,dd_dur
# )
# out.close()