Pinescript 策略延迟

huangapple go评论61阅读模式
英文:

Pinescript Strategy Delay

问题

这是您提供的内容的翻译:

Tradingview 图片
所以,这是问题:我开发了一个基于市场进入的背离概念的策略。有趣的是,这个策略涉及到识别背离并在指标图表上标记它们。然而,令人困惑的是,尽管早早识别出了这些背离,但实际的市场进入要等到经过了几根蜡烛之后才会发生。这个延迟让我感到有点困惑。

为了进一步阐述,我的策略围绕着精确定位价格行动和指标在相反方向移动的情况。这些背离通常表明市场可能出现转折点。因此,我已经设置了我的系统,以在它们出现时立即在指标图表上突出显示这些背离。但问题在于:尽管在发现背离后策略不会立即执行交易。相反,似乎会经过几根蜡烛之后才会进行任何实际的交易活动。

混淆的根源在于这种延迟背后的理由。为什么我的策略要等待进入市场,尽管识别出了似乎有望的设置?是否有特定的逻辑或市场行为驱动了这个等待期?这些是我目前正在处理的问题。理解这种延迟可能有助于阐明策略和其有效性的潜在动态。随着我深入研究这种方法的复杂性,解开市场进入时机背后的谜团成为优化策略以获得最佳结果的重要方面。

maType = input.string(title='移动平均线类型', options=['DPO', '动量', 'MACD', 'RSI', '随机指标'], defval='DPO')
len = input.int(title='回望期', minval=1, defval=117)
lbR = input(title='底部回望右侧', defval=5)
lbL = input.int(title='底部回望左侧', defval=5, maxval=21)
rangeUpper = input(title='回望范围的最大值', defval=500)
rangeLower = input(title='回望范围的最小值', defval=7)
plotBull = input(title='绘制看涨信号', defval=true)
plotHiddenBull = input(title='绘制隐藏看涨信号', defval=true)
plotBear = input(title='绘制看跌信号', defval=true)
plotHiddenBear = input(title='绘制隐藏看跌信号', defval=true)

bearColor = color.red
bullColor = color.green
hiddenBullColor = color.new(color.yellow, 70)
hiddenBearColor = color.new(color.orange, 70)
textColor = color.white
noneColor = color.new(color.white, 100)

plFound = na(ta.pivotlow(mom, lbL, lbR)) ? false : true
phFound = na(ta.pivothigh(mom, lbL, lbR)) ? false : true

