Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- strategy("RSI Exponential Smoothing (Expo)", overlay=true)
- // ~~ Inputs {
- import HeWhoMustNotBeNamed/mZigzag/12 as zg
- import HeWhoMustNotBeNamed/enhanced_ta/14 as eta
- import HeWhoMustNotBeNamed/arrays/1 as pa
- rsiperiod = input.int(14,minval=2, title="RSI Period", inline="RSI")
- ob = input.int(70,minval=50, maxval=100, title="Overbought", inline="obos")
- os = input.int(30,minval=0, maxval=50, title="Oversold", inline="obos")
- obosbands = input.bool(true, title="OBOS Band", inline="RSI")
- fibbands = input.bool(false, title="FIB Band", inline="RSI")
- labels = input.bool(true, title="RSI Labels", inline="RSI")
- // ~~ Line Colors {
- ema_col = input.color(color.rgb(0, 0, 255), title="EMA",inline="c")
- ema_ob_col = input.color(color.rgb(77, 255, 41), title="OB",inline="c")
- ema_os_col = input.color(color.rgb(255, 65, 47), title="OS",inline="c")
- ema_fib_col= input.color(color.blue, title="FIB",inline="c")
- //~~~}
- // ~~ Table Inputs {
- showTable = input.bool(true,title="Show Table", inline="tbl", group="Table")
- validity_check = input.bool(false,title="Show Validity check", inline="tbl", group="Table")
- TblSize = input.string(size.normal,title="",options=[size.auto,size.tiny,size.small,size.normal,size.large,size.huge],inline="tbl", group="Table")
- pos = input.string(position.top_right, title="",options =[position.top_right,position.top_center,
- position.top_left,position.bottom_right,position.bottom_center,position.bottom_left,position.middle_right,position.middle_left],inline="tbl", group="Table")
- textcolor = input.color(color.white, title="Text",inline="tbl_col", group="Table")
- bgcolor = input.color(color.new(color.blue,30), title="Bg",inline="tbl_col", group="Table")
- postrend = input.color(color.new(color.lime,0), title="Trend",inline="tbl_col", group="Table")
- negtrend = input.color(color.new(color.red,0), title="",inline="tbl_col", group="Table")
- ob_col = input.color(color.new(color.lime,0), title="OB/OS",inline="tbl_col", group="Table")
- os_col = input.color(color.new(color.red,0), title="",inline="tbl_col", group="Table")
- length = input.int(8, 'Length', group='Zigzag')
- oscillatorType = input.string("rsi", title="Oscillator", inline="osc", options=["cci", "cmo", "cog", "mfi", "roc", "rsi"], group='Oscillator')
- oscLength = input.int(14, title="", inline="osc", group='Oscillator')
- supertrendLength = input.int(5, 'History', inline='st', group='Supertrend')
- drawSupertrend = input.bool(false, "Draw Zigzag Supertrend", inline='st2', group='Supertrend')
- txtSize = input.string(size.tiny, '', [size.tiny, size.small, size.normal, size.large, size.huge], inline='txt', group = 'Stats and Display')
- txtColor = input.color(color.white, '', inline='txt', group = 'Stats and Display')
- showPivotLines = input.bool(true, 'Pivot Lines', inline='pli', group = 'Stats and Display')
- showPivotLabel = input.bool(false, 'Pivot Label', inline='pla', group = 'Stats and Display')
- //~~~}
- //~~~~~~}
- // ~~ EMA RSI Approx Calculation {
- // ~~ Exponential Smoothing Coefficient Calculation {
- exponential_period = 2 * rsiperiod - 1 // "exponential_period" is the length of the moving average, represented as "2 * rsiperiod - 1", where "rsiperiod" is a user-defined value.
- smoothing_coefficient = 2 / (exponential_period + 1) // "smoothing_coefficient" is a value used to calculate the EMA, represented as "2 / (exponential_period + 1)". This value determines the weight given to the current observation versus the previous average in the calculation.
- //~~~}
- // ~~ Exponential Smoothing RSI Calculation {
- // The code calculates two running averages, "averageUp" and "averageDown", using the exponential smoothing formula with the smoothing coefficient value defined in the previous code snippet.
- // The "netValue" variable is then calculated as the weighted difference between the "averageUp" and "averageDown" values, with a factor determined by the user-defined "rsiperiod" value.
- // The "result" variable is calculated based on the sign of "netValue". If "netValue" is positive, the result is equal to the current close price plus "netValue". If "netValue" is negative, the result is equal to the close price plus a fraction of "netValue" determined by the "level" parameter
- emaresult(level)=>
- averageUp = 0.0
- averageDown = 0.0
- averageUp := close > close[1] ? smoothing_coefficient* (close - close[1]) + (1 - smoothing_coefficient) * (averageUp[1] ? averageUp[1] : 1) : (1-smoothing_coefficient) * (averageUp[1] ? averageUp[1] : 1)
- averageDown := close > close[1] ? (1-smoothing_coefficient) * (averageDown[1] ? averageDown[1] : 1) : smoothing_coefficient * (close[1] - close) + (1 - smoothing_coefficient) * (averageDown[1] ? averageDown[1] : 1)
- netValue = (rsiperiod - 1) * (averageDown * level / (100 - level) - averageUp)
- result = netValue >= 0 ? close + netValue : close + netValue * (100 - level) / level
- //~~~}
- // ~~ Return EMA {
- ema_result = emaresult(50)
- // Plot the Optimal EMA based on the RSI value
- plot(ema_result, color=ema_col, title="Optimal EMA")
- //~~~}
- //~~~~~~}
- // ~~ Calculate RSI Standard Deviation, in order to calculate the Deviation {
- StandardDeviation(src, period)=>
- mean = ta.sma(src, period)
- squared_diff = ((src - mean) * (src - mean))
- sum_squared_diff = math.sum(squared_diff, period)
- std_dev = math.sqrt(sum_squared_diff/period)
- //~~~}
- // ~~ Return RSI Standard Deviation {
- rsi = ta.rsi(close,rsiperiod)
- std_dev = StandardDeviation(rsi, rsiperiod)
- //~~~}
- // ~~ Calculate Deviation {
- //It calculates the difference between the RSI value of (level) and the RSI mean value, then divides that difference by the standard deviation.
- //This number represents how many standard deviations away the RSI Level is from its mean.
- dev(obos)=>
- deviating = (obos-ema_result)/std_dev
- //~~~}
- // ~~ Hull Moving Average Calculation, in order to smooth the Upper OB and Lower OS lines {
- hma(src,len)=>
- wma1 = ta.wma(2 * ta.wma(src, len/2) - ta.wma(src, len), math.round(math.sqrt(0.5)))
- wma2 = ta.wma(2 * ta.wma(wma1, len/2) - ta.wma(wma1, len), math.round(math.sqrt(0.5)))
- //~~~}
- // ~~ Return EMA OB/OS Lines {
- ob_dev = dev(emaresult(ob)) // The deviation of the OB level from the mean. This number represents how many standard deviations away the OB Level is from its mean.
- os_dev = dev(emaresult(os)) // The deviation of the OS level from the mean. This number represents how many standard deviations away the OS Level is from its mean.
- upper = hma(ta.rma(ema_result + (ob_dev*std_dev),3),10) // Returns the upper OB line on the chart.
- lower = hma(ta.rma(ema_result + (os_dev*std_dev),3),10) // Returns the upper OS line on the chart.
- // ~~ Plot Upper OB and Lower OS lines on the chart {
- plot(obosbands?upper:na, "Upper OB", color=ema_ob_col)
- plot(obosbands?lower:na, "Lower OS", color=ema_os_col)
- plot(obosbands?upper - ta.stdev(upper,2):na, "Upper OB", color=ema_ob_col)
- plot(obosbands?lower + ta.stdev(lower,2):na, "Lower OS", color=ema_os_col)
- //~~~}
- //~~~~~~}
- // ~~ Calculate fib 50% from OB to 50 level and OS to 50 level {
- fib(src)=>
- fib = ema_result + (src-ema_result)*0.5
- // ~~ Plot the Fib lines on the chart {
- plot(fibbands?fib(upper):na, "Upper Fib 50%", color=ema_fib_col)
- plot(fibbands?fib(lower):na, "Lower Fib 50%", color=ema_fib_col)
- //~~~}
- //~~~~~~}
- // ~~ Validity Checks {
- // ~~ Store crossover values to check Validity {
- // ~~ Optimal EMA Length {
- emavalue = 2*rsiperiod-1
- ema = ta.ema(close,emavalue)
- //~~~}
- var occurrence = 0
- var non_occurrence = 0
- if (ta.crossover(rsi,50) and ta.crossover(close,ema)) or (ta.crossunder(rsi,50) and ta.crossunder(close,ema))
- occurrence += 1
- if (ta.crossover(rsi,50) and not ta.crossover(close,ema)) or (ta.crossunder(rsi,50) and not ta.crossunder(close,ema))
- non_occurrence += 1
- //~~~~~~}
- // ~~ Precentage Check {
- //The resulting value tells you how many occurrences there are relative to the total number of events, expressed as a percentage.
- percent = ((math.abs(occurrence)/(occurrence + non_occurrence) * 100))
- //~~~}
- // ~~ Inputs to Table {
- // ~~ Trend Direction {
- dir = rsi>50?true:false
- sign = dir == true?" > 50 ":" < 50"
- trend = dir == true?"Bullish":"Bearish"
- col = dir == true?postrend:negtrend
- //~~~}
- // ~~ Overbought & Oversold {
- ob_ = rsi>ob ?true:false
- os_ = rsi<os?true:false
- obos_sign = ob_? " > " + str.tostring(ob):os_? " < " + str.tostring(os):""
- obos_text = ob_? "Overbought":os_? "Oversold":""
- obos_col = ob_?ob_col:os_?os_col:color.white
- //~~~}
- //~~~~~~}
- // ~~ Table {
- tbl = table.new(pos, 2, 11, frame_color=chart.bg_color, frame_width=2, border_width=2, border_color=chart.bg_color)
- if barstate.islast and showTable
- tbl.cell(0, 0, text="RSI Value", text_color=textcolor, bgcolor=bgcolor, text_size=TblSize)
- tbl.cell(0, 1, text=str.tostring(rsiperiod), text_halign=text.align_center, bgcolor=bgcolor, text_color=textcolor, text_size=TblSize)
- tbl.cell(1, 0, text="EMA Value", text_color=textcolor, bgcolor=bgcolor, text_size=TblSize)
- tbl.cell(1, 1, text=str.tostring(emavalue), text_halign=text.align_center, bgcolor=bgcolor, text_color=textcolor, text_size=TblSize)
- tbl.cell(0, 2, text="RSI" + sign + "", text_color=textcolor, bgcolor=bgcolor, text_size=TblSize)
- tbl.cell(1, 2, text=trend, text_halign=text.align_center, bgcolor=col, text_color=textcolor, text_size=TblSize)
- if (ob_) or (os_)
- tbl.cell(0, 3, text="RSI" + obos_sign + "", text_color=textcolor, bgcolor=bgcolor, text_size=TblSize)
- tbl.cell(1, 3, text=obos_text, text_halign=text.align_center, bgcolor=obos_col, text_color=textcolor, text_size=TblSize)
- if validity_check
- tbl.cell(0, 4, text="Validity Check", text_color=textcolor, bgcolor=bgcolor, text_size=TblSize)
- tbl.cell(1, 4, text=str.tostring(percent,format.percent), text_halign=text.align_center, bgcolor=color.new(color.lime,30), text_color=textcolor, text_size=TblSize)
- tbl.cell(0, 5, text="RSI Stdev", text_color=textcolor, bgcolor=bgcolor, text_size=TblSize)
- tbl.cell(1, 5, text=str.tostring(math.round(std_dev)), text_halign=text.align_center, bgcolor=color.new(color.lime,30), text_color=textcolor, text_size=TblSize)
- // ~~ Labels {
- labelfunc(bool_,n, line_, text_, label_col, text_col, size_)=>
- label l = bool_?label.new(bar_index + n,line_,text=text_,color=label_col,
- style=label.style_label_left,textcolor=text_col,size=size_,
- textalign=text.align_left):na
- label.delete(l[1])
- labelfunc(labels,5, ema_result, "RSI 50" , color.blue, color.white, size.small)
- labelfunc(labels,5, upper, "RSI " + str.tostring(ob) , color.green, color.white, size.small)
- labelfunc(labels,5, lower, "RSI " + str.tostring(os) , color.red, color.white, size.small)
- labelfunc(fibbands and labels,1, fib(upper), "FIB 50 %" , color.blue, color.white, size.tiny)
- labelfunc(fibbands and labels,1, fib(lower), "FIB 50 %" , color.blue, color.white, size.tiny)
- //~~~}
- increment(mtx, row, col, val=1)=>matrix.set(mtx, row, col, matrix.get(mtx, row, col)+val)
- gettrendindex(int price, int osc, int trend)=>
- trendFactor = trend > 0 ? 0 : 1
- priceFactor = math.abs(price) > 1? 1 : 0
- oscFactor = math.abs(osc) > 1? 1 : 0
- trendFactor*4 + priceFactor*2 + oscFactor
- getSentimentDetails(pDir, oDir, sDir) =>
- sentiment = pDir == oDir ? sDir == pDir or sDir * 2 == -pDir ? -sDir : sDir * 4 : sDir == pDir or sDir == -oDir ? 0 : (math.abs(oDir) > math.abs(pDir) ? sDir : -sDir) * (sDir == oDir ? 2 : 3)
- sentimentSymbol = sentiment == 4 ? '⬆' : sentiment == -4 ? '⬇' : sentiment == 3 ? '↗' : sentiment == -3 ? '↘' : sentiment == 2 ? '⤴' : sentiment == -2 ? '⤵' : sentiment == 1 ? '⤒' : sentiment == -1 ? '⤓' : '▣'
- sentimentColor = sentiment == 4 ? color.green : sentiment == -4 ? color.red : sentiment == 3 ? color.lime : sentiment == -3 ? color.orange : sentiment == 2 ? color.rgb(202, 224, 13, 0) : sentiment == -2 ? color.rgb(250, 128, 114, 0) : color.silver
- sentimentLabel = math.abs(sentiment) == 4 ? 'C' : math.abs(sentiment) == 3 ? 'H' : math.abs(sentiment) == 2 ? 'D' : 'I'
- [sentimentSymbol, sentimentLabel, sentimentColor]
- getStatus(int trendIndex, int pivotDir)=>
- trendFactor = int(trendIndex/4)
- remainder = trendIndex % 4
- priceFactor = int(remainder/2)+1
- oscFactor = (remainder % 2)+1
- trendChar = (trendFactor == 0)? 'U' : 'D'
- priceChar = pivotDir > 0? (priceFactor == 2? 'HH' : 'LH') : (priceFactor == 2? 'LL' : 'HL')
- oscChar = pivotDir > 0? (oscFactor == 2? 'HH' : 'LH') : (oscFactor == 2? 'LL' : 'HL')
- trendChar + ' - ' + priceChar + '/'+oscChar
- draw_zg_line(idx1, idx2, zigzaglines, zigzaglabels, valueMatrix, directionMatrix, ratioMatrix, divergenceMatrix, doubleDivergenceMatrix,
- barArray, trendArray, lineColor, lineWidth, lineStyle) =>
- if matrix.rows(valueMatrix) > 2
- idxLen1 = matrix.rows(valueMatrix)-idx1
- idxLen2 = matrix.rows(valueMatrix)-idx2
- lastValues = matrix.row(valueMatrix, idxLen1)
- llastValues = matrix.row(valueMatrix, idxLen2)
- lastDirections = matrix.row(directionMatrix, idxLen1)
- lastRatios = matrix.row(ratioMatrix, idxLen1)
- lastDivergence = matrix.row(divergenceMatrix, idxLen1)
- lastDoubleDivergence = matrix.row(doubleDivergenceMatrix, idxLen1)
- y1 = array.get(lastValues, 0)
- y2 = array.get(llastValues, 0)
- x1 = array.get(barArray, idxLen1)
- x2 = array.get(barArray, idxLen2)
- zline = line.new(x1=x1, y1=y1, x2=x2, y2=y2, color=lineColor, width=lineWidth, style=lineStyle)
- currentDir = y1 > y2? 1 : -1
- priceDir = array.get(lastDirections, 0)
- oscDir = array.get(lastDirections, 1)
- trendDir = array.get(trendArray, idxLen1)
- trendIndex = gettrendindex(priceDir, oscDir, trendDir)
- trendLabel = getStatus(trendIndex, currentDir)
- [sentimentSymbol, sentimentLabel, sentimentColor] = getSentimentDetails(priceDir, oscDir, trendDir)
- labelStyle = currentDir > 0? label.style_label_down : label.style_label_up
- zlabel = showPivotLabel ? label.new(x=x1, y=y1, yloc=yloc.price, color=sentimentColor, style=labelStyle, text=sentimentSymbol + ' ' + trendLabel,
- textcolor=color.black, size = size.small, tooltip=sentimentLabel) : na
- if array.size(zigzaglines) > 0
- lastLine = array.get(zigzaglines, array.size(zigzaglines)-1)
- if line.get_x2(lastLine) == x2 and line.get_x1(lastLine) <= x1
- pa.pop(zigzaglines)
- pa.pop(zigzaglabels)
- pa.push(zigzaglines, zline, 500)
- pa.push(zigzaglabels, zlabel, 500)
- draw(matrix<float> valueMatrix, matrix<int> directionMatrix, matrix<float> ratioMatrix, matrix<int> divergenceMatrix, matrix<int> doubleDivergenceMatrix, array<int> barArray, array<int> trendArray,
- bool newZG, bool doubleZG, color lineColor = color.blue, int lineWidth = 1, string lineStyle = line.style_solid)=>
- var zigzaglines = array.new_line(0)
- var zigzaglabels = array.new_label(0)
- if(newZG)
- if doubleZG
- draw_zg_line(2, 3, zigzaglines, zigzaglabels, valueMatrix, directionMatrix, ratioMatrix, divergenceMatrix, doubleDivergenceMatrix, barArray, trendArray,
- lineColor, lineWidth, lineStyle)
- if matrix.rows(valueMatrix) >= 2
- draw_zg_line(1, 2, zigzaglines, zigzaglabels, valueMatrix, directionMatrix, ratioMatrix, divergenceMatrix, doubleDivergenceMatrix, barArray, trendArray,
- lineColor, lineWidth, lineStyle)
- [valueMatrix, directionMatrix, ratioMatrix, divergenceMatrix, doubleDivergenceMatrix, barArray, zigzaglines, zigzaglabels]
- indicatorHigh = array.new_float()
- indicatorLow = array.new_float()
- indicatorLabels = array.new_string()
- [oscHigh, _, _] = eta.oscillator(oscillatorType, oscLength, oscLength, oscLength, high)
- [oscLow, _, _] = eta.oscillator(oscillatorType, oscLength, oscLength, oscLength, low)
- array.push(indicatorHigh, math.round(oscHigh,2))
- array.push(indicatorLow, math.round(oscLow,2))
- array.push(indicatorLabels, oscillatorType+str.tostring(oscLength))
- [valueMatrix, directionMatrix, ratioMatrix, divergenceMatrix, doubleDivergenceMatrix, barArray, trendArray, supertrendDir, supertrend, newZG, doubleZG] =
- zg.calculate(length, array.from(high, low), indicatorHigh, indicatorLow, supertrendLength = supertrendLength)
- nextDirection = matrix.rows(directionMatrix) > 0? matrix.row(directionMatrix, matrix.rows(directionMatrix)-1) : array.new_int()
- lastDirection = matrix.rows(directionMatrix) > 1? matrix.row(directionMatrix, matrix.rows(directionMatrix)-2) : array.new_int()
- llastDirection = matrix.rows(directionMatrix) > 2? matrix.row(directionMatrix, matrix.rows(directionMatrix)-3) : array.new_int()
- lllastDirection = matrix.rows(directionMatrix) > 3? matrix.row(directionMatrix, matrix.rows(directionMatrix)-4) : array.new_int()
- var pivotHighStats = matrix.new<int>(64, 3, 0)
- var pivotLowStats = matrix.new<int>(64, 3, 0)
- var pivotHighRatios = matrix.new<float>(64, 3, 0)
- var pivotLowRatios = matrix.new<float>(64, 3, 0)
- var pivotHighBars = matrix.new<float>(64, 3, 0)
- var pivotLowBars = matrix.new<float>(64, 3, 0)
- currentTotalTrendIndex = 0
- nextTotalTrendIndex = 0
- currentDir = matrix.rows(directionMatrix) > 0? matrix.get(directionMatrix, matrix.rows(directionMatrix)-1, 0) : 0
- currentPDir = lastDirection.size() > 0 ? lastDirection.first() : na
- changePriceDirection = ta.change(math.sign(currentPDir))
- if(array.size(lllastDirection) > 0)
- priceDirection = array.get(lastDirection, 0)
- priceRatio = matrix.get(ratioMatrix, matrix.rows(ratioMatrix)-2, 0)
- numberOfBars = array.get(barArray, array.size(barArray)-2) - array.get(barArray, array.size(barArray)-3)
- currentPriceDirection = array.get(lastDirection, 0)
- currentOscDirection = array.get(lastDirection, 1)
- currentTrend = array.get(trendArray, array.size(trendArray)-2)
- nextPriceDirection = array.get(nextDirection, 0)
- nextOscDirection = array.get(nextDirection, 1)
- nextTrend = array.get(trendArray, array.size(trendArray)-1)
- lastPriceDirection = array.get(llastDirection, 0)
- lastOscDirection = array.get(llastDirection, 1)
- lastTrend = array.get(trendArray, array.size(trendArray)-3)
- llastPriceDirection = array.get(lllastDirection, 0)
- llastOscDirection = array.get(lllastDirection, 1)
- llastTrend = array.get(trendArray, array.size(trendArray)-4)
- colLast = math.abs(priceDirection) %2
- nextTrendIndex = gettrendindex(nextPriceDirection, nextOscDirection, nextTrend)
- currentTrendIndex = gettrendindex(currentPriceDirection, currentOscDirection, currentTrend)
- lastTrendIndex = gettrendindex(lastPriceDirection, lastOscDirection, lastTrend)
- llastTrendIndex = gettrendindex(llastPriceDirection, llastOscDirection, llastTrend)
- totalIndex = lastTrendIndex*8 + llastTrendIndex
- currentTotalTrendIndex := currentTrendIndex*8 + lastTrendIndex
- nextTotalTrendIndex := nextTrendIndex*8 + currentTrendIndex
- matrixToSet = math.sign(priceDirection) > 0? pivotHighStats : pivotLowStats
- ratioMatrixToSet = math.sign(priceDirection) > 0? pivotHighRatios : pivotLowRatios
- barMatrixToSet = math.sign(priceDirection) > 0? pivotHighBars : pivotLowBars
- if(not na(currentPDir) and changePriceDirection)
- increment(matrixToSet, totalIndex, colLast)
- increment(ratioMatrixToSet, totalIndex, colLast, priceRatio)
- increment(barMatrixToSet, totalIndex, colLast, numberOfBars)
- increment(matrixToSet, totalIndex, 2)
- increment(ratioMatrixToSet, totalIndex, 2, priceRatio)
- increment(barMatrixToSet, totalIndex, 2, numberOfBars)
- var float hhValue = na
- var float lhValue = na
- var float llValue = na
- var float hlValue = na
- var float hhProbability = na
- var float llProbability = na
- var float htRatio = na
- var float ltRatio = na
- if(array.size(barArray) > 10)
- currentBar = array.get(barArray, array.size(barArray)-1)
- lastBar = array.get(barArray, array.size(barArray)-2)
- currentPrice = matrix.get(valueMatrix, matrix.rows(valueMatrix)-1, 0)
- lastPrice = matrix.get(valueMatrix, matrix.rows(valueMatrix)-2, 0)
- llastPrice = matrix.get(valueMatrix, matrix.rows(valueMatrix)-3, 0)
- startRow = 2
- var statsTable = showTable ? table.new(position=position.top_right, columns=10, rows=64+startRow, border_color = color.black, border_width = 2) : na
- phSortIndices = array.sort_indices(matrix.col(pivotHighStats, 2), order.descending)
- phColStart = currentDir > 0 ? 0 : 5
- hlr = startRow
- for i=0 to 63
- si = array.get(phSortIndices, i)
- lastTrendIndex = int(si/8)
- llastTrendIndex = si%8
- lastPivotStatus = getStatus(lastTrendIndex, -1)
- llastPivotStatus = getStatus(llastTrendIndex, 1)
- hhStats = matrix.get(pivotHighStats, si, 0)
- lhStats = matrix.get(pivotHighStats, si, 1)
- tStats = matrix.get(pivotHighStats, si, 2)
- hhRatio = math.round(matrix.get(pivotHighRatios, si, 0)/hhStats, 3)
- lhRatio = math.round(matrix.get(pivotHighRatios, si, 1)/lhStats, 3)
- tRatio = math.round(matrix.get(pivotHighRatios, si, 2)/tStats, 3)
- hhBars = math.round(matrix.get(pivotHighBars, si, 0)/hhStats)
- lhBars = math.round(matrix.get(pivotHighBars, si, 1)/lhStats)
- tBars = math.round(matrix.get(pivotHighBars, si, 2)/tStats)
- highlight = math.sign(currentDir) < 0 ? nextTotalTrendIndex == si : currentTotalTrendIndex == si
- hhTooltip = 'Average Ratio - '+str.tostring(hhRatio)+'\n'+'Average Bars - '+str.tostring(hhBars)
- lhTooltip = 'Average Ratio - '+str.tostring(lhRatio)+'\n'+'Average Bars - '+str.tostring(lhBars)
- tTooltip = 'Average Ratio - '+str.tostring(tRatio)+'\n'+'Average Bars - '+str.tostring(tBars)
- if(highlight)
- var line hhLine = na
- var line lhLine = na
- var line tLine = na
- var label hhLabel = na
- var label lhLabel = na
- var label tLabel = na
- line.delete(hhLine)
- line.delete(lhLine)
- line.delete(tLine)
- label.delete(hhLabel)
- label.delete(lhLabel)
- label.delete(tLabel)
- x1 = math.sign(currentDir) < 0 ? currentBar : lastBar
- hhX2 = x1 + hhBars
- lhX2 = x1 + lhBars
- tX2 = x1 + tBars
- y1 = math.sign(currentDir) < 0 ? currentPrice : lastPrice
- prev = math.sign(currentDir) < 0 ? lastPrice : llastPrice
- hhY2 = math.round_to_mintick(y1 + math.abs(y1-prev)*hhRatio)
- lhY2 = math.round_to_mintick(y1 + math.abs(y1-prev)*lhRatio)
- tY2 = math.round_to_mintick(y1 + math.abs(y1-prev)*tRatio)
- hhLine := line.new(x1, y1, hhX2, hhY2, xloc=xloc.bar_index, color=color.green, style=line.style_arrow_right)
- lhLine := line.new(x1, y1, lhX2, lhY2, xloc=xloc.bar_index, color=color.lime, style=line.style_arrow_right)
- tLine := line.new(x1, y1, tX2, tY2, xloc=xloc.bar_index, color=color.yellow, style=line.style_arrow_right)
- hhPercent = str.tostring(hhStats*100/tStats, format.percent)
- lhPercent = str.tostring(lhStats*100/tStats, format.percent)
- hhText = 'Number of Historical References :'+str.tostring(hhStats)+'/'+str.tostring(tStats)+
- '\nProbability of Higher High :'+hhPercent+
- '\nAverage Higher High Ratio :'+str.tostring(hhRatio) +
- '\nAverage Higher High Bars :'+str.tostring(hhBars)
- lhText = 'Number of Historical References :'+str.tostring(lhStats)+'/'+str.tostring(tStats)+
- '\nProbability of Lower High :'+lhPercent+
- '\nAverage Lower High Ratio :'+str.tostring(lhRatio) +
- '\nAverage Lower High Bars :'+str.tostring(lhBars)
- tText = 'Number of Historical References :'+str.tostring(tStats)+
- '\nAverage Fib Ratio :'+str.tostring(tRatio)+
- '\nAverage Bars :'+str.tostring(tBars)
- hhLabel := label.new(hhX2, hhY2, str.tostring(hhY2)+ ' - ' +hhPercent, style=label.style_label_lower_left, color=color.new(color.green, 70), textcolor = color.white, size=size.small, tooltip=hhText)
- lhLabel := label.new(lhX2, lhY2, str.tostring(lhY2)+ ' - ' +lhPercent, style=label.style_label_upper_left, color=color.new(color.lime, 70), textcolor = color.white, size=size.small, tooltip=lhText)
- tLabel := label.new(tX2, tY2, str.tostring(tY2)+ '@'+str.tostring(tRatio), style=label.style_label_left, color=color.new(color.yellow, 70), textcolor = color.white, size=size.small, tooltip=tText)
- hhValue := hhY2
- lhValue := lhY2
- hhProbability := hhStats*100/tStats
- htRatio := tRatio
- plColStart = currentDir < 0 ? 0 : 5
- llr = startRow
- for i=0 to 63
- si = array.get(plSortIndices, i)
- lastTrendIndex = int(si/8)
- llastTrendIndex = si%8
- lastPivotStatus = getStatus(lastTrendIndex, 1)
- llastPivotStatus = getStatus(llastTrendIndex, -1)
- llStats = matrix.get(pivotLowStats, si, 0)
- hlStats = matrix.get(pivotLowStats, si, 1)
- tStats = matrix.get(pivotLowStats, si, 2)
- llRatio = math.round(matrix.get(pivotLowRatios, si, 0)/llStats, 3)
- hlRatio = math.round(matrix.get(pivotLowRatios, si, 1)/hlStats, 3)
- tRatio = math.round(matrix.get(pivotLowRatios, si, 2)/tStats, 3)
- llBars = math.round(matrix.get(pivotLowBars, si, 0)/llStats)
- hlBars = math.round(matrix.get(pivotLowBars, si, 1)/hlStats)
- tBars = math.round(matrix.get(pivotLowBars, si, 2)/tStats)
- highlight = math.sign(currentDir) > 0 ? nextTotalTrendIndex== si : currentTotalTrendIndex == si
- llTooltip = 'Average Ratio - '+str.tostring(llRatio)+'\n'+'Average Bars - '+str.tostring(llBars)
- hlTooltip = 'Average Ratio - '+str.tostring(hlRatio)+'\n'+'Average Bars - '+str.tostring(hlBars)
- tTooltip = 'Average Ratio - '+str.tostring(tRatio)+'\n'+'Average Bars - '+str.tostring(tBars)
- fill(hhp, lhp, color=color.new(hhProbability > 50 ? color.green : hhProbability < 50? color.red : color.silver, 90))
- fill(llp, hlp, color=color.new(llProbability > 50 ? color.red : llProbability < 50? color.green : color.silver, 90))
- longCondition = ta.crossover(hhProbability, 70) and ta.crossover(close, upper)
- if (longCondition)
- strategy.entry("Long", strategy.long)
- shortCondition = ta.crossover(llProbability, 70) and ta.crossunder(close, lower)
- if (shortCondition)
- strategy.entry("Short", strategy.short)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement