手工交易策略实现固定金额 止损止盈 代码很简单
from CTP import *
class 止损止盈Strategy(Strategy):
def __init__(self):
super().__init__()
self.symbol_lsit = ["rb2305"] #订阅合约
self.BarType = BarType.Min #K线周期
self.StrategyType = StrategyType.Tick #策略类型 StrategyType.Renko StrategyType.Bar StrategyType.Tick
self.止损 = -100
self.止盈 = 100
def on_tick(self, tick=None):
# print(tick.InstrumentID,tick.LastPrice)
# symbol = tick.InstrumentID #合约代码
# pos = self.Get_Position(symbol) # 获取单合约
# print(pos)
pos_lsit = self.Get_Position() # 获取全部持仓
# print(pos_lsit)
for i in pos_lsit:
Pos = i
symbol= i["symbol"]
print(Pos) #合约
pDepthMarketData = self.Get_tick(symbol) #合约
if pDepthMarketData == "":
return
symbol = pDepthMarketData.InstrumentID #合约代码
Bid = pDepthMarketData.BidPrice1 #买价
Ask = pDepthMarketData.AskPrice1 #卖价
LastPrice = pDepthMarketData.LastPrice #最新价
if Pos["Direction"]=='Long':
if Pos["PosProfit"] <= float(self.止损):
if Pos["ExchangeID"]=='SHFE' and Pos["Volume"]==Pos["yd_amount"]:
self.send(symbol, DirectionType.Sell, OffsetType.CloseYesterday, Bid, Pos['Volume'], OrderType.Limit)
elif Pos["ExchangeID"]=='SHFE' and Pos["Volume"]==Pos["td_amount"]:
self.send(symbol, DirectionType.Sell, OffsetType.CloseToday, Bid, Pos['Volume'], OrderType.Limit)
else:
self.send(symbol, DirectionType.Sell, OffsetType.Close, Pos['LastPrice']-Pos['priceTick'], Pos['Volume'], OrderType.Limit)
elif Pos["PosProfit"] >= float(self.止盈):
if Pos["ExchangeID"]=='SHFE' and Pos["Volume"]==Pos["yd_amount"]:
self.send(symbol, DirectionType.Sell, OffsetType.CloseYesterday, Bid, Pos['Volume'], OrderType.Limit)
elif Pos["ExchangeID"]=='SHFE' and Pos["Volume"]==Pos["td_amount"]:
self.send(symbol, DirectionType.Sell, OffsetType.CloseToday, Bid, Pos['Volume'], OrderType.Limit)
else:
self.send(symbol, DirectionType.Sell, OffsetType.Close, Bid, Pos['Volume'], OrderType.Limit)
if Pos["Direction"]=='Short':
if Pos["PosProfit"] <= float(self.止损):
if Pos["ExchangeID"]=='SHFE' and Pos["Volume"]==Pos["yd_amount"]:
self.send(symbol, DirectionType.Buy, OffsetType.CloseYesterday, Ask, Pos['Volume'], OrderType.Limit)
elif Pos["ExchangeID"]=='SHFE' and Pos["Volume"]==Pos["td_amount"]:
self.send(symbol, DirectionType.Buy, OffsetType.CloseToday, Ask, Pos['Volume'], OrderType.Limit)
else:
self.send(symbol, DirectionType.Buy, OffsetType.Close, Ask, Pos['Volume'], OrderType.Limit)
elif Pos["PosProfit"] >= float(self.止盈):
if Pos["ExchangeID"]=='SHFE' and Pos["Volume"]==Pos["yd_amount"]:
self.send(symbol, DirectionType.Buy, OffsetType.CloseYesterday, Ask, Pos['Volume'], OrderType.Limit)
elif Pos["ExchangeID"]=='SHFE' and Pos["Volume"]==Pos["td_amount"]:
self.send(symbol, DirectionType.Buy, OffsetType.CloseToday, Ask, Pos['Volume'], OrderType.Limit)
else:
self.send(symbol, DirectionType.Buy, OffsetType.Close, Ask, Pos['Volume'], OrderType.Limit)
Config = {'brokerid':'9999', 'userid':'123456', 'password':'*******', 'appid':'simnow_client_test', 'auth_code':'0000000000000000', 'product_info':'python dll', 'td_address':'tcp://180.168.146.187:10130', 'md_address':'tcp://180.168.146.187:10131'}
if __name__ == '__main__':
t = CtpGateway()
t.add_Strategy(止损止盈Strategy())
t.add_Config(Config)
t.Start()
为了测试我设置了止盈100 止损100 看测试效果
文章来源:https://www.toymoban.com/news/detail-657214.html
股指期货止损太小刚下单就被止损了 文章来源地址https://www.toymoban.com/news/detail-657214.html
到了这里,关于ctp python接口实现 固定金额 止损 止盈的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!