Skip to content

Instantly share code, notes, and snippets.

@pequet
Created July 8, 2018 19:49
Show Gist options
  • Save pequet/408148be4b306341f5590041e0956ede to your computer and use it in GitHub Desktop.
Save pequet/408148be4b306341f5590041e0956ede to your computer and use it in GitHub Desktop.
// Created by @pequet (https://www.tradingview.com/u/pequet) July 4 2018
// https://github.com/pequet/
// @version=3
// Reference: https://www.tradingview.com/x/j7TADzBK/
study("Doji Gaps v0.0.4", shorttitle="Doji Gaps", precision=8, overlay=false, scale=scale.right)
dojiInput = input(10, title="Doji is defined by (High-Low) / Body greater than")
rrInput = input(2.0, step=0.5, title="Risk/Reward Ratio")
// doji
IsDoji(n) =>
(high[n] - low[n]) / abs(open[n] - close[n]) > dojiInput
// gap
IsGap(n) =>
max(open[n+1], close[n+1]) < min(open[n], close[n]) ? 1 : min(open[n+1], close[n+1]) > max(open[n], close[n]) ? -1 : 0
dojiGapCond = IsDoji(1) ? IsGap(0) : 0
// alert: doji, gap, and this candle breaking over the doji high/low
activatedCond = dojiGapCond == 1 and close > high[1] ? 1 : dojiGapCond == -1 and close < low[1] ? -1 : 0
// confirmed: look for break of the candle after gap high/low
confirmedCond = activatedCond[1] == 1 and high > high[1] ? 1 : activatedCond[1] == -1 and low < low[1] ? -1 : 0
// risk to bottom of doji
entry = activatedCond==1 ? high : activatedCond==-1 ? low : confirmedCond==1 ? high[1] : confirmedCond==-1 ? low[1] : na
stop = activatedCond==1 ? low[1] : activatedCond==-1 ? high[1] : confirmedCond==1 ? low[2] : confirmedCond==-1 ? high[2] : na
target = activatedCond!=0 or confirmedCond!= 0 ? entry + rrInput * (entry - stop) : na
_candleColor = open > close ? silver : gray
_wickColor = gray
plotcandle(open, high, low, close, color=_candleColor, wickcolor=_wickColor, title="")
// confirmed
stopPlotObj = plot(stop, color=color(red, 10), style=linebr, linewidth=2, title="Stop", offset=1) // print level early
targetPlotObj = plot(target, color=color(green, 10), style=linebr, linewidth=2, title="Target", offset=1) // print level early
stopPlotObj1 = plot(not na(stop)?stop:not na(stop[1])?stop[1]:na, color=color(red, 0), style=linebr, linewidth=2, title="Stop", offset=1)
targetPlotObj1 = plot(not na(target)?target:not na(target[1])?target[1]:na, color=color(green, 0), style=linebr, linewidth=2, title="Target", offset=1)
plotshape(activatedCond!=0 ? avg(open, close[1]) : na, location=location.absolute, style=shape.circle, color=activatedCond==1 ? green : activatedCond==-1 ? red : na, textcolor=activatedCond==1 ? green : activatedCond==-1 ? red : na, size=size.small, title="Doji Gap", text="Doji Gap", offset=0)
// watch entry level
entryPlotObj = plot(entry, color=green, transp=10, style=linebr, linewidth=2, title="Entry", offset=1) // print level early
entryPlotObj1 = plot(not na(entry)?entry:not na(entry[1])?entry[1]:not na(entry[2])?entry[2]:na, color=green, transp=0, style=linebr, linewidth=2, title="Entry", offset=0)
fill(stopPlotObj1, entryPlotObj1, color=red, transp=50)
fill(targetPlotObj1, entryPlotObj1, color=green, transp=50)
alertcondition(activatedCond!= 0, title='Doji gap (potential)', message='Doji gap! (potential)')
alertcondition(confirmedCond!= 0, title='Doji gap', message='Doji gap!')
// -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment