3.64K 浏览
0

from tqsdk import TargetPosTask,TqApi,TqSim
from tqsdk.tafunc import ma
from tqsdk.ta import SAR,ATR,BOLL,MA

SYMBOL = ‘DCE.p2101’

long_stop = []
short_stop = []
af = 0
Acceleration = 0.05
mark_high = []
mark_low = []
api = TqApi()
position = api.get_position(SYMBOL)
klines = api.get_kline_serial(SYMBOL,300)
target_pos = TargetPosTask(api,SYMBOL)
quote = api.get_quote(SYMBOL)
current_price = klines[‘close’].iloc[-1]

def ma_lines(quote,klines):
    n1 = MA(klines,5)
    n2 = MA(klines,10)
    n3 = MA(klines,20)
    ma_n1 = n1.ma.iloc[-1]
    ma_n2 = n2.ma.iloc[-1]
    ma_n3 = n3.ma.iloc[-1]
    #print(ma_n1,ma_n2,ma_n3)
    return ma_n1,ma_n2,ma_n3

def boll_lines(quote,klilnes):
    boll = BOLL(klines,26,2)
    mid = boll[‘mid’].iloc[-1]
    top = boll[‘top’].iloc[-1]
    bottom = boll[‘bottom’].iloc[-1]
    #print(mid,top,bottom)
    return mid,top,bottom

def hs_atr(quote,klines):
    atr = ATR(klines,14)
    return atr

ma_n1,ma_n2,ma_n3 = ma_lines(quote,klines)
mid,top,bottom = boll_lines(quote,klines)
atr = hs_atr(quote,klines)

while True:
    api.wait_update()

    if api.is_changing(klines.iloc[-1],’datetime’):
        ma_n1,ma_n2,ma_n3 = ma_lines(quote,klines)
        mid,top,bottom = boll_lines(quote,klines)
        atr = hs_atr(quote,klines)
        current_price = klines[‘close’].iloc[-1]
        print(‘5分钟:%2f,10分钟:%2f,20分钟:%2f,中轨:%2f’ %(ma_n1,ma_n2,ma_n3,mid))
        if position.pos_long > 0:
            print(‘记录的最高价:%2f,K线最高价:%2f’%(mark_high[-1],klines.high.iloc[-2]))
            mark_high.append(max(mark_high[-1],klines.high.iloc[-2]))
            if mark_high[-1] > mark_high[-2] and af < 0.2:
                af = af + min(Acceleration,0.2 – af)
            long_stop = long_stop + af * (mark_high[-1] – long_stop)
            print(‘多头止盈线:%2f’ %long_stop.iloc[-1])

        elif position.pos_short > 0:
            print(‘记录的最低价:%2f,K线最低价:%2f’%(mark_low[-1],klines.low.iloc[-2]))
            mark_low.append(min(mark_low[-1],klines.low.iloc[-2]))
            if mark_low[-1] < mark_low[-2] and af < 0.2:
                af = af + min(Acceleration,0.2 – af)
            short_stop = short_stop – af * (short_stop – mark_low[-1])
            print(‘空头止盈线:%2f’ %short_stop.iloc[-1])

    if api.is_changing(quote,’last_price’):
        if position.pos_long == 0 and position.pos_short == 0:
            if (ma_n1 > ma_n2) and (ma_n2 > ma_n3) and (current_price > mid):
                print(‘做多时机,开多’)
                target_pos.set_target_volume(2)
                mark_high.append(klines.high.iloc[-2])
                long_stop = klines.low.iloc[-2] – atr.iloc[-1] * 1.2
                af = Acceleration

            elif (ma_n1 < ma_n2) and (ma_n2 < ma_n3) and (current_price < mid):
                print(‘做空时机,开空’)
                target_pos.set_target_volume(-2)
                mark_low.append(klines.low.iloc[-2])
                short_stop = klines.high.iloc[-2] + atr.iloc[-1] * 1.2
                af = Acceleration

        elif position.pos_long > 0 and quote.last_price < long_stop[-1]:
                target_pos.set_target_volume(0)
                print(‘多头平仓’)
                long_stop = 0
                short_stop = 0
                af = 0
                mark_high.clear()
                mark_low.clear()
                print(mark_high,mark_low)

        elif position.pos_short > 0 and quote.last_price > short_stop[-1]:
                target_pos.set_target_volume(0)
                print(‘空头平仓’)
                long_stop = 0
                short_stop = 0
                af = 0
                mark_high.clear()
                mark_low.clear()
                print(mark_high,mark_low)

api.close()

Shoe X 已回答的问题 2020年9月23日
0

你这是实盘代码,没有进一步的信息无法复现你的异常,建议把错误也贴一下或者改成回测的逻辑然后指出具体发生异常的时间点

lookis 已回答的问题 2020年9月22日
您正在查看2个答案中的1个,单击此处查看所有答案。