【问题标题】:PineScript - ATR % StoplossPine 脚本 - ATR % 止损
【发布时间】:2021-10-27 15:32:33
【问题描述】:

我想在策略放置/检测到入场时添加 ATR 止损。到目前为止,我想出了这个脚本:

@version=4    
// Stop Loss inputs atr     
longLossPerc = input(title="Long Stop Loss (%)",type=input.float, minval=0.0, step=0.1, defval=1) * 0.01    
atrLength = input(title="ATR Length", type=input.integer, defval=6, minval=1)
userStructure = input(title="Use Structure", type=input.bool, defval=true)    
lookback = input(title="How far to look back for High/Low",type=input.integer, defval=7, minval=1)    
atrStopMultiplier = input(title="ATR x ? ", type=input.float, defval=1.0, minval=0.1)    
longStopPrice = strategy.position_avg_price * (1 - longLossPerc)

// calculate data atr    
atr=atr(atrLength)    
longStop = (userStructure ? lowest(low, lookback) : close) - atr * atrStopMultiplier    
shortStop = (userStructure ? highest(high,lookback) : close) + atr * atrStopMultiplier

// plot atr Long/Short    
plot(longStop, color=color.green, style=plot.style_linebr, title="Long Trailing Stop-ATR")    
plot(shortStop, color=color.red, style=plot.style_linebr, title="Short Trailing Stop-ATR")

我的问题是,我不知道如何将此脚本关联/连接到我的脚本参数。我是否必须创建一个新变量,然后将其插入到我的 strategy.close 中?

strategy.entry("LongA", strategy.long,1, when= x and y)    
strategy.close("LongA", when= z or t )

注意:x,y,z,t 是预定义变量。

【问题讨论】:

    标签: pine-script algorithmic-trading trading pine-script-v4


    【解决方案1】:

    您想使用strategy.exit() 函数。它有stoploss 参数。

    loss (series int/float) 可选参数。止损(指定在 蜱)。如果指定,则放置止损单以退出市场 达到指定的损失量(以滴答计)时的位置。这 默认值为“NaN”。

    stop (series int/float) 可选参数。止损(需要 具体价格)。如果指定,则放置止损单以退出 以指定价格(或更差)的市场地位。优先级 参数'stop'高于参数'loss'的优先级 (如果它的值不是“NaN”,则使用“stop”而不是“loss”)。这 默认值为“NaN”。

    strategy.entry("LongA", strategy.long,1, when= x and y)
    strategy.exit("Long Exit", "LongA", loss=longStop)
    

    【讨论】:

    • 但是我也可以在 strategy.exit 中添加一个退出变量吗?例如:strategy.exit("Long Exit", "LongA", loss=longStop, when= b) 所以我希望在变量“b”为真时关闭多头入场,否则,它应该保持交易直到 ATR止损(仅根据入场蜡烛计算)为真。
    • 是的,strategy.exit有一个when参数,这样就可以有条件了。
    • 好的,感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2021-12-14
    • 2020-02-10
    • 2022-12-09
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    相关资源
    最近更新 更多