1
votes

This is my first post so here it goes. I have recently picked up the pine script language and found for the most part to be intuitive. However, I'm stuck on a concept for the past week and I cant see how other codes have managed to achieve what I am trying to do. That being said the concept itself is very simple, and I can't see why it won't work. Essentially I am looking to set a real-time alert to indicate something has happened on a historical bar based on current conditions. The concept in question is, when I see a hammer, check if the previous bar was red. Then do nothing. If the next bar after the hammer closes green, then this is a legitimate hammer signal. Then go back and paint the hammer candle.

Im using the basic hammer script

Ham = input(title="Hammer", type=input.bool, defval=true)


bodySize = abs (open - close)
HlowerShadow = abs (low - open)
HupperShadow = abs (high - close)

Hammer =  (HlowerShadow > bodySize * 2) and (low <= (lowest(low,10))) and (HlowerShadow > HupperShadow * 2) and (close <= open[1]) 

plotcandle (open,high,low,close, color=lookbackAlert ? #76FF7A :  Hammer ? #1E90FF : na, bordercolor=na, wickcolor=na)

No problem to check bars before the hammer (prevbar = close[1] < open[1])

But is there a way to only 'repaint' a historical candle, based on current data? Any help or thoughts much appreciated

1
You can't go back and repaint the candle. You can make a delay and not display the graph until your conditions are met. - AnyDozer

1 Answers

0
votes

This will plot two markers, one in the past on the confirmed hammer, and one when your condition is detected. In realtime, it will only print those markers on the realtime bar's close, to avoid repainting.

Note how we are defining the confirmedHammer condition using the history-referencing operator to refer to past values of variables:

//@version=4
study("", "", true)
Ham = input(title="Hammer", type=input.bool, defval=true)


bodySize = abs (open - close)
HlowerShadow = abs (low - open)
HupperShadow = abs (high - close)

Hammer =  (HlowerShadow > bodySize * 2) and (low <= (lowest(low,10))) and (HlowerShadow > HupperShadow * 2) and (close <= open[1]) 
barUp = close > open
barDn = close < open

confirmedHammer = barstate.isconfirmed and barDn[2] and Hammer[1] and barUp
// Plot confirmed location of hammer in the past.
plotchar(confirmedHammer, "confirmedHammer", "▲", location.belowbar, size = size.tiny, offset = -1)
// Plot when the confirmed hammer is detected.
plotchar(confirmedHammer, "confirmedHammer", "•", location.belowbar, size = size.tiny)

enter image description here