4.59K 浏览
0

A线=(C-LLV(L,21))/(HHV(H,21)-LLV(L,21))

B线=(HHV(H,55)-C)/(HHV(H,55)-LLV(L,55))+0.1

求哪位老师帮忙编辑一个天勤公式:当A线上穿B线形成金叉时开多,当A线下穿B线形成死叉时开空。亏损5%时止损,盈利20%时止盈的策略,谢谢

聪 王 已回答的问题 2020年1月2日
1
from tqsdk import TqApi, TqSim, TargetPosTask, tafunc

api = TqApi()
# 周期参数
lenth1, lenth2 = 21, 55
# 获得 rb2005 10秒K线的引用
klines = api.get_kline_serial("SHFE.rb2005", 60, max(lenth1,lenth2)+2)
quote = api.get_quote("SHFE.rb2005")
position =api.get_position("SHFE.rb2005")
gainPrice, lossPrice = 0, 0
account = api.get_account()
while True:
    #判断开仓条件
    while True:
        api.wait_update()
        if api.is_changing(klines):
            ## A线 = (C - LLV(L, 21)) / (HHV(H, 21) - LLV(L, 21))
            ## B线 = (HHV(H, 55) - C) / (HHV(H, 55) - LLV(L, 55)) + 0.1
            A = (klines.close - tafunc.llv(klines.low, lenth1))/(tafunc.hhv(klines.high, lenth1) - tafunc.llv(klines.low, lenth1))
            B = (tafunc.hhv(klines.high, lenth2) - klines.close)/(tafunc.hhv(klines.high, lenth2) - tafunc.llv(klines.low, lenth2))+0.1
            #print("A=", list(A.iloc[-3:-1]))
            #print("B=", list(B.iloc[-3:-1]))
            if A.iloc[-3] <= B.iloc[-3] and A.iloc[-2] > B.iloc[-2]:
                print("最新价=", klines.close.iloc[-1])
                order = api.insert_order(symbol="SHFE.rb2005", direction="BUY", offset="OPEN", volume=1, limit_price=quote.ask_price1)
                break
            elif A.iloc[-3] >= B.iloc[-3] and A.iloc[-2] < B.iloc[-2]:
                print("最新价=", klines.close.iloc[-1])
                order = api.insert_order(symbol="SHFE.rb2005", direction="SELL", offset="OPEN", volume=1, limit_price=quote.bid_price1)
                break
    # 判断平仓条件
    while True:
        api.wait_update()
        if order.status == "FINISHED":
            if position.pos > 0:
                margin_rate = position.margin_long / position.open_cost_long
                gainPrice = position.open_price_long * (1 + margin_rate * 0.2)
                lossPrice = position.open_price_long * (1 - margin_rate * 0.05)
                if klines.high.iloc[-1] >= gainPrice or klines.low.iloc[-1] <= lossPrice:
                    if position.pos_long_his > 0:
                        api.insert_order(symbol="SHFE.rb2005", direction="SELL", offset="CLOSE", volume=1,
                                         limit_price=quote.bid_price1)
                    else:
                        api.insert_order(symbol="SHFE.rb2005", direction="SELL", offset="CLOSETODAY", volume=1,
                                         limit_price=quote.bid_price1)
                    break
            else:
                margin_rate = position.margin_short / position.open_cost_short
                gainPrice = position.open_price_short * (1 - margin_rate * 0.2)
                lossPrice = position.open_price_short * (1 + margin_rate * 0.05)
                if klines.low.iloc[-1] <= gainPrice or klines.high.iloc[-1] >= lossPrice:
                    if position.pos_short_his > 0:
                        api.insert_order(symbol="SHFE.rb2005", direction="BUY", offset="CLOSE", volume=1,
                                         limit_price=quote.ask_price1)
                    else:
                        api.insert_order(symbol="SHFE.rb2005", direction="BUY", offset="CLOSETODAY", volume=1,
                                         limit_price=quote.ask_price1)
                    break
    print("当前权益=", account["balance"])

我是新手,所以编辑了段代码练手

用回测调整了一下,不知道是否符合你的要求

ZS C 发表新评论 2019年12月31日

谢谢,我也是新手,刚接触天勤所以随便找个代码练习的

谢谢了,这几天测试了一下,但是还有点小问题想问一下。按这个策略运行的话只要是A线大于B线或者是小于B线在中途平仓后会马上开仓。在TB里是只有AB线交叉时才开仓,开仓平仓后只有等下一次交叉才会开仓。在天勤也可以这样吗?

这个策略应该是收定后才开仓,所以实际是AB交叉后下一根K线出来时立刻开仓,同时平仓后马上开仓的原因是刚好达到交叉的条件了吧

0

策略编程服务(有偿):QQ:2163244320

聪 王 已回答的问题 2020年1月2日