You are on page 1of 5

//@version=5

indicator(title="Divergence And GoldenCross", format=format.price,


max_bars_back=5000)
len = input.int(title="RSI Period", minval=1, defval=14)

lbR = input(title="Pivot Lookback Right", defval=5)


lbL = input(title="Pivot Lookback Left", defval=5)
rangeUpper = input(title="Max of Lookback Range", defval=60)
rangeLower = input(title="Min of Lookback Range", defval=5)
plotBull = input(title="Plot Bullish", defval=true)
plotHiddenBull = input(title="Plot Hidden Bullish", defval=true)
plotBear = input(title="Plot Bearish", defval=false)
plotHiddenBear = input(title="Plot Hidden Bearish", defval=false)
bearColor = color.red
bullColor = color.green
hiddenBullColor = #6bad6c
hiddenBearColor = color.new(color.red, 50)
textColor = color.white
noneColor = color.new(color.white, 100)
osc = ta.rsi(close, len)

plot(osc, title="RSI", linewidth=2, color=#d53df3)


hline(50, title="Middle Line / ma2", color=#577bff,
linestyle=hline.style_solid,linewidth = 2)
obLevel = hline(70, title="Overbought", color=#787B86,
linestyle=hline.style_dotted)
osLevel = hline(30, title="Oversold", color=#787B86, linestyle=hline.style_dotted)
fill(obLevel, osLevel, title="Background", color=color.rgb(33, 150, 243, 90))

plFound = na(ta.pivotlow(osc, lbL, lbR)) ? false : true


phFound = na(ta.pivothigh(osc, lbL, lbR)) ? false : true

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

//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low

oscHL = osc[lbR] > ta.valuewhen(plFound, osc[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 ? osc[lbR] : na,offset=-lbR,title="Regular


Bullish",linewidth=2,color=(bullCond ? bullColor : noneColor))

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low

oscLL = osc[lbR] < ta.valuewhen(plFound, osc[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 ? osc[lbR] : na,offset=-lbR,title="Hidden


Bullish",linewidth=2,color=(hiddenBullCond ? hiddenBullColor : noneColor))

//------------------------------------------------------------------------------
// Regular Bearish
// Osc: Lower High

oscLH = osc[lbR] < ta.valuewhen(phFound, osc[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 ? osc[lbR] : na,offset=-lbR, title="Regular Bearish", linewidth=2,


color=(bearCond ? bearColor : noneColor))

//------------------------------------------------------------------------------
// Hidden Bearish
// Osc: Higher High

oscHH = osc[lbR] > ta.valuewhen(phFound, osc[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 ? osc[lbR] : na, offset=-lbR, title="Hidden Bearish", linewidth=2,


color=(hiddenBearCond ? hiddenBearColor : noneColor))

xxx = input.bool(false,"──────────────────")
hdiv = input.bool(true,"Hidden Bullish Div")
hb = hdiv? hiddenBullCond : false

var check = 10

if hb or bullCond
check := 10

malen1 = input.int(10, "이평선 1 길이")


malen2 = input.int(20, "이평선 2 길이")
ma1 = ta.sma(close,malen1)
ma2 = ta.sma(close, malen2)
gc = ta.crossover(ma1, ma2)

if gc
check:=check-1

buy = check[1]==10 and check==9

plotshape(buy, "buy signal",


style=shape.triangleup,text="buy",textcolor=color.green,color=color.green,location=
location.bottom)
if buy
alert("Buy", alert.freq_once_per_bar_close)

a = 20/(ta.highest(ma1/ma2-1,100)-ta.lowest(ma1/ma2-1,100))

plot(((ma1/ma2)-1)*a+50,"ma1",color=color.red)

// Input:
var int n = input.int(10, title="Fractal Periods", minval=2)
//

bool upflagDown = true, bool upflagUp0 = true, bool upflagUp1 = true


bool upflagUp2 = true, bool upflagUp3 = true, bool upflagUp4 = true

for i = 1 to n
upflagDown := upflagDown and (osc[n-i] < osc[n])
upflagUp0 := upflagUp0 and (osc[n+i] < osc[n])
upflagUp1 := upflagUp1 and (osc[n+1] <= osc[n] and osc[n+i + 1] < osc[n])
upflagUp2 := upflagUp2 and (osc[n+1] <= osc[n] and osc[n+2] <= osc[n] and
osc[n+i + 2] < osc[n])
upflagUp3 := upflagUp3 and (osc[n+1] <= osc[n] and osc[n+2] <= osc[n] and
osc[n+3] <= osc[n] and osc[n+i + 3] < osc[n])
upflagUp4 := upflagUp4 and (osc[n+1] <= osc[n] and osc[n+2] <= osc[n] and
osc[n+3] <= osc[n] and osc[n+4] <= osc[n] and osc[n+i + 4] < osc[n])
flagUp = upflagUp0 or upflagUp1 or upflagUp2 or upflagUp3 or upflagUp4

bool downflagDown = true, bool downflagUp0 = true, bool downflagUp1 = true


bool downflagUp2 = true, bool downflagUp3 = true, bool downflagUp4 = true

for i = 1 to n
downflagDown := downflagDown and (osc[n-i] > osc[n])
downflagUp0 := downflagUp0 and (osc[n+i] > osc[n])
downflagUp1 := downflagUp1 and (osc[n+1] >= osc[n] and osc[n+i + 1] > osc[n])
downflagUp2 := downflagUp2 and (osc[n+1] >= osc[n] and osc[n+2] >= osc[n] and
osc[n+i + 2] > osc[n])
downflagUp3 := downflagUp3 and (osc[n+1] >= osc[n] and osc[n+2] >= osc[n] and
osc[n+3] >= osc[n] and osc[n+i + 3] > osc[n])
downflagUp4 := downflagUp4 and (osc[n+1] >= osc[n] and osc[n+2] >= osc[n] and
osc[n+3] >= osc[n] and osc[n+4] >= osc[n] and osc[n+i + 4] > osc[n])
flagDown = downflagUp0 or downflagUp1 or downflagUp2 or downflagUp3 or downflagUp4

bool upfract = upflagDown[1] and flagUp[1]


bool downfract = downflagDown[1] and flagDown[1]

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * *
// Auto trendlines

// Input:
var string GROUP_ATL = "Auto trendlines"
var int LINE_WIDTH1 = 1, var string LINE_WIDTH1_STR = "Width 1"
var int LINE_WIDTH2 = 2, var string LINE_WIDTH2_STR = "Width 2"

var string subgroup1 = "recent line"


var color ln_col_recent = input.color(color.new(color.red, 0), title="Recent Line",
group=GROUP_ATL, inline=subgroup1)
int _lnwidth_recent = switch input.string(LINE_WIDTH1_STR,
options=[LINE_WIDTH1_STR, LINE_WIDTH2_STR], title="", inline=subgroup1,
group=GROUP_ATL)
LINE_WIDTH1_STR => LINE_WIDTH1
LINE_WIDTH2_STR => LINE_WIDTH2
var int lnwidth_recent = _lnwidth_recent

var string subgroup2 = "historical line"


var color ln_col_prev = input.color(color.new(color.gray, 50), title="Historical
Line", group=GROUP_ATL, inline=subgroup2)
int _lnwidth_prev = switch input.string(LINE_WIDTH1_STR, options=[LINE_WIDTH1_STR,
LINE_WIDTH2_STR], title="", inline=subgroup2, group=GROUP_ATL)
LINE_WIDTH1_STR => LINE_WIDTH1
LINE_WIDTH2_STR => LINE_WIDTH2
var int lnwidth_prev = _lnwidth_prev

var int max_tl = input.int(10, title="추세선 페어 갯수", maxval=250,


minval=1, group=GROUP_ATL)*2
var string _str_extend = input.string("Right", options=["Right", "None"],
title="Which way to extend lines", group=GROUP_ATL)
var string str_extend = _str_extend == "None" ? extend.none : extend.right

//

// Recent fractals.
var float recent_dn1 = osc, var int i_recent_dn1 = bar_index
var float recent_up1 = osc, var int i_recent_up1 = bar_index
var float recent_dn2 = osc, var int i_recent_dn2 = bar_index
var float recent_up2 = osc, var int i_recent_up2 = bar_index

// Array of lines, newest elements inserted to front.


var line[] arr_ln_up = array.new_line()
var line[] arr_ln_dn = array.new_line()

// @function : Clean up
// @param arr : either arr_ln_up/arr_ln_dn should be passed into it.
// @returns void : To delete excess lines and assign new color to historical lines.
cleanup(line[] arr) =>
if array.size(arr) > 1
line.set_color(array.get(arr, 1), ln_col_prev)
line.set_width(array.get(arr, 1), lnwidth_prev)
while array.size(arr) > math.floor(max_tl/2)
line.delete(array.pop(arr))

// Checking for fractals & Drawing trendlines


if downfract
recent_dn2:=recent_dn1
i_recent_dn2:=i_recent_dn1
recent_dn1:=osc[n+1]
i_recent_dn1 := bar_index-n-1

ln_dn = line.new(i_recent_dn2, recent_dn2, i_recent_dn1, recent_dn1,


xloc.bar_index, str_extend, color=ln_col_recent, style=line.style_dashed,
width=lnwidth_recent)
array.unshift(arr_ln_dn, ln_dn)
cleanup(arr_ln_dn)

if upfract
recent_up2:=recent_up1
i_recent_up2:=i_recent_up1
recent_up1:=osc[n+1]
i_recent_up1 := bar_index-n-1

ln_up = line.new(i_recent_up2, recent_up2, i_recent_up1, recent_up1,


xloc.bar_index, str_extend, color=ln_col_recent, style=line.style_dashed,
width=lnwidth_recent)
array.unshift(arr_ln_up, ln_up)
cleanup(arr_ln_up)

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
// Finding crosses

// @function : Get slope


// @param int xA , xB: x values of known points A and B
// @param float yA, yB : y values of known points A and B
// @returns float m : Slope value, commonly expressed as rise over run.
get_slope(xA, yA, xB, yB) =>
(yB - yA) / (xB - xA)

// Linear extrapolation
// Solving for price at current x (bar_index), given two pairs of fractals with x
values < bar_index.
float m_dn = get_slope(i_recent_dn1, recent_dn1, i_recent_dn2, recent_dn2)
float y_dn = (m_dn * bar_index) + recent_dn1 - (m_dn * i_recent_dn1)

float m_up = get_slope(i_recent_up1, recent_up1, i_recent_up2, recent_up2)


float y_up = (m_up * bar_index) + recent_up1 - (m_up * i_recent_up1)
//

You might also like