繁体   English   中英

止盈position一定比例,离开rest止损

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

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

  • 如果价格上涨 1%,则出售 1/3 的 position
  • 离开 position (2/3) 的 rest 并让它退出当前止损逻辑(基于swinglows / high的逻辑)

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

我还查看了文档strategy.order ,但文档中似乎没有可用的示例。 这是我最终尝试在输入时下额外订单但它没有在策略测试器 output 中提供数据的结果:

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 策略中实现多个退出,这样我就可以同时做这两个,当达到初始价格的某个 % 时,保护我的 position 的一部分,并让 rest 停止损失规则(在战略背景)。

任何意见真正赞赏

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


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调用,在qty_percent旁边指定一个stop参数。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM