VeighNa量化社区
你的开源社区量化交易平台
mxw
mxw's Avatar
Member
离线
8 帖子
声望: 1

Traceback (most recent call last):
File "D:\conda\vnpy\app\data_manager\ui\widget.py", line 381, in update_data
overview.end
File "D:\conda\vnpy\app\data_manager\engine.py", line 208, in download_bar_data
data = rqdata_client.query_history(req)
File "D:\conda\vnpy\trader\rqdata.py", line 182, in query_history
dt = row.name.to_pydatetime() - adjustment
AttributeError: 'tuple' object has no attribute 'to_pydatetime'

组合策略加载多个不同交易时间段合约报错,比如加载IF、RB、SC,在15点和23点之后几分钟内策略报错(可能由于交易所休市原因),这会导致策略进程终止,但没有写入策略数据字典,导致后续策略执行有误。

from datetime import datetime
from vnpy.trader.constant import Exchange,Interval
from vnpy.trader.database import database_manager
import matplotlib.pyplot as plt
import matplotlib.dates as pld
import talib
import numpy as np
import pandas as pd

Load history data

bars =database_manager.load_bar_data(
symbol="RBL8",
exchange=Exchange.SHFE,
interval=Interval.MINUTE,
start=datetime(2021, 5, 25),
end=datetime(2021, 5, 30)
)
window = 15
window_bar = 0

取历史开收高低量时间数据

o = []
c = []
h = []
l = []
v = []
t = []
open_price = 0.0
close_price = 0.0
high_price = 0.0
low_price = 0.0
volume = 0.0
for bar in bars:
if open_price > 0.0:
if window_bar == 1:
if (datetime.time(time).minute + 1) % window > 0:
time = bar.datetime.replace(second=59, microsecond=0)
high_price = max(high_price,bar.high_price)
low_price = min(low_price,bar.low_price)
volume += int(bar.volume)
if (datetime.time(time).minute + 1) % window == 0:
time = bar.datetime.replace(second=59, microsecond=0)
close_price = bar.close_price
volume += int(bar.volume)
t.append(time)
o.append(open_price)
c.append(close_price)
h.append(high_price)
l.append(low_price)
v.append(volume)
open_price = 0.0
close_price = 0.0
high_price = 0.0
low_price = 0.0
volume = 0.0

else: 
    if window_bar == 1:
        time = bar.datetime.replace(second=59, microsecond=0)
        open_price = bar.open_price
        close_price = bar.close_price
        high_price = bar.high_price
        low_price = bar.low_price
        volume = bar.volume
    if window_bar == 0:
        time = bar.datetime.replace(second=59, microsecond=0)
        open_price = bar.open_price
        close_price = bar.close_price
        high_price = bar.high_price
        low_price = bar.low_price
        volume = bar.volume
        if (datetime.time(time).minute + 1) % window == 0:    
            t.append(time)
            o.append(open_price)
            c.append(close_price)
            h.append(high_price)
            l.append(low_price)
            v.append(volume)
            open_price = 0.0
            close_price = 0.0
            high_price = 0.0
            low_price = 0.0
            volume = 0.0
        window_bar = 1

listup,listdown = [],[]
mxwdata = pd.DataFrame()
mxwdata['time'] =(t)
mxwdata['open'] =(o)
mxwdata['close'] =(c)
mxwdata['high'] =(h)
mxwdata['low'] =(l)
mxwdata['colors'] ="r"
mxwdata['edge_colors'] ="b"
onset = 40 /len(mxwdata)
mxwdata['position'] = [x-onset for x in range(len(mxwdata))]
for x in range(len(mxwdata)):
if mxwdata['open'][x] > mxwdata['close'][x]:
mxwdata['colors'][x] = "r"
else:
mxwdata['colors'][x] = "g"
mxwdata['sma5'] = talib.SMA(np.array(mxwdata['close']) ,5)
mxwdata['sma10'] = talib.SMA(np.array(mxwdata['close']) ,10)

for i in range(1,len(mxwdata['close'])):
if mxwdata.loc[i,'sma5'] > mxwdata.loc[i,'sma10'] and mxwdata.loc[i-1,'sma5'] < mxwdata.loc[i-1,'sma10']:
listup.append(i)
elif mxwdata.loc[i,'sma5'] < mxwdata.loc[i,'sma10'] and mxwdata.loc[i-1,'sma5'] > mxwdata.loc[i-1,'sma10']:
listdown.append(i)

fig=plt.figure(figsize=(40,20))

plt.plot(logdata['close'], color='b', lw=3.)

plt.bar(mxwdata['position'], (mxwdata['close'] - mxwdata['open']),0.8,bottom=mxwdata['open'],color=mxwdata['colors'],edgecolor=mxwdata['edge_colors'], zorder=3)
plt.vlines(mxwdata['position'], mxwdata['low'], mxwdata['high'], color=mxwdata['edge_colors'],lw = 8)

plt.plot(mxwdata['sma5'], color='r', lw=2.)
plt.plot(mxwdata['sma10'], color='g', lw=2.)
plt.plot(mxwdata['close'], '^', markersize=5, color='r', label='UP signal', markevery=listup)
plt.plot(mxwdata['close'], 'v', markersize=5, color='g', label='DOWN signal', markevery=listdown)
plt.legend()

plt.gcf().autofmt_xdate()

plt.show()

subdata = pd.DataFrame()
subdata['std20'] = talib.STDDEV( np.array(mxwdata['close']) ,20)
subdata['rsi30'] = talib.RSI(np.array(mxwdata['close']) ,30)
subdata.plot(subplots=True,figsize=(40,20))

输入tjpd 输出 a,b
tjpd a b
0 0 1
0 0 2
0 0 3
0 0 4
1 1 5
0 0 6
0 0 7
0 0 8
0 0 9
1 2 10
0 0 11
0 0 12
0 0 13
1 3 14
需要在vnpy框架下写,请帮检查一下这个函数的一些问题:
1、tjpd是个条件数组,这样定义对吗?
2、a、b是否要定义初始值呢?

def ceshi(
    self,
    tjpd: bool,
    array: bool = False
) -> Union[
    Tuple[np.ndarray, np.ndarray],
    Tuple[int, int]
]:
    """
    ceshi
    """
    i=0
    j=0
    while(a[-i]<=3):
        b[-i-1]=b[-i]+1
        if tjpd[-i]:
            a[-i]=j+1
            j=j+1
        else :
            a[-i]=0
        i=i+1
    if array:
        return a, b
    return a[-1], b[-1]
© 2015-2022 微信 18391752892
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】