“数组中的数据仅存在于当前柱形。如何引用历史数值?”

huangapple go评论89阅读模式
英文:

Data from array only exists for current bar. How can I reference historical values?

问题

以下是您提供的代码的翻译部分:

  1. 脚本在当前K线上打印了两个标签。这些标签是突破关键水平的看涨和看跌概率。我已将这些字符串拆分,只提取百分比字符串值,并转换为浮点数值,以便可以使用它们创建规则。这些浮点数存储在它们各自的变量"bull_prob""bear_prob"中。
  2. 问题在于这些变量只包含当前K线的数据,在历史K线上都是零值。我创建了两个新数组bull_hist[]和bear_hist[],并将当前K线的值推送到它们,以便可以在历史上引用它们。当我检查数组大小时,我可以看到它已经推送了所有历史K线的值。然而,除了最后一个索引之外,它们仍然都是零值。
  3. 为了测试,我创建了基本的指标规则,简单地表示在看涨蜡烛上买入,在看跌蜡烛上卖出,但也考虑了概率规则是否成立。
  4. 当前结果:指标仅在当前K线上正确运行。这意味着在实时或回放期间,规则会适当处理。但除非回放,否则无法看到任何过去的指标。这也意味着如果我将其转换为策略,它将找不到任何交易。
  5. 预期结果:bull_probbear_prob应该显示在数据窗口中看到的那个K线上的概率值。我目前正在将它们的输出绘制到数据窗口中。以前的指标应该在图表上可见。

希望这有助于您理解代码的翻译部分。如果您有任何进一步的问题或需要进一步的帮助,请随时提问。

英文:

The script prints 2 labels on the current bar. These labels are the bullish and bearish probabilities of breaking a key level. I have split these strings to pull out just the percentage string value and converted to float values so that rules can be created with them. These floats are stored in their respective variables "bull_prob" and "bear_prob".

The problem is these variables only have data on the current bar and zero values on historical bars. I created 2 new arrays bull_hist[] and bear_hist[] and push the current bar value to them so that they can be referenced historically. When I check the array size I can see that it has pushed values for all historical bars. However they are still all zero values except for the last index.

For testing I have created basic indicator rules that simply indicate buy on a bull candle and sell on a bear candle, but also considers if the probability rule is true.

Current result: Indicator works correctly for the current bar only. This means that while live or during replay the rules are processed appropriately. However you cannot see any past indicators unless you replay. This also means if I convert this to a strategy it would find zero trades.

Intended result: bull_prob and bear_prob should show the probability value that existed on that bar as seen in the data window. I'm currently plotting their output to data window. Previous indicators should be visible on the chart.

