Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- // Author: ©K1ngAntzz
- // Disclaimer: I am not a financial advisor.
- // For purpose educate only. Use at your own risk.
- study("Yahweh's Limitless Resource", shorttitle="LionsofJudah", overlay=true)
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- len = input(9, minval=1, title="Length")
- src = input(close, title="Source")
- offset = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)
- out = ema(src, len)
- plot(out, title="9 EMA", color=color.yellow, offset=offset)
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- len := input(55, minval=1, title="Length")
- src := input(close, title="Source")
- offset := input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)
- out := ema(src, len)
- plot(out, title="55 EMA", color=color.green, offset=offset)
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- len := input(200, minval=1, title="Length")
- src := input(close, title="Source")
- offset := input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)
- out := sma(src, len)
- plot(out, color=color.red, title="200 MA", offset=offset)
- /////////////////////////////////////////////////////////////////////////////////////////
- //********** Functions
- funcDonchian(_len) =>
- avg(lowest(_len), highest(_len))
- funcEmaSma(_src, _period, _strategy) =>
- tresholdPositive = 50
- tresholdNegative = -50
- // EMA/SMA
- maLengthBuy_1_Default = 14
- maLengthBuy_2_Default = 3
- maLengthSell_1_Default = 8
- maLengthSell_2_Default = 1
- // setting 1
- maLengthBuy_1 = _period == "1" ? 6 : _period == "3" ? 5 : _period == "5" ? 5 : _period == "15" ? 42 : _period == "30" ? 42 : _period == "45" ? 16 : _period == "60" ? 20 : timeframe.isdaily ? 14 : timeframe.isweekly ? 14 : timeframe.ismonthly ? 5 : 14
- maLengthBuy_2 = _period == "1" ? 1 : _period == "3" ? 1 : _period == "5" ? 1 : _period == "15" ? 1 : _period == "30" ? 1 : _period == "45" ? 1 : _period == "60" ? 2 : timeframe.isdaily ? 5 : timeframe.isweekly ? 3 : timeframe.ismonthly ? 1 : 3
- maLengthSell_1 = _period == "1" ? 6 : _period == "3" ? 3 : _period == "5" ? 3 : _period == "15" ? 6 : _period == "30" ? 6 : _period == "45" ? 8 : _period == "60" ? 20 : timeframe.isdaily ? 12 : timeframe.isweekly ? 8 : timeframe.ismonthly ? 5 : 8
- maLengthSell_2 = _period == "1" ? 3 : _period == "3" ? 1 : _period == "5" ? 2 : _period == "15" ? 2 : _period == "30" ? 1 : _period == "45" ? 1 : _period == "60" ? 1 : timeframe.isdaily ? 1 : timeframe.isweekly ? 1 : timeframe.ismonthly ? 1 : 1
- // setting 2
- isTimeframeBased = _strategy == 'EMA/SMA TF based'
- maLengthBuyFinal_1 = isTimeframeBased ? maLengthBuy_1 : maLengthBuy_1_Default
- maLengthBuyFinal_2 = isTimeframeBased ? maLengthBuy_2 : maLengthBuy_2_Default
- maLengthSellFinal_1 = isTimeframeBased ? maLengthSell_1 : maLengthSell_1_Default
- maLengthSellFinal_2 = isTimeframeBased ? maLengthSell_2 : maLengthSell_2_Default
- // BUY
- esaBuy = ema(_src, maLengthBuyFinal_1)
- sourceEmaBuy = ema(abs(_src - esaBuy), maLengthBuyFinal_1)
- ciBuy = (_src - esaBuy) / (0.015 * sourceEmaBuy)
- trendEmaBuy = ema(ciBuy, maLengthBuyFinal_2)
- trendSmaBuy = sma(trendEmaBuy, 4)
- // SELL
- esaSell = ema(_src, maLengthSellFinal_1)
- sourceEmaSell = ema(abs(_src - esaSell), maLengthSellFinal_1)
- ciSell = (_src - esaSell) / (0.015 * sourceEmaSell)
- trendEmaSell = ema(ciSell, maLengthSellFinal_2)
- trendSmaSell = sma(trendEmaSell, 4)
- // prepare signals
- buyEmaSma = crossover(trendEmaBuy, trendSmaBuy) and trendEmaBuy <= tresholdNegative
- sellEmaSma = crossunder(trendEmaSell, trendSmaSell) and trendEmaSell >= tresholdPositive
- [buyEmaSma, sellEmaSma]
- // Percentage Price Oscillator
- funcPPO(_src) =>
- valPPOfast = 12
- valPPOslow = 26
- valPPOsmooth = 2
- valPPOfastMA = ema(_src, valPPOfast)
- valPPOslowMA = ema(_src, valPPOslow)
- valPPOmacd = valPPOfastMA - valPPOslowMA
- valPPOmacd2 = (valPPOmacd/valPPOslowMA)*100
- valPPOresult = sma(valPPOmacd2, valPPOsmooth)
- valPPOoscMin = valPPOresult > valPPOresult[1] and valPPOresult[1] < valPPOresult[2]
- valPPOoscMax = valPPOresult < valPPOresult[1] and valPPOresult[1] > valPPOresult[2]
- valPPOdivergenceBull = valPPOoscMin ? valPPOresult[1] : na
- valPPOdivergenceBear = valPPOoscMax ? valPPOresult[1]: na
- // prepare signals
- buyPPO = valPPOdivergenceBull != 0
- sellPPO = valPPOdivergenceBear != 0
- [buyPPO, sellPPO]
- // Kaufman's Adaptive Moving Average (KAMA)
- funcKAMA(_src) =>
- // settings
- valKamaFastLength = 12
- valKamaFastLengthKAMA = 2
- valKamaSlowLength = 26
- valKamaSlowLengthKAMA = 30
- valKamaSignalLength = 9
- // fast
- valKamaFastLag = (valKamaFastLength - 1) / 2
- valKamaFastData = _src + (_src - _src[valKamaFastLag])
- valKamaFastZeroLagEMA = ema(valKamaFastData, valKamaFastLength)
- valKamaFastEma = ema(_src, valKamaFastLength)
- valKamaFastVolatility = sum(abs(_src - _src[1]), valKamaFastLength)
- valKamaFastEfficiencyRatio = valKamaFastVolatility != 0 ? abs(_src - _src[valKamaFastLength]) / valKamaFastVolatility : 0
- valKamaFast = 0.0
- valKamaFast := nz(valKamaFast[1]) + pow(valKamaFastEfficiencyRatio * (2 / (valKamaFastLengthKAMA + 1) - 2 / (valKamaSlowLengthKAMA + 1)) + 2 / (valKamaSlowLengthKAMA + 1), 2) * (_src - nz(valKamaFast[1]))
- // slow
- valKamaSlowLag = (valKamaSlowLength - 1) / 2
- valKamaSlowData = _src + (_src - _src[valKamaSlowLag])
- valKamaSlowZeroLagEMA = ema(valKamaSlowData, valKamaSlowLength)
- valKamaSlowEma = ema(_src, valKamaSlowLength)
- valKamaSlowVolatility = sum(abs(_src - _src[1]), valKamaSlowLength)
- valKamaSlowEfficiencyRatio = valKamaSlowVolatility != 0 ? abs(_src - _src[valKamaSlowLength]) / valKamaSlowVolatility : 0
- valKamaSlow = 0.0
- valKamaSlow := nz(valKamaSlow[1]) + pow(valKamaSlowEfficiencyRatio * (2 / (valKamaFastLengthKAMA + 1) - 2 / (valKamaSlowLengthKAMA + 1)) + 2 / (valKamaSlowLengthKAMA + 1), 2) * (_src - nz(valKamaSlow[1]))
- // Moving Average Convergence Divergence (MACD)
- valKamaMacd1 = valKamaFastZeroLagEMA - valKamaSlowZeroLagEMA
- valKamaMacd2 = valKamaFastEma - valKamaSlowEma
- valKamaMacd3 = valKamaFast - valKamaSlow
- valKamaMacdAverage = (valKamaMacd1 + valKamaMacd2 + valKamaMacd3) / 3
- // prepare signals
- valKamaSignal = ema(valKamaMacdAverage, valKamaSignalLength)
- //valKamaSignal = ema(valKamaMacdAverage + (valKamaMacdAverage - valKamaMacdAverage[(valKamaSignalLength - 1) / 2]), valKamaSignalLength)
- // prepare signals
- buyKama = crossover(valKamaMacdAverage, valKamaSignal)
- sellKama = crossunder(valKamaMacdAverage, valKamaSignal)
- [buyKama, sellKama]
- funcIchimoku(_period, _src) =>
- conversionLinePeriod = _period == "Ichimoku_9-26-52" ? 9 : _period == "Ichimoku_10-30-60" ? 10 : _period == "Ichimoku_20-60-120" ? 20 : 9
- baseLinePeriod = _period == "Ichimoku_9-26-52" ? 26 : _period == "Ichimoku_10-30-60" ? 30 : _period == "Ichimoku_20-60-120" ? 60 : 26
- laggingSpanPeriod = _period == "Ichimoku_9-26-52" ? 52 : _period == "Ichimoku_10-30-60" ? 60 : _period == "Ichimoku_20-60-120" ? 120 : 52
- // calc
- tenkanSenConversionLine = funcDonchian(conversionLinePeriod)
- kijunSenBaseLine = funcDonchian(baseLinePeriod)
- leadingSpanA = avg(tenkanSenConversionLine, kijunSenBaseLine)
- leadingSpanB = funcDonchian(laggingSpanPeriod)
- // RSI Settings
- ichiRsiLength = 14
- ichiRsiOverBought = 50
- ichiRsiOverSold = 100
- ichiRsiDefault = rsi(_src, ichiRsiLength)
- // lines, span
- tenkanSenLine = tenkanSenConversionLine[baseLinePeriod]
- kijunSenLine = kijunSenBaseLine[baseLinePeriod]
- senkouSpanA = leadingSpanA[baseLinePeriod * 2]
- senkouSpanB = leadingSpanB[baseLinePeriod * 2]
- chikuSpan = _src[0]
- // smallest/greatest of multiple given values
- ichiMin = min(senkouSpanA, senkouSpanB)
- ichiMax = max(senkouSpanA, senkouSpanB)
- // up/down
- upTrend = chikuSpan > ichiMax
- downTrend = chikuSpan < ichiMin
- // bull/bear
- bull = crossover(tenkanSenLine, kijunSenLine)
- bear = crossunder(tenkanSenLine, kijunSenLine)
- // prepare signals
- longIchi = bull and upTrend and ichiRsiDefault <= ichiRsiOverSold
- shortIchi = bear and downTrend and ichiRsiDefault >= ichiRsiOverBought
- [longIchi, shortIchi]
- funcDirectionalMovement(_len) =>
- _up = change(high)
- _down = -change(low)
- _plusDM = na(_up) ? na : (_up > _down and _up > 0 ? _up : 0)
- _minusDM = na(_down) ? na : (_down > _up and _down > 0 ? _down : 0)
- _trueRange = rma(tr, _len)
- _plus = fixnan(100 * rma(_plusDM, _len) / _trueRange)
- _minus = fixnan(100 * rma(_minusDM, _len) / _trueRange)
- [_plus, _minus]
- funcAdx(valAdxDirectionalIndexLength, _len) =>
- [_plus, _minus] = funcDirectionalMovement(valAdxDirectionalIndexLength)
- sum = _plus + _minus
- funcAdx = 100 * rma(abs(_plus - _minus) / (sum == 0 ? 1 : sum), _len)
- funcAdxLowHigh(_len, _type) =>
- [_plus, _minus] = funcDirectionalMovement(_len)
- return = _type == 'high' ? _plus : _minus
- funcHullMovingAverage(_src, _len) =>
- tmpVal = max(2, _len)
- return = wma((2 * wma(_src, tmpVal/2)) - wma(_src, tmpVal), round(sqrt(tmpVal)))
- funcHullMovingAverage3(_src, _len) =>
- p = _len/2
- wma(wma(hlc3, p/3) * 3 - wma(hlc3, p/2) - wma(hlc3, p), p)
- funcHullTrend(_src, _period) =>
- valHullTrendLength = _period == "1" ? 30 : _period == "3" ? 29 : _period == "5" ? 28 : _period == "15" ? 27 : _period == "30" ? 26 : _period == "45" ? 24 : _period == "60" ? 24 : timeframe.isdaily ? 24 : timeframe.isweekly ? 24 : timeframe.ismonthly ? 24 : 24
- boolHullTrendA = funcHullMovingAverage(_src, valHullTrendLength)
- boolHullTrendB = funcHullMovingAverage3(_src, valHullTrendLength)
- buyHullTrend = boolHullTrendB > boolHullTrendA and boolHullTrendB[1] < boolHullTrendA[1]
- sellHullTrend = boolHullTrendA > boolHullTrendB and boolHullTrendA[1] < boolHullTrendB[1]
- [buyHullTrend, sellHullTrend]
- funcFractalCalculated(_type, _variant) =>
- _regular = _variant == 1 ? high[4] < high[3] and high[3] < high[2] and high[2] > high[1] and high[1] > high[0] : _variant == -1 ? low[4] > low[3] and low[3] > low[2] and low[2] < low[1] and low[1] < low[0] : false
- _billWilliams = _variant == 1 ? high[4] < high[2] and high[3] < high[2] and high[2] > high[1] and high[2] > high[0] : _variant == -1 ? low[4] > low[2] and low[3] > low[2] and low[2] < low[1] and low[2] < low[0] : false
- return = _type == 'Fractal Regular' ? _regular : _billWilliams
- //********** Inputs
- inputSource = input(title="Calculation Source", defval=hlc3) // The HLC stands for High, Low, Close and the /3 divides the result by 3 to give you the average.
- inputStrategy = input(title="Strategy", type=input.string, defval="EMA/SMA", options=["EMA/SMA", "EMA/SMA TF based", "Fractal Regular", "Fractal Bill Williams", "Percentage Price Oscillator", "Hull Trend", "Kaufman Adaptive Moving Average"])
- inputStrategyLongShort = input(title="Strategy - Long/Short", type=input.string, defval="Ichimoku_9-26-52", options=["DISABLED", "ADX", "Ichimoku_9-26-52", "Ichimoku_10-30-60", "Ichimoku_20-60-120"])
- inputStyleLabelSize = input(title="Label - Size", type=input.string, defval="small", options=["auto", "huge", "large", "normal", "small", "tiny"])
- inputStyleLabelType = input(title="Label - Type", type=input.string, defval="label", options=["label", "triangle"])
- inputStyleLabelPosition = input(title="Label - Position", type=input.string, defval="default", options=["default", "lowhigh"])
- inputShowPriceOnLabel = input(title="Show Price on Labels", type=input.bool, defval=true)
- inputShowBuyLabel = input(title="Show BUY Signal", type=input.bool, defval=true)
- inputShowSellLabel = input(title="Show SELL Signal", type=input.bool, defval=true)
- inputActivateAlerts = input(title="Activate Alerts", type=input.bool, defval=false)
- //********** globals
- valCurrentTimeFramePeriod = timeframe.period
- valPrice = close <= 0.99999 ? tostring(close, '#.####') : close <= 99.99 ? tostring(close, '#.###') : close <= 999.99 ? tostring(close, '#.##') : tostring(close, '#')
- valXPosTimeBased = time[1] //offset -1
- valLabelTextBuy = 'BUY' + (inputShowPriceOnLabel == true ? ': ' + valPrice : '')
- valLabelTextSell = 'SELL' + (inputShowPriceOnLabel == true ? ': ' + valPrice : '')
- //********** Average Directional Index (ADX)
- valAdxLength = 13
- valAdxDirectionalIndexLength = 14
- valAdxAverageTrueRange = 20
- valAdxHigherLowTrend = 40
- boolAdxDirectionalIndexHigh = funcAdxLowHigh(valAdxDirectionalIndexLength, 'high')
- boolAdxDirectionalIndexLow = funcAdxLowHigh(valAdxDirectionalIndexLength, 'low')
- boolAdxDirectionalIndexCross = cross(boolAdxDirectionalIndexHigh, boolAdxDirectionalIndexLow)
- funcAdxUp() => funcAdx(valAdxDirectionalIndexLength, valAdxLength) >= valAdxAverageTrueRange
- funcAdxDirectionalIndexUp() => boolAdxDirectionalIndexHigh >= boolAdxDirectionalIndexLow
- funcAdxDirectionalIndexUps() => boolAdxDirectionalIndexHigh >= valAdxHigherLowTrend
- funcAdxDirectionalIndexDown() => boolAdxDirectionalIndexLow > boolAdxDirectionalIndexHigh
- funcAdxDirectionalIndexDowns() => boolAdxDirectionalIndexLow > valAdxHigherLowTrend
- funcAdxFinal() =>
- // bool
- boolAdxBuyCondition = funcAdxUp() and funcAdxDirectionalIndexUp() and funcAdxDirectionalIndexUps()
- boolAdxBuyCloseCondition = boolAdxDirectionalIndexCross and funcAdxDirectionalIndexUp()[1]
- boolAdxSellCondition = funcAdxUp() and funcAdxDirectionalIndexDown() and funcAdxDirectionalIndexDowns()
- boolAdxSellCloseCondition = boolAdxDirectionalIndexCross and funcAdxDirectionalIndexDown()[1]
- // counter
- adxCounter = 0
- adxCounter :=
- boolAdxBuyCloseCondition ? 0 :
- boolAdxSellCloseCondition ? 0 :
- boolAdxBuyCondition ? 1 :
- boolAdxSellCondition ? -1:
- nz(adxCounter[1])
- // prepare signals
- longAdx = adxCounter == 1 and adxCounter[1] == 0
- shortAdx = adxCounter == -1 and adxCounter[1] == 0
- [longAdx, shortAdx]
- //********** Styling Labels
- valFinalLabelSize = (inputStyleLabelSize == "auto") ? size.auto : (inputStyleLabelSize == "huge") ? size.huge : (inputStyleLabelSize == "large") ? size.large : (inputStyleLabelSize == "normal") ? size.normal : (inputStyleLabelSize == "small") ? size.small: (inputStyleLabelSize == "tiny") ? size.tiny : size.normal
- valFinalLabelStyleUp = (inputStyleLabelType == "label") ? label.style_labelup : (inputStyleLabelType == "triangle") ? label.style_triangleup : label.style_labelup
- valFinalLabelStyleDown = (inputStyleLabelType == "label") ? label.style_labeldown : (inputStyleLabelType == "triangle") ? label.style_triangledown : label.style_labeldown
- //********** Set Signals - Buy/Sell
- [buyEmaSma, sellEmaSma] = funcEmaSma(inputSource, valCurrentTimeFramePeriod, inputStrategy)
- signalFinalBuy = buyEmaSma
- signalFinalSell = sellEmaSma
- if (inputStrategy == 'Kaufman Adaptive Moving Average')
- [buyKama, sellKama] = funcKAMA(inputSource)
- signalFinalBuy := buyKama
- signalFinalSell := sellKama
- else if (inputStrategy == 'Percentage Price Oscillator')
- [buyPPO, sellPPO] = funcPPO(inputSource)
- signalFinalBuy := buyPPO
- signalFinalSell := sellPPO
- else if (inputStrategy == 'Hull Trend')
- [buyHullTrend, sellHullTrend] = funcHullTrend(inputSource, valCurrentTimeFramePeriod)
- signalFinalBuy := buyHullTrend
- signalFinalSell := sellHullTrend
- else if (inputStrategy == 'Fractal Regular' or inputStrategy == 'Fractal Bill Williams')
- signalBuyFractal = funcFractalCalculated(inputStrategy, -1)
- signalSellFractal = funcFractalCalculated(inputStrategy, 1)
- signalFinalBuy := signalBuyFractal
- signalFinalSell := signalSellFractal
- //********** Set Signals - Long/Short
- signalFinalLong = false
- signalFinalShort = false
- if (inputStrategyLongShort == 'Ichimoku_9-26-52' or inputStrategyLongShort == 'Ichimoku_10-30-60' or inputStrategyLongShort == 'Ichimoku_20-60-120')
- [signalLongIchi, signalShortIchi] = funcIchimoku(inputStrategyLongShort, inputSource)
- signalFinalLong := signalLongIchi
- signalFinalShort := signalShortIchi
- else if (inputStrategyLongShort == 'ADX')
- [longAdx, shortAdx] = funcAdxFinal()
- signalFinalLong := longAdx
- signalFinalShort := shortAdx
- //************ Plotting
- // BUY
- label.new(
- x = signalFinalBuy and inputShowBuyLabel == true ? valXPosTimeBased : na,
- y = inputStyleLabelPosition == 'default' ? na : low,
- xloc = xloc.bar_time,
- yloc = inputStyleLabelPosition == 'default' ? yloc.belowbar : yloc.price,
- text = valLabelTextBuy,
- color = color.green,
- textcolor = color.white,
- style = valFinalLabelStyleUp,
- size = valFinalLabelSize)
- // SELL
- label.new(
- x = signalFinalSell and inputShowSellLabel == true ? valXPosTimeBased : na,
- y = inputStyleLabelPosition == 'default' ? na : high,
- xloc = xloc.bar_time,
- yloc = inputStyleLabelPosition == 'default' ? yloc.abovebar : yloc.price,
- text = valLabelTextSell,
- color = color.red,
- textcolor = color.white,
- style = valFinalLabelStyleDown,
- size = valFinalLabelSize)
- // LONG
- label.new(
- x = signalFinalLong ? valXPosTimeBased : na,
- y = low,
- xloc = xloc.bar_time,
- yloc = yloc.price,
- text = 'LONG',
- color = color.blue,
- textcolor = color.white,
- style = valFinalLabelStyleUp,
- size = size.tiny)
- // SHORT
- label.new(
- x = signalFinalShort ? valXPosTimeBased : na,
- y = high,
- xloc = xloc.bar_time,
- yloc = yloc.price,
- text = 'SHORT',
- color = color.fuchsia,
- textcolor = color.white,
- style = valFinalLabelStyleDown,
- size = size.tiny)
- //************ Alerting
- // BUY
- alertcondition(inputActivateAlerts and signalFinalBuy, title='BUY', message='BUY')
- // SELL
- alertcondition(inputActivateAlerts and signalFinalSell, title='SELL', message='SELL')
- // LONG
- alertcondition(inputActivateAlerts and signalFinalLong, title='LONG', message='LONG')
- // SHORT
- alertcondition(inputActivateAlerts and signalFinalShort, title='SHORT', message='SHORT')
- ////////////////////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 200, defval = 11)
- sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=true)
- lin_reg = input(title="Lin Reg", type=input.bool, defval=true)
- linreg_length = input(title="Linear Regression Length", type=input.integer, minval = 1, maxval = 200, defval = 11)
- bopen = lin_reg ? linreg(open, linreg_length, 0) : open
- bhigh = lin_reg ? linreg(high, linreg_length, 0) : high
- blow = lin_reg ? linreg(low, linreg_length, 0) : low
- bclose = lin_reg ? linreg(close, linreg_length, 0) : close
- r = bopen < bclose
- signal = sma_signal ? sma(bclose, signal_length) : ema(bclose, signal_length)
- 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(signal, color=color.white)
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //###### FUNCTIONS ##################################//
- truncate(number, decimals) =>
- factor = pow(10, decimals)
- int(number * factor) / factor
- nround(x) =>
- n = round(x / syminfo.mintick) * syminfo.mintick
- chosenColor(c_)=> c_ == "aqua" ? #00FFFFff :
- c_ == "blue" ? #0040FFff :
- c_ == "fuchsia" ? #FF00FFff :
- c_ == "gray" ? #808080ff :
- c_ == "green" ? #008000ff :
- c_ == "lime" ? #00FF00ff :
- c_ == "maroon" ? #800000ff :
- c_ == "navy" ? #000099ff :
- c_ == "olive" ? #808000ff :
- c_ == "orange" ? #FF8000ff :
- c_ == "purple" ? #8000FFff :
- c_ == "red" ? #ff0000ff :
- c_ == "silver" ? #C0C0C0ff :
- c_ == "teal" ? #008080ff :
- c_ == "white" ? #FFFFFFff : #00000000
- //###### DRAW TREND CHANNEL#########################//
- period = input( 40, "Period" , input.integer, minval=3)
- deviations = input( 2.0, "Deviation(s)" , input.float , minval=0.1, step=0.1)
- extendType = input("Right", "Extend Method", input.string , options=["Right","None"])=="None" ? extend.none : extend.right
- 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
- //####### DRAWING LINES SECTION ####
- var line upperChannelLine = na ,
- var line medianChannelLine = na ,
- var line lowerChannelLine = na
- line.delete(upperChannelLine[1]),
- line.delete(medianChannelLine[1]),
- line.delete(lowerChannelLine[1])
- // added color inputs
- upperLineColor = input(title="Upper Channel Color", defval="red", options=["aqua", "black", "blue", "fuchsia", "gray", "green", "lime", "maroon", "navy", "olive", "orange", "purple", "red", "silver", "teal", "white", "yellow"])
- middleLineColor = input(title="Middle Channel Color", defval="orange", options=["aqua", "black", "blue", "fuchsia", "gray", "green", "lime", "maroon", "navy", "olive", "orange", "purple", "red", "silver", "teal", "white", "yellow"])
- lowerChannelColor = input(title="Lower Channel Color", defval="green", options=["aqua", "black", "blue", "fuchsia", "gray", "green", "lime", "maroon", "navy", "olive", "orange", "purple", "red", "silver", "teal", "white", "yellow"])
- // draw channels
- upperChannelLine := line.new(bar_index - period + 1, startingPointY + deviation, bar_index, linearRegression + deviation, xloc.bar_index, extendType, chosenColor(upperLineColor) , line.style_solid , 2)
- medianChannelLine := line.new(bar_index - period + 1, startingPointY , bar_index, linearRegression , xloc.bar_index, extendType, chosenColor(middleLineColor), line.style_solid , 1)
- lowerChannelLine := line.new(bar_index - period + 1, startingPointY - deviation, bar_index, linearRegression - deviation, xloc.bar_index, extendType, chosenColor(lowerChannelColor), line.style_solid , 2)
- //####### INFORMATION PANEL SECTION ####
- // Idea is developed from https://www.tradingview.com/script/gcp7bgN4-Linear-low-high/
- // by https://www.tradingview.com/u/RafaelZioni/
- disp_panels = input(true, title="Display info panels?")
- linear_label_off = input(-1, title="linear label offset", minval = -1)
- linear_label_size = input(size.normal, options=[size.tiny, size.small, size.normal, size.large, size.huge], title="linear label size")
- pos_x = timenow + round(change(time)*linear_label_off)
- pos_y_upper = nround(linearRegression + deviation)
- candle_look_back = input(defval = 3, title="Look back", minval = 1, maxval = 5, step=1)
- pos_y_upper_previous = nround(linearRegression[candle_look_back] + deviation[candle_look_back])
- // pos_y_middle = nround(linearRegression) // doesn't need midde line
- pos_y_lower = truncate(nround(linearRegression - deviation), 5)
- pos_y_lower_previous = nround(linearRegression[candle_look_back] - deviation[candle_look_back])
- label_upper = "Current Sell Entry: " + tostring(pos_y_upper) + "\n Previous Sell Entry (using Look Back): " + tostring(pos_y_upper_previous)
- // label_upper = "Entry High: " + tostring(pos_y_upper)
- label_lower = "Current Buy Entry: " + tostring(pos_y_lower) + "\n Previous Buy Entry (using Look Back): " + tostring(pos_y_lower_previous)
- draw_upper_label = disp_panels ? label.new(x=pos_x, y=pos_y_upper, text=label_upper, xloc=xloc.bar_time, yloc=yloc.price, color=color.red, style=label.style_labeldown, textcolor=color.black, size=linear_label_size) : na
- draw_lower_label = disp_panels ? label.new(x=pos_x, y=pos_y_lower, text=label_lower, xloc=xloc.bar_time, yloc=yloc.price, color=color.blue, style=label.style_labelup, textcolor=color.black, size=linear_label_size) : na
- label.delete(draw_upper_label[1])
- label.delete(draw_lower_label[1])
- // to memorize entries
- plot(pos_y_upper, title="Upper Entry Line", color=color.fuchsia, transp = 80)
- plot(pos_y_lower, title="Lower Entry Line", color=color.fuchsia, transp = 80)
- //####### ALERT PANEL SECTION ####
- ready_to_sell = crossover(close, pos_y_upper_previous)
- ready_to_buy = crossunder(close, pos_y_lower_previous)
- alertcondition(ready_to_sell, title='Sell Alert v0.2', message='Sell at {{plot("Upper Entry Line")}}')
- alertcondition(ready_to_buy, title='Buy Alert v0.2', message='Buy at {{plot("Lower Entry Line")}}')
- alertcondition(ready_to_buy or ready_to_sell, title='General Alert v0.2', message='Price is close to upper/lower channel.')
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- GiaLonNhat7to15 = highest(close[7], 8)
- GiaLonNhat15 = highest(close[1], 15)
- DK1 = GiaLonNhat7to15 >= GiaLonNhat15
- DK2 = close > GiaLonNhat7to15
- KhoiLuongLonNhat15 = highest(volume[1], 15)
- DK3 = volume > KhoiLuongLonNhat15
- GiaBeNhat15 = lowest(close[1], 15)
- DK4 = GiaBeNhat15 <= close * 0.95 and GiaBeNhat15 >= close * 0.9
- GiaBeNhatThang = lowest(close[15], 30)
- DK5 = GiaBeNhatThang <= close * 0.9 and GiaBeNhatThang >= close * 0.8
- DK6 = volume > 100000
- Buy = DK1 and DK2 and DK3 and DK4 and DK5 and DK6
- plotshape(Buy, title="Buy", style=shape.triangleup, location=location.belowbar, color=#0000FF, size=size.small,text="BUY",textcolor=color.black)
- barcolor(Buy ? #0000FF : na)
- alertcondition(Buy, title="Buy Signal", message="Buy")
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- conversionPeriods = input(9, minval=1, title="Conversion Line Periods"),
- basePeriods = input(26, minval=1, title="Base Line Periods")
- laggingSpan2Periods = input(52, minval=1, title="Lagging Span 2 Periods"),
- displacement = input(26, minval=1, title="Displacement")
- donchian(len) => avg(lowest(len), highest(len))
- conversionLine = donchian(conversionPeriods)
- baseLine = donchian(basePeriods)
- spanA = avg(conversionLine, baseLine)
- spanB = donchian(laggingSpan2Periods)
- plot(conversionLine, color=#0496ff, title="Conversion Line")
- plot(baseLine, color=#991515, title="Base Line")
- plot(close, offset = -displacement + 1, color=#459915, title="Lagging Span")
- p1 = plot(spanA, offset = displacement - 1, color=color.green,
- title="Span A")
- p2 = plot(spanB, offset = displacement - 1, color=color.red,
- title="Span B")
- fill(p1, p2, color = spanA > spanB ? color.green : color.red)
- plot(spanA[25], color=color.orange)
- plot(spanB[25], color=color.yellow)
- priceincloud = (close>spanA[25] and close<spanB[25]) or (close<spanA[25] and close>spanB[25])
- //TRADING RULES
- //Conversion Line and Baseline crossover with Laggin Span Filter
- ConvBaseBuy = crossover(conversionLine, baseLine)
- ConvBaseSell = crossunder(conversionLine, baseLine)
- //Laggin Span Filter
- // cblagBuy = ConvBaseBuy==true and (close > high[25])
- // cblagSell = ConvBaseSell == true and (close < low[25])
- //test plot for cb filter
- // plotshape(series=cblagBuy, text = "LS Buy", title="Lagging Span Buy", location=location.belowbar, style=shape.triangleup, color=color.lime, size=size.small)
- // plotshape(series=cblagSell, text="LS Sell", title="Laggin Span Sell", location=location.abovebar, style=shape.triangledown, color=color.red, size=size.small)
- //Conversion Line and Baseline crossover with Kumo Cloud Filter
- //Kumo Cloud Rule
- idealbuy = (close>high[25]) and conversionLine>baseLine and (close>spanA[25] and close>spanB[25]) and (low>spanA[25] and low>spanB[25])
- idealsell = (close<low[25]) and conversionLine<baseLine and (close<spanB[25] and close<spanB[25]) and (high<spanA[25] and high<spanB[25])
- buymem = false
- sellmem = false
- buymem := idealbuy?true:idealsell?false:buymem[1]
- sellmem := idealsell?true:idealbuy?false:sellmem[1]
- longCond = idealbuy and not(buymem[1])
- shortCond = idealsell and not(sellmem[1])
- plotshape(series=longCond, text = "Ichi Buy", title="Ichi Buy", location=location.belowbar, style=shape.triangleup, color=color.lime, size=size.small)
- plotshape(series=shortCond, text="Ichi Sell", title="Ichi Sell", location=location.abovebar, style=shape.triangledown, color=color.red, size=size.small)
- alertcondition(longCond, title="Ichi Buy", message="Buy it like a snail")
- alertcondition(shortCond, title="Ichi Sell", message="Sell it like a snail")
Advertisement
Add Comment
Please, Sign In to add comment