ricequant / rqalpha

A extendable, replaceable Python algorithmic backtest && trading framework supporting multiple securities
http://rqalpha.io
Other
5.38k stars 1.61k forks source link

执行日线回测,在某一段时间market_value变为nan #177

Closed lpty closed 7 years ago

lpty commented 7 years ago

提 ISSUE 须知

请先阅读文档 rqalpha文档

如果仍有问题的话请在 issue列表 中寻找是否有相关问题的解决方案

如果没有的话 麻烦开一个issue 描述以下问题:

1. RQAlpha的版本

Current Version:  (3, 0, 2)

2. Python的版本

Python 2.7.12

3. 是Windows/Linux/MacOS or others?

Ubuntu16.04 LTS

4. 问题描述

(1)使用自己数据(类似tushare),安装mod修改数据源 (2)执行日线回测,在某一段时间market_value变为nan,且后续下单提示‘Cannot convert NaN to integer’,导致下单失败,最后matplotlib曲线对应时间段曲线缺失

(3)上述两处accounts值打印于rqalpha源码处,具体如下: 文件名:executor.py

        ....
        elif event.event_type == EVENT.BAR:
            bar_dict.update_dt(event.calendar_dt, self._env.portfolio.accounts)
       ....

文件名:bar.py

class BarMap(object):
    def __init__(self, data_proxy, frequency):
        self._dt = None
        self._data_proxy = data_proxy
        self._frequency = frequency
        self._cache = {}

    def update_dt(self, dt, e='aaa'):
        print(e)
        self._dt = dt
        self._cache.clear()
        print(e)
        i = 1

(4)除上述代码未修改源码其他地方,怀疑是self_cache字典清空导致数据出现问题,麻烦分析一下

lpty commented 7 years ago