Full Code:

  1. //@version=5
  2. indicator("Probability Analyzer", overlay = true)
  3. import HeWhoMustNotBeNamed/Logger/2 as log
  4. var logger = log.Logger.new(minimumLevel='Trace', textSize = size.normal)
  5. logger.init()
  6. // Tooltips
  7. string t1 = "Set the pivot period"
  8. string t2 = "Set the response period. A low value returns a short-term structure and a high value returns a long-term structure.
  9. If you disable this option the pivot length above will be used."
  10. // Inputs
  11. int pa_len = input.int(defval = 20
  12. , title = "Structure Period"
  13. , minval = 1
  14. , tooltip = t1)
  15. int pa_res = input.int(defval = 7
  16. , title = "Structure Response"
  17. , minval = 1
  18. , tooltip = t2)
  19. n = bar_index
  20. var pup = float(na)
  21. var pdn = float(na)
  22. var piup = int(na)
  23. var pidn = int(na)
  24. // Matrix & Array
  25. var vals = matrix.new<float>(9, 4, 0.0)
  26. var string [] txt = array.new<string>(2, "")
  27. // Functions
  28. Current(v)=>
  29. str = ""
  30. val1 = float(na)
  31. val2 = float(na)
  32. if v >= 0
  33. if v == 1
  34. str := "SMS: "
  35. val1 := matrix.get(vals, 0, 1)
  36. val2 := matrix.get(vals, 0, 3)
  37. else if v == 2
  38. str := "BMS: "
  39. val1 := matrix.get(vals, 1, 1)
  40. val2 := matrix.get(vals, 1, 3)
  41. else if v > 2
  42. str := "BMS: "
  43. val1 := matrix.get(vals, 2, 1)
  44. val2 := matrix.get(vals, 2, 3)
  45. else if v <= 0
  46. if v == -1
  47. str := "SMS: "
  48. val1 := matrix.get(vals, 3, 1)
  49. val2 := matrix.get(vals, 3, 3)
  50. else if v == -2
  51. str := "BMS: "
  52. val1 := matrix.get(vals, 4, 1)
  53. val2 := matrix.get(vals, 4, 3)
  54. else if v < -2
  55. str := "BMS: "
  56. val1 := matrix.get(vals, 5, 1)
  57. val2 := matrix.get(vals, 5, 3)
  58. [str, val1, val2]
  59. // ~~ Pivots {
  60. pup := math.max(pup[1], high)
  61. pdn := math.min(pdn[1], low)
  62. pa_high = ta.pivothigh(high, pa_len, pa_len)
  63. pa_low = ta.pivotlow(low, pa_len, pa_len)
  64. if pa_high
  65. pup := pa_high
  66. else if pa_low
  67. pdn := pa_low
  68. // Structure
  69. var pos = 0
  70. if pup > pup[1]
  71. piup := n
  72. centerBull = math.round(math.avg(piup[1] ,n))
  73. if pos <= 0
  74. pos := 1
  75. matrix.set(vals, 6, 0, matrix.get(vals, 6, 0) + 1)
  76. else if pos == 1 and pup > pup[1] and pup[1] == pup[pa_res]
  77. pos := 2
  78. matrix.set(vals, 6, 1,matrix.get(vals ,6 ,1) + 1)
  79. else if pos > 1 and pup > pup[1] and pup[1] == pup[pa_res]
  80. pos := pos + 1
  81. matrix.set(vals ,6 ,2,matrix.get(vals, 6, 2) + 1)
  82. else if pup < pup[1]
  83. piup := n - pa_len
  84. if pdn < pdn[1]
  85. pidn := n
  86. centerBear = math.round(math.avg(pidn[1], n))
  87. if pos >= 0
  88. pos := -1
  89. matrix.set(vals, 7, 0, matrix.get(vals, 7, 0) + 1)
  90. else if pos == -1 and pdn < pdn[1] and pdn[1] == pdn[pa_res]
  91. pos := -2
  92. matrix.set(vals, 7, 1, matrix.get(vals, 7, 1) + 1)
  93. else if pos < -1 and pdn < pdn[1] and pdn[1] == pdn[pa_res]
  94. pos := pos - 1
  95. matrix.set(vals, 7, 2, matrix.get(vals, 7, 2) + 1)
  96. else if pdn > pdn[1]
  97. pidn := n - pa_len
  98. var float[] bull_hist_txt = array.new_float()
  99. var float[] bear_hist_txt = array.new_float()
  100. // Probability Calculation
  101. if ta.change(pos)
  102. if pos > 0 and pos[1] > 0 or pos < 0 and pos[1] < 0
  103. if matrix.get(vals, 8, 0) < matrix.get(vals, 8, 1)
  104. matrix.set(vals, 8, 2, matrix.get(vals, 8, 2) + 1)
  105. else
  106. matrix.set(vals, 8, 3, matrix.get(vals, 8, 3) + 1)
  107. else
  108. if matrix.get(vals, 8, 0) > matrix.get(vals, 8, 1)
  109. matrix.set(vals, 8, 2, matrix.get(vals, 8, 2) + 1)
  110. else
  111. matrix.set(vals,8, 3, matrix.get(vals, 8, 3) + 1)
  112. //Score Calulation
  113. float buC0 = matrix.get(vals, 0, 0), float buC1 = matrix.get(vals, 0, 2)
  114. float buS0 = matrix.get(vals, 1, 0), float buS1 = matrix.get(vals, 1, 2)
  115. float buB0 = matrix.get(vals, 2, 0), float buB1 = matrix.get(vals, 2, 2)
  116. float beC0 = matrix.get(vals, 3, 0), float beC1 = matrix.get(vals, 3, 2)
  117. float beS0 = matrix.get(vals, 4, 0), float beS1 = matrix.get(vals, 4, 2)
  118. float beB0 = matrix.get(vals, 5, 0), float beB1 = matrix.get(vals, 5, 2)
  119. float tbuC = matrix.get(vals, 6, 0), float tbuS = matrix.get(vals, 6, 1)
  120. float tbuB = matrix.get(vals, 6, 2), float tbeC = matrix.get(vals, 7, 0)
  121. float tbeS = matrix.get(vals, 7, 1), float tbeB = matrix.get(vals, 7, 2)
  122. // Bull
  123. if (pos[1] == 1 or pos[1] == 0) and pos < 0
  124. matrix.set(vals, 0, 0, buC0 + 1)
  125. matrix.set(vals, 0, 1, math.round(((buC0 + 1) / tbuC) * 100, 2))
  126. if (pos[1] == 1 or pos[1] == 0) and pos == 2
  127. matrix.set(vals, 0, 2, buC1 +1 )
  128. matrix.set(vals, 0, 3, math.round(((buC1 + 1) / tbuC) * 100, 2))
  129. if pos[1] == 2 and pos < 0
  130. matrix.set(vals, 1, 0, buS0 + 1)
  131. matrix.set(vals, 1, 1, math.round(((buS0 +1 ) / tbuS) * 100, 2))
  132. if pos[1] == 2 and pos > 2
  133. matrix.set(vals, 1, 2, buS1 + 1)
  134. matrix.set(vals, 1, 3, math.round(((buS1 + 1) / tbuS) * 100, 2))
  135. if pos[1] > 2 and pos < 0
  136. matrix.set(vals, 2, 0, buB0 + 1)
  137. matrix.set(vals, 2, 1, math.round(((buB0 + 1) / tbuB) * 100, 2))
  138. if pos[1] > 2 and pos > pos[1]
  139. matrix.set(vals, 2, 2, buB1 + 1)
  140. matrix.set(vals, 2, 3, math.round(((buB1 + 1) / tbuB) * 100, 2))
  141. // Bear
  142. if (pos[1] == -1 or pos[1] == 0) and pos > 0
  143. matrix.set(vals, 3, 0, beC0 + 1)
  144. matrix.set(vals, 3, 1, math.round(((beC0 + 1) / tbeC) * 100, 2))
  145. if (pos[1] == -1 or pos[1] == 0) and pos == -2
  146. matrix.set(vals, 3, 2, beC1 + 1)
  147. matrix.set(vals, 3, 3, math.round(((beC1 + 1) / tbeC) * 100, 2))
  148. if pos[1] == -2 and pos > 0
  149. matrix.set(vals, 4, 0, beS0 + 1)
  150. matrix.set(vals, 4, 1, math.round(((beS0 + 1) / tbeS) * 100, 2))
  151. if pos[1] == -2 and pos < -2
  152. matrix.set(vals, 4, 2, beS1 + 1)
  153. matrix.set(vals, 4, 3, math.round(((beS1 + 1) / tbeS) * 100, 2))
  154. if pos[1] < -2 and pos > 0
  155. matrix.set(vals, 5, 0, beB0 + 1)
  156. matrix.set(vals, 5, 1, math.round(((beB0 + 1) / tbeB) * 100, 2))
  157. if pos[1] < -2 and pos < pos[1]
  158. matrix.set(vals, 5, 2, beB1 + 1)
  159. matrix.set(vals, 5, 3, math.round(((beB1 + 1) / tbeB) * 100, 2))
  160. [str, val1, val2] = Current(pos)
  161. array.set(txt, 0, "CHoCH: " + str.tostring(val1, format.percent))
  162. array.set(txt, 1, str + str.tostring(val2, format.percent))
  163. matrix.set(vals, 8, 0, val1)
  164. matrix.set(vals, 8, 1, val2)
  165. // Probability
  166. var prob1 = label.new(na, na, na
  167. , color = color(na)
  168. , textcolor = chart.fg_color
  169. , style = label.style_label_left)
  170. var prob2 = label.new(na, na, na
  171. , color = color(na)
  172. , textcolor = chart.fg_color
  173. , style = label.style_label_left)
  174. var float bull_prob = 0.0
  175. var float bear_prob = 0.0
  176. var float[] bull_hist = array.new_float()
  177. var float[] bear_hist = array.new_float()
  178. if barstate.islast
  179. str1 = pos < 0 ? array.get(txt, 0) : array.get(txt, 1)
  180. label.set_xy(prob1, n, pup)
  181. label.set_text(prob1, str1)
  182. str2 = pos > 0 ? array.get(txt, 0) : array.get(txt, 1)
  183. label.set_xy(prob2, n, pdn)
  184. label.set_text(prob2, str2)
  185. // Split the strings to pull out percentage and convert to integer
  186. str1_split1 = str.split(str1, ':')
  187. str1_split2 = str.split(array.get(str1_split1, 1), '%')
  188. bull_prob := str.tonumber(array.get(str1_split2, 0)) // Need to access this value for all historical bars
  189. str2_split1 = str.split(str2, ':')
  190. str2_split2 = str.split(array.get(str2_split1, 1), '%')
  191. bear_prob := str.tonumber(array.get(str2_split2, 0)) // Need to access this value for all historical bars
  192. array.push(bull_hist, bull_prob)
  193. array.push(bear_hist, bear_prob)
  194. bull_prob_up = bull_prob > bear_prob
  195. bear_prob_dn = bear_prob > bull_prob
  196. plotchar(bull_prob, 'Bull Prob', color=color.new(#ffffff, 100))
  197. plotchar(bear_prob, 'Bear Prob', color=color.new(#ffffff, 100))
  198. plotshape(close > open and bull_prob_up
  199. , title = 'Type 1 - Buy'
  200. , style = shape.triangleup
  201. , size = size.small
  202. , location = location.belowbar
  203. , color = color.green)
  204. plotshape(close < open and bear_prob_dn
  205. , title = 'Type 1 - Sell'
  206. , style = shape.triangledown
  207. , size = size.small
  208. , location = location.abovebar
  209. , color = color.green)

I have tried more things than I can count or remember. I an not set on any particular method. Any way that can produce the intended result is fine by me “数组中的数据仅存在于当前柱形。如何引用历史数值?”

答案1

得分: 0

使用for循环和array.get(id, index)来获取数组的历史数值。

英文:

Use for loops and array.get(id, index) to get the historical values of the array.

答案2

得分: 0

以下是您提供的代码的翻译部分:

  1. //@version=5
  2. indicator("概率分析器", overlay = true)
  3. import HeWhoMustNotBeNamed/Logger/2 as log
  4. var logger = log.Logger.new(minimumLevel='Trace', textSize = size.normal)
  5. logger.init()
  6. // 工具提示
  7. string t1 = "设置枢轴期间"
  8. string t2 = "设置响应期间。低值返回短期结构,高值返回长期结构。如果禁用此选项,则将使用上面的枢轴长度。"
  9. // 输入
  10. int pa_len = input.int(defval = 20
  11. , title = "结构期间"
  12. , minval = 1
  13. , tooltip = t1)
  14. int pa_res = input.int(defval = 7
  15. , title = "结构响应"
  16. , minval = 1
  17. , tooltip = t2)
  18. n = bar_index
  19. var pup = float(na)
  20. var pdn = float(na)
  21. var piup = int(na)
  22. var pidn = int(na)
  23. // 矩阵和数组
  24. var vals = matrix.new<float>(9, 4, 0.0)
  25. var string [] txt = array.new<string>(2, "")
  26. // 函数
  27. Current(v)=>
  28. str = ""
  29. val1 = float(na)
  30. val2 = float(na)
  31. if v >= 0
  32. if v == 1
  33. str := "短信: "
  34. val1 := matrix.get(vals, 0, 1)
  35. val2 := matrix.get(vals, 0, 3)
  36. else if v == 2
  37. str := "BMS: "
  38. val1 := matrix.get(vals, 1, 1)
  39. val2 := matrix.get(vals, 1, 3)
  40. else if v > 2
  41. str := "BMS: "
  42. val1 := matrix.get(vals, 2, 1)
  43. val2 := matrix.get(vals, 2, 3)
  44. else if v < 0
  45. if v == -1
  46. str := "短信: "
  47. val1 := matrix.get(vals, 3, 1)
  48. val2 := matrix.get(vals, 3, 3)
  49. else if v == -2
  50. str := "BMS: "
  51. val1 := matrix.get(vals, 4, 1)
  52. val2 := matrix.get(vals, 4, 3)
  53. else if v < -2
  54. str := "BMS: "
  55. val1 := matrix.get(vals, 5, 1)
  56. val2 := matrix.get(vals, 5, 3)
  57. [str, val1, val2]
  58. // ~~ 枢轴 {
  59. pup := math.max(pup[1], high)
  60. pdn := math.min(pdn[1], low)
  61. pa_high = ta.pivothigh(high, pa_len, pa_len)
  62. pa_low = ta.pivotlow(low, pa_len, pa_len)
  63. if pa_high
  64. pup := pa_high
  65. else if pa_low
  66. pdn := pa_low
  67. // 结构
  68. var pos = 0
  69. if pup > pup[1]
  70. piup := n
  71. centerBull = math.round(math.avg(piup[1] ,n))
  72. if pos <= 0
  73. pos := 1
  74. matrix.set(vals, 6, 0, matrix.get(vals, 6, 0) + 1)
  75. else if pos == 1 and pup > pup[1] and pup[1] == pup[pa_res]
  76. pos := 2
  77. matrix.set(vals, 6, 1,matrix.get(vals ,6 ,1) + 1)
  78. else if pos > 1 and pup > pup[1] and pup[1] == pup[pa_res]
  79. pos := pos + 1
  80. matrix.set(vals ,6 ,2,matrix.get(vals, 6, 2) + 1)
  81. else if pup < pup[1]
  82. piup := n - pa_len
  83. if pdn < pdn[1]
  84. pidn := n
  85. centerBear = math.round(math.avg(pidn[1], n))
  86. if pos >= 0
  87. pos := -1
  88. matrix.set(vals, 7, 0, matrix.get(vals, 7, 0) + 1)
  89. else if pos == -1 and pdn < pdn[1] and pdn[1] == pdn[pa_res]
  90. pos := -2
  91. matrix.set(vals, 7, 1, matrix.get(vals, 7, 1) + 1)
  92. else if pos < -1 and pdn < pdn[1] and pdn[1] == pdn[pa_res]
  93. pos := pos - 1
  94. matrix.set(vals, 7, 2, matrix.get(vals, 7, 2) + 1)
  95. else if pdn > pdn[1]
  96. pidn := n - pa_len
  97. var float[] val1_hist = array.new_float(n+1)
  98. var float[] val2_hist = array.new_float(n+1)
  99. // 概率计算
  100. if ta.change(pos)
  101. if pos > 0 and pos[1] > 0 or pos < 0 and pos[1] < 0
  102. if matrix.get(vals, 8, 0) < matrix.get(vals, 8, 1)
  103. matrix.set(vals, 8, 2, matrix.get(vals, 8, 2) + 1)
  104. else
  105. matrix.set(vals, 8, 3, matrix.get(vals, 8, 3) + 1)
  106. else
  107. if matrix.get(vals, 8, 0) > matrix.get(vals, 8, 1)
  108. matrix.set(vals, 8, 2, matrix.get(vals, 8, 2) + 1)
  109. else
  110. matrix.set(vals,8, 3, matrix.get(vals, 8, 3) + 1)
  111. // 分数计算
  112. float buC0 = matrix.get(vals, 0, 0), float buC1 = matrix.get(vals, 0, 2)
  113. float buS0 = matrix.get(vals, 1, 0), float buS1 = matrix.get(vals, 1, 2)
  114. float buB0 = matrix.get(vals, 2, 0), float buB1 = matrix.get(vals, 2, 2)
  115. float beC0 = matrix.get(vals, 3, 0), float beC1 = matrix.get(vals, 3, 2)
  116. float beS0 = matrix.get(vals, 4, 0), float beS1 = matrix.get(vals, 4, 2)
  117. float beB0 = matrix.get(vals, 5, 0), float beB1 = matrix.get
  118. <details>
  119. <summary>英文:</summary>
  120. //@version=5
  121. indicator(&quot;Probability Analyzer&quot;, overlay = true)
  122. import HeWhoMustNotBeNamed/Logger/2 as log
  123. var logger = log.Logger.new(minimumLevel=&#39;Trace&#39;, textSize = size.normal)
  124. logger.init()
  125. // Tooltips
  126. string t1 = &quot;Set the pivot period&quot;
  127. string t2 = &quot;Set the response period. A low value returns a short-term structure and a high value returns a long-term structure.
  128. If you disable this option the pivot length above will be used.&quot;
  129. // Inputs
  130. int pa_len = input.int(defval = 20
  131. , title = &quot;Structure Period&quot;
  132. , minval = 1
  133. , tooltip = t1)
  134. int pa_res = input.int(defval = 7
  135. , title = &quot;Structure Response&quot;
  136. , minval = 1
  137. , tooltip = t2)
  138. n = bar_index
  139. var pup = float(na)
  140. var pdn = float(na)
  141. var piup = int(na)
  142. var pidn = int(na)
  143. // Matrix &amp; Array
  144. var vals = matrix.new&lt;float&gt;(9, 4, 0.0)
  145. var string [] txt = array.new&lt;string&gt;(2, &quot;&quot;)
  146. // Functions
  147. Current(v)=&gt;
  148. str = &quot;&quot;
  149. val1 = float(na)
  150. val2 = float(na)
  151. if v &gt;= 0
  152. if v == 1
  153. str := &quot;SMS: &quot;
  154. val1 := matrix.get(vals, 0, 1)
  155. val2 := matrix.get(vals, 0, 3)
  156. else if v == 2
  157. str := &quot;BMS: &quot;
  158. val1 := matrix.get(vals, 1, 1)
  159. val2 := matrix.get(vals, 1, 3)
  160. else if v &gt; 2
  161. str := &quot;BMS: &quot;
  162. val1 := matrix.get(vals, 2, 1)
  163. val2 := matrix.get(vals, 2, 3)
  164. else if v &lt;= 0
  165. if v == -1
  166. str := &quot;SMS: &quot;
  167. val1 := matrix.get(vals, 3, 1)
  168. val2 := matrix.get(vals, 3, 3)
  169. else if v == -2
  170. str := &quot;BMS: &quot;
  171. val1 := matrix.get(vals, 4, 1)
  172. val2 := matrix.get(vals, 4, 3)
  173. else if v &lt; -2
  174. str := &quot;BMS: &quot;
  175. val1 := matrix.get(vals, 5, 1)
  176. val2 := matrix.get(vals, 5, 3)
  177. [str, val1, val2]
  178. // ~~ Pivots {
  179. pup := math.max(pup[1], high)
  180. pdn := math.min(pdn[1], low)
  181. pa_high = ta.pivothigh(high, pa_len, pa_len)
  182. pa_low = ta.pivotlow(low, pa_len, pa_len)
  183. if pa_high
  184. pup := pa_high
  185. else if pa_low
  186. pdn := pa_low
  187. // Structure
  188. var pos = 0
  189. if pup &gt; pup[1]
  190. piup := n
  191. centerBull = math.round(math.avg(piup[1] ,n))
  192. if pos &lt;= 0
  193. pos := 1
  194. matrix.set(vals, 6, 0, matrix.get(vals, 6, 0) + 1)
  195. else if pos == 1 and pup &gt; pup[1] and pup[1] == pup[pa_res]
  196. pos := 2
  197. matrix.set(vals, 6, 1,matrix.get(vals ,6 ,1) + 1)
  198. else if pos &gt; 1 and pup &gt; pup[1] and pup[1] == pup[pa_res]
  199. pos := pos + 1
  200. matrix.set(vals ,6 ,2,matrix.get(vals, 6, 2) + 1)
  201. else if pup &lt; pup[1]
  202. piup := n - pa_len
  203. if pdn &lt; pdn[1]
  204. pidn := n
  205. centerBear = math.round(math.avg(pidn[1], n))
  206. if pos &gt;= 0
  207. pos := -1
  208. matrix.set(vals, 7, 0, matrix.get(vals, 7, 0) + 1)
  209. else if pos == -1 and pdn &lt; pdn[1] and pdn[1] == pdn[pa_res]
  210. pos := -2
  211. matrix.set(vals, 7, 1, matrix.get(vals, 7, 1) + 1)
  212. else if pos &lt; -1 and pdn &lt; pdn[1] and pdn[1] == pdn[pa_res]
  213. pos := pos - 1
  214. matrix.set(vals, 7, 2, matrix.get(vals, 7, 2) + 1)
  215. else if pdn &gt; pdn[1]
  216. pidn := n - pa_len
  217. var float[] val1_hist = array.new_float(n+1)
  218. var float[] val2_hist = array.new_float(n+1)
  219. // Probability Calculation
  220. if ta.change(pos)
  221. if pos &gt; 0 and pos[1] &gt; 0 or pos &lt; 0 and pos[1] &lt; 0
  222. if matrix.get(vals, 8, 0) &lt; matrix.get(vals, 8, 1)
  223. matrix.set(vals, 8, 2, matrix.get(vals, 8, 2) + 1)
  224. else
  225. matrix.set(vals, 8, 3, matrix.get(vals, 8, 3) + 1)
  226. else
  227. if matrix.get(vals, 8, 0) &gt; matrix.get(vals, 8, 1)
  228. matrix.set(vals, 8, 2, matrix.get(vals, 8, 2) + 1)
  229. else
  230. matrix.set(vals,8, 3, matrix.get(vals, 8, 3) + 1)
  231. //Score Calulation
  232. float buC0 = matrix.get(vals, 0, 0), float buC1 = matrix.get(vals, 0, 2)
  233. float buS0 = matrix.get(vals, 1, 0), float buS1 = matrix.get(vals, 1, 2)
  234. float buB0 = matrix.get(vals, 2, 0), float buB1 = matrix.get(vals, 2, 2)
  235. float beC0 = matrix.get(vals, 3, 0), float beC1 = matrix.get(vals, 3, 2)
  236. float beS0 = matrix.get(vals, 4, 0), float beS1 = matrix.get(vals, 4, 2)
  237. float beB0 = matrix.get(vals, 5, 0), float beB1 = matrix.get(vals, 5, 2)
  238. float tbuC = matrix.get(vals, 6, 0), float tbuS = matrix.get(vals, 6, 1)
  239. float tbuB = matrix.get(vals, 6, 2), float tbeC = matrix.get(vals, 7, 0)
  240. float tbeS = matrix.get(vals, 7, 1), float tbeB = matrix.get(vals, 7, 2)
  241. // Bull
  242. if (pos[1] == 1 or pos[1] == 0) and pos &lt; 0
  243. matrix.set(vals, 0, 0, buC0 + 1)
  244. matrix.set(vals, 0, 1, math.round(((buC0 + 1) / tbuC) * 100, 2))
  245. if (pos[1] == 1 or pos[1] == 0) and pos == 2
  246. matrix.set(vals, 0, 2, buC1 +1 )
  247. matrix.set(vals, 0, 3, math.round(((buC1 + 1) / tbuC) * 100, 2))
  248. if pos[1] == 2 and pos &lt; 0
  249. matrix.set(vals, 1, 0, buS0 + 1)
  250. matrix.set(vals, 1, 1, math.round(((buS0 +1 ) / tbuS) * 100, 2))
  251. if pos[1] == 2 and pos &gt; 2
  252. matrix.set(vals, 1, 2, buS1 + 1)
  253. matrix.set(vals, 1, 3, math.round(((buS1 + 1) / tbuS) * 100, 2))
  254. if pos[1] &gt; 2 and pos &lt; 0
  255. matrix.set(vals, 2, 0, buB0 + 1)
  256. matrix.set(vals, 2, 1, math.round(((buB0 + 1) / tbuB) * 100, 2))
  257. if pos[1] &gt; 2 and pos &gt; pos[1]
  258. matrix.set(vals, 2, 2, buB1 + 1)
  259. matrix.set(vals, 2, 3, math.round(((buB1 + 1) / tbuB) * 100, 2))
  260. // Bear
  261. if (pos[1] == -1 or pos[1] == 0) and pos &gt; 0
  262. matrix.set(vals, 3, 0, beC0 + 1)
  263. matrix.set(vals, 3, 1, math.round(((beC0 + 1) / tbeC) * 100, 2))
  264. if (pos[1] == -1 or pos[1] == 0) and pos == -2
  265. matrix.set(vals, 3, 2, beC1 + 1)
  266. matrix.set(vals, 3, 3, math.round(((beC1 + 1) / tbeC) * 100, 2))
  267. if pos[1] == -2 and pos &gt; 0
  268. matrix.set(vals, 4, 0, beS0 + 1)
  269. matrix.set(vals, 4, 1, math.round(((beS0 + 1) / tbeS) * 100, 2))
  270. if pos[1] == -2 and pos &lt; -2
  271. matrix.set(vals, 4, 2, beS1 + 1)
  272. matrix.set(vals, 4, 3, math.round(((beS1 + 1) / tbeS) * 100, 2))
  273. if pos[1] &lt; -2 and pos &gt; 0
  274. matrix.set(vals, 5, 0, beB0 + 1)
  275. matrix.set(vals, 5, 1, math.round(((beB0 + 1) / tbeB) * 100, 2))
  276. if pos[1] &lt; -2 and pos &lt; pos[1]
  277. matrix.set(vals, 5, 2, beB1 + 1)
  278. matrix.set(vals, 5, 3, math.round(((beB1 + 1) / tbeB) * 100, 2))
  279. [str, val1, val2] = Current(pos)
  280. array.set(txt, 0, &quot;CHoCH: &quot; + str.tostring(val1, format.percent))
  281. array.set(txt, 1, str + str.tostring(val2, format.percent))
  282. matrix.set(vals, 8, 0, val1)
  283. matrix.set(vals, 8, 1, val2)
  284. array.push(val1_hist, val1)
  285. array.push(val2_hist, val2)
  286. logger.info(str.tostring(array.size(val1_hist)))
  287. // Probability
  288. var prob1 = label.new(na, na, na
  289. , color = color(na)
  290. , textcolor = chart.fg_color
  291. , style = label.style_label_left)
  292. var prob2 = label.new(na, na, na
  293. , color = color(na)
  294. , textcolor = chart.fg_color
  295. , style = label.style_label_left)
  296. if barstate.islast
  297. str1 = pos &lt; 0 ? array.get(txt, 0) : array.get(txt, 1)
  298. label.set_xy(prob1, n, pup)
  299. label.set_text(prob1, str1)
  300. str2 = pos &gt; 0 ? array.get(txt, 0) : array.get(txt, 1)
  301. label.set_xy(prob2, n, pdn)
  302. label.set_text(prob2, str2)
  303. int val1_size = array.size(val1_hist) -1
  304. int val2_size = array.size(val2_hist) -1
  305. float bear_val = pos &gt; 0 ? array.get(val1_hist, val1_size) : array.get(val2_hist, val2_size)
  306. float bull_val = pos &lt; 0 ? array.get(val1_hist, val1_size) : array.get(val2_hist, val2_size)
  307. bull_prob = bull_val &gt; bear_val
  308. bear_prob = bear_val &gt; bull_val
  309. plotshape(close &gt; open and bull_prob
  310. , title = &#39;Type 1 - Buy&#39;
  311. , style = shape.triangleup
  312. , size = size.small
  313. , location = location.belowbar
  314. , color = color.green)
  315. plotshape(close &lt; open and bear_prob
  316. , title = &#39;Type 1 - Sell&#39;
  317. , style = shape.triangledown
  318. , size = size.small
  319. , location = location.abovebar
  320. , color = color.green)
  321. plotchar(bull_val, &#39;Bull Prob&#39;, color=color.new(#ffffff, 100))
  322. plotchar(bear_val, &#39;Bear Prob&#39;, color=color.new(#ffffff, 100))
  323. </details>

huangapple
  • 本文由 发表于 2023年5月7日 00:05:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定