You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法设置买入限价单,请求修复Pine Script策略限价单逻辑

修复Pine Script策略中的限价买入单逻辑

我有15年交易经验,但首次编写Pine Script策略,目前无法设置买入限价单,已耗费大量精力仍未解决。以下是我的策略代码,请帮忙修复限价单入场逻辑:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// TraderHub

//@version=5
strategy('Bafitos', overlay = false , process_orders_on_close = true)
// Get user input
rsiLen      = input.int(title="RSI Length", defval=9)
rsiOB       = input.float(title="RSI Overbought", defval=80.0)
rsiOS       = input.float(title="RSI Oversold", defval=20.0)

// Price data on candle close or current price
price = close
rsi = ta.rsi(price, rsiLen)

// Draw signal lines on chart for signal visual test.
band1 = hline(rsiOS, 'Long', color=#08721f)
band0 = hline(rsiOB, 'Short', color=#f30b0b)

//  RSI Line  
plot(rsi, 'RSI', color=color.new(#969b96, 11))

// Signal lines for calculation of entry price (visual)

Longsignal = ta.crossover(rsi, rsiOS)
Shortsignal = ta.crossunder(rsi, rsiOB)
 
// Draw Lines green for a buy signal , red for a sell signal only for a visual test
bgcolor(Longsignal ? color.green : na)
bgcolor(Shortsignal ? color.red : na)

// Formula for entry Long position or Short position ( need to take % out of signal price for perfect limit order. % will set individualy for diferent ticker
percentTreshold = 0
confirm = true
purchasePrice = 0.0
buySignal = purchasePrice
testSignal = 0.0

// Entry price calculation logic
if (confirm)
    purchasePrice := close - (close * percentTreshold)
else
    purchasePrice := close + (close * percentTreshold)// not using yet .

//if close <= purchasePrice
  //testSignal := close

shouldPlaceOrder = purchasePrice// your enter condition
barsSincePlacingOrder = ta.barssince(shouldPlaceOrder)
shouldCancelOrder = barsSincePlacingOrder == 1 and strategy.position_size == 0

strategy.entry(limit = , strategy.long, quantity = 1, when = shouldPlaceOrder)
strategy.exit(id, loss = 10, profit = 20)
strategy.cancel(id, when = shouldCancelOrder)

问题分析与修复方案

原代码存在多个关键问题导致限价单无法正常工作,以下是修复后的完整代码及核心说明:

修复后的代码

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// TraderHub

//@version=5
strategy('Bafitos', overlay = false , process_orders_on_close = true, default_qty_type=strategy.cash, default_qty_value=10000)
// Get user input
rsiLen      = input.int(title="RSI Length", defval=9)
rsiOB       = input.float(title="RSI Overbought", defval=80.0)
rsiOS       = input.float(title="RSI Oversold", defval=20.0)
// 新增限价单折扣百分比输入
longLimitDiscount = input.float(title="Long Limit Discount (%)", defval=1.0, minval=0.0, maxval=10.0, tooltip="限价单价格相对于信号K线收盘价的折扣百分比")
// 新增限价单有效期(K线数量)
limitOrderExpiry = input.int(title="Limit Order Expiry (Bars)", defval=3, minval=1, tooltip="限价单未成交时,多少根K线后自动取消")

// Price data on candle close or current price
price = close
rsi = ta.rsi(price, rsiLen)

// Draw signal lines on chart for signal visual test.
band1 = hline(rsiOS, 'Long', color=#08721f)
band0 = hline(rsiOB, 'Short', color=#f30b0b)

//  RSI Line  
plot(rsi, 'RSI', color=color.new(#969b96, 11))

// Signal lines for calculation of entry price (visual)
Longsignal = ta.crossover(rsi, rsiOS)
Shortsignal = ta.crossunder(rsi, rsiOB)
 
// Draw Lines green for a buy signal , red for a sell signal only for a visual test
bgcolor(Longsignal ? color.green : na)
bgcolor(Shortsignal ? color.red : na)

// 订单ID定义,统一用于入场、出场、取消操作
longOrderId = "LongLimitOrder"
// 计算限价买入价格
purchasePrice = close * (1 - longLimitDiscount / 100)

// 记录信号出现后的K线数量
barsSinceSignal = ta.barssince(Longsignal)
// 触发限价单的条件:出现多头信号且当前无持仓
shouldPlaceOrder = Longsignal and strategy.position_size == 0
// 取消限价单的条件:超过有效期仍未成交
shouldCancelOrder = barsSinceSignal >= limitOrderExpiry and strategy.position_size == 0

// 发送限价买入单
if (shouldPlaceOrder)
    strategy.entry(longOrderId, strategy.long, limit=purchasePrice)

// 设置止损止盈
strategy.exit(longOrderId + "Exit", longOrderId, loss=10, profit=20)

// 取消未成交的限价单
strategy.cancel(longOrderId, when=shouldCancelOrder)

// 可视化限价单价格
plotshape(shouldPlaceOrder, title="Limit Price", location=location.belowbar, color=color.blue, style=shape.labelup, text=str.tostring(purchasePrice, "%.2f"))

关键修复点

  • 语法错误修正:补全strategy.entry中缺失的limit参数赋值,传入计算好的限价单价格。
  • 参数化配置:将限价单折扣比例、有效期设为可自定义的输入参数,适配不同品种需求。
  • 下单逻辑优化:仅在出现Longsignal且无持仓时触发限价单,避免原代码中无意义的持续下单尝试。
  • 订单ID统一:定义唯一订单ID,确保止损止盈、订单取消操作能精准关联目标订单。
  • 有效期逻辑完善:通过barsSinceSignal跟踪信号后的K线数量,超过设定时长自动取消未成交订单。
  • 可视化增强:添加限价单价格标签,直观展示下单价格。

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 17:05:23