Heikin ++

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 21

//@version=5

indicator('heikin ++', overlay=false, max_bars_back=3000, max_lines_count=500,


max_labels_count=500, format=format.price)
bgcolor(color.rgb(26, 30, 39))
/// plot HA candle
timeframe_candle = input.timeframe(defval='', title='candle timeframe')
can_type = input.string(defval='heikinashi', title='candle-type',
options=['heikinashi', 'normal', 'kagi', 'point n figure'])
symb = input.string(defval='USOIL', title='Symbol ?', options=['USOIL',
'CRUDEOIL1!'])
ha_t = can_type == 'heikinashi' ? ticker.heikinashi(symb) : can_type == 'kagi' ?
ticker.kagi(symb,3): can_type == 'point n figure' ? ticker.pointfigure(symb, "hl",
"Traditional", 1, 3) : ticker.standard(symb)
[open1,high1,low1,close1,tr,volume1,hlc] = request.security(symb, timeframe_candle,
[open,high,low,close,ta.tr,volume,hlc3], barmerge.gaps_off, barmerge.lookahead_off)
[openh,highh,lowh,closeh,trh] = request.security(ha_t, timeframe_candle,
[open,high,low,close,ta.tr], barmerge.gaps_off, barmerge.lookahead_off)
[openh1,highh1,lowh1,closeh1,trh1,hlch1] = request.security(symb, "30",
[open,high,low,close,ta.tr,hlc3], barmerge.gaps_off, barmerge.lookahead_on)
paletteColor = closeh >= openh ? color.teal : color.red
plotcandle(openh, highh, lowh, closeh, color = paletteColor,wickcolor =
paletteColor, bordercolor = paletteColor)

var l1x = label.new(bar_index+4, close1, style=label.style_label_left,


color=color.navy, textcolor=color.white,size=size.small)

// set up new xy params to labels on new bar


label.set_xy(l1x, bar_index+4, close1)
label.set_text(l1x, '' + str.tostring(close1))
//label.delete(l1x[1])

///////////////////////////////////////
/// RSI ///
//////////////////////////////////////
color_bars = input(false, title='color bars?')
len_rsi = input(14,'RSI length')
xxx = (2 * len_rsi) - 1
timeframe_rsi = input.timeframe(defval='30', title='RSI timeframe')
timeframe_rsi2 = input.timeframe(defval='30', title='RSI timeframe 2')
ohlc = request.security(ha_t, timeframe_rsi, ta.ema(close1, xxx))
atr = request.security(ha_t, timeframe_rsi, ta.atr(xxx))
range_1 = atr
rsi_50 = ohlc
rsi_10 = rsi_50 - 4 * range_1
rsi_20 = rsi_50 - 3 * range_1
rsi_30 = rsi_50 - 2 * range_1
rsi_40 = rsi_50 - range_1
rsi_60 = rsi_50 + range_1
rsi_70 = rsi_50 + 2 * range_1
rsi_80 = rsi_50 + 3 * range_1
rsi_90 = rsi_50 + 4 * range_1

