"""
关注公众号: Ctp接口量化
"""
from API import *
class MACDStrategy(Strategy):
def __init__(self):
super().__init__()
# self.symbol_lsit = ["UR301","rb2301","au2212","IF2211","IC2211","i2301","eb2211","MA301"] #订阅合约
self.symbol_lsit = ["i2301"] #订阅合约
self.BarType = BarType.Min5 #K线周期
self.StrategyType = StrategyType.Bar #策略类型 StrategyType.Renko StrategyType.Bar StrategyType.Tick
def on_tick(self, tick=None):
print(tick.InstrumentID,tick.LastPrice)
def on_bar(self, tick=None, Bar=None):
symbol = tick.InstrumentID #合约代码
Bid = tick.BidPrice1 #买价
Ask = tick.AskPrice1 #卖价
LastPrice = tick.LastPrice #最新价
#print(symbol) #合约
#print(tick.LastPrice) #合约tick.UpdateTime
# print(tick.UpdateTime) #合约tick.UpdateTime
# print(Bar[0]["symbol"]) #合约
kline = Bar[0]["data"] # K 线数据
if len(kline) <= 35: # 小于35 条 退出
return
# K,D,J = self.KDJ(kline) # 取KDJ指标数组
# UP,MB,DN = self.BOLL(kline) # 取BOLL指标数组
# EMA = self.EMA(kline,60) # 取EMA指标数组
# RSI = self.RSI(kline) # 取RSI指标数组
# MA1 = self.MA(kline,30) # 取MA指标数组Channels
# MA2 = self.MA(kline,60) # 取MA指标数组Channels
dif,dea,macd = self.MACD(kline) # 取MACD指标数组
close,High,low = self.tick(kline) # 取收盘价数组 # 获取最新价格(卖价)
# print(self.Get_Position(symbol))
Pos = self.GetPosition(symbol)
# print(Pos)
# 开多单
if Pos["Direction"]=="None" and dif[-1]>dea[-1] and dif[-2] < dea[-2] and dea[-1] > 0:
print(symbol) #合约
print("MACD策略开多")
最低价 = min(low[-10:])
止盈 = Ask + (Ask-最低价)*3
# self.send(symbol, DirectionType.Buy, OffsetType.Open, Ask, 3, OrderType.Limit) # # OrderType.FOK OrderType.FAK OrderType.Market
self.send(symbol, DirectionType.Buy, OffsetType.Open, Ask, 3, OrderType.Limit,止损=最低价,止盈=止盈,移动止损=最低价) # # OrderType.FOK OrderType.FAK OrderType.Market
# # 开空单
if Pos["Direction"]=="None" and dif[-1]<dea[-1] and dif[-2] > dea[-2] and dea[-1] < 0:
print(symbol) #合约
print("MACD策略开空")
最高价 = max(High[-10:])
止盈 = Bid - (最高价-Bid)*3
# self.send(symbol, DirectionType.Sell, OffsetType.Open, Bid, 3, OrderType.Limit) # # OffsetType.Open OffsetType.Close OffsetType.CloseToday OffsetType.CloseYesterday
self.send(symbol, DirectionType.Sell, OffsetType.Open, Bid, 3, OrderType.Limit,止损=最高价,止盈=止盈,移动止损=最高价) # # OffsetType.Open OffsetType.Close OffsetType.CloseToday OffsetType.CloseYesterday
# # 平多单
if Pos["Direction"]==DirectionType.Buy and dif[-1]<dea[-1] and dif[-2] > dea[-2]:
print(symbol) #合约
print("MACD策略平多单")
self.send(symbol, DirectionType.Sell, OffsetType.Close, Bid, Pos['Volume'], OrderType.Limit)
# # 平空单
if Pos["Direction"]==DirectionType.Sell and dif[-1]>dea[-1] and dif[-2] < dea[-2]:
print(symbol) #合约
print("MACD策略平空单")
self.send(symbol, DirectionType.Buy, OffsetType.Close, Ask, Pos['Volume'], OrderType.Limit)
if 'kwargs' in Pos.keys():
# print(Pos["kwargs"])
if Pos["Direction"]==DirectionType.Buy and LastPrice<Pos["kwargs"]["止损"]:
print(symbol) #合约
print("MACD策略止损多单")
self.send(symbol, DirectionType.Sell, OffsetType.Close, Bid, Pos['Volume'], OrderType.Limit)
# # 平空单
if Pos["Direction"]==DirectionType.Sell and LastPrice>Pos["kwargs"]["止损"]:
print(symbol) #合约
print("MACD策略止损空单")
self.send(symbol, DirectionType.Buy,
CTP期货接口python写多策略多品种多周期案例源码
最新推荐文章于 2024-08-08 08:10:49 发布