Advertisement
NKactive

Untitled

Oct 30th, 2023
191
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.05 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © NKactive
  3. // Based on code loxx
  4.  
  5. //@version=5
  6. strategy("GKYZ-Filtered, Non-Linear Regression MA [Loxx]", shorttitle = "GKYZFNLRMA [Loxx]", overlay = true) //, timeframe="",timeframe_gaps = true)
  7.  
  8. import loxx/loxxexpandedsourcetypes/4
  9. import EliCobra/CobraMetrics/4 as cobra
  10. //// PLOT DATA
  11. disp_ind = input.string ("None" , title = "Display Curve" , tooltip = "Choose which data you would like to display", options=["Strategy", "Equity", "Open Profit", "Gross Profit", "Net Profit", "None"], group = "🐍 𝓒𝓸𝓫𝓻𝓪 𝓜𝓮𝓽𝓻𝓲𝓬𝓼 🐍")
  12. pos_table = input.string("Middle Left", "Table Position", options = ["Top Left", "Middle Left", "Bottom Left", "Top Right", "Middle Right", "Bottom Right", "Top Center", "Bottom Center"], group = "🐍 𝓒𝓸𝓫𝓻𝓪 𝓜𝓮𝓽𝓻𝓲𝓬𝓼 🐍")
  13. type_table = input.string("None", "Table Type", options = ["Full", "Simple", "None"], group = "🐍 𝓒𝓸𝓫𝓻𝓪 𝓜𝓮𝓽𝓻𝓲𝓬𝓼 🐍")
  14.  
  15. plot(cobra.curve(disp_ind))
  16. cobra.cobraTable(type_table, pos_table)
  17.  
  18.  
  19. greencolor = #2DD204
  20. redcolor = #D2042D
  21.  
  22. nonLinearRegression(float src, int per)=>
  23. float AvgX = 0
  24. float AvgY = 0
  25.  
  26. float[] nlrXValue = array.new<float>(per, 0)
  27. float[] nlrYValue = array.new<float>(per, 0)
  28.  
  29. for i = 0 to per - 1
  30. array.set(nlrXValue, i, i)
  31. array.set(nlrYValue, i, nz(src[i]))
  32. AvgX += array.get(nlrXValue, i)
  33. AvgY += array.get(nlrYValue, i)
  34.  
  35. AvgX /= per
  36. AvgY /= per
  37.  
  38. float SXX = 0
  39. float SXY = 0
  40. float SYY = 0
  41. float SXX2 = 0
  42. float SX2X2 = 0
  43. float SYX2 = 0
  44.  
  45. for i = 0 to per - 1
  46. float XM = array.get(nlrXValue, i) - AvgX
  47. float YM = array.get(nlrYValue, i) - AvgY
  48. float XM2 = array.get(nlrXValue, i) * array.get(nlrXValue, i) - AvgX * AvgX
  49. SXX += XM * XM
  50. SXY += XM * YM
  51. SYY += YM * YM
  52. SXX2 += XM * XM2
  53. SX2X2 += XM2 * XM2
  54. SYX2 += YM * XM2
  55.  
  56. float tmp = 0
  57. float ACoeff = 0
  58. float BCoeff = 0
  59. float CCoeff = 0
  60.  
  61. tmp := SXX * SX2X2 - SXX2 * SXX2
  62.  
  63. if tmp != 0
  64. BCoeff := (SXY * SX2X2 - SYX2 * SXX2) / tmp
  65. CCoeff := (SXX * SYX2 - SXX2 * SXY) / tmp
  66.  
  67. ACoeff := AvgY - BCoeff * AvgX - CCoeff * AvgX * AvgX
  68. tmp := ACoeff + CCoeff
  69. tmp
  70.  
  71. gkyzvol(int per)=>
  72. float gzkylog = math.log(open / nz(close[1]))
  73. float pklog = math.log(high / low)
  74. float gklog = math.log(close / open)
  75. float garmult = (2 * math.log(2) - 1)
  76.  
  77. float gkyzsum = 1 / per * math.sum(math.pow(gzkylog, 2), per)
  78. float parkinsonsum = 1 / (2 * per) * math.sum(math.pow(pklog, 2), per)
  79. float garmansum = garmult / per * math.sum(math.pow(gklog, 2), per)
  80.  
  81. float sum = gkyzsum + parkinsonsum - garmansum
  82. float devpercent = math.sqrt(sum)
  83. devpercent
  84.  
  85. gkyzFilter(float src, int len, float filter)=>
  86. float price = src
  87. float filtdev = filter * gkyzvol(len) * src
  88. price := math.abs(price - nz(price[1])) < filtdev ? nz(price[1]) : price
  89. price
  90.  
  91.  
  92. smthtype = input.string("Kaufman", "Heikin-Ashi Better Caculation Type", options = ["AMA", "T3", "Kaufman"], group = "Source Settings")
  93.  
  94. srcin = input.string("Close", "Source", group= "Source Settings",
  95. options =
  96. ["Close", "Open", "High", "Low", "Median", "Typical", "Weighted", "Average", "Average Median Body", "Trend Biased", "Trend Biased (Extreme)",
  97. "HA Close", "HA Open", "HA High", "HA Low", "HA Median", "HA Typical", "HA Weighted", "HA Average", "HA Average Median Body", "HA Trend Biased", "HA Trend Biased (Extreme)",
  98. "HAB Close", "HAB Open", "HAB High", "HAB Low", "HAB Median", "HAB Typical", "HAB Weighted", "HAB Average", "HAB Average Median Body", "HAB Trend Biased", "HAB Trend Biased (Extreme)"])
  99.  
  100. per = input.int(60, "Period", group = "Basic Settings")
  101.  
  102. filterop = input.string("Both", "Filter Options", options = ["Price", "GKYZFNLRMA", "Both", "None"], group= "Filter Settings")
  103. filter = input.float(0.5, "Filter Multiple", minval = 0, group= "Filter Settings")
  104. filterperiod = input.int(15, "Filter Period", minval = 0, group= "Filter Settings")
  105.  
  106. colorbars = input.bool(true, "Color bars?", group= "UI Options")
  107. showSigs = input.bool(true, "Show Signals?", group = "UI Options")
  108.  
  109. kfl=input.float(0.666, title="* Kaufman's Adaptive MA (KAMA) Only - Fast End", group = "Moving Average Inputs")
  110. ksl=input.float(0.0645, title="* Kaufman's Adaptive MA (KAMA) Only - Slow End", group = "Moving Average Inputs")
  111. amafl = input.int(2, title="* Adaptive Moving Average (AMA) Only - Fast", group = "Moving Average Inputs")
  112. amasl = input.int(30, title="* Adaptive Moving Average (AMA) Only - Slow", group = "Moving Average Inputs")
  113.  
  114. haclose = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close)
  115. haopen = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, open)
  116. hahigh = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, high)
  117. halow = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, low)
  118. hamedian = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hl2)
  119. hatypical = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hlc3)
  120. haweighted = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hlcc4)
  121. haaverage = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, ohlc4)
  122.  
  123. float src = switch srcin
  124. "Close" => loxxexpandedsourcetypes.rclose()
  125. "Open" => loxxexpandedsourcetypes.ropen()
  126. "High" => loxxexpandedsourcetypes.rhigh()
  127. "Low" => loxxexpandedsourcetypes.rlow()
  128. "Median" => loxxexpandedsourcetypes.rmedian()
  129. "Typical" => loxxexpandedsourcetypes.rtypical()
  130. "Weighted" => loxxexpandedsourcetypes.rweighted()
  131. "Average" => loxxexpandedsourcetypes.raverage()
  132. "Average Median Body" => loxxexpandedsourcetypes.ravemedbody()
  133. "Trend Biased" => loxxexpandedsourcetypes.rtrendb()
  134. "Trend Biased (Extreme)" => loxxexpandedsourcetypes.rtrendbext()
  135. "HA Close" => loxxexpandedsourcetypes.haclose(haclose)
  136. "HA Open" => loxxexpandedsourcetypes.haopen(haopen)
  137. "HA High" => loxxexpandedsourcetypes.hahigh(hahigh)
  138. "HA Low" => loxxexpandedsourcetypes.halow(halow)
  139. "HA Median" => loxxexpandedsourcetypes.hamedian(hamedian)
  140. "HA Typical" => loxxexpandedsourcetypes.hatypical(hatypical)
  141. "HA Weighted" => loxxexpandedsourcetypes.haweighted(haweighted)
  142. "HA Average" => loxxexpandedsourcetypes.haaverage(haaverage)
  143. "HA Average Median Body" => loxxexpandedsourcetypes.haavemedbody(haclose, haopen)
  144. "HA Trend Biased" => loxxexpandedsourcetypes.hatrendb(haclose, haopen, hahigh, halow)
  145. "HA Trend Biased (Extreme)" => loxxexpandedsourcetypes.hatrendb(haclose, haopen, hahigh, halow)
  146. "HAB Close" => loxxexpandedsourcetypes.habclose(smthtype, amafl, amasl, kfl, ksl)
  147. "HAB Open" => loxxexpandedsourcetypes.habopen(smthtype, amafl, amasl, kfl, ksl)
  148. "HAB High" => loxxexpandedsourcetypes.habhigh(smthtype, amafl, amasl, kfl, ksl)
  149. "HAB Low" => loxxexpandedsourcetypes.hablow(smthtype, amafl, amasl, kfl, ksl)
  150. "HAB Median" => loxxexpandedsourcetypes.habmedian(smthtype, amafl, amasl, kfl, ksl)
  151. "HAB Typical" => loxxexpandedsourcetypes.habtypical(smthtype, amafl, amasl, kfl, ksl)
  152. "HAB Weighted" => loxxexpandedsourcetypes.habweighted(smthtype, amafl, amasl, kfl, ksl)
  153. "HAB Average" => loxxexpandedsourcetypes.habaverage(smthtype, amafl, amasl, kfl, ksl)
  154. "HAB Average Median Body" => loxxexpandedsourcetypes.habavemedbody(smthtype, amafl, amasl, kfl, ksl)
  155. "HAB Trend Biased" => loxxexpandedsourcetypes.habtrendb(smthtype, amafl, amasl, kfl, ksl)
  156. "HAB Trend Biased (Extreme)" => loxxexpandedsourcetypes.habtrendbext(smthtype, amafl, amasl, kfl, ksl)
  157. => haclose
  158.  
  159. src := filterop == "Both" or filterop == "Price" and filter > 0 ? gkyzFilter(src, filterperiod, filter) : src
  160.  
  161. out = nonLinearRegression(src, per)
  162.  
  163. out := filterop == "Both" or filterop == "GKYZFNLRMA" and filter > 0 ? gkyzFilter(out, filterperiod, filter) : out
  164.  
  165. sig = out[1]
  166.  
  167. goLong_pre = ta.crossover(out, sig)
  168. goShort_pre = ta.crossunder(out, sig)
  169.  
  170. contSwitch = 0
  171. contSwitch := nz(contSwitch[1])
  172. contSwitch := goLong_pre ? 1 : goShort_pre ? -1 : contSwitch
  173.  
  174. goLong = goLong_pre and ta.change(contSwitch)
  175. goShort = goShort_pre and ta.change(contSwitch)
  176.  
  177. colorout = contSwitch == 1 ? greencolor : redcolor
  178.  
  179. barcolor(colorbars ? colorout : na)
  180. plot(out, "GKYZFNLRMA", color = colorout, linewidth = 3)
  181.  
  182. //plotshape(showSigs and goLong, title = "Long", color = color.yellow, textcolor = color.yellow, text = "L", style = shape.triangleup, location = location.belowbar, size = size.tiny)
  183. //plotshape(showSigs and goShort, title = "Short", color = color.fuchsia, textcolor = color.fuchsia, text = "S", style = shape.triangledown, location = location.abovebar, size = size.tiny)
  184.  
  185. //alertcondition(goLong, title="Long", message="JFD-Adaptive, GKYZ-Filtered KAMA [Loxx]: Long\nSymbol: {{ticker}}\nPrice: {{close}}")
  186. //alertcondition(goShort, title="Short", message="JFD-Adaptive, GKYZ-Filtered KAMA [Loxx]: Short\nSymbol: {{ticker}}\nPrice: {{close}}")
  187.  
  188. // ****************************************************************************************************************************************************************
  189. // Call combine signals and execute buy/sell positions within timeframe
  190. //.****************************************************************************************************************************************************************
  191. // Date Range To Include
  192. startDate = timestamp("2018-01-01T00:00")
  193. endDate = time
  194. // Check if the current timestamp is within the restricted range
  195. inRestrictedRange = time >= startDate and time <= endDate
  196. //
  197. // Buy Signals on overbought and oversold
  198. //
  199. if inRestrictedRange and showSigs and goLong // ADD OTHER BUY SIGNAL BOOLS
  200. strategy.entry("My Long Entry Id", strategy.long, 100)
  201. if inRestrictedRange and showSigs and goShort // ADD OTHER BUY SIGNAL BOOLS
  202. strategy.entry("My Short Entry Id", strategy.short, 100)
Advertisement
Comments
  • # text 0.12 KB | 0 0
    1. download all types of premium tradingview indicators codes available on telegram - https://t.me/tradingview_premium_indicator
Add Comment
Please, Sign In to add comment
Advertisement