【发布时间】:2022-01-07 03:30:57
【问题描述】:
在 tradingview 中,我使用一项研究及其相关策略版本来回测指标。目前,我在策略中使用非常基本的代码来退出交易(基于脚本中前面计算的从 swinglow/high 派生的止损价格),策略订单退出逻辑所在的脚本末尾看起来像这样:
...
// Determine stop loss price based on swinglow/high
longStopPrice = periodHighestSwingLow
shortStopPrice = periodLowestSwingHigh
// Submit entry orders
if (enterLong)
strategy.entry(id="EL", long=true)
if (enterShort)
strategy.entry(id="ES", long=false)
// Submit exit orders based on calculated stop loss price
if (strategy.position_size > 0)
strategy.exit(id="XL STP", stop=longStopPrice)
if (strategy.position_size < 0)
strategy.exit(id="XS STP", stop=shortStopPrice)
当我使用 alertatron 与交易所互动时,您可以使用止盈来获取一定比例的头寸(为了记录,通过他们称为 trailling take profit 的某些功能)引起了我的注意。我现在期待在交易视图中实现相应的代码来回测以下场景:
- 如果价格上涨 1%,卖出 1/3 的头寸
- 保留您的剩余头寸 (2/3),并使用当前的止损/亏损逻辑(基于摆动低点/高点的逻辑)退出
到目前为止,我尝试的是实施受this tradingview article 和this one 启发的逻辑但没有成功(因为它们实际上都没有使用基于多个退出订单的逻辑来退出其仓位)。
我还查看了文档strategy.order,但文档中似乎没有可用的示例。这是我最终尝试在入场时下额外订单但它没有在策略测试器输出中提供数据的结果:
if (enterLong)
strategy.entry(id="EL", long=true)
strategy.order(id="stopLossLong", long=true, qty=(strategy.position_size/3), stop=(close + (close*0.01)))
if (enterShort)
strategy.entry(id="ES", long=false)
strategy.order(id="stopLossShort", long=false, qty=(strategy.position_size/3), stop=(close - (close*0.01)))
我目前的尝试是使用具有相同 ID 的不同 strategy.exit 调用,但是,基于 % 的止盈似乎永远不会被以下代码触发。
// Submit entry orders
if (enterLong)
strategy.entry(id="EL", long=true)
if (enterShort)
strategy.entry(id="ES", long=false)
// STEP 3: Submit exit orders based on calculated stop loss price
if (strategy.position_size > 0)
// if current closing price is upper position entry price plus 1%
target_take_profit_long = strategy.position_avg_price * (1 + 0.01)
if close >= target_take_profit_long
strategy.exit('XS STP', 'Short', limit=target_take_profit_long, qty_percent=25, comment='Close-Sell-Profit')
// else, wait for current stop loss
strategy.exit(id="XL STP", stop=longStopPrice)
if (strategy.position_size < 0)
// if current price (close) is below position entry price minus 1%
target_take_profit_short = strategy.position_avg_price * (1 - 0.01)
if close <= target_take_profit_short
strategy.exit('XS STP', 'Long', limit=target_take_profit_short, qty_percent=25, comment='Close-Buy-Profit')
// else, wait for current stop loss
strategy.exit(id="XS STP", stop=shortStopPrice)
所以问题来了:有什么方法可以在 TradingView 策略中实现多个退出,这样我就可以同时做到这两个,当达到初始价格的某个 % 时将我的部分头寸保全,其余的留给止损规则(在战略的背景下)。
任何意见都非常感谢
【问题讨论】:
标签: pine-script pine-script-v4