一个具有跟踪止损止盈功能的半自动委托工具。可以根据人工判断的下单位置下单,也可以指定入场价格等待入场,然后自动生成计划退场委托,退场委托使用跟踪止损止盈机制。
/*backtest start: 2022-09-14 09:00:00 end: 2022-09-16 15:00:00 period: 1m basePeriod: 1m exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}] args: [["RunMode",1,360008],["ContractType","rb2301",360008]] */ strategy("futures", overlay = true) varip targetPrice = na varip high_lowPrice = na varip isTrade = false varip isAlert = false varip isAlertMinTick = false varip isAlertFinished = false varip offset = input(30, "offset", "跟踪止损止盈偏移") varip limit = input(-1, "limit", "初始开仓价格,-1为不开仓,0为立即开仓,其它具体数值为限价价格") varip amount = input(1, "amount", "开仓量") varip loss = input(30, "loss", "止损") varip targetOffset = input(30, "targetOffset", "触发跟踪止盈止损偏移量") varip minTick = input(1, "minTick", "价格一跳") tradeType = input.string("long", "下单方向", tooltip="下单方向,long做多,short做空", options=["long", "short"]) if not barstate.ishistory and not isAlertMinTick runtime.log("检查syminfo.mintick是否正确!syminfo.mintick:", syminfo.mintick, "#FF0000") if syminfo.mintick < minTick runtime.error("系统syminfo.mintick < minTick参数", "#FF0000") isAlertMinTick := true if not barstate.ishistory and limit == -1 and not isAlert runtime.log("没有设置开仓价格,当前limit为-1(防止误开仓,初始默认limit为-1),禁止开仓", "#FF0000") isAlert := true if isTrade and strategy.position_size == 0 and not isAlertFinished and strategy.closedtrades.size(strategy.closedtrades - 1) != 0 runtime.log("所有委托流程执行完毕,仓位为0", "#FF0000") isAlertFinished := true if not barstate.ishistory and not isTrade and limit != -1 if limit == 0 strategy.entry("open", tradeType == "long" ? strategy.long : strategy.short, amount) else if limit > 0 strategy.entry("open", tradeType == "long" ? strategy.long : strategy.short, amount, limit=limit) if tradeType == "long" targetPrice := (limit == 0 ? close : limit) + targetOffset else targetPrice := (limit == 0 ? close : limit) - targetOffset strategy.exit("exit", "open", amount, loss=loss, trail_price=targetPrice, trail_offset=offset) runtime.log("每点价格为:", syminfo.mintick, ",当前close:", close) isTrade := true if ((close > targetPrice and strategy.position_size > 0) or (close < targetPrice and strategy.position_size < 0)) and not barstate.ishistory high_lowPrice := na(high_lowPrice) ? close : high_lowPrice if strategy.position_size > 0 high_lowPrice := close > high_lowPrice ? close : high_lowPrice else high_lowPrice := close < high_lowPrice ? close : high_lowPrice plot(targetPrice, "trail_price 触发线") plot(strategy.position_size!=0 ? high_lowPrice : na, "当前最高价/最低价") plot(strategy.position_size!=0 ? (strategy.position_size > 0 ? high_lowPrice-syminfo.mintick*offset : high_lowPrice+syminfo.mintick*offset) : na, "移动止损触发线")template: strategy.tpl:40:21: executing "strategy.tpl" at <.api.GetStrategyListByName>: wrong number of args for GetStrategyListByName: want 7 got 6