【问题标题】:Set take profit based on stop loss of previous lowest low/highest high根据之前最低低/最高高的止损设置止盈
【发布时间】:2022-08-20 01:26:49
【问题描述】:

我正在基于几个不同的指标在 PineScript 中编写 Tradingview 策略,并且我想根据止损百分比设置止盈目标,例如;如果基于之前最低低点的止损<2.5%,那么我想要5倍的TP,但如果之前的最低低点>2.5%,那么我想要4倍的TP。

这是代码的 TP/SL 部分的当前版本,它不工作。

// ENTRY

strategy.entry(\"LONG\", strategy.long, when=long)
strategy.entry(\"SHORT\", strategy.short, when=short)

// LONG SL & TP

stopPerlong = (close-ta.lowest(low, 10)) / ta.lowest(low, 10)   

takePerlong = if stopPerlong > 0.05 
    stopPerlong * 3
    
else if stopPerlong > 0.025
    stopPerlong * 4

else if stopPerlong < 0.025
    stopPerlong * 5

else
    na
    
// SHORT SL & TP

stopPershort = (close-ta.highest(high, 10)) / ta.highest(high, 10)    

takePershort = if stopPershort < -0.05 
    stopPerlong * 3
    
else if stopPershort < -0.025
    stopPerlong * 4

else if stopPershort > -0.025
    stopPerlong * 5

else
    na
    

// DEPLOYING TP & SL

longStop = strategy.position_avg_price * (1 - stopPerlong)
shortStop = strategy.position_avg_price * (1 + stopPershort)
shortTake = strategy.position_avg_price * (1 - takePershort)
longTake = strategy.position_avg_price * (1 + takePerlong)

if strategy.position_size > 0 
    strategy.exit(id=\"Close Long\", stop=longStop, limit=longTake)
if strategy.position_size < 0 
    strategy.exit(id=\"Close Short\", stop=shortStop, limit=shortTake)

    标签: pine-script


    【解决方案1】:

    这是我如何在第 5 版中设置止损和获利的快速示例。假设我想在看涨吞没模式上开仓并根据我计算的止损和获利平仓。

    声明声明。我认为只有“close_entries_rule =”ANY”是你应该放置的东西,因为它似乎控制着 strategy.exit() 中的“from_entry”。

    strategy(title="Bullish Engulfing Pattern", overlay=true, close_entries_rule = "ANY", precision = 16, initial_capital = 10000 , default_qty_value = 1, default_qty_type = strategy.percent_of_equity)
    

    检测您是否有开仓。

    bool inTrade = strategy.position_size > 0
    

    如果是这样,请计算条形,以便您知道上次条件为真的时间。

    int countBars = ta.barssince(inTrade == false)
    

    设置您的止盈。

    int crv = 3
    

    获取您的入门价格。

    float entry = strategy.position_avg_price
    

    “保存”计算止损所需的值。 ta.barssince 在您的仓位未平仓时开始计数。所以你可以参考你想要的蜡烛。

    float candleLow = open[countBars]
    float atr = ta.atr(14)[countBars]
    

    计算您的止损,然后计算您的获利。我更喜欢使用 ATR 指标来定义我的止损。在这种情况下,我在开仓前使用蜡烛的开盘价 (candleLow)。

    float sl = candleLow - atr
    float tp = entry + (crv * (entry - sl[1]))
    

    获取两者的百分比值。

    float slInPercent = 1 - ((entry - sl) / entry)
    float tpInPercent = 1 + (((entry - sl) / entry) * crv)
    

    打开你的位置

    if bullishEngulfing
        strategy.entry(id = "long", direction = strategy.long)
    

    使用百分比值设置未平仓头寸时的 sl 和 tp 值。

    if inTrade == true
        strategy.exit(id = "Exit Long", from_entry="long", stop = strategy.position_avg_price * slInPercent, limit = strategy.position_avg_price * tpInPercent, comment_loss = "Hit by stop loss", comment_profit = "Took profit!")
    

    我希望这能给你一个想法。 最好的祝愿!

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 1970-01-01
      • 2017-06-15
      • 2016-08-26
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多