【问题标题】:How to have multiple stop losses / profit takers in pinescript (pyramiding)如何在 pinescript 中设置多个止损/获利者(金字塔)
【发布时间】:2021-10-05 18:21:57
【问题描述】:

我正在尝试为我的策略创建多个止损/获利单。但是,一旦添加了第二个头寸(例如 2 个多头(但可以是无限多头或空头)),设置为入场蜡烛低点的止损参数将移动到第二个多头头寸入场蜡烛的低点,导致我同时被阻止出两个位置。目标是为每个交易信号设置独立的止损和利润目标。我对 java 的了解很少,建议使用带有数组的计数器之类的东西来存储值,但我不清楚它是如何完成的,过去几个小时的试验并没有让我走得更远。任何帮助或指导都会很棒,如果您需要更多详细信息,请告诉我。 这是我当前的代码,只有一个止损/获利单:如您所见,sl 和 pt 的变量已被覆盖

strategy("My Script"), overlay=true

//filters
timezone = input(title="Timezone", type=input.session, defval="0950-1500")
days = input(title="Days To Trade", defval="23456")

inSession(sess) => na(time(timeframe.period, sess + ":" + days)) == false

//identify engulfing candle
bullishEC = close >= high[1] and close[1] < open[1]
bearishEC = close <= low[1] and close[1] > open[1]

//get ema user input
emaLength1 = input(title="EMA 1 Length", type=input.integer, defval=110)

//get ema
ema1 = ema(close, emaLength1)

//plot ema
plot(ema1, color=close > ema1 ? color.green : color.red, linewidth=2)

//macd parameter
[macdline, signalline, macdhist] = macd(close, 12,26,9) //built in function

bearishM = macdline < signalline
bullishM = macdline > signalline


tradeSignal = ((close >= ema1) and bullishEC) or ((close < ema1) and bearishEC)

plotshape(tradeSignal and bearishEC and bearishM and barstate.isconfirmed and 
inSession(timezone), title="bearish", location=location.abovebar, color=color.red, 
style=shape.triangledown, text="Sell")
plotshape(tradeSignal and bullishEC and bullishM and barstate.isconfirmed and 
inSession(timezone), title="bullish", location=location.belowbar, color=color.green, 
style=shape.triangleup, text="Buy")


alertcondition(tradeSignal and bullishEC and bullishM, title="BUY SIGNAL", message="GO LONG 
on {{ticker}}")
alertcondition(tradeSignal and bearishEC and bearishM, title="SHORT SIGNAL", message="GO SHORT 
on {{ticker}}")

//setting stop & targets for current trade
var shortStopPrice = 0.0
var shortTargetPrice = 0.0
var shortStopDistance = 0.0
var longStopPrice = 0.0
var longTargetPrice = 0.0
var longStopDistance = 0.0


if (tradeSignal and bullishEC and bullishM and barstate.isconfirmed and inSession(timezone))
    longStopPrice := low 
    longStopDistance := close - longStopPrice
    longTargetPrice := close + (longStopDistance * 2)
    strategy.entry(id="long", long=strategy.long)


if (tradeSignal and bearishEC and bearishM and barstate.isconfirmed and inSession(timezone))
    shortStopPrice := high 
    shortStopDistance := shortStopPrice - close
    shortTargetPrice := close - (shortStopDistance * 2)
    strategy.entry(id="short", long=strategy.short)


//exit trade when stop or target is hit
strategy.exit("id=exit", from_entry="short", limit=shortTargetPrice, stop=shortStopPrice)
strategy.exit("id=exit", from_entry="long", limit=longTargetPrice, stop=longStopPrice)
//@version=4

【问题讨论】:

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


    【解决方案1】:

    附加订单需要新的 strategy.entry 调用和唯一的交易标识符。运行一个循环并使用数组可以实现这一点,但是如果你只要做两个,那么只做两个可能会更容易。请注意,您所说的移动止损的第二个条目是由于您如何允许在另一个长警报上重新初始化止损值。如何将第一组与另一组隔离的一个示例是在输入条件中添加位置检查,例如“and strategy.position_size == 0”。这样,第一组停靠点只会在第一个条目上分配。您还可以使用“strategy.opentrades”之类的内置函数来计算有多少未平仓,并仅将它们分配在 1,2 或 3 个未平仓等之下。

    另一个提示是绘制您的出口和停靠点,以便我们了解正在发生的事情、发生的地点以及原因。我们可以根据位置大小绘制条件图,因此我们仅在某个位置时才能看到级别。

    设置中还有一个“金字塔”参数,我们可以在 strategy() 标头中指定一个参数来指定一次允许打开多少笔交易。

    干杯!

    【讨论】:

    • 嘿,我已经尝试通过创建数组来存储独立的条目/止损/利润目标来实施您的一些建议,因为我有时一次有 6 个以上的未结订单,因此仅复制会效率低下。但是,我遇到了不同的错误。我已经发表了一篇关于它的新帖子,如果你有几分钟的时间,如果你能检查一下,那就太棒了。无论如何,感谢您迄今为止的帮助!
    • 只有你给我最后一个答案的分数! Jk 有时间我会看看。保重
    猜你喜欢
    • 2021-12-26
    • 2017-06-02
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 2012-07-14
    相关资源
    最近更新 更多