Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- study(" Weight Systen", "Weight system", true, format.inherit)
- //code from linear regression used https://www.tradingview.com/script/CD7yUWRV-Linear-Regression-Trend-Channel/
- //code from pivots taken from https://www.tradingview.com/script/RqdDzaRp-Traditional-Pivot-Points-Alerts/
- // code for linear lines taken from https://www.tradingview.com/v/Mk7G47nS/
- period = input( 100, "Period" , input.integer, minval=3)
- deviations = input( 2.0, "Deviation(s)" , input.float , minval=0.1, step=0.1)
- periodMinusOne = period-1
- Ex = 0.0, Ey = 0.0, Ex2 = 0.0, Exy = 0.0, for i=0 to periodMinusOne
- closeI = nz(close[i]), Ex := Ex + i, Ey := Ey + closeI, Ex2 := Ex2 + (i * i), Exy := Exy + (closeI * i)
- ExEx = Ex * Ex, slope = Ex2==ExEx ? 0.0 : (period * Exy - Ex * Ey) / (period * Ex2 - ExEx)
- linearRegression = (Ey - slope * Ex) / period
- intercept = linearRegression + bar_index * slope
- deviation = 0.0, for i=0 to periodMinusOne
- deviation := deviation + pow(nz(close[i]) - (intercept - slope * (bar_index[i])), 2.0)
- deviation := deviations * sqrt(deviation / periodMinusOne)
- startingPointY = linearRegression + slope / periodMinusOne
- lineColor = startingPointY > startingPointY[1] ? color.blue : color.red
- //
- ///////////////
- // FUNCTIONS //
- ///////////////
- // Function outputs 1 when it's the first bar of the D/W/M/Y
- is_newbar(res) =>
- ch = 0
- if(res == 'Y')
- t = year(time('D'))
- ch := change(t) != 0 ? 1 : 0
- else
- t = time(res)
- ch := change(t) != 0 ? 1 : 0
- ch
- // Rounding levels to min tick
- nround(x) =>
- n = round(x / syminfo.mintick) * syminfo.mintick
- ////////////
- // INPUTS //
- ////////////
- pp_type = input(title = 'Pivot Type', defval = "Linear")
- pp_period = input(title = "Period", defval = "Day", type = input.string, options = ['Day', 'Week', 'Month', 'Year'])
- show_historical_levels = input(title = "Show Historical Levels?", defval = false, type = input.bool)
- show_level_value = input(title = "Show Levels Value?", defval = true, type = input.bool)
- show_current_levels = input(title = "Show Current Levels", defval = false, type = input.bool)
- pp_res = pp_period == 'Day' ? 'D' : pp_period == 'Week' ? 'W' : pp_period == 'Month' ? 'M' : 'Y'
- /////////////////////
- // Get HLC from HT //
- // Calc Open
- open_cur = 0.0
- open_cur := is_newbar(pp_res) ? open : open_cur[1]
- popen = 0.0
- popen := is_newbar(pp_res) ? open_cur[1] : popen[1]
- // Calc High
- high_cur = 0.0
- high_cur := is_newbar(pp_res) ? high : max(high_cur[1], high)
- phigh = 0.0
- phigh := is_newbar(pp_res) ? high_cur[1] : phigh[1]
- // Calc Low
- low_cur = 0.0
- low_cur := is_newbar(pp_res) ? low : min(low_cur[1], low)
- plow = 0.0
- plow := is_newbar(pp_res) ? low_cur[1] : plow[1]
- // Calc Close
- pclose = 0.0
- pclose := is_newbar(pp_res) ? close[1] : pclose[1]
- ////////////////////////////
- // CALCULATE PIVOT POINTS //
- ////////////////////////////
- PP = 0.0
- R1 = 0.0
- S1 = 0.0
- if (pp_type == "Linear")
- PP := startingPointY
- R1 := PP + deviation
- S1 := PP - deviation
- // Projected levels
- prPP = 0.0
- prR1 = 0.0
- prS1 = 0.0
- if (pp_type == "Linear")
- prPP := startingPointY
- prR1 := prPP + deviation
- prS1 := prPP - deviation
- //////////////
- // PLOTTING //
- bars_sinse = 0
- bars_sinse := is_newbar(pp_res) ? 0 : bars_sinse[1] + 1
- ////////////////////////
- // PLOT PIVOTS LEVELS //
- vpp_p = line.new(bar_index[min(bars_sinse, 300)], PP, bar_index, PP, color=color.gray, style = line.style_solid, extend = extend.none)
- vs1_p = line.new(bar_index[min(bars_sinse, 300)], S1, bar_index, S1, color=color.red, style = line.style_solid, extend = extend.none)
- vr1_p = line.new(bar_index[min(bars_sinse, 300)], R1, bar_index, R1, color=color.green, style = line.style_solid, extend = extend.none)
- // delete previous lines in the same period
- if (not is_newbar(pp_res) or not show_historical_levels)
- line.delete(vpp_p[1])
- line.delete(vs1_p[1])
- line.delete(vr1_p[1])
- // Add labels
- label_vpp = label.new(bar_index, PP, text=show_level_value ? ("P" + " " + tostring(nround(PP))) : "P", style= label.style_none)
- label_vs1 = label.new(bar_index, S1, text=show_level_value ? ("S1" + " " + tostring(nround(S1))) : "S1", style= label.style_none)
- label_vr1 = label.new(bar_index, R1, text=show_level_value ? ("R1" + " " + tostring(nround(R1))) : "R1", style= label.style_none)
- label.delete(label_vpp[1])
- label.delete(label_vs1[1])
- label.delete(label_vr1[1])
- // Projected levels
- line vpp_pr = na
- line vs1_pr = na
- line vr1_pr = na
- if (show_current_levels)
- vpp_pr = line.new(bar_index[1], prPP, bar_index, prPP, color=color.gray, style = line.style_dashed, extend = extend.right)
- vs1_pr = line.new(bar_index[1], prS1, bar_index, prS1, color=color.red, style = line.style_dashed, extend = extend.right)
- vr1_pr = line.new(bar_index[1], prR1, bar_index, prR1, color=color.green, style = line.style_dashed, extend = extend.right)
- line.delete(vpp_pr[1])
- line.delete(vs1_pr[1])
- line.delete(vr1_pr[1])
- //
- //
- extra = input(false)
- a5 = 0.
- a5 := PP
- //----
- upper = 0.
- lower = 0.
- upper :=R1
- lower := S1
- //----
- n = bar_index
- if barstate.islast and extra == true
- line A = line.new(n[1],upper[1],n,upper,extend=extend.right,color=#0080FF,style=line.style_dashed,width=2)
- line B = line.new(n[1],a5[1],n,a5,extend=extend.right,color=#e65100,style=line.style_dashed,width=2)
- line C = line.new(n[1],lower[1],n,lower,extend=extend.right,color=#ff1100,style=line.style_dashed,width=2)
- line.delete(A[1])
- line.delete(B[1])
- line.delete(C[1])
- //
- //==== REGRESSION SLOPE
- src1 = input(close, title="Source")
- ext=input(true,title="Extend Lines")
- ln=ext?extend.right:extend.none
- //-- MEAN
- calcSlope(src1, period) =>
- if not barstate.islast
- [float(na), float(na), float(na)]
- else
- sumX = 0.0
- sumY = 0.0
- sumXSqr = 0.0
- sumXY = 0.0
- for i = 0 to period - 1
- val = src1[i]
- per = i + 1.0
- sumX := sumX + per
- sumY := sumY + val
- sumXSqr := sumXSqr + per * per
- sumXY := sumXY + val * per
- slope = (period * sumXY - sumX * sumY) / (period * sumXSqr - sumX * sumX)
- average = sumY / period
- intercept = average - slope * sumX / period + slope
- [slope, average, intercept]
- [s, a1, i] = calcSlope(src1, period)
- //deviation code by jwammo12
- lrc = linreg(src1, period, 0)
- lrc1 = linreg(src1,period,1)
- lrSlope = (lrc-lrc1)
- lrIntercept = lrc - bar_index*lrSlope
- deviationSum = 0.0
- for z=0 to period-1
- deviationSum:= deviationSum + pow(src1[z]-(lrSlope*(bar_index-z)+lrIntercept), 2)
- deviation5 = sqrt(deviationSum/(period))
- up = deviation5 * deviations //+lrc
- dn = deviation5 * deviations
- startPrice = (i + s * (period - 1))
- endPrice = i
- ///////////////////
- var line baseLine3 = na
- if na(baseLine3)
- baseLine3 := line.new(bar_index - period + 1, startPrice, bar_index, endPrice, width=5, color=color.blue, style=line.style_dotted)
- else
- line.set_xy1(baseLine3, bar_index - period + 1, startPrice)
- line.set_xy2(baseLine3, bar_index, endPrice)
- na //
- //////////High Channel//////////////
- var line baseLine1 = na
- if na(baseLine1)
- baseLine1 := line.new(bar_index - period + 1, startPrice + up, bar_index, endPrice + up, width=5, color=color.red, style=line.style_dotted)
- else
- line.set_xy1(baseLine1, bar_index - period + 1, startPrice + up)
- line.set_xy2(baseLine1, bar_index, endPrice + up)
- na //
- ////////////////Low Channel///////////////////
- var line baseLine2 = na
- if na(baseLine2)
- baseLine2 := line.new(bar_index - period + 1, startPrice-dn, bar_index, endPrice-dn, width=5, color=color.red, style=line.style_dotted)
- else
- line.set_xy1(baseLine2, bar_index - period + 1, startPrice- dn)
- line.set_xy2(baseLine2, bar_index, endPrice- dn)
- na //
- hilow = ((high - low)*100)
- openclose = ((close - open)*100)
- vol1 = (volume / hilow)
- spreadvol = (openclose * vol1)
- VPT = spreadvol + cum(spreadvol)
- window_len = 28
- v_len = 14
- price_spread = stdev(high-low, window_len)
- vp = spreadvol + cum(spreadvol)
- smooth = sma(vp, v_len)
- v_spread = stdev(vp - smooth, window_len)
- shadow = (vp - smooth) / v_spread * price_spread
- out1 = shadow > 0 ? high + shadow : low + shadow
- //plot(out, style=line,linewidth=3, color=color)
- len=input(5)
- vpt=ema(out1,len)
- // INPUTS //
- st_mult = input(3, title = 'SuperTrend Multiplier', minval = 0, maxval = 100, step = 0.01)
- st_period = input(7, title = 'SuperTrend Period', minval = 1)
- // CALCULATIONS //
- up_lev = vpt - (st_mult * atr(st_period))
- dn_lev = vpt + (st_mult * atr(st_period))
- up_trend = 0.0
- up_trend := close[1] > up_trend[1] ? max(up_lev, up_trend[1]) : up_lev
- down_trend = 0.0
- down_trend := close[1] < down_trend[1] ? min(dn_lev, down_trend[1]) : dn_lev
- // Calculate trend var
- trend10 = 0
- trend10 := close > down_trend[1] ? 1: close < up_trend[1] ? -1 : nz(trend10[1], 1)
- // Calculate SuperTrend Line
- st_line = trend10 ==1 ? up_trend : down_trend
- //
- // inputs
- Depth = input(12, title="Depth") // Depth
- Deviation = input(5, title="Deviation") // Deviation
- // ZigZag
- lastlow = 0.0, lasthigh = 0.0
- lastlow := nz(lastlow[1])
- lasthigh := nz(lasthigh[1])
- data(x) =>
- d = security(syminfo.tickerid, timeframe.period, x, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
- d
- getLow(x, y, z, a) =>
- lastlow = y
- v = data(x)
- m = v==lastlow or data(z) - v > a*syminfo.mintick
- if v!=lastlow
- lastlow := v
- if m
- v := 0.0
- [v,lastlow]
- getHigh(x, y, z, a) =>
- lasthigh = y
- v = data(x)
- m = v==lasthigh or v - data(z) > a*syminfo.mintick
- if v!=lasthigh
- lasthigh := v
- if m
- v := 0.0
- [v,lasthigh]
- [v,e] = getLow(lowest(Depth), lastlow, low, Deviation)
- lastlow := e
- zBB = v != 0.0
- [v1,e1] = getHigh(highest(Depth), lasthigh, high, Deviation)
- lasthigh := e1
- zSS = v1 != 0.0
- zigzagDirection = -1
- zigzagHigh = 0
- zigzagLow = 0
- zigzagDirection := zBB ? 0 : zSS ? 1 : nz(zigzagDirection[1], -1)
- virtualLow = zigzagLow[1] + 1
- if not zBB or (zBB and zigzagDirection == zigzagDirection[1] and low > low[virtualLow])
- zigzagLow := nz(zigzagLow[1]) + 1
- virtualHigh = zigzagHigh[1] + 1
- if not zSS or (zSS and zigzagDirection == zigzagDirection[1] and high < high[virtualHigh])
- zigzagHigh := nz(zigzagHigh[1]) + 1
- a=bar_index-zigzagLow
- b=bar_index-zigzagHigh
- var color c = na, c := fixnan(a < b[1] ? color.lime : a > b[1] ? color.red : na)
- //line zigzag = line.new(bar_index-zigzagLow, low[zigzagLow], bar_index-zigzagHigh, high[zigzagHigh], color=c, style=line.style_solid, width=4)
- //if (zigzagDirection == zigzagDirection[1])
- //line.delete(zigzag[1])
- zzPrevHigh = zigzagHigh[1]
- zzPrevLow = zigzagLow[1]
- if not na(zzPrevHigh[1])
- zzPrevHigh := zzPrevHigh[1] + 1
- if not na(zzPrevLow[1])
- zzPrevLow := zzPrevLow[1] + 1
- if zigzagDirection != zigzagDirection[1]
- if zigzagDirection == 1
- zzPrevHigh := zigzagHigh[1] + 1
- if zigzagDirection == 0
- zzPrevLow := zigzagLow[1] + 1
- //
- src = input(close, title="Source")
- //sma
- sma20 = sma(src, 20)
- smapoint = 0
- smapoint := src > sma20 ? smapoint + 1 : smapoint - 1
- //AO
- ao = sma(hl2,5) - sma(hl2,34)
- aopoint = ao > 0 ? 1 : ao < 0 ? -1 : 0
- //momentum
- mom = src - src[14]
- mompoint = mom > 0 ? 1 : mom < 0 ? -1 : 0
- //MACD
- fast_ma = ema(src, 12)
- slow_ma = ema(src, 26)
- macd = fast_ma - slow_ma
- signal = ema(macd, 9)
- hist = macd - signal
- histpoint = hist > hist[1] ? 3 : -3
- //Bull bear
- Length = 30
- r1=iff(close[1]<open,max(open-close[1],high-low),high-low)
- r2=iff(close[1]>open,max(close[1]-open,high-low),high-low)
- bull=iff(close==open,iff(high-close==close-low,iff(close[1]>open,max(high-open,close-low),r1),iff(high-close>close-low,iff(close[1]<open, max(high-close[1],close-low), high-open),r1)),iff(close<open,iff(close[1]<open,max(high-close[1],close-low), max(high-open,close-low)),r1))
- bear=iff(close==open,iff(high-close==close-low,iff(close[1]<open,max(open-low,high-close),r2),iff(high-close>close-low,r2,iff(close[1]>open,max(close[1]-low,high-close), open-low))),iff(close<open,r2,iff(close[1]>open,max(close[1]-low,high-close),max(open-low,high-close))))
- colors=iff(sma(bull-bear,Length)>0, color.green, color.red)
- // barcolor(colors)
- bbpoint = sma(bull-bear,Length)>0 ? 1 : -1
- //UO
- length7 = 7,
- length14 = 14,
- length28 = 28
- average(bp, tr_, length) => sum(bp, length) / sum(tr_, length)
- high_ = max(high, src[1])
- low_ = min(low, src[1])
- bp = src - low_
- tr_ = high_ - low_
- avg7 = average(bp, tr_, length7)
- avg14 = average(bp, tr_, length14)
- avg28 = average(bp, tr_, length28)
- uoout = 100 * (4*avg7 + 2*avg14 + avg28)/7
- uopoint = uoout > 70 ? 1 : uoout < 30 ? -1 : 0
- //IC
- conversionPeriods = 9
- basePeriods = 26
- laggingSpan2Periods = 52
- displacement = 26
- donchian(len) => avg(lowest(len), highest(len))
- baseLine = donchian(basePeriods)
- icpoint = src > baseLine ? 1 : -1
- //HMA
- hullma = wma(2*wma(src, 9/2)-wma(src, 21), round(sqrt(21)))
- hmapoint = src > hullma ? 2 : -2
- //
- //
- trendDetectionLength =4
- float trend = na
- float wave = na
- float vol = na
- mov = close>close[1] ? 1 : close<close[1] ? -1 : 0
- trend := (mov != 0) and (mov != mov[1]) ? mov : nz(trend[1])
- isTrending = rising(close, trendDetectionLength) or falling(close, trendDetectionLength)
- wave := (trend != nz(wave[1])) and isTrending ? trend : nz(wave[1])
- vol := wave == wave[1] ? (nz(vol[1])+volume) : volume
- up1 = wave == 1 ? vol : 0
- dn1 = wave == 1 ? 0 : vol
- Weis= up1 > dn1 ? 2 : -2
- //
- stochlen = 14
- roclen =20
- ccilen =21
- dilen = 5
- dirmov(len) =>
- up = change(high)
- down = -change(low)
- truerange = rma(tr, len)
- plus = fixnan(100 * rma(up > down and up > 0 ? up : 0, len) / truerange)
- minus = fixnan(100 * rma(down > up and down > 0 ? down : 0, len) / truerange)
- [plus, minus]
- f_draw_infopanel(_x, _y, _line, _text, _color)=>
- _rep_text = ""
- for _l = 0 to _line
- _rep_text := _rep_text + "\n"
- _rep_text := _rep_text + _text
- var label _la = na
- label.delete(_la)
- _la := label.new(
- x=_x, y=_y,
- text=_rep_text, xloc=xloc.bar_time, yloc=yloc.price,
- color=color.black, style=label.style_labelup, textcolor=_color, size=size.normal)
- TD = 0
- TS = 0
- TD := close > close[4] ? nz(TD[1]) + 1 : 0
- TS := close < close[4] ? nz(TS[1]) + 1 : 0
- TDUp = TD - valuewhen(TD < TD[1], TD , 1 )
- TDDn = TS - valuewhen(TS < TS[1], TS , 1 )
- stoch = stoch(close, high, low, stochlen)
- roc = roc(close, roclen)
- Roc=roc > 0 ? 1 : -1
- cci = cci(close, ccilen)
- CCI=cci > 0? 2 : -2
- [plus, minus] = dirmov(dilen)
- dmi = plus - minus
- DMI= dmi >= 0? 2 : -2
- //
- Zig=a<b? 1 : -1
- //
- STT=trend10 == 1 ? 1 : -1
- //
- AF_initial = input(0.02)
- AF_increment = input(0.02)
- AF_maximum = input(0.2)
- // start with uptrend
- uptrend = true
- newtrend = false
- EP = high
- SAR = low
- AF = AF_initial
- if not na(uptrend[1]) and not na(newtrend[1])
- if uptrend[1]
- EP := max(high, EP[1])
- else
- EP := min(low, EP[1])
- if newtrend[1]
- AF := AF_initial
- else
- if EP != EP[1]
- AF := min(AF_maximum, AF[1] + AF_increment)
- else
- AF := AF[1]
- SAR := SAR[1] + AF * (EP - SAR[1])
- if uptrend[1]
- if newtrend
- SAR := max(high, EP[1])
- EP := min(low, low[1])
- else
- SAR := min(SAR, low[1])
- if not na(low[2])
- SAR := min(SAR, low[2])
- if SAR > low
- uptrend := false
- newtrend := true
- SAR := max(high, EP[1])
- EP := min(low, low[1])
- else
- uptrend := true
- newtrend := false
- else
- if newtrend
- SAR := min(low, EP[1])
- EP := max(high, high[1])
- else
- SAR := max(SAR, high[1])
- if not na(high[2])
- SAR := max(SAR, high[2])
- if SAR < high
- uptrend := true
- newtrend := true
- SAR := min(low, EP[1])
- EP := max(high, high[1])
- else
- uptrend := false
- newtrend := false
- x=close-SAR
- //
- sar=x>0? 3 : -3
- //
- totalpoints =sar+STT+Zig+Roc+DMI+ CCI+Weis+smapoint + aopoint + mompoint + histpoint + bbpoint + icpoint + hmapoint
- //
- disp_panels = input(true, title="Display info panels?")
- h=R1
- info_label_off1 = input(70, title="Info panel offset")
- info_label_size = input(size.normal, options=[size.tiny, size.small, size.normal, size.large, size.huge], title="Info panel label size")
- info_panel_x = timenow + round(change(time)*info_label_off1)
- info_panel_y = h
- info_title= "-=-=-=-=- Info Panel -=-=-=-=-"
- info_div = "\n\n------------------------------"
- info_current_close = "\n\Total points : " + tostring(totalpoints)
- info_text =info_title+info_current_close
- info_panel = disp_panels ? label.new(x=info_panel_x, y=info_panel_y, text=info_text, xloc=xloc.bar_time, yloc=yloc.price, color=color.yellow, style=label.style_labelup, textcolor=color.black, size=info_label_size) : na
- label.delete(info_panel[1])
- //
- //
- info_label_off = input(30, title="Info panel offset")
- posx = timenow + round(change(time)*info_label_off)
- posy = R1
- f_draw_infopanel(posx, posy, 6, " Strong Buy",totalpoints>7 ? color.green : color.black)
- f_draw_infopanel(posx, posy, 4, "Strong Sell", totalpoints < -7 ? color.red : color.black)
- f_draw_infopanel(posx, posy, 2, "Buy",totalpoints>=1 and totalpoints<=7? color.lime : color.black)
- f_draw_infopanel(posx, posy, 0, "Sell", totalpoints <= -1 and totalpoints>=-7? color.orange : color.black)
Add Comment
Please, Sign In to add comment