1

在 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 与交易所互动时,您可以使用获利来获取一定比例的头寸(为了记录,通过他们称为追踪获利的某些功能)这一事实引起了我的注意。我现在期待在交易视图中实现相应的代码来回测以下场景:

  • 如果价格上涨 1%,卖出 1/3 的头寸
  • 离开你的剩余仓位 (2/3) 并让它以当前的止损逻辑退出(基于摆动低点/高点的逻辑)

到目前为止,我尝试的是实施从这个 tradingview 文章这篇文章中获得启发的逻辑,但没有成功(因为他们都没有真正使用基于多个退出订单的逻辑来退出他们的头寸)。

我也查看了文档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)))

我目前的尝试是使用strategy.exit具有相同 ID 的不同调用,然而,基于 % 的获利似乎从未被以下代码触发。

// 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 策略中实现多个退出,这样我就可以两者都做,当达到初始价格的某个 % 时,将我的部分头寸固定,并将其余部分留给止损规则(在战略背景)。

任何意见真正赞赏

4

1 回答 1

0

作为记录,我可以通过以下代码达到预期的结果:


if (strategy.position_size > 0)
    // if current closing price is upper position entry price plus 1%
    target_take_profit_long_1 = strategy.position_avg_price * (1 + 0.01)
    if close >= target_take_profit_long_1
        strategy.exit(id='PROFIT LONG 1', from_entry="EL", limit=target_take_profit_long_1, qty_percent=25, comment="long: +1% / 25% of pos / 1% total TP")
stop=longStopPrice, comment="swinglow long exit")
    strategy.exit(id="STOP LONG", from_entry="EL", stop=longStopPrice, comment="swinglow long exit")

if (strategy.position_size < 0)
    target_take_profit_short_1 = strategy.position_avg_price * (1 - 0.01)
    if close <= target_take_profit_short_1
        strategy.exit(id='PROFIT SHORT 1', from_entry="ES", limit=target_take_profit_short_1, qty_percent=25, comment="short: +1% / 25% of pos / 1% total TP")
    strategy.exit(id="STOP SHORT", from_entry="ES", stop=shortStopPrice, comment="swinghigh short exit")

这个想法是strategy.exit根据条件使用不同的调用,在一个stop参数旁边指定一个参数qty_percent

结果似乎在回测中起作用,在进入和退出之间根据需要添加交易事件,获取所需的百分比。

于 2021-12-01T15:13:22.693 回答