【问题标题】:Take profit for a certain percent of the position, leave the rest to stop loss一定比例的仓位获利,剩余的仓位止损
【发布时间】: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 articlethis 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


    【解决方案1】:

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

    
    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 参数。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-25
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 2017-06-05
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多