以文本方式查看主题

-  金字塔客服中心 - 专业程序化交易软件提供商  (http://weistock.com/bbs/index.asp)
--  高级功能研发区  (http://weistock.com/bbs/list.asp?boardid=5)
----  这个策略回测的时候报错,可以看看吗?  (http://weistock.com/bbs/dispbbs.asp?boardid=5&id=172022)

--  作者:wxx212
--  发布时间:2019/9/12 0:18:07
--  这个策略回测的时候报错,可以看看吗?
这个策略回测的时候报错,可以看看吗?
--  作者:wxx212
--  发布时间:2019/9/12 0:20:50
--  

#导入time,os,csv,talib四个模块,备用
import time
import os
import csv
import numpy
import talib as ta


#  在这个方法中编写任何的初始化逻辑。context对象将会在你的算法策略的任何方法之间做传递。--(必须实现)
def init(context):
    # 在context中保存全局变量
    #context.s1 = "rb00"   #螺纹钢连续月份
    context.s1 =context.run_info.base_book_id
    context.myvalues1=5#参数为5
    # print("策略启动") #调试打印输出
   

#获取K线数据,并且计算高低价的加权平均线
def k_data(context):
    #获取K线数据
    HID_hight = history_bars(\'context.s1\',10000,\'1m\',\'hight\')#获取1分钟最高价
    HID_low = history_bars(\'context.s1\',10000,\'1m\',\'LOW\')#获取1分钟最低价
    HID_close = history_bars(\'context.s1\',10000,\'1m\',\'close\')#获取1分钟最低价
    HID_close = history_bars(\'context.s1\',10000,\'1m\',\'open\')#获取1分钟最低价
    #计算高低价的加权平均线
   
    ma00_h =numpy.arange([HID_hight])
    ma0_hight =ma00_h[context.myvalues1:].max()#取一定周期内的最高值
    Ma1_hight = ta.EMA(ma0_hight,context.myvalues1)#最高价的加权平均
    ma_hight =ta.ema(Ma1_hight,context.myvalues1)#最终的高线
   
    ma00_l =numpy.arange([HID_low])
    ma0_low =ma00_l[-context.myvalues1:].min()
    Ma1_low = ta.EMA(ma0_low,context.myvalues1)#最低价的加权平均
    Ma_low = ta.EMA(ma1_low,context.myvalues1)#最终的低线
    #x上1:=wma(wma(hhv(h,xn),xn),xn),LINETHICK1,colorred;高线计算公式
    #x下1:=wma(wma(llv(l,xn),xn),xn),LINETHICK1,colorgreen;低线计算公式

#计算出红绿状态
def SetPrevStatus(hid,startindex,endindex,status):
    for index in range(startindex,endindex):
        hid[index].status=status

def SetStatus(hid,ma_high,ma_low):
    status=0 #0:未知,1-红色,2-绿色
    for index in range(len(hid)):
        if ma_high[index]==-1 or ma_low[index]==-1 :
            continue
        if status==1:
            if hid[index].close < ma_low[index]:
                status=2
        elif status==2:
            if hid[index].close > ma_high[index]:
                status=1
        else:#status==0
            if hid[index].close < ma_low[index]:
                status=2
                SetPrevStatus(hid,0,index,1)
            elif hid[index].close > ma_high[index]:
                status=1
                SetPrevStatus(hid,0,index,2)
        hid.status=status

#计算出新的K线的开收高低
def SumHid(hid):
    sumhid=[]
    item=null
    index=0
    for one in hid:
        if index==0:
            item.open=one.open
            item.high=one.high
            item.low=one.low
            item.status=one.status
        else:
            if one.status!=item.status:
                sumhid.append(item)
                item.open=one.open
                item.high=one.high
                item.low=one.low
                item.status=one.status
        item.close=one.close
        if item.high<one.high:
            item.high=one.high
        if item.low>one.low:
            item.low=one.low
        index+=1
    return sumhid

def handle_bar(context):
    pass
    DIFF = ta.EMA(item.close,12)-ta.EMA(item.close,26)
    DEA = ta.EMA(DIFF,9)
    column = (DIFF-DEA)*2
    #开平多头
    if DIFF[-1]>DEA[-1] and DIFF[-2]<DEA[-2] and DIFF>0:
        buy_open(context.s1, "market", volume=10)
    if DIFF[-1]<DEA[-1] and DIFF[-2]>DEA[-2]:
        portfolio = get_portfolio(context.s1,0)
        sell_close(context.s1,"market", volume=portfolio.buy_quantity)
   
    #开平空头
    if DIFF[-1]<DEA[-1] and DIFF[-2]>DEA[-2] and DIFF<0:
        sell_open(context.s1, "market", volume=10)
    if DIFF[-1]>DEA[-1] and DIFF[-2]<DEA[-2]:
        portfolio = get_portfolio(context.s1,0)
        buy_close(context.s1,"market", volume=portfolio.buy_quantity)

 

 


 


--  作者:yukizzc
--  发布时间:2019/9/16 9:41:09
--  
def handle_bar(context):
    pass
    DIFF = ta.EMA(item.close,12)-ta.EMA(item.close,26)
    DEA = ta.EMA(DIFF,9)
    column = (DIFF-DEA)*2
    #开平多头
    if DIFF[-1]>DEA[-1] and DIFF[-2]<DEA[-2] and DIFF>0:
        buy_open(context.s1, "market", volume=10)
    if DIFF[-1]<DEA[-1] and DIFF[-2]>DEA[-2]:
        portfolio = get_portfolio(context.s1,0)
        sell_close(context.s1,"market", volume=portfolio.buy_quantity)
   
    #开平空头
    if DIFF[-1]<DEA[-1] and DIFF[-2]>DEA[-2] and DIFF<0:
        sell_open(context.s1, "market", volume=10)
    if DIFF[-1]>DEA[-1] and DIFF[-2]<DEA[-2]:
        portfolio = get_portfolio(context.s1,0)
        buy_close(context.s1,"market", volume=portfolio.buy_quantity)



这个方法里DIFF = ta.EMA(item.close,12)-ta.EMA(item.close,26)
item.close没有定义,自然就报错了。
如果要设置全局变量用context.close去做传递