ctp python接口实现 固定金额 止损 止盈

这篇具有很好参考价值的文章主要介绍了ctp python接口实现 固定金额 止损 止盈。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  手工交易策略实现固定金额 止损止盈  代码很简单 



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 看测试效果

python止损代码,笔记,python,区块链,开发语言

 股指期货止损太小刚下单就被止损了 文章来源地址https://www.toymoban.com/news/detail-657214.html

到了这里,关于ctp python接口实现 固定金额 止损 止盈的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包