plot(rsi_90, title='rsi 90', linewidth=2, style=plot.style_line,


color=color.new(#F08080, 0))
plot(rsi_80, title='rsi 80', linewidth=2, style=plot.style_line,
color=color.new(#F08080, 0))
plot(rsi_70, title='rsi 70', linewidth=3, style=plot.style_line,
color=color.new(#F08080, 0))
plot(rsi_60, title='rsi 60', linewidth=3, style=plot.style_line,
color=color.new(#F08080, 0))
plot(rsi_50, title='rsi 50', linewidth=3, style=plot.style_line, color=color.white)
plot(rsi_40, title='rsi 40', linewidth=3, style=plot.style_line,
color=color.new(color.olive, 0))
plot(rsi_30, title='rsi 30', linewidth=3, style=plot.style_line,
color=color.new(color.olive, 0))
plot(rsi_20, title='rsi 20', linewidth=2, style=plot.style_line,
color=color.new(color.olive, 0))
plot(rsi_10, title='rsi 10', linewidth=2, style=plot.style_line,
color=color.new(color.olive, 0))

/// divergence
prd = input.int(defval=5, title='Pivot Period', minval=1, maxval=50)
source = input.string(defval='Close', title='Source for Pivot Points',
options=['Close', 'High/Low'])
searchdiv = input.string(defval='Regular/Hidden', title='Divergence Type',
options=['Regular', 'Hidden', 'Regular/Hidden'])
showindis = input.string(defval='First Letter', title='Show Indicator Names',
options=['Full', 'First Letter', 'Don\'t Show'])
showlimit = input.int(1, title='Minimum Number of Divergence', minval=1, maxval=11)
maxpp = input.int(defval=10, title='Maximum Pivot Points to Check', minval=1,
maxval=20)
maxbars = input.int(defval=150, title='Maximum Bars to Check', minval=5,
maxval=200)
shownum = input(defval=true, title='Show Divergence Number')
showlast = input(defval=false, title='Show Only Last Divergence')
dontconfirm = input(defval=false, title='Don\'t Wait for Confirmation')
showlines = input(defval=false, title='Show Divergence Lines')
showpivot = input(defval=false, title='Show Pivot Points')
calcmacd = input(defval=true, title='MACD')
calcmacda = input(defval=true, title='MACD Histogram')
calcrsi = input(defval=true, title='RSI')
calcstoc = input(defval=true, title='Stochastic')
calccci = input(defval=true, title='CCI')
calcmom = input(defval=true, title='Momentum')
calcobv = input(defval=true, title='OBV')
calcvwmacd = input(true, title='VWmacd')
calccmf = input(true, title='Chaikin Money Flow')
calcmfi = input(true, title='Money Flow Index')
calcext = input(false, title='Check External Indicator')
externalindi = input(defval=close, title='External Indicator')
pos_reg_div_col = input(defval=color.yellow, title='Positive Regular Divergence')
neg_reg_div_col = input(defval=color.navy, title='Negative Regular Divergence')
pos_hid_div_col = input(defval=color.lime, title='Positive Hidden Divergence')
neg_hid_div_col = input(defval=color.red, title='Negative Hidden Divergence')
pos_div_text_col = input(defval=color.black, title='Positive Divergence Text
Color')
neg_div_text_col = input(defval=color.white, title='Negative Divergence Text
Color')
reg_div_l_style_ = input.string(defval='Solid', title='Regular Divergence Line
Style', options=['Solid', 'Dashed', 'Dotted'])
hid_div_l_style_ = input.string(defval='Dashed', title='Hdden Divergence Line
Style', options=['Solid', 'Dashed', 'Dotted'])
reg_div_l_width = input.int(defval=2, title='Regular Divergence Line Width',
minval=1, maxval=5)
hid_div_l_width = input.int(defval=1, title='Hidden Divergence Line Width',
minval=1, maxval=5)
showmas = input.bool(defval=false, title='Show MAs 50 & 200', inline='ma12')
cma1col = input.color(defval=color.lime, title='', inline='ma12')
cma2col = input.color(defval=color.red, title='', inline='ma12')

//plot(showmas ? ta.sma(close, 50) : na, color=showmas ? cma1col : na)


//plot(showmas ? ta.sma(close, 200) : na, color=showmas ? cma2col : na)

// set line styles


var reg_div_l_style = reg_div_l_style_ == 'Solid' ? line.style_solid :
reg_div_l_style_ == 'Dashed' ? line.style_dashed : line.style_dotted
var hid_div_l_style = hid_div_l_style_ == 'Solid' ? line.style_solid :
hid_div_l_style_ == 'Dashed' ? line.style_dashed : line.style_dotted

// get indicators
rsi = ta.rsi(closeh, 14) // RSI
[macd, signal, deltamacd] = ta.macd(closeh, 12, 26, 9) // MACD
moment = ta.mom(closeh, 10) // Momentum
cci = ta.cci(closeh, 10) // CCI
Obv = ta.obv // OBV
stk = ta.sma(ta.stoch(closeh, highh, lowh, 14), 3) // Stoch
maFast = ta.vwma(closeh, 12) // volume weighted macd
maSlow = ta.vwma(closeh, 26)
vwmacd = maFast - maSlow
Cmfm = (closeh - lowh - (highh - closeh)) / (highh - lowh) // Chaikin money flow
Cmfv = Cmfm * volume
cmf = ta.sma(Cmfv, 21) / ta.sma(volume, 21)
Mfi = ta.mfi(closeh, 14) // Moneyt Flow Index

// keep indicators names and colors in arrays


var indicators_name = array.new_string(11)
var div_colors = array.new_color(4)
if barstate.isfirst
// names
array.set(indicators_name, 0, showindis == 'Full' ? 'MACD' : 'M')
array.set(indicators_name, 1, showindis == 'Full' ? 'Hist' : 'H')
array.set(indicators_name, 2, showindis == 'Full' ? 'RSI' : 'E')
array.set(indicators_name, 3, showindis == 'Full' ? 'Stoch' : 'S')
array.set(indicators_name, 4, showindis == 'Full' ? 'CCI' : 'C')
array.set(indicators_name, 5, showindis == 'Full' ? 'MOM' : 'M')
array.set(indicators_name, 6, showindis == 'Full' ? 'OBV' : 'O')
array.set(indicators_name, 7, showindis == 'Full' ? 'VWMACD' : 'V')
array.set(indicators_name, 8, showindis == 'Full' ? 'CMF' : 'C')
array.set(indicators_name, 9, showindis == 'Full' ? 'MFI' : 'M')
array.set(indicators_name, 10, showindis == 'Full' ? 'Extrn' : 'X')
//colors
array.set(div_colors, 0, pos_reg_div_col)
array.set(div_colors, 1, neg_reg_div_col)
array.set(div_colors, 2, pos_hid_div_col)
array.set(div_colors, 3, neg_hid_div_col)

// Check if we get new Pivot High Or Pivot Low


float ph = ta.pivothigh(source == 'Close' ? closeh : highh, prd, prd)
float pl = ta.pivotlow(source == 'Close' ? closeh : lowh, prd, prd)
//plotshape(ph and showpivot, text='H', style=shape.labeldown,
color=color.new(color.white, 100), textcolor=color.new(color.red, 0),
location=location.abovebar, offset=-prd)
//plotshape(pl and showpivot, text='L', style=shape.labelup,
color=color.new(color.white, 100), textcolor=color.new(color.lime, 0),
location=location.belowbar, offset=-prd)
// keep values and positions of Pivot Highs/Lows in the arrays
var int maxarraysize = 20
var ph_positions = array.new_int(maxarraysize, 0)
var pl_positions = array.new_int(maxarraysize, 0)
var ph_vals = array.new_float(maxarraysize, 0.)
var pl_vals = array.new_float(maxarraysize, 0.)

// add PHs to the array


if ph
array.unshift(ph_positions, bar_index)
array.unshift(ph_vals, ph)
if array.size(ph_positions) > maxarraysize
array.pop(ph_positions)
array.pop(ph_vals)

// add PLs to the array


if pl
array.unshift(pl_positions, bar_index)
array.unshift(pl_vals, pl)
if array.size(pl_positions) > maxarraysize
array.pop(pl_positions)
array.pop(pl_vals)

// functions to check Regular Divergences and Hidden Divergences

// function to check positive regular or negative hidden divergence


// cond == 1 => positive_regular, cond == 2=> negative_hidden
positive_regular_positive_hidden_divergence(src, cond) =>
divlen = 0
prsc = source == 'Close' ? closeh : lowh
// if indicators higher than last value and close price is higher than las
close
if dontconfirm or src > src[1] or closeh > closeh[1]
startpoint = dontconfirm ? 0 : 1 // don't check last candle
// we search last 15 PPs
for x = 0 to maxpp - 1 by 1
len = bar_index - array.get(pl_positions, x) + prd
// if we reach non valued array element or arrived 101. or previous
bars then we don't search more
if array.get(pl_positions, x) == 0 or len > maxbars
break
if len > 5 and (cond == 1 and src[startpoint] > src[len] and
prsc[startpoint] < nz(array.get(pl_vals, x)) or cond == 2 and src[startpoint] <
src[len] and prsc[startpoint] > nz(array.get(pl_vals, x)))
slope1 = (src[startpoint] - src[len]) / (len - startpoint)
virtual_line1 = src[startpoint] - slope1
slope2 = (closeh[startpoint] - closeh[len]) / (len - startpoint)
virtual_line2 = closeh[startpoint] - slope2
arrived = true
for y = 1 + startpoint to len - 1 by 1
if src[y] < virtual_line1 or nz(closeh[y]) < virtual_line2
arrived := false
break
virtual_line1 -= slope1
virtual_line2 -= slope2
virtual_line2

if arrived
divlen := len
break
divlen

// function to check negative regular or positive hidden divergence


// cond == 1 => negative_regular, cond == 2=> positive_hidden
negative_regular_negative_hidden_divergence(src, cond) =>
divlen = 0
prsc = source == 'Close' ? closeh : highh
// if indicators higher than last value and close price is higher than las
close
if dontconfirm or src < src[1] or closeh < closeh[1]
startpoint = dontconfirm ? 0 : 1 // don't check last candle
// we search last 15 PPs
for x = 0 to maxpp - 1 by 1
len = bar_index - array.get(ph_positions, x) + prd
// if we reach non valued array element or arrived 101. or previous
bars then we don't search more
if array.get(ph_positions, x) == 0 or len > maxbars
break
if len > 5 and (cond == 1 and src[startpoint] < src[len] and
prsc[startpoint] > nz(array.get(ph_vals, x)) or cond == 2 and src[startpoint] >
src[len] and prsc[startpoint] < nz(array.get(ph_vals, x)))
slope1 = (src[startpoint] - src[len]) / (len - startpoint)
virtual_line1 = src[startpoint] - slope1
slope2 = (closeh[startpoint] - nz(closeh[len])) / (len -
startpoint)
virtual_line2 = closeh[startpoint] - slope2
arrived = true
for y = 1 + startpoint to len - 1 by 1
if src[y] > virtual_line1 or nz(closeh[y]) > virtual_line2
arrived := false
break
virtual_line1 -= slope1
virtual_line2 -= slope2
virtual_line2

if arrived
divlen := len
break
divlen

// calculate 4 types of divergence if enabled in the options and return divergences


in an array
calculate_divs(cond, indicator_1) =>
divs = array.new_int(4, 0)
array.set(divs, 0, cond and (searchdiv == 'Regular' or searchdiv ==
'Regular/Hidden') ? positive_regular_positive_hidden_divergence(indicator_1, 1) :
0)
array.set(divs, 1, cond and (searchdiv == 'Regular' or searchdiv ==
'Regular/Hidden') ? negative_regular_negative_hidden_divergence(indicator_1, 1) :
0)
array.set(divs, 2, cond and (searchdiv == 'Hidden' or searchdiv ==
'Regular/Hidden') ? positive_regular_positive_hidden_divergence(indicator_1, 2) :
0)
array.set(divs, 3, cond and (searchdiv == 'Hidden' or searchdiv ==
'Regular/Hidden') ? negative_regular_negative_hidden_divergence(indicator_1, 2) :
0)
divs
// array to keep all divergences
var all_divergences = array.new_int(44) // 11 indicators * 4 divergence = 44
elements
// set related array elements
array_set_divs(div_pointer, index) =>
for x = 0 to 3 by 1
array.set(all_divergences, index * 4 + x, array.get(div_pointer, x))

// set divergences array


array_set_divs(calculate_divs(calcmacd, macd), 0)
array_set_divs(calculate_divs(calcmacda, deltamacd), 1)
array_set_divs(calculate_divs(calcrsi, rsi), 2)
array_set_divs(calculate_divs(calcstoc, stk), 3)
array_set_divs(calculate_divs(calccci, cci), 4)
array_set_divs(calculate_divs(calcmom, moment), 5)
array_set_divs(calculate_divs(calcobv, Obv), 6)
array_set_divs(calculate_divs(calcvwmacd, vwmacd), 7)
array_set_divs(calculate_divs(calccmf, cmf), 8)
array_set_divs(calculate_divs(calcmfi, Mfi), 9)
array_set_divs(calculate_divs(calcext, externalindi), 10)

// check minimum number of divergence, if less than showlimit then delete all
divergence
total_div = 0
for x = 0 to array.size(all_divergences) - 1 by 1
total_div += math.round(math.sign(array.get(all_divergences, x)))
total_div

if total_div < showlimit


array.fill(all_divergences, 0)

// keep line in an array


var pos_div_lines = array.new_line(0)
var neg_div_lines = array.new_line(0)
var pos_div_labels = array.new_label(0)
var neg_div_labels = array.new_label(0)

// variables for Alerts


pos_reg_div_detected = false
neg_reg_div_detected = false
pos_hid_div_detected = false
neg_hid_div_detected = false

// to remove lines/labels until we met new // PH/PL


var last_pos_div_lines = 0
var last_neg_div_lines = 0
var remove_last_pos_divs = false
var remove_last_neg_divs = false
if pl
remove_last_pos_divs := false
last_pos_div_lines := 0
last_pos_div_lines
if ph
remove_last_neg_divs := false
last_neg_div_lines := 0
last_neg_div_lines

// draw divergences lines and labels


divergence_text_top = ''
divergence_text_bottom = ''
distances = array.new_int(0)
dnumdiv_top = 0
dnumdiv_bottom = 0
top_label_col = color.white
bottom_label_col = color.white
old_pos_divs_can_be_removed = true
old_neg_divs_can_be_removed = true
startpoint = dontconfirm ? 0 : 1 // used for don't confirm option

for x = 0 to 10 by 1
div_type = -1
for y = 0 to 3 by 1
if array.get(all_divergences, x * 4 + y) > 0 // any divergence?
div_type := y
if y % 2 == 1
dnumdiv_top += 1
top_label_col := array.get(div_colors, y)
top_label_col
if y % 2 == 0
dnumdiv_bottom += 1
bottom_label_col := array.get(div_colors, y)
bottom_label_col
if not array.includes(distances, array.get(all_divergences, x * 4 + y))
// line not exist ?
array.push(distances, array.get(all_divergences, x * 4 + y))

// set variables for alerts


if y == 0
pos_reg_div_detected := true
pos_reg_div_detected
if y == 1
neg_reg_div_detected := true
neg_reg_div_detected
if y == 2
pos_hid_div_detected := true
pos_hid_div_detected
if y == 3
neg_hid_div_detected := true
neg_hid_div_detected

// get text for labels


if div_type >= 0
divergence_text_top += (div_type % 2 == 1 ? showindis != 'Don\'t Show' ?
array.get(indicators_name, x) + '\n' : '' : '')
divergence_text_bottom += (div_type % 2 == 0 ? showindis != 'Don\'t Show' ?
array.get(indicators_name, x) + '\n' : '' : '')
divergence_text_bottom

// draw labels
if showindis != 'Don\'t Show' or shownum
if shownum and dnumdiv_top > 0
divergence_text_top += str.tostring(dnumdiv_top)
divergence_text_top
if shownum and dnumdiv_bottom > 0
divergence_text_bottom += str.tostring(dnumdiv_bottom)
divergence_text_bottom
if divergence_text_top != ''

array.push(neg_div_labels, label.new(x=bar_index, y=math.max(highh,


highh[1]), text=divergence_text_top, color=top_label_col,
textcolor=neg_div_text_col, style=label.style_label_down, size=size.tiny))

if divergence_text_bottom != ''

array.push(pos_div_labels, label.new(x=bar_index, y=math.min(lowh,


lowh[1]), text=divergence_text_bottom, color=bottom_label_col,
textcolor=pos_div_text_col, style=label.style_label_up, size=size.tiny))

////////////////////////////////////
/// Cpr
///////////////////////////////////

getSeries(e, timeFrame) =>


request.security(syminfo.tickerid, timeFrame, e,
lookahead=barmerge.lookahead_on)

//Previous day datas


PrevDayClose = close
PrevDayOpen = open
PrevDayHigh = high
PrevDayLow = low

//Day CPR levels


DayPivotLevel1 = (PrevDayHigh + PrevDayLow + PrevDayClose) / 3
DayBCPRlevel1 = (PrevDayHigh + PrevDayLow) / 2
DayTCPRlevel1 = DayPivotLevel1 - DayBCPRlevel1 + DayPivotLevel1
DayR1level1 = 2 * DayPivotLevel1 - PrevDayLow
DayS1level1 = 2 * DayPivotLevel1 - PrevDayHigh
DayR2level1 = DayPivotLevel1 + PrevDayHigh - PrevDayLow
DayS2level1 = DayPivotLevel1 - (PrevDayHigh - PrevDayLow)
DayR3level1 = PrevDayHigh + 2 * (DayPivotLevel1 - PrevDayLow)
DayS3level1 = PrevDayLow - 2 * (PrevDayHigh - DayPivotLevel1)
DayR4level1 = DayR3level1 + DayR2level1 - DayR1level1
DayS4level1 = DayS3level1 - (DayS1level1 - DayS2level1)

//UX Input Arguments


DayPP = color.white
DayBC = color.white
DayTC = color.white
DayS1 = color.blue
DayR1 = color.red
DayS2 = color.blue
DayR2 = color.red
DayS3 = color.blue
DayR3 = color.red
DayS4 = color.blue
DayR4 = color.red
PrevDHigh = #363a45
PrevDLow = #363a45
PrevDClose = color.gray

myWidthMainLevels = input.int(title='Line width for pivots', defval=3, minval=1)


DayPivot = input(true, title='Show Daily Pivot')
DaySR = input(true, title='Show Daily Support/Resistance')
DayCPR = input(true, title='Show Daily CPR Range')

[DayPivotLevel,DayBCPRlevel,DayTCPRlevel,DayR1level,DayS1level,DayR2level,DayS2leve
l,DayR3level,DayS3level] = request.security(ha_t, 'D',
[DayPivotLevel1[1],DayBCPRlevel1[1],DayTCPRlevel1[1],DayR1level1[1],DayS1level1[1],
DayR2level1[1],DayS2level1[1],DayR3level1[1],DayS3level1[1]], barmerge.gaps_off,
barmerge.lookahead_on)

plot(DayPivot and DayPivotLevel and timeframe.isintraday ? DayPivotLevel : na,


title='DayPP', color=color.new(DayPP, 0), linewidth=2, style=plot.style_circles,
offset=0)
plot(DayCPR and DayBCPRlevel and timeframe.isintraday ? DayBCPRlevel : na,
title='DayBC', color=color.new(DayBC, 0), linewidth=1, style=plot.style_circles,
offset=0)
plot(DayCPR and DayTCPRlevel and timeframe.isintraday ? DayTCPRlevel : na,
title='DayTC', color=color.new(DayTC, 0), linewidth=1, style=plot.style_circles,
offset=0)
plot(DaySR and DayR1level and timeframe.isintraday ? DayR1level : na,
title='DayR1', color=color.new(DayR1, 0), linewidth=2, style=plot.style_circles,
offset=0)
plot(DaySR and DayS1level and timeframe.isintraday ? DayS1level : na,
title='DayS1', color=color.new(DayS1, 0), linewidth=2, style=plot.style_circles,
offset=0)
plot(DaySR and DayR2level and timeframe.isintraday ? DayR2level : na,
title='DayR2', color=color.new(DayR2, 0), linewidth=2, style=plot.style_circles,
offset=0)
plot(DaySR and DayS2level and timeframe.isintraday ? DayS2level : na,
title='DayS2', color=color.new(DayS2, 0), linewidth=2, style=plot.style_circles,
offset=0)
//plot(DaySR and DayR3level and timeframe.isintraday ? DayR3level : na,
title='DayR3', color=color.new(DayR3, 0), linewidth=2, style=plot.style_circles,
offset=0)
//plot(DaySR and DayS3level and timeframe.isintraday ? DayS3level : na,
title='DayS3', color=color.new(DayS3, 0), linewidth=2, style=plot.style_circles,
offset=0)

//Previous Week datas


[WeekPivotLevel,WeekBCPRlevel,WeekTCPRlevel,WeekR1level,WeekS1level,WeekR2level,Wee
kS2level,WeekR3level,WeekS3level] = request.security(ha_t, 'W',
[DayPivotLevel1[1],DayBCPRlevel1[1],DayTCPRlevel1[1],DayR1level1[1],DayS1level1[1],
DayR2level1[1],DayS2level1[1],DayR3level1[1],DayS3level1[1]], barmerge.gaps_off,
barmerge.lookahead_on)

//UX Input Arguments


WeekPP = color.purple
WeekBC = color.purple
WeekTC = color.purple
WeekS1 = color.green
WeekR1 = #FF0000
WeekS2 = color.green
WeekR2 = #FF0000
WeekS3 = color.green
WeekR3 = #FF0000
WeekS4 = color.green
WeekR4 = #FF0000
PrevWHigh = #363a45
PrevWLow = #363a45

WeekPivot = input(true, title='Show Weekly Pivot')


WeekSR = input(true, title='Show Weekly Support/Resistance')
WeekCPR = input(true, title='Show Weekly CPR Range')

plot(WeekPivot and WeekPivotLevel and (timeframe.isintraday or timeframe.isdaily) ?


WeekPivotLevel : na, title='WeekPP', color=color.new(WeekPP, 0),
linewidth=myWidthMainLevels, style=plot.style_circles, offset=0)
plot(WeekCPR and WeekBCPRlevel and (timeframe.isintraday or timeframe.isdaily) ?
WeekBCPRlevel : na, title='WeekBC', color=color.new(WeekBC, 0), linewidth=1,
style=plot.style_circles, offset=0)
plot(WeekCPR and WeekTCPRlevel and (timeframe.isintraday or timeframe.isdaily) ?
WeekTCPRlevel : na, title='WeekTC', color=color.new(WeekTC, 0), linewidth=1,
style=plot.style_circles, offset=0)
plot(WeekSR and WeekR1level and (timeframe.isintraday or timeframe.isdaily) ?
WeekR1level : na, title='WeekR1', color=color.new(WeekR1, 0),
linewidth=myWidthMainLevels, style=plot.style_line, offset=0)
plot(WeekSR and WeekS1level and (timeframe.isintraday or timeframe.isdaily) ?
WeekS1level : na, title='WeekS1', color=color.new(WeekS1, 0),
linewidth=myWidthMainLevels, style=plot.style_line, offset=0)
plot(WeekSR and WeekR2level and (timeframe.isintraday or timeframe.isdaily) ?
WeekR2level : na, title='WeekR2', color=color.new(WeekR2, 0),
linewidth=myWidthMainLevels, style=plot.style_line, offset=0)
plot(WeekSR and WeekS2level and (timeframe.isintraday or timeframe.isdaily) ?
WeekS2level : na, title='WeekS2', color=color.new(WeekS2, 0),
linewidth=myWidthMainLevels, style=plot.style_line, offset=0)

//Previous Month datas


[MonthPivotLevel,MonthBCPRlevel,MonthTCPRlevel,MonthR1level,MonthS1level,MonthR2lev
el,MonthS2level,MonthR3level,MonthS3level] = request.security(ha_t, 'M',
[DayPivotLevel1[1],DayBCPRlevel1[1],DayTCPRlevel1[1],DayR1level1[1],DayS1level1[1],
DayR2level1[1],DayS2level1[1],DayR3level1[1],DayS3level1[1]], barmerge.gaps_off,
barmerge.lookahead_on)

//UX Input Arguments


MonthPP = color.yellow
MonthBC = color.yellow
MonthTC = color.yellow
MonthR1 = #E15382
MonthS1 = #009688
MonthR2 = #E15382
MonthS2 = #009688
MonthR3 = #E15382
MonthS3 = #009688
MonthR4 = #E15382
MonthS4 = #009688
PrevMHigh = #363a45
PrevMLow = #363a45

MonthPivot = input(true, title='Show Monthly Pivot')


MonthSR = input(true, title='Show Monthly Support/Resistance')
MonthCPR = input(true, title='Show Monthly CPR Range')
PrevMonthH = input(true, title='Show Previous Month High')
PrevMonthL = input(true, title='Show Previous Month Low')

//plot(MonthPivot and MonthPivotLevel and (timeframe.isintraday or


timeframe.isdaily or timeframe.isweekly) ? MonthPivotLevel : na, title='MonthPP',
color=color.new(MonthPP, 0), linewidth=myWidthMainLevels, style=plot.style_circles,
offset=0)
//plot(MonthCPR and MonthBCPRlevel and (timeframe.isintraday or timeframe.isdaily
or timeframe.isweekly) ? MonthBCPRlevel : na, title='MonthBC',
color=color.new(MonthBC, 0), linewidth=1, style=plot.style_circles, offset=0)
//plot(MonthCPR and MonthTCPRlevel and (timeframe.isintraday or timeframe.isdaily
or timeframe.isweekly) ? MonthTCPRlevel : na, title='MonthTC',
color=color.new(MonthTC, 0), linewidth=1, style=plot.style_circles, offset=0)

///////////////////////////////////
//////////// MTF SnR Levels
/////////////////////////////////////

bars = input.int(20, title = "Pivot")


tipe = input.string("Body", title = "Type", options = ["Wick",
"Body"])

//show_c = input.bool(false, title = "Current Timeframe", group =


"Timeframes")

show_5m = input.bool(true, title = "M10", group = "Timeframes")


show_15m = input.bool(true, title = "M15", group = "Timeframes")
show_30m = input.bool(true, title = "M30", group = "Timeframes")
show_1h = input.bool(true, title = "H1", group = "Timeframes")
//show_4h = input.bool(true, title = "H4", group = "Timeframes")

pivot_high_5m = fixnan(request.security(ha_t, "10", ta.pivothigh(tipe ==


"Wick" ? high : close, bars, bars)[1]))
pivot_high_15m = fixnan(request.security(ha_t, "15", ta.pivothigh(tipe ==
"Wick" ? high : close, bars, bars)[1]))
pivot_high_30m = fixnan(request.security(ha_t, "30", ta.pivothigh(tipe ==
"Wick" ? high : close, bars, bars)[1]))
pivot_high_1h = fixnan(request.security(ha_t, "60", ta.pivothigh(tipe ==
"Wick" ? high : close, bars, bars)[1]))
//pivot_high_4h = fixnan(request.security(ha_t, "240", ta.pivothigh(tipe
== "Wick" ? high : close, bars, bars)[1]))

