vn.py官网
开源量化社区
Member
avatar
加入于:
帖子: 6
声望: 1

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))

Member
avatar
加入于:
帖子: 6
声望: 1

几乎没发过帖子,好像发出来还是乱的

© 2015-2019 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3

沪公网安备 31011502017034号