xmd79

Tesla Coil

Dec 22nd, 2022
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 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. // © veryfid
  3. // contribution by @KryptoBeau
  4.  
  5. //@version=5
  6.  
  7. indicator(title='Tesla Coil', overlay=false, timeframe='')
  8. src = input(title='Source', defval=close)
  9. mirror = true
  10.  
  11. //////////
  12. //The Cosine IFM
  13. //Copyright DasanC 2019
  14. range_1 = input(title='Scanning Range', defval=100)
  15. len2 = input(40)
  16. PI = 3.14159265359
  17. s2 = 0.0
  18. s3 = 0.0
  19. delta1 = 0.0
  20. inst1 = 0.0
  21. len1 = 0.0
  22. v1 = 0.0
  23. v2 = 0.0
  24. v4 = 0.0
  25.  
  26. v1 := src - src[7]
  27. s2 := 0.2 * (v1[1] + v1) * (v1[1] + v1) + 0.8 * nz(s2[1])
  28. s3 := 0.2 * (v1[1] - v1) * (v1[1] - v1) + 0.8 * nz(s3[1])
  29. if s2 != 0
  30. v2 := math.sqrt(s3 / s2)
  31. v2
  32. if s3 != 0
  33. delta1 := 2 * math.atan(v2)
  34. delta1
  35. for i = 0 to range_1 by 1
  36. v4 += delta1[i]
  37. if v4 > 2 * PI and inst1 == 0.0
  38. inst1 := i - 1
  39. inst1
  40. if inst1 == 0.0
  41. inst1 := inst1[1]
  42. inst1
  43. len1 := 0.25 * inst1 + 0.75 * nz(len1[1])
  44.  
  45.  
  46.  
  47. //Robust Cycle Measurement Function (I-Q Principles)
  48. //Args: src is the series you want to find the period of
  49. // I cant remember where I got this code from...
  50. //##############################################################################
  51. getRCM(src) =>
  52. Price = src
  53. alpha = 0.1
  54. //Optional Parameters if the user wishes to change
  55. //alpha := input(title="Alpha", type=float, defval=0.07)
  56. Cycle = 0.0
  57. Q1 = 0.0
  58. I1 = 0.0
  59. I2 = 0.0
  60. Q2 = 0.0
  61. DeltaPhase = 0.0
  62. MedianDelta = 0.0
  63. DC = 0.0
  64. InstPeriod = 0.0
  65. Period = 0.0
  66.  
  67.  
  68. Smooth = (Price + 2 * Price[1] + 2 * Price[2] + Price[3]) / 6
  69. Cycle := (1 - 0.5 * alpha) * (1 - 0.5 * alpha) * (Smooth - 2 * Smooth[1] + Smooth[2]) + 2 * (1 - alpha) * nz(Cycle[1]) - (1 - alpha) * (1 - alpha) * nz(Cycle[2])
  70. if bar_index < 7
  71. Cycle := (Price - 2 * Price[1] + Price[2]) / 4
  72. Cycle
  73. Q1 := (0.0962 * Cycle + 0.5769 * Cycle[2] - 0.5769 * Cycle[4] - 0.0962 * Cycle[6]) * (0.5 + 0.08 * InstPeriod[1])
  74. I1 := Cycle[3]
  75. if Q1 != 0 and Q1[1] != 0
  76. DeltaPhase := (I1 / Q1 - I1[1] / Q1[1]) / (1 + I1 * I1[1] / (Q1 * Q1[1]))
  77. DeltaPhase
  78. if DeltaPhase < 0.1
  79. DeltaPhase := 0.1
  80. DeltaPhase
  81. if DeltaPhase > 1.1
  82. DeltaPhase := 1.1
  83. DeltaPhase
  84. MedianDelta := ta.percentile_nearest_rank(DeltaPhase, 5, 50)
  85. if MedianDelta == 0
  86. DC := 15
  87. DC
  88. else
  89. DC := 6.28318 / MedianDelta + 0.5
  90. DC
  91. InstPeriod := 0.33 * DC + 0.67 * nz(InstPeriod[1])
  92. Period := 0.15 * InstPeriod + 0.85 * nz(Period[1])
  93. Period := math.round(Period)
  94. Period
  95. //##############################################################################
  96.  
  97. rcm = getRCM(src)
  98.  
  99.  
  100. //#############################################################################
  101. //The In-phase & quadrature IFM
  102. //Copyright DasanC 2019
  103. //#############################################################################
  104.  
  105. imult = 0.635
  106. qmult = 0.338
  107. inphase = 0.0
  108. quadrature = 0.0
  109. re = 0.0
  110. im = 0.0
  111. delta = 0.0
  112. inst = 0.0
  113. len = 0.0
  114. V = 0.0
  115.  
  116. P = src - src[7]
  117.  
  118. inphase := 1.25 * (P[4] - imult * P[2]) + imult * nz(inphase[3])
  119. quadrature := P[2] - qmult * P + qmult * nz(quadrature[2])
  120. re := 0.2 * (inphase * inphase[1] + quadrature * quadrature[1]) + 0.8 * nz(re[1])
  121. im := 0.2 * (inphase * quadrature[1] - inphase[1] * quadrature) + 0.8 * nz(im[1])
  122. if re != 0.0
  123. delta := math.atan(im / re)
  124. delta
  125.  
  126. for i = 0 to 100 by 1
  127. V += delta[i]
  128. if V > 2 * PI and inst == 0.0
  129. inst := i
  130. inst
  131.  
  132. if inst == 0.0
  133. inst := nz(inst[1])
  134. inst
  135.  
  136. len := 0.25 * inst + 0.75 * nz(len[1])
  137.  
  138. average = math.avg(len1 * 3, rcm, len)
  139. tfig = input(50)
  140. tfig2 = len1 * 3 > len1 * 3 * -1 + len2 ? 50 : 0
  141. tfig3 = average > average * -1 + len2 ? 50 : 0
  142. tfig4 = len <= len * -1 + len2 ? 90 : 50
  143. cola= input.color(color.blue)
  144. colb= input.color(#bbd9fb)
  145. colc= input.color(#9575cd)
  146. plot(len, 'len', color=color.new(cola, tfig4))
  147. //plot(len1 * 3 ,"3xlen1", color=#bbd9fb)//original line
  148. len1x3 = len1 * 3 //KB edit
  149. plot(len1x3, '3xlen1', color=color.new(colb, tfig2)) //KB edit
  150.  
  151. //Mirror
  152. plot(mirror ? len * -1 + len2 : na, 'len mirror', color=color.new(cola, tfig4))
  153. plot(mirror ? len1 * 3 * -1 + len2 : na, 'len1 mirror', color=color.new(colb, tfig2))
  154. plot(mirror ? average * -1 + len2 : na, 'average mirror', color=color.new(colc, tfig3))
  155. plot(average, 'average', color=color.new(colc, tfig3))
  156.  
  157. alertcond = ta.cross(len1 * 3, 20) or ta.cross(average, average * -1 + len2) or ta.cross(len, len * -1 + len2 )
  158. alertcondition(alertcond, title='Tesla Coil', message='Tesla Coil Crossover')
  159.  
Advertisement
Add Comment
Please, Sign In to add comment