pivot_low_5m = fixnan(request.security(ha_t, "10", ta.pivotlow(tipe ==


"Wick" ? low : close, bars, bars)[1]))
pivot_low_15m = fixnan(request.security(ha_t, "15", ta.pivotlow(tipe ==
"Wick" ? low : close, bars, bars)[1]))
pivot_low_30m = fixnan(request.security(ha_t, "30", ta.pivotlow(tipe ==
"Wick" ? low : close, bars, bars)[1]))
pivot_low_1h = fixnan(request.security(ha_t, "60", ta.pivotlow(tipe ==
"Wick" ? low : close, bars, bars)[1]))
//pivot_low_4h = fixnan(request.security(ha_t, "240", ta.pivotlow(tipe ==
"Wick" ? low : close, bars, bars)[1]))

lwidth = input.int(3, title = "MTF SR linewidth")


ofset = input.int(-15)

//resistance_high_5m = plot(show_5m ? pivot_high_5m : na, color=#ffbf00, offset


= ofset, linewidth=lwidth,style=plot.style_cross, title="Resistance M5")
//resistance_low_5m = plot(show_5m ? pivot_low_5m : na, color=color.yellow,
offset = ofset, linewidth=lwidth,style=plot.style_cross, title="Support M5")

resistance_high_15m = plot(show_15m ? pivot_high_15m : na, color=color.white,


offset = ofset,style=plot.style_cross,linewidth=lwidth, title="Resistance M15")
resistance_low_15m = plot(show_15m ? pivot_low_15m : na, color=color.gray,
offset = ofset,style=plot.style_cross,linewidth=lwidth, title="Support M15")

