手工交易策略实现固定金额 止损止盈 代码很简单
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.Tickself.止损 = -100self.止盈 = 100def 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 = isymbol= 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, 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) 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 看测试效果
股指期货止损太小刚下单就被止损了