xmd79

Yahweh's Limitless Resource

Oct 6th, 2021
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.28 KB | None | 0 0
  1. //@version=4
  2. // Author: ©K1ngAntzz
  3. // Disclaimer: I am not a financial advisor.
  4. // For purpose educate only. Use at your own risk.
  5. study("Yahweh's Limitless Resource", shorttitle="LionsofJudah", overlay=true)
  6.  
  7. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8. len = input(9, minval=1, title="Length")
  9. src = input(close, title="Source")
  10. offset = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)
  11. out = ema(src, len)
  12. plot(out, title="9 EMA", color=color.yellow, offset=offset)
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  14. len := input(55, minval=1, title="Length")
  15. src := input(close, title="Source")
  16. offset := input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)
  17. out := ema(src, len)
  18. plot(out, title="55 EMA", color=color.green, offset=offset)
  19. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  20. len := input(200, minval=1, title="Length")
  21. src := input(close, title="Source")
  22. offset := input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)
  23. out := sma(src, len)
  24. plot(out, color=color.red, title="200 MA", offset=offset)
  25. /////////////////////////////////////////////////////////////////////////////////////////
  26. //********** Functions
  27. funcDonchian(_len) =>
  28. avg(lowest(_len), highest(_len))
  29.  
  30. funcEmaSma(_src, _period, _strategy) =>
  31. tresholdPositive = 50
  32. tresholdNegative = -50
  33. // EMA/SMA
  34. maLengthBuy_1_Default = 14
  35. maLengthBuy_2_Default = 3
  36. maLengthSell_1_Default = 8
  37. maLengthSell_2_Default = 1
  38. // setting 1
  39. 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
  40. 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
  41. 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
  42. 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
  43. // setting 2
  44. isTimeframeBased = _strategy == 'EMA/SMA TF based'
  45. maLengthBuyFinal_1 = isTimeframeBased ? maLengthBuy_1 : maLengthBuy_1_Default
  46. maLengthBuyFinal_2 = isTimeframeBased ? maLengthBuy_2 : maLengthBuy_2_Default
  47. maLengthSellFinal_1 = isTimeframeBased ? maLengthSell_1 : maLengthSell_1_Default
  48. maLengthSellFinal_2 = isTimeframeBased ? maLengthSell_2 : maLengthSell_2_Default
  49. // BUY
  50. esaBuy = ema(_src, maLengthBuyFinal_1)
  51. sourceEmaBuy = ema(abs(_src - esaBuy), maLengthBuyFinal_1)
  52. ciBuy = (_src - esaBuy) / (0.015 * sourceEmaBuy)
  53. trendEmaBuy = ema(ciBuy, maLengthBuyFinal_2)
  54. trendSmaBuy = sma(trendEmaBuy, 4)
  55. // SELL
  56. esaSell = ema(_src, maLengthSellFinal_1)
  57. sourceEmaSell = ema(abs(_src - esaSell), maLengthSellFinal_1)
  58. ciSell = (_src - esaSell) / (0.015 * sourceEmaSell)
  59. trendEmaSell = ema(ciSell, maLengthSellFinal_2)
  60. trendSmaSell = sma(trendEmaSell, 4)
  61. // prepare signals
  62. buyEmaSma = crossover(trendEmaBuy, trendSmaBuy) and trendEmaBuy <= tresholdNegative
  63. sellEmaSma = crossunder(trendEmaSell, trendSmaSell) and trendEmaSell >= tresholdPositive
  64. [buyEmaSma, sellEmaSma]
  65.  
  66. // Percentage Price Oscillator
  67. funcPPO(_src) =>
  68. valPPOfast = 12
  69. valPPOslow = 26
  70. valPPOsmooth = 2
  71. valPPOfastMA = ema(_src, valPPOfast)
  72. valPPOslowMA = ema(_src, valPPOslow)
  73. valPPOmacd = valPPOfastMA - valPPOslowMA
  74. valPPOmacd2 = (valPPOmacd/valPPOslowMA)*100
  75. valPPOresult = sma(valPPOmacd2, valPPOsmooth)
  76. valPPOoscMin = valPPOresult > valPPOresult[1] and valPPOresult[1] < valPPOresult[2]
  77. valPPOoscMax = valPPOresult < valPPOresult[1] and valPPOresult[1] > valPPOresult[2]
  78. valPPOdivergenceBull = valPPOoscMin ? valPPOresult[1] : na
  79. valPPOdivergenceBear = valPPOoscMax ? valPPOresult[1]: na
  80. // prepare signals
  81. buyPPO = valPPOdivergenceBull != 0
  82. sellPPO = valPPOdivergenceBear != 0
  83. [buyPPO, sellPPO]
  84.  
  85. // Kaufman's Adaptive Moving Average (KAMA)
  86. funcKAMA(_src) =>
  87. // settings
  88. valKamaFastLength = 12
  89. valKamaFastLengthKAMA = 2
  90. valKamaSlowLength = 26
  91. valKamaSlowLengthKAMA = 30
  92. valKamaSignalLength = 9
  93. // fast
  94. valKamaFastLag = (valKamaFastLength - 1) / 2
  95. valKamaFastData = _src + (_src - _src[valKamaFastLag])
  96. valKamaFastZeroLagEMA = ema(valKamaFastData, valKamaFastLength)
  97. valKamaFastEma = ema(_src, valKamaFastLength)
  98. valKamaFastVolatility = sum(abs(_src - _src[1]), valKamaFastLength)
  99. valKamaFastEfficiencyRatio = valKamaFastVolatility != 0 ? abs(_src - _src[valKamaFastLength]) / valKamaFastVolatility : 0
  100. valKamaFast = 0.0
  101. valKamaFast := nz(valKamaFast[1]) + pow(valKamaFastEfficiencyRatio * (2 / (valKamaFastLengthKAMA + 1) - 2 / (valKamaSlowLengthKAMA + 1)) + 2 / (valKamaSlowLengthKAMA + 1), 2) * (_src - nz(valKamaFast[1]))
  102. // slow
  103. valKamaSlowLag = (valKamaSlowLength - 1) / 2
  104. valKamaSlowData = _src + (_src - _src[valKamaSlowLag])
  105. valKamaSlowZeroLagEMA = ema(valKamaSlowData, valKamaSlowLength)
  106. valKamaSlowEma = ema(_src, valKamaSlowLength)
  107. valKamaSlowVolatility = sum(abs(_src - _src[1]), valKamaSlowLength)
  108. valKamaSlowEfficiencyRatio = valKamaSlowVolatility != 0 ? abs(_src - _src[valKamaSlowLength]) / valKamaSlowVolatility : 0
  109. valKamaSlow = 0.0
  110. valKamaSlow := nz(valKamaSlow[1]) + pow(valKamaSlowEfficiencyRatio * (2 / (valKamaFastLengthKAMA + 1) - 2 / (valKamaSlowLengthKAMA + 1)) + 2 / (valKamaSlowLengthKAMA + 1), 2) * (_src - nz(valKamaSlow[1]))
  111. // Moving Average Convergence Divergence (MACD)
  112. valKamaMacd1 = valKamaFastZeroLagEMA - valKamaSlowZeroLagEMA
  113. valKamaMacd2 = valKamaFastEma - valKamaSlowEma
  114. valKamaMacd3 = valKamaFast - valKamaSlow
  115. valKamaMacdAverage = (valKamaMacd1 + valKamaMacd2 + valKamaMacd3) / 3
  116. // prepare signals
  117. valKamaSignal = ema(valKamaMacdAverage, valKamaSignalLength)
  118. //valKamaSignal = ema(valKamaMacdAverage + (valKamaMacdAverage - valKamaMacdAverage[(valKamaSignalLength - 1) / 2]), valKamaSignalLength)
  119. // prepare signals
  120. buyKama = crossover(valKamaMacdAverage, valKamaSignal)
  121. sellKama = crossunder(valKamaMacdAverage, valKamaSignal)
  122. [buyKama, sellKama]
  123.  
  124. funcIchimoku(_period, _src) =>
  125. conversionLinePeriod = _period == "Ichimoku_9-26-52" ? 9 : _period == "Ichimoku_10-30-60" ? 10 : _period == "Ichimoku_20-60-120" ? 20 : 9
  126. baseLinePeriod = _period == "Ichimoku_9-26-52" ? 26 : _period == "Ichimoku_10-30-60" ? 30 : _period == "Ichimoku_20-60-120" ? 60 : 26
  127. laggingSpanPeriod = _period == "Ichimoku_9-26-52" ? 52 : _period == "Ichimoku_10-30-60" ? 60 : _period == "Ichimoku_20-60-120" ? 120 : 52
  128. // calc
  129. tenkanSenConversionLine = funcDonchian(conversionLinePeriod)
  130. kijunSenBaseLine = funcDonchian(baseLinePeriod)
  131. leadingSpanA = avg(tenkanSenConversionLine, kijunSenBaseLine)
  132. leadingSpanB = funcDonchian(laggingSpanPeriod)
  133. // RSI Settings
  134. ichiRsiLength = 14
  135. ichiRsiOverBought = 50
  136. ichiRsiOverSold = 100
  137. ichiRsiDefault = rsi(_src, ichiRsiLength)
  138. // lines, span
  139. tenkanSenLine = tenkanSenConversionLine[baseLinePeriod]
  140. kijunSenLine = kijunSenBaseLine[baseLinePeriod]
  141. senkouSpanA = leadingSpanA[baseLinePeriod * 2]
  142. senkouSpanB = leadingSpanB[baseLinePeriod * 2]
  143. chikuSpan = _src[0]
  144. // smallest/greatest of multiple given values
  145. ichiMin = min(senkouSpanA, senkouSpanB)
  146. ichiMax = max(senkouSpanA, senkouSpanB)
  147. // up/down
  148. upTrend = chikuSpan > ichiMax
  149. downTrend = chikuSpan < ichiMin
  150. // bull/bear
  151. bull = crossover(tenkanSenLine, kijunSenLine)
  152. bear = crossunder(tenkanSenLine, kijunSenLine)
  153. // prepare signals
  154. longIchi = bull and upTrend and ichiRsiDefault <= ichiRsiOverSold
  155. shortIchi = bear and downTrend and ichiRsiDefault >= ichiRsiOverBought
  156. [longIchi, shortIchi]
  157.  
  158. funcDirectionalMovement(_len) =>
  159. _up = change(high)
  160. _down = -change(low)
  161. _plusDM = na(_up) ? na : (_up > _down and _up > 0 ? _up : 0)
  162. _minusDM = na(_down) ? na : (_down > _up and _down > 0 ? _down : 0)
  163. _trueRange = rma(tr, _len)
  164. _plus = fixnan(100 * rma(_plusDM, _len) / _trueRange)
  165. _minus = fixnan(100 * rma(_minusDM, _len) / _trueRange)
  166. [_plus, _minus]
  167.  
  168. funcAdx(valAdxDirectionalIndexLength, _len) =>
  169. [_plus, _minus] = funcDirectionalMovement(valAdxDirectionalIndexLength)
  170. sum = _plus + _minus
  171. funcAdx = 100 * rma(abs(_plus - _minus) / (sum == 0 ? 1 : sum), _len)
  172.  
  173. funcAdxLowHigh(_len, _type) =>
  174. [_plus, _minus] = funcDirectionalMovement(_len)
  175. return = _type == 'high' ? _plus : _minus
  176.  
  177. funcHullMovingAverage(_src, _len) =>
  178. tmpVal = max(2, _len)
  179. return = wma((2 * wma(_src, tmpVal/2)) - wma(_src, tmpVal), round(sqrt(tmpVal)))
  180.  
  181. funcHullMovingAverage3(_src, _len) =>
  182. p = _len/2
  183. wma(wma(hlc3, p/3) * 3 - wma(hlc3, p/2) - wma(hlc3, p), p)
  184.  
  185. funcHullTrend(_src, _period) =>
  186. 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
  187. boolHullTrendA = funcHullMovingAverage(_src, valHullTrendLength)
  188. boolHullTrendB = funcHullMovingAverage3(_src, valHullTrendLength)
  189. buyHullTrend = boolHullTrendB > boolHullTrendA and boolHullTrendB[1] < boolHullTrendA[1]
  190. sellHullTrend = boolHullTrendA > boolHullTrendB and boolHullTrendA[1] < boolHullTrendB[1]
  191. [buyHullTrend, sellHullTrend]
  192.  
  193. funcFractalCalculated(_type, _variant) =>
  194. _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
  195. _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
  196. return = _type == 'Fractal Regular' ? _regular : _billWilliams
  197.  
  198.  
  199.  
  200. //********** Inputs
  201. 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.
  202. 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"])
  203. 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"])
  204. inputStyleLabelSize = input(title="Label - Size", type=input.string, defval="small", options=["auto", "huge", "large", "normal", "small", "tiny"])
  205. inputStyleLabelType = input(title="Label - Type", type=input.string, defval="label", options=["label", "triangle"])
  206. inputStyleLabelPosition = input(title="Label - Position", type=input.string, defval="default", options=["default", "lowhigh"])
  207. inputShowPriceOnLabel = input(title="Show Price on Labels", type=input.bool, defval=true)
  208. inputShowBuyLabel = input(title="Show BUY Signal", type=input.bool, defval=true)
  209. inputShowSellLabel = input(title="Show SELL Signal", type=input.bool, defval=true)
  210. inputActivateAlerts = input(title="Activate Alerts", type=input.bool, defval=false)
  211.  
  212.  
  213.  
  214. //********** globals
  215. valCurrentTimeFramePeriod = timeframe.period
  216. valPrice = close <= 0.99999 ? tostring(close, '#.####') : close <= 99.99 ? tostring(close, '#.###') : close <= 999.99 ? tostring(close, '#.##') : tostring(close, '#')
  217. valXPosTimeBased = time[1] //offset -1
  218. valLabelTextBuy = 'BUY' + (inputShowPriceOnLabel == true ? ': ' + valPrice : '')
  219. valLabelTextSell = 'SELL' + (inputShowPriceOnLabel == true ? ': ' + valPrice : '')
  220.  
  221.  
  222.  
  223. //********** Average Directional Index (ADX)
  224. valAdxLength = 13
  225. valAdxDirectionalIndexLength = 14
  226. valAdxAverageTrueRange = 20
  227. valAdxHigherLowTrend = 40
  228.  
  229. boolAdxDirectionalIndexHigh = funcAdxLowHigh(valAdxDirectionalIndexLength, 'high')
  230. boolAdxDirectionalIndexLow = funcAdxLowHigh(valAdxDirectionalIndexLength, 'low')
  231. boolAdxDirectionalIndexCross = cross(boolAdxDirectionalIndexHigh, boolAdxDirectionalIndexLow)
  232.  
  233. funcAdxUp() => funcAdx(valAdxDirectionalIndexLength, valAdxLength) >= valAdxAverageTrueRange
  234. funcAdxDirectionalIndexUp() => boolAdxDirectionalIndexHigh >= boolAdxDirectionalIndexLow
  235. funcAdxDirectionalIndexUps() => boolAdxDirectionalIndexHigh >= valAdxHigherLowTrend
  236. funcAdxDirectionalIndexDown() => boolAdxDirectionalIndexLow > boolAdxDirectionalIndexHigh
  237. funcAdxDirectionalIndexDowns() => boolAdxDirectionalIndexLow > valAdxHigherLowTrend
  238.  
  239. funcAdxFinal() =>
  240. // bool
  241. boolAdxBuyCondition = funcAdxUp() and funcAdxDirectionalIndexUp() and funcAdxDirectionalIndexUps()
  242. boolAdxBuyCloseCondition = boolAdxDirectionalIndexCross and funcAdxDirectionalIndexUp()[1]
  243. boolAdxSellCondition = funcAdxUp() and funcAdxDirectionalIndexDown() and funcAdxDirectionalIndexDowns()
  244. boolAdxSellCloseCondition = boolAdxDirectionalIndexCross and funcAdxDirectionalIndexDown()[1]
  245. // counter
  246. adxCounter = 0
  247. adxCounter :=
  248. boolAdxBuyCloseCondition ? 0 :
  249. boolAdxSellCloseCondition ? 0 :
  250. boolAdxBuyCondition ? 1 :
  251. boolAdxSellCondition ? -1:
  252. nz(adxCounter[1])
  253. // prepare signals
  254. longAdx = adxCounter == 1 and adxCounter[1] == 0
  255. shortAdx = adxCounter == -1 and adxCounter[1] == 0
  256. [longAdx, shortAdx]
  257.  
  258.  
  259.  
  260. //********** Styling Labels
  261. 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
  262. valFinalLabelStyleUp = (inputStyleLabelType == "label") ? label.style_labelup : (inputStyleLabelType == "triangle") ? label.style_triangleup : label.style_labelup
  263. valFinalLabelStyleDown = (inputStyleLabelType == "label") ? label.style_labeldown : (inputStyleLabelType == "triangle") ? label.style_triangledown : label.style_labeldown
  264.  
  265.  
  266.  
  267. //********** Set Signals - Buy/Sell
  268. [buyEmaSma, sellEmaSma] = funcEmaSma(inputSource, valCurrentTimeFramePeriod, inputStrategy)
  269. signalFinalBuy = buyEmaSma
  270. signalFinalSell = sellEmaSma
  271. if (inputStrategy == 'Kaufman Adaptive Moving Average')
  272. [buyKama, sellKama] = funcKAMA(inputSource)
  273. signalFinalBuy := buyKama
  274. signalFinalSell := sellKama
  275. else if (inputStrategy == 'Percentage Price Oscillator')
  276. [buyPPO, sellPPO] = funcPPO(inputSource)
  277. signalFinalBuy := buyPPO
  278. signalFinalSell := sellPPO
  279. else if (inputStrategy == 'Hull Trend')
  280. [buyHullTrend, sellHullTrend] = funcHullTrend(inputSource, valCurrentTimeFramePeriod)
  281. signalFinalBuy := buyHullTrend
  282. signalFinalSell := sellHullTrend
  283. else if (inputStrategy == 'Fractal Regular' or inputStrategy == 'Fractal Bill Williams')
  284. signalBuyFractal = funcFractalCalculated(inputStrategy, -1)
  285. signalSellFractal = funcFractalCalculated(inputStrategy, 1)
  286. signalFinalBuy := signalBuyFractal
  287. signalFinalSell := signalSellFractal
  288.  
  289.  
  290.  
  291. //********** Set Signals - Long/Short
  292. signalFinalLong = false
  293. signalFinalShort = false
  294. if (inputStrategyLongShort == 'Ichimoku_9-26-52' or inputStrategyLongShort == 'Ichimoku_10-30-60' or inputStrategyLongShort == 'Ichimoku_20-60-120')
  295. [signalLongIchi, signalShortIchi] = funcIchimoku(inputStrategyLongShort, inputSource)
  296. signalFinalLong := signalLongIchi
  297. signalFinalShort := signalShortIchi
  298. else if (inputStrategyLongShort == 'ADX')
  299. [longAdx, shortAdx] = funcAdxFinal()
  300. signalFinalLong := longAdx
  301. signalFinalShort := shortAdx
  302.  
  303.  
  304.  
  305. //************ Plotting
  306.  
  307. // BUY
  308. label.new(
  309. x = signalFinalBuy and inputShowBuyLabel == true ? valXPosTimeBased : na,
  310. y = inputStyleLabelPosition == 'default' ? na : low,
  311. xloc = xloc.bar_time,
  312. yloc = inputStyleLabelPosition == 'default' ? yloc.belowbar : yloc.price,
  313. text = valLabelTextBuy,
  314. color = color.green,
  315. textcolor = color.white,
  316. style = valFinalLabelStyleUp,
  317. size = valFinalLabelSize)
  318.  
  319.  
  320. // SELL
  321. label.new(
  322. x = signalFinalSell and inputShowSellLabel == true ? valXPosTimeBased : na,
  323. y = inputStyleLabelPosition == 'default' ? na : high,
  324. xloc = xloc.bar_time,
  325. yloc = inputStyleLabelPosition == 'default' ? yloc.abovebar : yloc.price,
  326. text = valLabelTextSell,
  327. color = color.red,
  328. textcolor = color.white,
  329. style = valFinalLabelStyleDown,
  330. size = valFinalLabelSize)
  331.  
  332.  
  333. // LONG
  334. label.new(
  335. x = signalFinalLong ? valXPosTimeBased : na,
  336. y = low,
  337. xloc = xloc.bar_time,
  338. yloc = yloc.price,
  339. text = 'LONG',
  340. color = color.blue,
  341. textcolor = color.white,
  342. style = valFinalLabelStyleUp,
  343. size = size.tiny)
  344.  
  345.  
  346. // SHORT
  347. label.new(
  348. x = signalFinalShort ? valXPosTimeBased : na,
  349. y = high,
  350. xloc = xloc.bar_time,
  351. yloc = yloc.price,
  352. text = 'SHORT',
  353. color = color.fuchsia,
  354. textcolor = color.white,
  355. style = valFinalLabelStyleDown,
  356. size = size.tiny)
  357.  
  358.  
  359.  
  360. //************ Alerting
  361.  
  362. // BUY
  363. alertcondition(inputActivateAlerts and signalFinalBuy, title='BUY', message='BUY')
  364. // SELL
  365. alertcondition(inputActivateAlerts and signalFinalSell, title='SELL', message='SELL')
  366. // LONG
  367. alertcondition(inputActivateAlerts and signalFinalLong, title='LONG', message='LONG')
  368. // SHORT
  369. alertcondition(inputActivateAlerts and signalFinalShort, title='SHORT', message='SHORT')
  370. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  371. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  372. signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 200, defval = 11)
  373. sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=true)
  374.  
  375. lin_reg = input(title="Lin Reg", type=input.bool, defval=true)
  376. linreg_length = input(title="Linear Regression Length", type=input.integer, minval = 1, maxval = 200, defval = 11)
  377.  
  378. bopen = lin_reg ? linreg(open, linreg_length, 0) : open
  379. bhigh = lin_reg ? linreg(high, linreg_length, 0) : high
  380. blow = lin_reg ? linreg(low, linreg_length, 0) : low
  381. bclose = lin_reg ? linreg(close, linreg_length, 0) : close
  382.  
  383. r = bopen < bclose
  384.  
  385. signal = sma_signal ? sma(bclose, signal_length) : ema(bclose, signal_length)
  386.  
  387. 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)
  388. 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)
  389.  
  390. plot(signal, color=color.white)
  391. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  392. //###### FUNCTIONS ##################################//
  393. truncate(number, decimals) =>
  394. factor = pow(10, decimals)
  395. int(number * factor) / factor
  396.  
  397. nround(x) =>
  398. n = round(x / syminfo.mintick) * syminfo.mintick
  399.  
  400. chosenColor(c_)=> c_ == "aqua" ? #00FFFFff :
  401. c_ == "blue" ? #0040FFff :
  402. c_ == "fuchsia" ? #FF00FFff :
  403. c_ == "gray" ? #808080ff :
  404. c_ == "green" ? #008000ff :
  405. c_ == "lime" ? #00FF00ff :
  406. c_ == "maroon" ? #800000ff :
  407. c_ == "navy" ? #000099ff :
  408. c_ == "olive" ? #808000ff :
  409. c_ == "orange" ? #FF8000ff :
  410. c_ == "purple" ? #8000FFff :
  411. c_ == "red" ? #ff0000ff :
  412. c_ == "silver" ? #C0C0C0ff :
  413. c_ == "teal" ? #008080ff :
  414. c_ == "white" ? #FFFFFFff : #00000000
  415.  
  416. //###### DRAW TREND CHANNEL#########################//
  417. period = input( 40, "Period" , input.integer, minval=3)
  418. deviations = input( 2.0, "Deviation(s)" , input.float , minval=0.1, step=0.1)
  419. extendType = input("Right", "Extend Method", input.string , options=["Right","None"])=="None" ? extend.none : extend.right
  420. periodMinusOne = period-1
  421. Ex = 0.0,
  422. Ey = 0.0,
  423. Ex2 = 0.0,
  424. Exy = 0.0,
  425. for i=0 to periodMinusOne
  426. closeI = nz(close[i]),
  427. Ex := Ex + i,
  428. Ey := Ey + closeI,
  429. Ex2 := Ex2 + (i * i),
  430. Exy := Exy + (closeI * i)
  431. ExEx = Ex * Ex, slope = Ex2==ExEx ? 0.0 : (period * Exy - Ex * Ey) / (period * Ex2 - ExEx)
  432. linearRegression = (Ey - slope * Ex) / period
  433. intercept = linearRegression + bar_index * slope
  434. deviation = 0.0, for i=0 to periodMinusOne
  435. deviation := deviation + pow(nz(close[i]) - (intercept - slope * (bar_index[i])), 2.0)
  436. deviation := deviations * sqrt(deviation / periodMinusOne)
  437. startingPointY = linearRegression + slope * periodMinusOne
  438.  
  439.  
  440. //####### DRAWING LINES SECTION ####
  441. var line upperChannelLine = na ,
  442. var line medianChannelLine = na ,
  443. var line lowerChannelLine = na
  444. line.delete(upperChannelLine[1]),
  445. line.delete(medianChannelLine[1]),
  446. line.delete(lowerChannelLine[1])
  447.  
  448. // added color inputs
  449. 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"])
  450. 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"])
  451. 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"])
  452.  
  453. // draw channels
  454. upperChannelLine := line.new(bar_index - period + 1, startingPointY + deviation, bar_index, linearRegression + deviation, xloc.bar_index, extendType, chosenColor(upperLineColor) , line.style_solid , 2)
  455. medianChannelLine := line.new(bar_index - period + 1, startingPointY , bar_index, linearRegression , xloc.bar_index, extendType, chosenColor(middleLineColor), line.style_solid , 1)
  456. lowerChannelLine := line.new(bar_index - period + 1, startingPointY - deviation, bar_index, linearRegression - deviation, xloc.bar_index, extendType, chosenColor(lowerChannelColor), line.style_solid , 2)
  457.  
  458.  
  459. //####### INFORMATION PANEL SECTION ####
  460. // Idea is developed from https://www.tradingview.com/script/gcp7bgN4-Linear-low-high/
  461. // by https://www.tradingview.com/u/RafaelZioni/
  462. disp_panels = input(true, title="Display info panels?")
  463. linear_label_off = input(-1, title="linear label offset", minval = -1)
  464. linear_label_size = input(size.normal, options=[size.tiny, size.small, size.normal, size.large, size.huge], title="linear label size")
  465. pos_x = timenow + round(change(time)*linear_label_off)
  466. pos_y_upper = nround(linearRegression + deviation)
  467. candle_look_back = input(defval = 3, title="Look back", minval = 1, maxval = 5, step=1)
  468. pos_y_upper_previous = nround(linearRegression[candle_look_back] + deviation[candle_look_back])
  469. // pos_y_middle = nround(linearRegression) // doesn't need midde line
  470. pos_y_lower = truncate(nround(linearRegression - deviation), 5)
  471. pos_y_lower_previous = nround(linearRegression[candle_look_back] - deviation[candle_look_back])
  472. label_upper = "Current Sell Entry: " + tostring(pos_y_upper) + "\n Previous Sell Entry (using Look Back): " + tostring(pos_y_upper_previous)
  473. // label_upper = "Entry High: " + tostring(pos_y_upper)
  474. label_lower = "Current Buy Entry: " + tostring(pos_y_lower) + "\n Previous Buy Entry (using Look Back): " + tostring(pos_y_lower_previous)
  475.  
  476. 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
  477. 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
  478.  
  479. label.delete(draw_upper_label[1])
  480. label.delete(draw_lower_label[1])
  481.  
  482. // to memorize entries
  483. plot(pos_y_upper, title="Upper Entry Line", color=color.fuchsia, transp = 80)
  484. plot(pos_y_lower, title="Lower Entry Line", color=color.fuchsia, transp = 80)
  485. //####### ALERT PANEL SECTION ####
  486. ready_to_sell = crossover(close, pos_y_upper_previous)
  487. ready_to_buy = crossunder(close, pos_y_lower_previous)
  488.  
  489. alertcondition(ready_to_sell, title='Sell Alert v0.2', message='Sell at {{plot("Upper Entry Line")}}')
  490. alertcondition(ready_to_buy, title='Buy Alert v0.2', message='Buy at {{plot("Lower Entry Line")}}')
  491. alertcondition(ready_to_buy or ready_to_sell, title='General Alert v0.2', message='Price is close to upper/lower channel.')
  492. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  493. GiaLonNhat7to15 = highest(close[7], 8)
  494. GiaLonNhat15 = highest(close[1], 15)
  495. DK1 = GiaLonNhat7to15 >= GiaLonNhat15
  496. DK2 = close > GiaLonNhat7to15
  497. KhoiLuongLonNhat15 = highest(volume[1], 15)
  498. DK3 = volume > KhoiLuongLonNhat15
  499. GiaBeNhat15 = lowest(close[1], 15)
  500. DK4 = GiaBeNhat15 <= close * 0.95 and GiaBeNhat15 >= close * 0.9
  501. GiaBeNhatThang = lowest(close[15], 30)
  502. DK5 = GiaBeNhatThang <= close * 0.9 and GiaBeNhatThang >= close * 0.8
  503. DK6 = volume > 100000
  504. Buy = DK1 and DK2 and DK3 and DK4 and DK5 and DK6
  505.  
  506. plotshape(Buy, title="Buy", style=shape.triangleup, location=location.belowbar, color=#0000FF, size=size.small,text="BUY",textcolor=color.black)
  507. barcolor(Buy ? #0000FF : na)
  508.  
  509. alertcondition(Buy, title="Buy Signal", message="Buy")
  510. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  511. conversionPeriods = input(9, minval=1, title="Conversion Line Periods"),
  512. basePeriods = input(26, minval=1, title="Base Line Periods")
  513. laggingSpan2Periods = input(52, minval=1, title="Lagging Span 2 Periods"),
  514. displacement = input(26, minval=1, title="Displacement")
  515.  
  516. donchian(len) => avg(lowest(len), highest(len))
  517.  
  518. conversionLine = donchian(conversionPeriods)
  519. baseLine = donchian(basePeriods)
  520. spanA = avg(conversionLine, baseLine)
  521. spanB = donchian(laggingSpan2Periods)
  522.  
  523. plot(conversionLine, color=#0496ff, title="Conversion Line")
  524. plot(baseLine, color=#991515, title="Base Line")
  525. plot(close, offset = -displacement + 1, color=#459915, title="Lagging Span")
  526.  
  527. p1 = plot(spanA, offset = displacement - 1, color=color.green,
  528. title="Span A")
  529. p2 = plot(spanB, offset = displacement - 1, color=color.red,
  530. title="Span B")
  531. fill(p1, p2, color = spanA > spanB ? color.green : color.red)
  532. plot(spanA[25], color=color.orange)
  533. plot(spanB[25], color=color.yellow)
  534. priceincloud = (close>spanA[25] and close<spanB[25]) or (close<spanA[25] and close>spanB[25])
  535. //TRADING RULES
  536. //Conversion Line and Baseline crossover with Laggin Span Filter
  537. ConvBaseBuy = crossover(conversionLine, baseLine)
  538. ConvBaseSell = crossunder(conversionLine, baseLine)
  539. //Laggin Span Filter
  540. // cblagBuy = ConvBaseBuy==true and (close > high[25])
  541. // cblagSell = ConvBaseSell == true and (close < low[25])
  542. //test plot for cb filter
  543. // plotshape(series=cblagBuy, text = "LS Buy", title="Lagging Span Buy", location=location.belowbar, style=shape.triangleup, color=color.lime, size=size.small)
  544. // plotshape(series=cblagSell, text="LS Sell", title="Laggin Span Sell", location=location.abovebar, style=shape.triangledown, color=color.red, size=size.small)
  545.  
  546. //Conversion Line and Baseline crossover with Kumo Cloud Filter
  547. //Kumo Cloud Rule
  548. idealbuy = (close>high[25]) and conversionLine>baseLine and (close>spanA[25] and close>spanB[25]) and (low>spanA[25] and low>spanB[25])
  549. idealsell = (close<low[25]) and conversionLine<baseLine and (close<spanB[25] and close<spanB[25]) and (high<spanA[25] and high<spanB[25])
  550.  
  551. buymem = false
  552. sellmem = false
  553.  
  554. buymem := idealbuy?true:idealsell?false:buymem[1]
  555. sellmem := idealsell?true:idealbuy?false:sellmem[1]
  556.  
  557. longCond = idealbuy and not(buymem[1])
  558. shortCond = idealsell and not(sellmem[1])
  559.  
  560. plotshape(series=longCond, text = "Ichi Buy", title="Ichi Buy", location=location.belowbar, style=shape.triangleup, color=color.lime, size=size.small)
  561. plotshape(series=shortCond, text="Ichi Sell", title="Ichi Sell", location=location.abovebar, style=shape.triangledown, color=color.red, size=size.small)
  562.  
  563. alertcondition(longCond, title="Ichi Buy", message="Buy it like a snail")
  564. alertcondition(shortCond, title="Ichi Sell", message="Sell it like a snail")
  565.  
  566.  
Advertisement
Add Comment
Please, Sign In to add comment