resistance_high_30m = plot(show_30m ? pivot_high_30m : na, color=#FF0000, offset


= ofset,style=plot.style_cross,linewidth=lwidth, title="Resistance M30")
resistance_low_30m = plot(show_30m ? pivot_low_30m : na, color=#00FF00, offset =
ofset,style=plot.style_cross,linewidth=lwidth,title="Support M30")

resistance_high_1h = plot(show_1h ? pivot_high_1h : na, color=#FF1493, offset =


ofset,style=plot.style_cross,linewidth=lwidth, title="Resistance H1")
resistance_low_1h = plot(show_1h ? pivot_low_1h : na, color=#b85765, offset =
ofset,style=plot.style_cross,linewidth=lwidth, title="Support H1")

//resistance_high_4h = plot(show_4h ? pivot_high_4h : na, color=color.olive,


offset = 0,style=plot.style_cross,linewidth=lwidth, title="Resistance H4")
//resistance_low_4h = plot(show_4h ? pivot_low_4h : na, color=color.olive,
offset = 0,style=plot.style_cross,linewidth=lwidth, title="Support H4")

///////////////////////////////////
//////////// MTF SnR Levels
/////////////////////////////////////
signal_length = input.int(title='Signal Smoothing', minval=1, maxval=200, defval=9)
sma_signal = input(title='Simple MA (Signal Line)', defval=true)

