Pinescript策略延迟

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

Pinescript Strategy Delay

问题

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

更具体地说,我的策略围绕着确定价格走势和指标在相反方向移动的情况。这些背离通常表明市场可能出现转折点。因此,我设置了系统,在背离出现后立即在指标图表上突出显示这些背离。但问题在于,策略并不会在发现背离后立即执行交易。相反,在实际进行任何交易活动之前,似乎会有一个经过几个蜡烛的等待期。

困惑的原因在于这种延迟背后的原理。为什么我的策略在识别出一个看似有前途的设置后不进入市场?是否有特定的逻辑或市场行为驱动这个等待期?这些是我目前正在努力解决的问题。理解这种延迟可能有助于揭示策略的基本动态和有效性。随着我深入研究这种方法的复杂性,揭示市场进入时机背后的谜团成为了优化策略以获得最佳结果的关键方面。

我尝试调整回溯范围,但没有任何效果。

英文:

Tradingview Pic
So, here's the deal: I'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's puzzling is that even though the divergences are identified early on, the actual entry into the market doesn't occur until several candles have passed. It's this delay that'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've set up my system to visually highlight these divergences on the indicator chart as soon as they emerge. But here's the catch: the strategy doesn'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'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.

答案1

得分: 0

你需要了解如何使用背离。为了找到背离,你的代码首先使用ta.pivotlow()ta.pivothigh()找到枢轴点。

ta.pivothigh(source, leftbars, rightbars) → series float

只有在形成了一些柱子之后,你才能找到一个枢轴点。这就是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

如果你使用柱状图回放功能,你可以更清楚地看到这一点。

英文:

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='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

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.

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

发表评论

匿名网友

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

确定