2016-06-06 INFO per bar {'STOCK': StockAccount({'market_value': nan, 'frozen_cash': 0.0, 'positions': [u'000651', u'600036', u'601166', u'601318'], 'total_value': nan, 'dividend_receivable': 915.0, 'cash': 24703.727200000008, 'transaction_cost': 0, 'type': 'STOCK'})} Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/rqalpha/core/strategy.py", line 84, in handle_bar self._handle_bar(self._user_context, bar_dict) │ │ └ BarMap() │ └ <rqalpha.core.strategy.Strategy object at 0x7fac916102d0> └ <rqalpha.core.strategy.Strategy object at 0x7fac916102d0> File "test.py", line 40, in handle_bar order_target_percent(stock, target_percent) │ │ └ 0.2459 │ └ u'601166' └ <function order_target_percent at 0x7fac9a969a28> File "/usr/local/lib/python2.7/dist-packages/rqalpha/execution_context.py", line 106, in wrapper return func(*args, kwargs) │ │ └ {} │ └ (u'601166', 0.2459) └ <function order_target_percent at 0x7fac9a969b18> File "/usr/local/lib/python2.7/dist-packages/rqalpha/utils/arg_checker.py", line 378, in api_rule_check_wrapper return func(*args, kwargs) │ │ └ {} │ └ (u'601166', 0.2459) └ <function order_target_percent at 0x7fac9a969aa0> File "/usr/local/lib/python2.7/dist-packages/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py", line 395, in order_target_percent return order_value(order_book_id, account.total_value percent - position.market_value, style=style) │ │ │ │ │ └ ORDER_TYPE.MARKET │ │ │ │ └ StockPosition({'market_value': 23115.0, 'value_percent': nan, 'last_price': 15.41, 'pnl': -430.4347826086925, 'avg_price': 15.69... │ │ │ └ 0.2459 │ │ └ StockAccount({'market_value': nan, 'frozen_cash': 0.0, 'positions': [u'000651', u'600036', u'601166', u'601318'], 'total_value':... │ └ u'601166' └ <function order_value at 0x7fac9a9692a8> File "/usr/local/lib/python2.7/dist-packages/rqalpha/execution_context.py", line 106, in wrapper return func(args, kwargs) │ │ └ {'style': ORDER_TYPE.MARKET} │ └ (u'601166', nan) └ <function order_value at 0x7fac9a969398> File "/usr/local/lib/python2.7/dist-packages/rqalpha/utils/arg_checker.py", line 378, in api_rule_check_wrapper return func(*args, kwargs) │ │ └ {'style': ORDER_TYPE.MARKET} │ └ (u'601166', nan) └ <function order_value at 0x7fac9a969320> File "/usr/local/lib/python2.7/dist-packages/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py", line 250, in order_value amount = int(Decimal(cash_amount) / Decimal(price) / Decimal(round_lot)) * round_lot │ │ │ │ │ │ └ 100 │ │ │ │ │ └ 100 │ │ │ │ └ <class 'decimal.Decimal'> │ │ │ └ 15.41 │ │ └ <class 'decimal.Decimal'> │ └ nan └ <class 'decimal.Decimal'> File "/usr/lib/python2.7/decimal.py", line 1595, in int raise ValueError("Cannot convert NaN to integer") ValueError: Cannot convert NaN to integer 2016-06-06 ERROR Traceback (most recent call last): File test.py, line 40 in handle_bar order_target_percent(stock, target_percent) --> dates = ['2015-01-28', '2015-01-29', '2015-03-18', '2015-04-02', '2015-04-15', '2015-04-29', '2015-04-30', '2015-05-07', '2015-05-14', '2015-06-19', '2015-06-23', '2015 ... --> now = '2016-06-06' --> intdates = [1422415457875L, 1422499480344L, 1426647838341L, 1427982581145L, 1429064147605L, 1430271655520L, 1430361301426L, 1430966248536L, 1431572110755L, 1434679163077L, ... --> symbol = 'ZH179167' --> stocks = [u'600000', u'600004', u'600006', u'600007', u'600008', u'600009', u'600010', u'600011', u'600012', u'600015', u'600016', u'600017', u'600018', u'600019', u'600 ... --> balances = [{u'stock_id': 1000839, u'target_weight': 24.59, u'weight': 24.59, u'prev_net_value': 0.27788191, u'target_volume': 0.01999978, u'created_at': 1465176647467L, u ... --> target_percent = 0.2459 --> s = {u'code': u'300678'} --> context = Context({intdates = [1422415457875L, 1422499480344L, 1426647838341L, 1427982581145L, 1429064147605L, 1430271655520L, 1430361301426L, 1430966248536L, 14315721107 ... --> table = Collection(Database(MongoClient(host=['10.253.101.47:27017'], document_class=dict, tz_aware=False, connect=True), u'zuhe'), u'xueqiu_rebalancing') --> balance = {u'stock_id': 1000839, u'target_weight': 24.59, u'weight': 24.59, u'prev_net_value': 0.27788191, u'target_volume': 0.01999978, u'created_at': 1465176647467L, u' ... --> bar_dict = BarMap() --> stock = u'601166'

ValueError: Cannot convert NaN to integer [2017-07-28 11:16:18.468149] ERROR: system_log: 策略运行产生异常 Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/rqalpha/main.py", line 293, in run Executor(env).run(bar_dict) File "/usr/local/lib/python2.7/dist-packages/rqalpha/core/executor.py", line 65, in run event_bus.publish_event(event) File "/usr/local/lib/python2.7/dist-packages/rqalpha/events.py", line 43, in publish_event if l(event): File "/usr/local/lib/python2.7/dist-packages/rqalpha/utils/init.py", line 180, in wrapper return func(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/rqalpha/core/strategy.py", line 84, in handle_bar self._handle_bar(self._user_context, bar_dict) File "/usr/local/lib/python2.7/dist-packages/rqalpha/execution_context.py", line 95, in exit raise user_exc CustomException

lpty commented 7 years ago

框架提供本地日行情数据在股票停牌时间进行了补齐,我使用的数据源未补齐出现的错误