lin_reg = input(title='Lin Reg', defval=true)


linreg_length = input.int(title='Linear Regression Length', minval=1, maxval=200,
defval=15)

[bopen,bhigh,blow,bclose] = request.security(symb, '30', [ta.linreg(open,


linreg_length, 0),ta.linreg(high, linreg_length, 0),ta.linreg(low, linreg_length,
0),ta.linreg(close, linreg_length, 0)], barmerge.gaps_off, barmerge.lookahead_on)

r = bopen < bclose

signalr = request.security(symb,"30", ta.sma(bclose, signal_length),


barmerge.gaps_on, barmerge.lookahead_on)
//plotcandle(r ? bopen : na, r ? bhigh : na, r ? blow : na, r ? bclose : na,
title='LinReg Candles', color=color.green, wickcolor=color.green,
bordercolor=color.green, editable=true)
//plotcandle(r ? na : bopen, r ? na : bhigh, r ? na : blow, r ? na : bclose,
title='LinReg Candles', color=color.red, wickcolor=color.red,
bordercolor=color.red, editable=true)

plot(signalr, color=color.new(color.blue, 0),linewidth = 2)

barsm = input.int(10, title = "Pivot")


tipem = input.string("Body", title = "Type", options = ["Wick",
"Body"])

//show_cm = input.bool(false, title = "Current Timeframe", group =


"Timeframes")

show_5mm = input.bool(true, title = "M10", group = "Timeframes")


show_15mm = input.bool(true, title = "M15", group = "Timeframes")
show_30mm = input.bool(true, title = "M30", group = "Timeframes")
show_1hm = input.bool(true, title = "H1", group = "Timeframes")

pivot_high_5mm = fixnan(request.security(ha_t, "10", ta.pivothigh(tipe ==


"Wick" ? bhigh : bclose, barsm, barsm)[1]))
pivot_high_15mm = fixnan(request.security(ha_t, "15", ta.pivothigh(tipe ==
"Wick" ? bhigh : bclose, barsm, barsm)[1]))
pivot_high_30mm = fixnan(request.security(ha_t, "30", ta.pivothigh(tipe ==
"Wick" ? bhigh : bclose, barsm, barsm)[1]))
pivot_high_1hm = fixnan(request.security(ha_t, "60", ta.pivothigh(tipe ==
"Wick" ? bhigh : bclose, barsm, barsm)[1]))

pivot_low_5mm = fixnan(request.security(ha_t, "10", ta.pivotlow(tipe ==


"Wick" ? blow : bclose, barsm, barsm)[1]))
pivot_low_15mm = fixnan(request.security(ha_t, "15", ta.pivotlow(tipe ==
"Wick" ? blow : bclose, barsm, barsm)[1]))
pivot_low_30mm = fixnan(request.security(ha_t, "30", ta.pivotlow(tipe ==
"Wick" ? blow : bclose, barsm, barsm)[1]))
pivot_low_1hm = fixnan(request.security(ha_t, "60", ta.pivotlow(tipe ==
"Wick" ? blow : bclose, barsm, barsm)[1]))

lwidthm = input.int(2, title = "MTF SR linewidth")


lstylem = plot.style_cross

//resistance_high_5mm = plot(show_5mm ? pivot_high_5mm : na, color=#ffbf00,


offset = ofset, linewidth=lwidthm,style=lstylem, title="Resistance M5")
//resistance_low_5mm = plot(show_5mm ? pivot_low_5mm : na, color=color.yellow,
offset = ofset, linewidth=lwidthm,style=lstylem, title="Support M5")

resistance_high_15mm = plot(show_15mm ? pivot_high_15mm : na, color=color.white,


offset = ofset,style=lstylem,linewidth=lwidthm, title="Resistance M15")
resistance_low_15mm = plot(show_15mm ? pivot_low_15mm : na, color=color.white,
offset = ofset,style=lstylem,linewidth=lwidthm, title="Support M15")

resistance_high_30mm = plot(show_30mm ? pivot_high_30mm : na, color=#FF0000,


offset = ofset,style=lstylem,linewidth=lwidthm, title="Resistance M30")
resistance_low_30mm = plot(show_30mm ? pivot_low_30mm : na, color=#00FF00,
offset = ofset,style=lstylem,linewidth=lwidthm,title="Support M30")

resistance_high_1hm = plot(show_1hm ? pivot_high_1hm : na, color=#FF1493,


offset = ofset,style=lstylem,linewidth=lwidthm, title="Resistance H1")
resistance_low_1hm = plot(show_1hm ? pivot_low_1hm : na, color=#b85765, offset
= ofset,style=lstylem,linewidth=lwidthm, title="Support H1")

////////////////////////////////
// ginley
///////////////////////////////
length = input.int(14, minval=1)
length1 = input.int(20, minval=1)
length2 = input.int(50, minval=1)
length21 = input.int(100, minval=1)
length3 = input.int(200, minval=1)
length4 = input.int(500, minval=1)
sourc = closeh1
mg = 0.0
mg := na(mg[1]) ? ta.ema(sourc, length) : mg[1] + (sourc - mg[1]) / (length *
math.pow(sourc/mg[1], 4))
mg1 = 0.0
mg1 := na(mg1[1]) ? ta.ema(sourc, length1) : mg1[1] + (sourc - mg1[1]) / (length1 *
math.pow(sourc/mg1[1], 4))
mg2 = 0.0
mg2 := na(mg2[1]) ? ta.ema(sourc, length2) : mg2[1] + (sourc - mg2[1]) / (length2 *
math.pow(sourc/mg2[1], 4))
mg21 = 0.0
mg21 := na(mg21[1]) ? ta.ema(sourc, length21) : mg21[1] + (sourc - mg21[1]) /
(length21 * math.pow(sourc/mg21[1], 4))
mg3 = 0.0
mg3 := na(mg3[1]) ? ta.ema(sourc, length3) : mg3[1] + (sourc - mg3[1]) / (length3 *
math.pow(sourc/mg3[1], 4))
mg4 = 0.0
mg4 := na(mg4[1]) ? ta.ema(sourc, length4) : mg4[1] + (sourc - mg4[1]) / (length4 *
math.pow(sourc/mg4[1], 4))

