from tqsdk import TqApi,TqAuth,tafunc,TqKq,TargetPosTask,TqSim from tqsdk.tafunc import ma import time from tqsdk.ta import MA,MACD import numpy as np SHORT = 30 # 短周期 LONG = 60 # 长周期 SYMBOL = "SHFE.rb2401" # 合约代码 api = TqApi(TqKq(),auth=TqAuth("XXXXX","XXXXXX")) print("策略开始运行") data_length = LONG + 2 # k线数据长度 # "duration_seconds=60"为一分钟线, 日线的duration_seconds参数为: 24*60*60 klines = api.get_kline_serial(SYMBOL, duration_seconds=60, data_length=data_length) target_pos = TargetPosTask(api, SYMBOL) while True: api.wait_update() if api.is_changing(klines.iloc[-1], "datetime"): # 产生新k线:重新计算SMA short_avg = MA(klines, SHORT)['ma'] # 短周期 long_avg = MA(klines, LONG)['ma'] # 长周期 macd=MACD(klines, 12, 26, 9) mdif=(list(macd["diff"])) mdea=(list(macd["dea"])) mbar=(list(macd["bar"])) #0轴上,慢线(dea)下穿快线(dif),做空;做空开仓后,若慢线上穿快线,平仓 if mdif > 0 and mdea > 0 and mdea < mdif: target_pos.set_target_volume(-5) print("做空") if mdea > mdif: target_pos.set_target_volume(0) print("平仓") #0轴下,慢线(dea)上穿快线(dif),做多;做多开仓后,若慢线下穿快线,平仓 if mdif < 0 and mdea < 0 and mdea > mdif: target_pos.set_target_volume(5) print("做多") if mdea < mdif: target_pos.set_target_volume(0) print("平仓")
forevertc 未选择答案 2023年12月13日
那我应该怎样修改呢?我想表达的是dea dif 在0轴上或者下