_inRange(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper

//------------------------------------------------------------------------------
// 常规看涨信号

// 振荡器: 更高的低点
oscHL = mom[lbR] > ta.valuewhen(plFound, mom[lbR], 1) and _inRange(plFound[1])

// 价格: 更低的低点
priceLL = low[lbR] < ta.valuewhen(plFound, low[lbR], 1)

bullCond = plotBull and priceLL and oscHL and plFound

plot(plFound ? mom[lbR] : na, offset=-lbR, title='常规看涨信号', linewidth=2, color=bullCond ? bullColor : noneColor)

plotshape(bullCond ? mom[lbR] : na, offset=-lbR, title='常规看涨信号标签', text=' 看涨 ', style=shape.labelup, location=location.bottom, color=color.new(bullColor, 0), size=size.small, textcolor=color.new(textColor, 0))

//------------------------------------------------------------------------------
// 隐藏看涨信号

// 振荡器: 更低的低点
oscLL = mom[lbR] < ta.valuewhen(plFound, mom[lbR], 1) and _inRange(plFound[1])

// 价格: 更高的低点
priceHL = low[lbR] > ta.valuewhen(plFound, low[lbR], 1)

hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound

plot(plFound ? mom[lbR] : na, offset=-lbR, title='隐藏看涨信号', linewidth=2, color=hiddenBullCond ? hiddenBullColor : noneColor)

plotshape(hiddenBullCond ? mom[lbR] : na, offset=-lbR, title='隐藏看涨信号标签', text=' H 看涨 ', style=shape.labelup, location=location.bottom, color=hiddenBullColor, size=size.small, textcolor=color.new(textColor, 0))

//------------------------------------------------------------------------------
// 常规看跌信号

// 振荡器: 更低的高点
oscLH = mom[lbR] < ta.valuewhen(phFound, mom[lbR], 1) and _inRange(phFound[1])

// 价格: 更高的高点
priceHH = high[lbR] > ta.valuewhen(phFound, high[lbR], 1)

bearCond = plotBear and priceHH and oscLH and phFound

plot(phFound ? mom[lbR] : na, offset=-lbR, title='常规看跌信号', linewidth=2, color=bearCond ? bearColor : noneColor)

plotshape(bearCond ? mom[lbR] : na, offset=-lbR, title='常规

<details>
<summary>英文:</summary>

[Tradingview Pic](https://i.stack.imgur.com/rAtRp.png)
So, here&#39;s the deal: I&#39;ve developed a strategy that hinges on the concept of divergences for market entries. The interesting thing is that this strategy involves identifying divergences and marking them on the indicator chart. However, what&#39;s puzzling is that even though the divergences are identified early on, the actual entry into the market doesn&#39;t occur until several candles have passed. It&#39;s this delay that&#39;s got me a bit perplexed.

To elaborate further, my strategy revolves around pinpointing instances where the price action and the indicator move in opposite directions. These divergences often indicate potential turning points in the market. So, I&#39;ve set up my system to visually highlight these divergences on the indicator chart as soon as they emerge. But here&#39;s the catch: the strategy doesn&#39;t execute trades immediately after a divergence is spotted. Instead, there seems to be a deliberate waiting period spanning several candlesticks before any actual trading activity takes place.

The confusion stems from the rationale behind this delay. Why does my strategy hold off on entering the market despite identifying what seems to be a promising setup? Is there a specific logic or market behavior that drives this waiting period? These are the questions that I&#39;m currently grappling with. Understanding this delay could potentially shed light on the underlying dynamics of the strategy and its effectiveness. As I delve deeper into the intricacies of this approach, unraveling the mystery behind the timing of market entries becomes a crucial aspect of refining and fine-tuning the strategy for optimal results.


maType = input.string(title='Moving Average Type', options=['DPO', 'Momentum', 'MACD', 'RSI', 'Stoch'], defval='DPO')
len = input.int(title='LookBack Period', minval=1, defval=117)
lbR = input(title='Pivot Lookback Right', defval=5)
lbL = input.int(title='Pivot Lookback Left', defval=5, maxval=21)
rangeUpper = input(title='Max of Lookback Range', defval=500)
rangeLower = input(title='Min of Lookback Range', defval=7)
plotBull = input(title='Plot Bullish', defval=true)
plotHiddenBull = input(title='Plot Hidden Bullish', defval=true)
plotBear = input(title='Plot Bearish', defval=true)
plotHiddenBear = input(title='Plot Hidden Bearish', defval=true)

bearColor = color.red
bullColor = color.green
hiddenBullColor = color.new(color.yellow, 70)
hiddenBearColor = color.new(color.orange, 70)
textColor = color.white
noneColor = color.new(color.white, 100)

plFound = na(ta.pivotlow(mom, lbL, lbR)) ? false : true
phFound = na(ta.pivothigh(mom, lbL, lbR)) ? false : true

_inRange(cond) =>
bars = ta.barssince(cond == true)
rangeLower <= bars and bars <= rangeUpper

//------------------------------------------------------------------------------
// Regular Bullish

// Osc: Higher Low
oscHL = mom[lbR] > ta.valuewhen(plFound, mom[lbR], 1) and _inRange(plFound[1])

// Price: Lower Low
priceLL = low[lbR] < ta.valuewhen(plFound, low[lbR], 1)

bullCond = plotBull and priceLL and oscHL and plFound

plot(plFound ? mom[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond ? bullColor : noneColor)

plotshape(bullCond ? mom[lbR] : na, offset=-lbR, title='Regular Bullish Label', text=' Bull ', style=shape.labelup, location=location.bottom, color=color.new(bullColor, 0), size=size.small, textcolor=color.new(textColor, 0))

//------------------------------------------------------------------------------
// Hidden Bullish

// Osc: Lower Low
oscLL = mom[lbR] < ta.valuewhen(plFound, mom[lbR], 1) and _inRange(plFound[1])

// Price: Higher Low
priceHL = low[lbR] > ta.valuewhen(plFound, low[lbR], 1)

hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound

plot(plFound ? mom[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond ? hiddenBullColor : noneColor)

plotshape(hiddenBullCond ? mom[lbR] : na, offset=-lbR, title='Hidden Bullish Label', text=' H Bull ', style=shape.labelup, location=location.bottom, color=hiddenBullColor, size=size.small, textcolor=color.new(textColor, 0))

//------------------------------------------------------------------------------
// Regular Bearish

// Osc: Lower High
oscLH = mom[lbR] < ta.valuewhen(phFound, mom[lbR], 1) and _inRange(phFound[1])

// Price: Higher High
priceHH = high[lbR] > ta.valuewhen(phFound, high[lbR], 1)

bearCond = plotBear and priceHH and oscLH and phFound

plot(phFound ? mom[lbR] : na, offset=-lbR, title='Regular Bearish', linewidth=2, color=bearCond ? bearColor : noneColor)

plotshape(bearCond ? mom[lbR] : na, offset=-lbR, title='Regular Bearish Label', text=' Bear ', style=shape.labeldown, location=location.top, color=color.new(bearColor, 0), size=size.small, textcolor=color.new(textColor, 0))

//------------------------------------------------------------------------------
// Hidden Bearish

// Osc: Higher High
oscHH = mom[lbR] > ta.valuewhen(phFound, mom[lbR], 1) and _inRange(phFound[1])

// Price: Lower High
priceLH = high[lbR] < ta.valuewhen(phFound, high[lbR], 1)

hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound

plot(phFound ? mom[lbR] : na, offset=-lbR, title='Hidden Bearish', linewidth=2, color=hiddenBearCond ? hiddenBearColor : noneColor)

plotshape(hiddenBearCond ? mom[lbR] : na, offset=-lbR, title='Hidden Bearish Label', text=' H Bear ', style=shape.labeldown, location=location.top, color=hiddenBearColor, size=size.tiny, textcolor=color.new(textColor, 0))

bull = bullCond[lbR] or hiddenBullCond[lbR]
bear = bearCond[lbR] or hiddenBearCond[lbR]

tp = input(110,"tp1")
sl = input(1000,"sl1")

longCondition = bull
if (longCondition)
strategy.entry("Calls", strategy.long)
strategy.exit("TP1", from_entry="Calls", qty_percent = 100, profit = tp, loss = sl)

shortCondition = bear
if (shortCondition)
strategy.entry("Puts", strategy.short)
strategy.exit("TP1", from_entry="Puts", qty_percent = 100, profit = tp, loss = sl)


I tried manipulating the look back ranges but nothing.
</details>
# 答案1
**得分**: 0
你需要了解背离是如何工作的。为了找到背离,你的代码首先使用 `ta.pivotlow()` 和 `ta.pivothigh()` 找到枢轴点。
ta.pivothigh(source, leftbars, rightbars) → 系列浮点数
只有在一些柱子形成后才能找到枢轴点。这就是你的 `rightbars` 参数。在你的情况下,它是 `lbR`。
它告诉你的是,它将等待 `lbR` 个柱子的时间,看是否有一个枢轴点。这个延迟来自于你无法知道当前柱子是否是枢轴柱子。你需要等一段时间,回顾价格的走势并确认潜在的枢轴点。
你的绘图也使用了 `offset`。
plotshape(bullCond ? mom[lbR] : na, offset=-lbR, title='Regular Bullish Label', text=' Bull ', style=shape.labelup, location=location.bottom, color=color.new(bullColor, 0), size=size.small, textcolor=color.new(textColor, 0))
`offset=-lbR`
这就是为什么你认为它可以立即识别背离。实际发生的是,它找到了枢轴点(发生在过去),然后使用偏移将你的 `plotshape` 移动到那里。如果你注意到,偏移也设置为 `lbR`。
如果你使用柱子回放功能,你可以更清楚地看到这一点。
<details>
<summary>英文:</summary>
You need to understand how divergences work. In order to find divergences, your code first finds the pivot points with `ta.pivotlow()` and `ta.pivothigh()`. 
ta.pivothigh(source, leftbars, rightbars) → series float
You can only find a pivot point after some bars formed. That is the `rightbars` argument for you. In your case it is `lbR`.
What it tells you is, it will wait for `lbR` number of bars to see if there was a pivot point. This delay comes from the fact that you cannot know if the current bar is a pivot bar or not. You need to wait for some time and look back to see how the price moved and confirm a potential pivot point.
Your plots also use `offset`.
plotshape(bullCond ? mom[lbR] : na, offset=-lbR, title=&#39;Regular Bullish Label&#39;, text=&#39; Bull &#39;, style=shape.labelup, location=location.bottom, color=color.new(bullColor, 0), size=size.small, textcolor=color.new(textColor, 0))
`offset=-lbR`
This is why you think it identifies the divergence instantly. What really happens is, it finds the pivot (that happened in the past) and moves your `plotshape` to there using the offset. If you notice, the offset is also set to  `lbR`.
You can see this more clearly if you use the bar replay function.
</details>

huangapple
  • 本文由 发表于 2023年8月9日 10:51:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864273-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定