lwidth2 = 3
plot(mg, title='ginley 14', linewidth=lwidth2, style=plot.style_line,
color=color.new(color.red, 0))
plot(mg1, title='ginley 20', linewidth=lwidth2, style=plot.style_line,
color=color.new(color.orange, 0))
plot(mg2, title='ginley 50', linewidth=lwidth2, style=plot.style_line,
color=color.new(#49f343, 7))
plot(mg21, title='ginley 100', linewidth=lwidth2, style=plot.style_line,
color=color.new(#42eddc, 0))
plot(mg3, title='ginley 200', linewidth=lwidth2, style=plot.style_line,
color=color.new(#e84aa6, 0))
//plot(mg4, title='ginley 500', linewidth=lwidth2, style=plot.style_line,
color=color.new(color.white, 0))

/////////////////////////
/// gann
/////////////////////////

disp_d_mrt = input(true, title='Display Daily Levels')


disp_g_lvl = input(true, title='Gann Square Levels')

phigh_d = request.security(ha_t, 'D', high[1], barmerge.gaps_off,


barmerge.lookahead_on)
plow_d = request.security(ha_t, 'D', low[1], barmerge.gaps_off,
barmerge.lookahead_on)
pclose_d = request.security(ha_t, 'D', close[1], barmerge.gaps_off,
barmerge.lookahead_on)

//GANN DEG //
pcsq = math.sqrt(pclose_d)

deg_11 = 630 * 0.00555


deg_10 = 540 * 0.00555
deg_9 = 450 * 0.00555
deg_8 = 360 * 0.00555
deg_7 = 360 * 0.00555
deg_6 = 270 * 0.00555
deg_5 = 270 * 0.00555
deg_4 = 180 * 0.00555
deg_3 = 180 * 0.00555
deg_2 = 90 * 0.00555
deg_1 = 45 * 0.00555
deg_0 = 25 * 0.00555

label_size = input.string('normal', options=['auto', 'tiny', 'small', 'normal',


'large', 'huge'], title='Label size')
off_mult = 34

l_size = label_size == 'auto' ? size.auto : label_size == 'tiny' ? size.tiny :


label_size == 'small' ? size.small : label_size == 'normal' ? size.normal :
label_size == 'large' ? size.large : size.huge
// DAILY GANN LEVELS //

gsc_r = pcsq + deg_0


gsc_r1 = pcsq + deg_1
gsc_r2 = pcsq + deg_2
gsc_r3 = pcsq + deg_3
gsc_r4 = pcsq + deg_4
gsc_r5 = pcsq + deg_5
gsc_r6 = pcsq + deg_6
gsc_r7 = pcsq + deg_7
gsc_r8 = pcsq + deg_8
gsc_r9 = pcsq + deg_9
gsc_r10 = pcsq + deg_10
gsc_r11 = pcsq + deg_11

//GANN DEG //

gsc_s = deg_0 - pcsq


gsc_s1 = deg_1 - pcsq
gsc_s2 = deg_2 - pcsq
gsc_s3 = deg_3 - pcsq
gsc_s4 = deg_4 - pcsq
gsc_s5 = deg_5 - pcsq
gsc_s6 = deg_6 - pcsq
gsc_s7 = deg_7 - pcsq
gsc_s8 = deg_8 - pcsq
gsc_s9 = deg_9 - pcsq
gsc_s10 = deg_10 - pcsq
gsc_s11 = deg_11 - pcsq

//GANN MULTI//

gann_br = gsc_r * gsc_r


gann_br1 = gsc_r1 * gsc_r1
gann_br2 = gsc_r2 * gsc_r2
gann_br3 = gsc_r3 * gsc_r3
gann_br4 = gsc_r4 * gsc_r4
gann_br5 = gsc_r5 * gsc_r5
gann_br6 = gsc_r6 * gsc_r6
gann_br7 = gsc_r7 * gsc_r7
gann_br8 = gsc_r8 * gsc_r8
gann_br9 = gsc_r9 * gsc_r9
gann_br10 = gsc_r10 * gsc_r10
gann_br11 = gsc_r11 * gsc_r11

//////////////////////////////////////////////////////////

gann_ss = gsc_s * gsc_s


gann_ss1 = gsc_s1 * gsc_s1
gann_ss2 = gsc_s2 * gsc_s2
gann_ss3 = gsc_s3 * gsc_s3
gann_ss4 = gsc_s4 * gsc_s4
gann_ss5 = gsc_s5 * gsc_s5
gann_ss6 = gsc_s6 * gsc_s6
gann_ss7 = gsc_s7 * gsc_s7
gann_ss8 = gsc_s8 * gsc_s8
gann_ss9 = gsc_s9 * gsc_s9
gann_ss10 = gsc_s10 * gsc_s10
gann_ss11 = gsc_s11 * gsc_s11

//Gann SQ Levels //

plot(disp_g_lvl ? gann_br : na, color=color.new(color.fuchsia, 0), title='L1_25',


style=plot.style_line, linewidth=2)
plot(disp_g_lvl ? gann_br1 : na, color=color.new(color.fuchsia, 0), title='L1_45',
style=plot.style_line, linewidth=2)
plot(disp_g_lvl ? gann_br2 : na, color=color.new(color.olive, 0), title='L2_90',
style=plot.style_line, linewidth=2)
plot(disp_g_lvl ? gann_br3 : na, color=color.new(color.teal, 0), title='L3_180',
style=plot.style_line, linewidth=2)

plot(disp_g_lvl ? gann_ss : na, color=color.new(color.blue, 0), title='L1_25',


style=plot.style_line, linewidth=2)
plot(disp_g_lvl ? gann_ss1 : na, color=color.new(color.blue, 0), title='L1_45',
style=plot.style_line, linewidth=2)
plot(disp_g_lvl ? gann_ss2 : na, color=color.new(color.olive, 0), title='L2_90',
style=plot.style_line, linewidth=2)
plot(disp_g_lvl ? gann_ss3 : na, color=color.new(color.teal, 0), title='L3_180',
style=plot.style_line, linewidth=2)

//GANN LABEL//

ll_offset = bar_index+2

label_gann_br = disp_g_lvl ? label.new(x=ll_offset, y=gann_br, text='Sell║L1║' +


str.tostring(gann_br), style=label.style_label_left,color=color.new(color.blue,
100), textcolor=color.red, size=l_size) : na
label.delete(label_gann_br[1])
label_gann_br1 = disp_g_lvl ? label.new(x=ll_offset, y=gann_br1, text='Sell║L1║' +
str.tostring(gann_br1), style=label.style_label_left, color=color.new(color.blue,
100),textcolor=color.red, size=l_size) : na
label.delete(label_gann_br1[1])
label_gann_br2 = disp_g_lvl ? label.new(x=ll_offset, y=gann_br2, text='Stop
Loss║L2║' + str.tostring(gann_br2),
style=label.style_label_left,color=color.new(color.blue, 100),
textcolor=color.yellow, size=l_size) : na
label.delete(label_gann_br2[1])
label_gann_br3 = disp_g_lvl ? label.new(x=ll_offset, y=gann_br3,
text='Breakout║L3║' + str.tostring(gann_br3), style=label.style_label_left,
color=color.new(color.blue, 100),textcolor=color.green, size=l_size) : na
label.delete(label_gann_br3[1])
label_gann_br4 = disp_g_lvl ? label.new(x=ll_offset, y=gann_br4,
text='Breakout║L3║' + str.tostring(gann_br4),
style=label.style_label_left,color=color.new(color.blue, 100),
textcolor=color.green, size=l_size) : na
label.delete(label_gann_br4[1])
label_gann_br5 = disp_g_lvl ? label.new(x=ll_offset, y=gann_br5, text='Sell║L4║' +
str.tostring(gann_br5), style=label.style_label_left, color=color.new(color.blue,
100),textcolor=color.red, size=l_size) : na
label.delete(label_gann_br5[1])
label_gann_br6 = disp_g_lvl ? label.new(x=ll_offset, y=gann_br6, text='Sell║L4║' +
str.tostring(gann_br6), style=label.style_label_left,color=color.new(color.blue,
100), textcolor=color.red, size=l_size) : na
label.delete(label_gann_br6[1])
label_gann_br7 = disp_g_lvl ? label.new(x=ll_offset, y=gann_br7, text='watch║L5║' +
str.tostring(gann_br7), style=label.style_label_left,color=color.new(color.blue,
100), textcolor=color.lime, size=l_size) : na
label.delete(label_gann_br7[1])
label_gann_br8 = disp_g_lvl ? label.new(x=ll_offset, y=gann_br8, text='watch║L5║' +
str.tostring(gann_br8), style=label.style_label_left,color=color.new(color.blue,
100), textcolor=color.green, size=l_size) : na
label.delete(label_gann_br8[1])
label_gann_br9 = disp_g_lvl ? label.new(x=ll_offset, y=gann_br9, text='watch║L6║' +
str.tostring(gann_br9), style=label.style_label_left,color=color.new(color.blue,
100), textcolor=color.green, size=l_size) : na
label.delete(label_gann_br9[1])
label_gann_br10 = disp_g_lvl ? label.new(x=ll_offset, y=gann_br10, text='Sell║L7║ '
+ str.tostring(gann_br10), style=label.style_label_left,color=color.new(color.blue,
100), textcolor=color.red, size=l_size) : na
label.delete(label_gann_br10[1])
label_gann_br11 = disp_g_lvl ? label.new(x=ll_offset, y=gann_br11, text='Stop
Loss║L8║ ' + str.tostring(gann_br11),
style=label.style_label_left,color=color.new(color.blue, 100),
textcolor=color.green, size=l_size) : na
label.delete(label_gann_br11[1])

label_gann_ss = disp_g_lvl ? label.new(x=ll_offset, y=gann_ss, text='Buy║ L1║' +


str.tostring(gann_ss), style=label.style_label_left,color=color.new(color.blue,
100), textcolor=color.green, size=l_size) : na
label.delete(label_gann_ss[1])
label_gann_ss1 = disp_g_lvl ? label.new(x=ll_offset, y=gann_ss1, text='Buy║ L1║' +
str.tostring(gann_ss1), style=label.style_label_left,color=color.new(color.blue,
100), textcolor=color.green, size=l_size) : na
label.delete(label_gann_ss1[1])
label_gann_ss2 = disp_g_lvl ? label.new(x=ll_offset, y=gann_ss2, text='Stop Loss║
L2║' + str.tostring(gann_ss2),
style=label.style_label_left,color=color.new(color.blue, 100),
textcolor=color.yellow, size=l_size) : na
label.delete(label_gann_ss2[1])
label_gann_ss3 = disp_g_lvl ? label.new(x=ll_offset, y=gann_ss3,
text='Breakout║L3║' + str.tostring(gann_ss3),
style=label.style_label_left,color=color.new(color.blue, 100), textcolor=color.red,
size=l_size) : na
label.delete(label_gann_ss3[1])
label_gann_ss4 = disp_g_lvl ? label.new(x=ll_offset, y=gann_ss4,text='Breakout║L3║'
+ str.tostring(gann_ss4), style=label.style_label_left,color=color.new(color.blue,
100), textcolor=color.maroon, size=l_size) : na
label.delete(label_gann_ss4[1])
label_gann_ss5 = disp_g_lvl ? label.new(x=ll_offset, y=gann_ss5,text='Buy║L4║' +
str.tostring(gann_ss5), style=label.style_label_left,color=color.new(color.blue,
100), textcolor=color.green, size=l_size) : na
label.delete(label_gann_ss5[1])
label_gann_ss6 = disp_g_lvl ? label.new(x=ll_offset, y=gann_ss6, text='Buy║L4║' +
str.tostring(gann_ss6), style=label.style_label_left,color=color.new(color.blue,
100), textcolor=color.green, size=l_size) : na
label.delete(label_gann_ss6[1])
label_gann_ss7 = disp_g_lvl ? label.new(x=ll_offset, y=gann_ss7, text='watch║L5║' +
str.tostring(gann_ss7), style=label.style_label_left,color=color.new(color.blue,
100), textcolor=color.red, size=l_size) : na
label.delete(label_gann_ss7[1])
label_gann_ss8 = disp_g_lvl ? label.new(x=ll_offset, y=gann_ss8, text='watch║L5║' +
str.tostring(gann_ss8), style=label.style_label_left,color=color.new(color.blue,
100), textcolor=color.red, size=l_size) : na
label.delete(label_gann_ss8[1])
label_gann_ss9 = disp_g_lvl ? label.new(x=ll_offset, y=gann_ss9, text='watch║L6║' +
str.tostring(gann_ss9), style=label.style_label_left,color=color.new(color.blue,
100), textcolor=color.red, size=l_size) : na
label.delete(label_gann_ss9[1])
label_gann_ss10 = disp_g_lvl ? label.new(x=ll_offset, y=gann_ss10, text='Buy║L7║' +
str.tostring(gann_ss10), style=label.style_label_left,color=color.new(color.blue,
100), textcolor=color.green, size=l_size) : na
label.delete(label_gann_ss10[1])
label_gann_ss11 = disp_g_lvl ? label.new(x=ll_offset, y=gann_ss11, text='Stop
Loss║L8║' + str.tostring(gann_ss11),
style=label.style_label_left,color=color.new(color.blue, 100), textcolor=color.red,
size=l_size) : na
label.delete(label_gann_ss11[1])

/////////////////END/////////////////////////////////

///////////////////////////
///// breakout
////////////////////////////

// === BACKTEST RANGE ===


From_Year = input(defval=2019, title='From Year')
From_Month = input.int(defval=1, title='From Month', minval=1, maxval=12)
From_Day = input.int(defval=1, title='From Day', minval=1, maxval=31)
To_Year = input(defval=9999, title='To Year')
To_Month = input.int(defval=1, title='To Month', minval=1, maxval=12)
To_Day = input.int(defval=1, title='To Day', minval=1, maxval=31)
Start = timestamp(From_Year, From_Month, From_Day, 00, 00) // backtest start
window
Finish = timestamp(To_Year, To_Month, To_Day, 23, 59) // backtest finish window

// A switch to control background coloring of the test period - Use for easy
visualization of backtest range and manual calculation of
// buy and hold (via measurement) if doing prior periods since value in Strategy
Tester extends to current date by default
testPeriodBackground = input(title='Color Background - Test Period?', defval=false)
testPeriodBackgroundColor = testPeriodBackground and time >= Start and time <=
Finish ? #00FF00 : na
//bgcolor(testPeriodBackgroundColor, transp=95)

// == FILTERING ==
// Inputs
useMaFilter = input(title='Use MA for Filtering?', defval=false)
maType = input.string(defval='EMA', options=['EMA', 'SMA'], title='MA Type For
Filtering')
maLength = input.int(defval=200, title='MA Period for Filtering', minval=1)

// Declare function to be able to swap out EMA/SMA


ma(maType, src, length) =>
maType == 'EMA' ? request.security(symb,"30",ta.ema(src,
length),barmerge.gaps_off, barmerge.lookahead_on) : ta.sma(src, length) //Ternary
Operator (if maType equals EMA, then do ema calc, else do sma calc)
maFilter = ma(maType, closeh1, maLength)
//plot(maFilter, title = "Trend Filter MA", color = color.green, linewidth = 3,
style = plot.style_line, transp = 50)

// Check to see if the useMaFilter check box is checked, this then inputs this
conditional "maFilterCheck" variable into the strategy entry
maFilterCheck = if useMaFilter == true
maFilter
else
0

// === PLOT SWING HIGH/LOW AND MOST RECENT LOW TO USE AS STOP LOSS EXIT POINT ===
// Inputs
//pvtLenL = input(3, minval=1, title="Pivot Length Left Hand Side") //use if
you want to change this to an input
//pvtLenR = input(3, minval=1, title="Pivot Length Right Hand Side") //use if
you want to change this to an input
pvtLenL = 3
pvtLenR = 3

// Get High and Low Pivot Points


pvthi_ = request.security(symb,"30",ta.pivothigh(highh1, pvtLenL, pvtLenR))
pvtlo_ = request.security(symb,"30",ta.pivotlow(lowh1, pvtLenL, pvtLenR))

// Force Pivot completion before plotting.


Shunt = 1 //Wait for close before printing pivot? 1 for true 0 for flase
maxLvlLen = 0 //Maximum Extension Length
pvthi = pvthi_[Shunt]
pvtlo = pvtlo_[Shunt]

// Count How many candles for current Pivot Level, If new reset.
counthi = request.security(symb,"30",ta.barssince(not na(pvthi)))
countlo = request.security(symb,"30",ta.barssince(not na(pvtlo)))

pvthis = fixnan(pvthi)
pvtlos = fixnan(pvtlo)
hipc = request.security(symb,"30",ta.change(pvthis)) != 0 ? na : color.maroon
lopc = request.security(symb,"30",ta.change(pvtlos)) != 0 ? na : color.green

// Display Pivot lines


//plot(maxLvlLen == 0 or counthi < maxLvlLen ? pvthis : na, color=hipc,
linewidth=1, offset=-pvtLenR - Shunt, title='Top Levels', transp=0)
//plot(maxLvlLen == 0 or countlo < maxLvlLen ? pvtlos : na, color=lopc,
linewidth=1, offset=-pvtLenR - Shunt, title='Bottom Levels', transp=0)
//plot(maxLvlLen == 0 or counthi < maxLvlLen ? pvthis : na, color=hipc,
linewidth=1, offset=0, title='Top Levels 2', transp=0)
//plot(maxLvlLen == 0 or countlo < maxLvlLen ? pvtlos : na, color=lopc,
linewidth=1, offset=0, title='Bottom Levels 2', transp=0)

// Stop Levels
stopBuff = input.float(0.0, minval=-2, title='Stop Loss Buffer off Swing Low (%)')
stopPerc = stopBuff * .01 // Turn stop buffer input into a percentage
stopLevel = request.security(symb,"30",ta.valuewhen(pvtlo_, lowh[pvtLenR], 0))
//Stop Level at Swing Low
stopLevel2 = stopLevel - stopLevel * stopPerc // Stop Level with user-defined
buffer to avoid stop hunts and give breathing room
//plot(stopLevel2, style=plot.style_line, color=color.new(color.orange, 50),
show_last=1, linewidth=1, trackprice=true)
buyLevel = request.security(symb,"30",ta.valuewhen(pvthi_, highh[pvtLenR], 0))
//Buy level at Swing High
buyLevel2 = buyLevel + buyLevel * stopPerc // Buy-stop level with user-defined
buffer to avoid stop hunts and give breathing room
//plot(buyLevel2, style=plot.style_line, color=color.new(color.aqua, 50),
show_last=1, linewidth=1, trackprice=true)

