forked from hhuhhu/trade
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
365 lines (300 loc) · 13.7 KB
/
main.py
File metadata and controls
365 lines (300 loc) · 13.7 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# -*- coding: utf-8 -*-
#
# Copyright 2017 Ricequant, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
import tarfile
import tempfile
import datetime
import shutil
import click
import jsonpickle.ext.numpy as jsonpickle_numpy
import pytz
import requests
import six
import better_exceptions
import const
from api import helper as api_helper
from core.strategy_loader import FileStrategyLoader, SourceCodeStrategyLoader, UserFuncStrategyLoader
from core.strategy import Strategy
from core.strategy_universe import StrategyUniverse
from core.global_var import GlobalVars
from core.strategy_context import StrategyContext
from data.base_data_source import BaseDataSource
from data.data_proxy import DataProxy
from environment import Environment
from events import EVENT, Event
from execution_context import ExecutionContext
from interface import Persistable
from mod import ModHandler
from model.bar import BarMap
from utils import create_custom_exception, run_with_user_log_disabled, scheduler as mod_scheduler
from utils.exception import CustomException, is_user_exc, patch_user_exc
from utils.i18n import gettext as _
from utils.logger import user_log, user_system_log, system_log, user_print, user_detail_log
from utils.persisit_helper import CoreObjectsPersistProxy, PersistHelper
from utils.scheduler import Scheduler
from utils.config import set_locale
jsonpickle_numpy.register_handlers()
def _adjust_start_date(config, data_proxy):
origin_start_date, origin_end_date = config.base.start_date, config.base.end_date
start, end = data_proxy.available_data_range(config.base.frequency)
# print(repr(start), repr(end))
config.base.start_date = max(start, config.base.start_date)
config.base.end_date = min(end, config.base.end_date)
config.base.trading_calendar = data_proxy.get_trading_dates(config.base.start_date, config.base.end_date)
if len(config.base.trading_calendar) == 0:
raise patch_user_exc(ValueError(_(u"There is no trading day between {start_date} and {end_date}.").format(
start_date=origin_start_date, end_date=origin_end_date)))
config.base.start_date = config.base.trading_calendar[0].date()
config.base.end_date = config.base.trading_calendar[-1].date()
config.base.timezone = pytz.utc
def _validate_benchmark(config, data_proxy):
benchmark = config.base.benchmark
if benchmark is None:
return
instrument = data_proxy.instruments(benchmark)
if instrument is None:
raise patch_user_exc(ValueError(_(u"invalid benchmark {}").format(benchmark)))
if instrument.order_book_id == "000300.XSHG":
# 000300.XSHG 数据进行了补齐,因此认为只要benchmark设置了000300.XSHG,就存在数据,不受限于上市日期。
return
config = Environment.get_instance().config
start_date = config.base.start_date
end_date = config.base.end_date
if instrument.listed_date.date() > start_date:
raise patch_user_exc(ValueError(
_(u"benchmark {benchmark} has not been listed on {start_date}").format(benchmark=benchmark,
start_date=start_date)))
if instrument.de_listed_date.date() < end_date:
raise patch_user_exc(ValueError(
_(u"benchmark {benchmark} has been de_listed on {end_date}").format(benchmark=benchmark,
end_date=end_date)))
def create_benchmark_portfolio(env):
if env.config.base.benchmark is None:
return None
from const import ACCOUNT_TYPE
from model.account import BenchmarkAccount
from model.position import Positions, StockPosition
from model.portfolio import Portfolio
accounts = {}
config = env.config
start_date = config.base.start_date
total_cash = 0
for account_type in config.base.account_list:
if account_type == ACCOUNT_TYPE.STOCK:
total_cash += config.base.stock_starting_cash
elif account_type == ACCOUNT_TYPE.FUTURE:
total_cash += config.base.future_starting_cash
else:
raise NotImplementedError
accounts[ACCOUNT_TYPE.BENCHMARK] = BenchmarkAccount(total_cash, Positions(StockPosition))
return Portfolio(start_date, 1, total_cash, accounts)
def create_base_scope():
import copy
import user_module
scope = copy.copy(user_module.__dict__)
scope.update({
"logger": user_log,
"print": user_print,
})
return scope
def update_bundle(data_bundle_path=None, locale="zh_Hans_CN", confirm=True):
set_locale(locale)
default_bundle_path = os.path.abspath(os.path.expanduser("~/.rqalpha/bundle/"))
if data_bundle_path is None:
data_bundle_path = default_bundle_path
else:
data_bundle_path = os.path.abspath(os.path.join(data_bundle_path, './bundle/'))
if (confirm and os.path.exists(data_bundle_path) and data_bundle_path != default_bundle_path and
os.listdir(data_bundle_path)):
click.confirm(_(u"""
[WARNING]
Target bundle path {data_bundle_path} is not empty.
The content of this folder will be REMOVED before updating.
Are you sure to continue?""").format(data_bundle_path=data_bundle_path), abort=True)
day = datetime.date.today()
tmp = os.path.join(tempfile.gettempdir(), 'rq.bundle')
while True:
url = 'http://7xjci3.com1.z0.glb.clouddn.com/bundles_v3/rqbundle_%04d%02d%02d.tar.bz2' % (
day.year, day.month, day.day)
# url = 'http://7xjci3.com1.z0.glb.clouddn.com/bundles_v3/rqbundle_%04d%02d%02d.tar.bz2' % (
# 2017, 3, 2)
six.print_(_(u"try {} ...").format(url))
r = requests.get(url, stream=True)
if r.status_code != 200:
day = day - datetime.timedelta(days=1)
continue
out = open(tmp, 'wb')
total_length = int(r.headers.get('content-length'))
with click.progressbar(length=total_length, label=_(u"downloading ...")) as bar:
for data in r.iter_content(chunk_size=8192):
bar.update(len(data))
out.write(data)
out.close()
break
shutil.rmtree(data_bundle_path, ignore_errors=True)
os.makedirs(data_bundle_path)
tar = tarfile.open(tmp, 'r:bz2')
tar.extractall(data_bundle_path)
tar.close()
os.remove(tmp)
six.print_(_(u"Data bundle download successfully in {bundle_path}").format(bundle_path=data_bundle_path))
def run(config, source_code=None, user_funcs=None):
env = Environment(config)
persist_helper = None
init_succeed = False
mod_handler = ModHandler()
try:
if source_code is not None:
env.set_strategy_loader(SourceCodeStrategyLoader(source_code))
elif user_funcs is not None:
env.set_strategy_loader(UserFuncStrategyLoader(user_funcs))
else:
env.set_strategy_loader(FileStrategyLoader(config.base.strategy_file))
env.set_global_vars(GlobalVars())
mod_handler.set_env(env)
mod_handler.start_up()
if not env.data_source:
env.set_data_source(BaseDataSource(config.base.data_bundle_path))
env.set_data_proxy(DataProxy(env.data_source))
Scheduler.set_trading_dates_(env.data_source.get_trading_calendar())
scheduler = Scheduler(config.base.frequency)
mod_scheduler._scheduler = scheduler
env._universe = StrategyUniverse()
_adjust_start_date(env.config, env.data_proxy)
_validate_benchmark(env.config, env.data_proxy)
broker = env.broker
assert broker is not None
env.portfolio = broker.get_portfolio()
env.benchmark_portfolio = create_benchmark_portfolio(env)
event_source = env.event_source
assert event_source is not None
bar_dict = BarMap(env.data_proxy, config.base.frequency)
env.set_bar_dict(bar_dict)
if env.price_board is None:
from core.bar_dict_price_board import BarDictPriceBoard
env.price_board = BarDictPriceBoard()
ctx = ExecutionContext(const.EXECUTION_PHASE.GLOBAL)
ctx._push()
# FIXME
start_dt = datetime.datetime.combine(config.base.start_date, datetime.datetime.min.time())
env.calendar_dt = start_dt
env.trading_dt = start_dt
env.event_bus.publish_event(Event(EVENT.POST_SYSTEM_INIT))
scope = create_base_scope()
scope.update({
"g": env.global_vars
})
apis = api_helper.get_apis(config.base.account_list)
scope.update(apis)
scope = env.strategy_loader.load(scope)
if env.config.extra.enable_profiler:
enable_profiler(env, scope)
ucontext = StrategyContext()
user_strategy = Strategy(env.event_bus, scope, ucontext)
scheduler.set_user_context(ucontext)
if not config.extra.force_run_init_when_pt_resume:
with run_with_user_log_disabled(disabled=config.base.resume_mode):
user_strategy.init()
if config.extra.context_vars:
for k, v in six.iteritems(config.extra.context_vars):
setattr(ucontext, k, v)
if config.base.persist:
persist_provider = env.persist_provider
persist_helper = PersistHelper(persist_provider, env.event_bus, config.base.persist_mode)
persist_helper.register('core', CoreObjectsPersistProxy(scheduler))
persist_helper.register('user_context', ucontext)
persist_helper.register('global_vars', env.global_vars)
persist_helper.register('universe', env._universe)
if isinstance(event_source, Persistable):
persist_helper.register('event_source', event_source)
persist_helper.register('portfolio', env.portfolio)
if env.benchmark_portfolio:
persist_helper.register('benchmark_portfolio', env.benchmark_portfolio)
for name, module in six.iteritems(env.mod_dict):
if isinstance(module, Persistable):
persist_helper.register('mod_{}'.format(name), module)
# broker will restore open orders from account
if isinstance(broker, Persistable):
persist_helper.register('broker', broker)
persist_helper.restore()
env.event_bus.publish_event(Event(EVENT.POST_SYSTEM_RESTORED))
init_succeed = True
# When force_run_init_when_pt_resume is active,
# we should run `init` after restore persist data
if config.extra.force_run_init_when_pt_resume:
assert config.base.resume_mode == True
with run_with_user_log_disabled(disabled=False):
user_strategy.init()
from core.executor import Executor
Executor(env).run(bar_dict)
# print(env.__dict__)
if env.profile_deco:
output_profile_result(env)
result = mod_handler.tear_down(const.EXIT_CODE.EXIT_SUCCESS)
system_log.debug(_(u"strategy run successfully, normal exit"))
return result
except CustomException as e:
if init_succeed and env.config.base.persist and persist_helper:
persist_helper.persist()
user_detail_log.exception(_(u"strategy execute exception"))
user_system_log.error(e.error)
better_exceptions.excepthook(e.error.exc_type, e.error.exc_val, e.error.exc_tb)
mod_handler.tear_down(const.EXIT_CODE.EXIT_USER_ERROR, e)
except Exception as e:
if init_succeed and env.config.base.persist and persist_helper:
persist_helper.persist()
exc_type, exc_val, exc_tb = sys.exc_info()
user_exc = create_custom_exception(exc_type, exc_val, exc_tb, config.base.strategy_file)
better_exceptions.excepthook(exc_type, exc_val, exc_tb)
user_system_log.error(user_exc.error)
code = const.EXIT_CODE.EXIT_USER_ERROR
if not is_user_exc(exc_val):
system_log.exception(_(u"strategy execute exception"))
code = const.EXIT_CODE.EXIT_INTERNAL_ERROR
else:
user_detail_log.exception(_(u"strategy execute exception"))
mod_handler.tear_down(code, user_exc)
def enable_profiler(env, scope):
# decorate line profiler
import line_profiler
import inspect
env.profile_deco = profile_deco = line_profiler.LineProfiler()
for name in scope:
obj = scope[name]
if getattr(obj, "__module__", None) != "rqalpha.user_module":
continue
if inspect.isfunction(obj):
scope[name] = profile_deco(obj)
if inspect.isclass(obj):
for key, val in six.iteritems(obj.__dict__):
if inspect.isfunction(val):
setattr(obj, key, profile_deco(val))
def output_profile_result(env):
stdout_trap = six.StringIO()
env.profile_deco.print_stats(stdout_trap)
profile_output = stdout_trap.getvalue()
profile_output = profile_output.rstrip()
six.print_(profile_output)
env.event_bus.publish_event(Event(EVENT.ON_LINE_PROFILER_RESULT, result=profile_output))
if __name__ == '__main__':
from util import load_config
from utils.config import RqAttrDict
config = load_config(config_path='config.yml')
config = RqAttrDict(config)
result = run(config=config)
print(result)
# update_bundle()