一定比例的仓位止盈,其余止损

Take profit for a certain percent of the position, leave the rest to stop loss

在交易视图中,我使用研究及其相关策略版本来回测指标。目前,我在策略中使用非常基本的代码来退出交易(基于从脚本前面计算的 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 的某个函数)这一事实引起了我的注意.我现在期待在交易视图中实现相应的代码来回测以下场景:

到目前为止,我尝试的是实施受 this tradingview article and this one 启发的逻辑,但没有成功(因为 none 他们实际上使用基于多个退出订单的逻辑来退出他们的头寸)。

我也查看了文档 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 策略中实施多个退出,这样我就可以同时执行这两项操作,当达到初始价格的 % 时,将我的部分头寸安全化,然后让其余部分停止损失规则(在策略的上下文中)。

真正感谢任何意见

郑重声明,我可以通过以下代码实现预期的结果:


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

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