// Conditions for entry and exit


buySignal = highh1 > buyLevel2
buy1 = buySignal and time > Start and time < Finish and buyLevel2 >
maFilterCheck // All these conditions need to be met to buy
sellSignal = lowh1 < stopLevel2 // Code to act like a stop-loss for the Study

// (STRATEGY ONLY) Comment out for Study


// strategy.entry("Long", strategy.long, stop = buyLevel2, when = time > Start and
time < Finish and buyLevel2 > maFilterCheck)
// strategy.exit("Exit Long", from_entry = "Long", stop=stopLevel2)

// == (STUDY ONLY) Comment out for Strategy ==


// Check if in position or not
inPosition = bool(na)
inPosition := buy1[1] ? true : sellSignal[1] ? false : inPosition[1]
flat = bool(na)
flat := not inPosition
buyStudy = buy1 and flat
sellStudy = sellSignal and inPosition
//Plot indicators on chart and set up alerts for Study
//plotshape(buyStudy, style=shape.triangleup, location=location.belowbar,
color=color.new(#1E90FF, 0), text='', size=size.small)
//plotshape(sellStudy, style=shape.triangledown, location=location.abovebar,
color=color.new(#EE82EE, 0), text='', size=size.small)

if buyStudy
label.new(x=bar_index, y=math.min(low1, low1[1]), text="B", color=color.blue,
textcolor=color.white, style=label.style_label_up, size=size.small)

if sellStudy
label.new(x=bar_index, y=math.max(high1, high1[1]), text="S",
color=color.fuchsia, textcolor=color.white, style=label.style_label_down,
size=size.small)

////////////////////////////////////////////
//// vwap week
///////////////////////////////////////////////
q = timenow - 7*86400000

f(x, y, z, a)=>
ini = time>=x and time[1]<x
src = hlc * volume1
vol = volume1
dev = hlc*hlc*volume1
src := ini?src:src+src[1]
vol := ini?vol:vol+vol[1]
dev := ini?dev:dev+dev[1]
vwap = src/vol
dev1 = math.sqrt(dev/vol-vwap*vwap)*y
dev2 = math.sqrt(dev/vol-vwap*vwap)*z
dev3 = math.sqrt(dev/vol-vwap*vwap)*a
[vwap, dev1, dev2, dev3]

vlwidth = 3
[vwap, dev1, dev2, dev3] = f(q, 1.0, 2.0, 3.0)
plot(vwap,color=color.white,linewidth=vlwidth)
plot(vwap+dev1,color=color.red,linewidth=vlwidth)
plot(vwap-dev1,color=color.red,linewidth=vlwidth)
plot(vwap+dev2,color=color.blue,linewidth=vlwidth)
plot(vwap-dev2,color=color.blue,linewidth=vlwidth)
plot(vwap+dev3,color=color.teal,linewidth=vlwidth)
plot(vwap-dev3,color=color.teal,linewidth=vlwidth)

upperBand = vwap+dev1
lowerBand = vwap-dev1
upperBand2 = vwap+dev2
lowerBand2 = vwap-dev2
upperBand3 = vwap+dev3
lowerBand3 = vwap-dev3
vol_ema = ta.ema(volume1, 20)

if volume1 > vol_ema and open1 < lowerBand and close1 > lowerBand
label.new(x=bar_index, y=math.max(low1, low1[1]), color=color.teal, text =
'VB',textcolor=color.white, style=label.style_label_up, size=size.tiny)
if volume1 > vol_ema and open1 < lowerBand2 and close1 > lowerBand2
label.new(x=bar_index, y=math.max(low1, low1[1]), color=color.teal, text =
'VB',textcolor=color.white, style=label.style_label_up, size=size.tiny)
if volume1 > vol_ema and open1 < lowerBand3 and close1 > lowerBand3
label.new(x=bar_index, y=math.max(low1, low1[1]), color=color.teal, text =
'VB',textcolor=color.white, style=label.style_label_up, size=size.tiny)
if volume1 > vol_ema and open1 < vwap and close1 > vwap
label.new(x=bar_index, y=math.max(low1, low1[1]), color=color.teal, text =
'VB',textcolor=color.white, style=label.style_label_up, size=size.tiny)

if volume1 > vol_ema and open1 > lowerBand and close1 < lowerBand
label.new(x=bar_index, y=math.max(high1, high1[1]), color=color.orange, text =
'VB',textcolor=color.white, style=label.style_label_down, size=size.tiny)
if volume1 > vol_ema and open1 > lowerBand2 and close1 < lowerBand2
label.new(x=bar_index, y=math.max(high1, high1[1]), color=color.orange, text =
'VB',textcolor=color.white, style=label.style_label_down, size=size.tiny)
if volume1 > vol_ema and open1 > lowerBand3 and close1 < lowerBand3
label.new(x=bar_index, y=math.max(high1, high1[1]), color=color.orange, text =
'VB',textcolor=color.white, style=label.style_label_down, size=size.tiny)
if volume1 > vol_ema and open1 > vwap and close1 < vwap
label.new(x=bar_index, y=math.max(high1, high1[1]), color=color.orange, text =
'VB',textcolor=color.white, style=label.style_label_down, size=size.tiny)

You might also like