如何向 Pine 脚本代码中添加简单函数

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

How can I add a simple function to a pine script code

问题

我需要帮忙添加此功能,因为我对松树脚本仍然处于起步阶段。该指标寻找公平的间隙值,但它给出了太多间隙,所以我想通过寻找特定的蜡烛图案来限制这些间隙,例如...我希望指标只显示来自绿蜡烛、红蜡烛、红蜡烛...或红蜡烛、红蜡烛、红蜡烛的间隙...我只想为特定的蜡烛图案进行回测,这就是为什么它需要这个功能。

谢谢提前帮助,这是代码。

英文:

I need help in adding this function as I am still at the beginning with pine script.
This indicator looks for fair gap value but it gives to many gaps so I want to limit those gaps by looking for a specific candle pattern, for example...I want the indicator to show me only gaps from green candle, red candle, red candle...or red candle, red candle, red candle...i want to backtest only for a specific candle patter so that s why it needs this feature.

Thanks in advance, here is the code.

  1. //@version=5
  2. indicator("Fair Value Gap", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
  3. // ———————————————————— Inputs {
  4. // Standard practice declared input variables with i_ easier to identify
  5. i_tf = input.timeframe("D", "MTF Timeframe", group = "MTF Settings")
  6. i_mtf = input.string(defval = "Current TF",group = "MTF Settings", title = "MTF Options", options = ["Current TF", "Current + HTF", "HTF"])
  7. i_tfos = input.int(defval = 10,title = "Offset", minval = 0, maxval = 500 ,group = "MTF Settings", inline = "OS")
  8. i_mtfos = input.int(defval = 20,title = "MTF Offset", minval = 0, maxval = 500 ,group = "MTF Settings", inline = "OS")
  9. i_fillByMid = input.bool(false, "MidPoint Fill",group = "General Settings", tooltip = "When enabled FVG is filled when midpoint is tested")
  10. i_deleteonfill = input.bool(true, "Delete Old On Fill",group = "General Settings")
  11. i_labeltf = input.bool(true,"Label FVG Timeframe",group = "General Settings")
  12. i_bullishfvgcolor = input.color(color.new(color.green,90), "Bullish FVG", group = "Coloring", inline = "BLFVG")
  13. i_mtfbullishfvgcolor = input.color(color.new(color.lime,80), "MTF Bullish FVG", group = "Coloring", inline = "BLFVG")
  14. i_bearishfvgcolor = input.color(color.new(color.red,90), "Bearish FVG", group = "Coloring", inline = "BRFVG")
  15. i_mtfbearishfvgcolor = input.color(color.new(color.maroon,80), "MTF Bearish FVG", group = "Coloring", inline = "BRFVG")
  16. i_midPointColor = input.color(color.new(color.white,85), "MidPoint Color", group = "Coloring")
  17. i_textColor = input.color(color.white, "Text Color", group = "Coloring")
  18. // }
  19. // ———————————————————— Global data {
  20. //Using current bar data for HTF highs and lows instead of security to prevent future leaking
  21. var htfH = open
  22. var htfL = open
  23. if close > htfH
  24. htfH:= close
  25. if close < htfL
  26. htfL := close
  27. //Security Data, used for HTF Bar Data reference
  28. sClose = request.security(syminfo.tickerid, i_tf, close[1], barmerge.gaps_off, barmerge.lookahead_on)
  29. sHighP2 = request.security(syminfo.tickerid, i_tf, high[2], barmerge.gaps_off, barmerge.lookahead_on)
  30. sLowP2 = request.security(syminfo.tickerid, i_tf, low[2], barmerge.gaps_off, barmerge.lookahead_on)
  31. sOpen = request.security(syminfo.tickerid, i_tf, open[1], barmerge.gaps_off, barmerge.lookahead_on)
  32. sBar = request.security(syminfo.tickerid, i_tf, bar_index, barmerge.gaps_off, barmerge.lookahead_on)
  33. // }
  34. //var keyword can be used to hold data in memory, with pinescript all data is lost including variables unless the var keyword is used to preserve this data
  35. var bullishgapholder = array.new_box(0)
  36. var bearishgapholder = array.new_box(0)
  37. var bullishmidholder = array.new_line(0)
  38. var bearishmidholder = array.new_line(0)
  39. var bullishlabelholder = array.new_label(0)
  40. var bearishlabelholder = array.new_label(0)
  41. var transparentcolor = color.new(color.white,100)
  42. // ———————————————————— Functions {
  43. //function paramaters best declared with '_' this helps defer from variables in the function scope declaration and elsewhere e.g. close => _close
  44. f_gapCreation(_upperlimit,_lowerlimit,_midlimit,_bar,_boxholder,_midholder,_labelholder,_boxcolor,_mtfboxcolor, _htf)=>
  45. timeholder = str.tostring(i_tf)
  46. offset = i_mtfos
  47. boxbgcolor = _mtfboxcolor
  48. if _htf == false
  49. timeholder := str.tostring(timeframe.period)
  50. offset := i_tfos
  51. boxbgcolor := _boxcolor
  52. array.push(_boxholder,box.new(_bar,_upperlimit,_bar+1,_lowerlimit,border_color=transparentcolor,bgcolor = boxbgcolor, extend = extend.right))
  53. if i_fillByMid
  54. array.push(_midholder,line.new(_bar,_midlimit,_bar+1,_midlimit,color = i_midPointColor, extend = extend.right))
  55. if i_labeltf
  56. array.push(_labelholder,label.new(_bar+ offset,_midlimit * 0.999, text = timeholder + " FVG", style =label.style_none, size = size.normal, textcolor = i_textColor))
  57. //checks for gap between current candle and 2 previous candle e.g. low of current candle and high of the candle before last, this is the fair value gap.
  58. f_gapLogic(_close,_high,_highp2,_low,_lowp2,_open,_bar,_htf)=>
  59. if _open > _close
  60. if _high - _lowp2 < 0
  61. upperlimit = _close - (_close - _lowp2 )
  62. lowerlimit = _close - (_close-_high)
  63. midlimit = (upperlimit + lowerlimit) / 2
  64. f_gapCreation(upperlimit,lowerlimit,midlimit,_bar,bullishgapholder,bullishmidholder,bullishlabelholder,i_bullishfvgcolor,i_mtfbullishfvgcolor,_htf)
  65. else
  66. if _low - _highp2 > 0
  67. upperlimit = _close - (_close-_low)
  68. lowerlimit = _close- (_close - _highp2),
  69. midlimit = (upperlimit + lowerlimit) / 2
  70. f_gapCreation(upperlimit,lowerlimit,midlimit,_bar,bearishgapholder,bearishmidholder,bearishlabelholder,i_bearishfvgcolor,i_mtfbearishfvgcolor,_htf)
  71. //Used to remove the gap from its relevant array as a result of it being filled.
  72. f_gapDeletion(_currentgap,_i,_boxholder,_midholder,_labelholder)=>
  73. array.remove(_boxholder,_i)
  74. if i_fillByMid
  75. currentmid=array.get(_midholder,_i)
  76. array.remove(_midholder,_i)
  77. if i_deleteonfill
  78. line.delete(currentmid)
  79. else
  80. line.set_extend(currentmid, extend.none)
  81. line.set_x2(currentmid,bar_index)
  82. if i_deleteonfill
  83. box.delete(_currentgap)
  84. else
  85. box.set_extend(_currentgap,extend.none)
  86. box.set_right(_currentgap,bar_index)
  87. if i_labeltf
  88. currentlabel=array.get(_labelholder,_i)
  89. array.remove(_labelholder,_i)
  90. if i_deleteonfill
  91. label.delete(currentlabel)
  92. //checks if gap has been filled either by 0.5 fill (i_fillByMid) or SHRINKS the gap to reflect the true value gap left.
  93. f_gapCheck(_high,_low)=>
  94. if array.size(bullishgapholder) > 0
  95. for i = array.size(bullishgapholder)-1 to 0
  96. currentgap = array.get(bullishgapholder,i)
  97. currenttop = box.get_top(currentgap)
  98. if i_fillByMid
  99. currentmid = array.get(bullishmidholder,i)
  100. currenttop := line.get_y1(currentmid)
  101. if _high >= currenttop
  102. f_gapDeletion(currentgap,i,bullishgapholder,bullishmidholder,bullishlabelholder)
  103. if _high > box.get_bottom(currentgap) and _high < box.get_top(currentgap)
  104. box.set_bottom(currentgap,_high)
  105. if array.size(bearishgapholder) > 0
  106. for i = array.size(bearishgapholder)-1 to 0
  107. currentgap = array.get(bearishgapholder,i)
  108. currentbottom = box.get_bottom(currentgap)
  109. if i_fillByMid
  110. currentmid = array.get(bearishmidholder,i)
  111. currentbottom := line.get_y1(currentmid)
  112. if _low <= currentbottom
  113. f_gapDeletion(currentgap,i,bearishgapholder,bearishmidholder,bearishlabelholder)
  114. if _low < box.get_top(currentgap) and _low > box.get_bottom(currentgap)
  115. box.set_top(currentgap,_low)
  116. // pine provided function to determine a new bar
  117. is_newbar(res) =>
  118. t = time(res)
  119. not na(t) and (na(t[1]) or t > t[1])
  120. if is_newbar(i_tf)
  121. htfH := open
  122. htfL := open
  123. // }
  124. // User Input, allow MTF data calculations
  125. if is_newbar(i_tf) and (i_mtf == "Current + HTF" or i_mtf == "HTF")
  126. f_gapLogic(sClose,htfH,sHighP2,htfL,sLowP2,sOpen,bar_index,true)
  127. // Use current Timeframe data to provide gap logic
  128. if (i_mtf == "Current + HTF" or i_mtf == "Current TF")
  129. f_gapLogic(close[1],high,high[2],low,low[2],open[1],bar_index,false)
  130. f_gapCheck(high,low)

I have tried to look for some tutorials on how to do it but all attempts failed because it s a bit too complex for my knowledge.

答案1

得分: 0

在以下脚本中,我只添加了根据3根绿色蜡烛或3根红色蜡烛以及图表时间帧进行筛选的过滤器。

你可以在这个优秀的脚本中找到更多图案的代码:进入链接描述

  1. //@version=5
  2. 指标("公平价值间隙", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
  3. // ———————————————————— 输入 {
  4. // 使用标准惯例声明带有 i_ 前缀的输入变量,更容易识别
  5. i_tf = input.timeframe("D", "MTF 时间框架", group = "MTF 设置")
  6. i_mtf = input.string(defval = "当前 TF",group = "MTF 设置", title = "MTF 选项", options = ["当前 TF", "当前 + HTF", "HTF"])
  7. i_tfos = input.int(defval = 10,title = "偏移量", minval = 0, maxval = 500 ,group = "MTF 设置", inline = "OS")
  8. i_mtfos = input.int(defval = 20,title = "MTF 偏移量", minval = 0, maxval = 500 ,group = "MTF 设置", inline = "OS")
  9. i_fillByMid = input.bool(false, "中间点填充",group = "通用设置", tooltip = "启用时,当测试中点时填充 FVG")
  10. i_deleteonfill = input.bool(true, "填充后删除旧数据",group = "通用设置")
  11. i_labeltf = input.bool(true,"标记 FVG 时间框架",group = "通用设置")
  12. i_candlePattern = input.string(group = "通过蜡烛图案筛选", defval = "无", title = "按蜡烛图案筛选", options = ["无", "cpGgg", "cpRrr"])
  13. i_bullishfvgcolor = input.color(color.new(color.green,90), "看涨 FVG", group = "着色", inline = "BLFVG")
  14. i_mtfbullishfvgcolor = input.color(color.new(color.lime,80), "MTF 看涨 FVG", group = "着色", inline = "BLFVG")
  15. i_bearishfvgcolor = input.color(color.new(color.red,90), "看跌 FVG", group = "着色", inline = "BRFVG")
  16. i_mtfbearishfvgcolor = input.color(color.new(color.maroon,80), "MTF 看跌 FVG", group = "着色", inline = "BRFVG")
  17. i_midPointColor = input.color(color.new(color.white,85), "中点颜色", group = "着色")
  18. i_textColor = input.color(color.white, "文本颜色", group = "着色")
  19. // }
  20. // ———————————————————— 全局数据 {
  21. // 使用当前柱数据作为 HTF 高点和低点,而不是使用安全性来防止未来泄漏
  22. var htfH = open
  23. var htfL = open
  24. if close > htfH
  25. htfH:= close
  26. if close < htfL
  27. htfL := close
  28. // 使用 HTF 条数据引用的安全数据
  29. sClose = request.security(syminfo.tickerid, i_tf, close[1], barmerge.gaps_off, barmerge.lookahead_on)
  30. sHighP2 = request.security(syminfo.tickerid, i_tf, high[2], barmerge.gaps_off, barmerge.lookahead_on)
  31. sLowP2 = request.security(syminfo.tickerid, i_tf, low[2], barmerge.gaps_off, barmerge.lookahead_on)
  32. sOpen = request.security(syminfo.tickerid, i_tf, open[1], barmerge.gaps_off, barmerge.lookahead_on)
  33. sBar = request.security(syminfo.tickerid, i_tf, bar_index, barmerge.gaps_off, barmerge.lookahead_on)
  34. // }
  35. // ...
英文:

In the script below I added filter just by 3 green candles or 3 red candles and only for the chart timeframe.

You can find code for more patterns, e.g., in this great script: enter link description here

  1. //@version=5
  2. indicator("Fair Value Gap", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
  3. // ———————————————————— Inputs {
  4. // Standard practice declared input variables with i_ easier to identify
  5. i_tf = input.timeframe("D", "MTF Timeframe", group = "MTF Settings")
  6. i_mtf = input.string(defval = "Current TF",group = "MTF Settings", title = "MTF Options", options = ["Current TF", "Current + HTF", "HTF"])
  7. i_tfos = input.int(defval = 10,title = "Offset", minval = 0, maxval = 500 ,group = "MTF Settings", inline = "OS")
  8. i_mtfos = input.int(defval = 20,title = "MTF Offset", minval = 0, maxval = 500 ,group = "MTF Settings", inline = "OS")
  9. i_fillByMid = input.bool(false, "MidPoint Fill",group = "General Settings", tooltip = "When enabled FVG is filled when midpoint is tested")
  10. i_deleteonfill = input.bool(true, "Delete Old On Fill",group = "General Settings")
  11. i_labeltf = input.bool(true,"Label FVG Timeframe",group = "General Settings")
  12. i_candlePattern = input.string(group = "Filter by candle patterns", defval = "None", title = "Filter by candle pattern", options = ["None", "cpGgg", "cpRrr"])
  13. i_bullishfvgcolor = input.color(color.new(color.green,90), "Bullish FVG", group = "Coloring", inline = "BLFVG")
  14. i_mtfbullishfvgcolor = input.color(color.new(color.lime,80), "MTF Bullish FVG", group = "Coloring", inline = "BLFVG")
  15. i_bearishfvgcolor = input.color(color.new(color.red,90), "Bearish FVG", group = "Coloring", inline = "BRFVG")
  16. i_mtfbearishfvgcolor = input.color(color.new(color.maroon,80), "MTF Bearish FVG", group = "Coloring", inline = "BRFVG")
  17. i_midPointColor = input.color(color.new(color.white,85), "MidPoint Color", group = "Coloring")
  18. i_textColor = input.color(color.white, "Text Color", group = "Coloring")
  19. // }
  20. // ———————————————————— Global data {
  21. //Using current bar data for HTF highs and lows instead of security to prevent future leaking
  22. var htfH = open
  23. var htfL = open
  24. if close > htfH
  25. htfH:= close
  26. if close < htfL
  27. htfL := close
  28. //Security Data, used for HTF Bar Data reference
  29. sClose = request.security(syminfo.tickerid, i_tf, close[1], barmerge.gaps_off, barmerge.lookahead_on)
  30. sHighP2 = request.security(syminfo.tickerid, i_tf, high[2], barmerge.gaps_off, barmerge.lookahead_on)
  31. sLowP2 = request.security(syminfo.tickerid, i_tf, low[2], barmerge.gaps_off, barmerge.lookahead_on)
  32. sOpen = request.security(syminfo.tickerid, i_tf, open[1], barmerge.gaps_off, barmerge.lookahead_on)
  33. sBar = request.security(syminfo.tickerid, i_tf, bar_index, barmerge.gaps_off, barmerge.lookahead_on)
  34. // }
  35. //var keyword can be used to hold data in memory, with pinescript all data is lost including variables unless the var keyword is used to preserve this data
  36. var bullishgapholder = array.new_box(0)
  37. var bearishgapholder = array.new_box(0)
  38. var bullishmidholder = array.new_line(0)
  39. var bearishmidholder = array.new_line(0)
  40. var bullishlabelholder = array.new_label(0)
  41. var bearishlabelholder = array.new_label(0)
  42. var transparentcolor = color.new(color.white,100)
  43. // ———————————————————— Candle patterns {
  44. cpRrr = close < open and close[1] < open[1] and close[2] < open[2]
  45. cpGgg = close > open and close[1] > open[1] and close[2] > open[2]
  46. cpFilter = switch i_candlePattern
  47. "cpGgg" => cpGgg
  48. "cpRrr" => cpRrr
  49. => true
  50. // END OF candle patterns }
  51. // ———————————————————— Functions {
  52. //function paramaters best declared with '_' this helps defer from variables in the function scope declaration and elsewhere e.g. close => _close
  53. f_gapCreation(_upperlimit,_lowerlimit,_midlimit,_bar,_boxholder,_midholder,_labelholder,_boxcolor,_mtfboxcolor, _htf)=>
  54. timeholder = str.tostring(i_tf)
  55. offset = i_mtfos
  56. boxbgcolor = _mtfboxcolor
  57. if _htf == false
  58. timeholder := str.tostring(timeframe.period)
  59. offset := i_tfos
  60. boxbgcolor := _boxcolor
  61. array.push(_boxholder,box.new(_bar,_upperlimit,_bar+1,_lowerlimit,border_color=transparentcolor,bgcolor = boxbgcolor, extend = extend.right))
  62. if i_fillByMid
  63. array.push(_midholder,line.new(_bar,_midlimit,_bar+1,_midlimit,color = i_midPointColor, extend = extend.right))
  64. if i_labeltf
  65. array.push(_labelholder,label.new(_bar+ offset,_midlimit * 0.999, text = timeholder + " FVG", style =label.style_none, size = size.normal, textcolor = i_textColor))
  66. //checks for gap between current candle and 2 previous candle e.g. low of current candle and high of the candle before last, this is the fair value gap.
  67. f_gapLogic(_close,_high,_highp2,_low,_lowp2,_open,_bar,_htf)=>
  68. if _open > _close
  69. if _high - _lowp2 < 0
  70. upperlimit = _close - (_close - _lowp2 )
  71. lowerlimit = _close - (_close-_high)
  72. midlimit = (upperlimit + lowerlimit) / 2
  73. f_gapCreation(upperlimit,lowerlimit,midlimit,_bar,bullishgapholder,bullishmidholder,bullishlabelholder,i_bullishfvgcolor,i_mtfbullishfvgcolor,_htf)
  74. else
  75. if _low - _highp2 > 0
  76. upperlimit = _close - (_close-_low)
  77. lowerlimit = _close- (_close - _highp2),
  78. midlimit = (upperlimit + lowerlimit) / 2
  79. f_gapCreation(upperlimit,lowerlimit,midlimit,_bar,bearishgapholder,bearishmidholder,bearishlabelholder,i_bearishfvgcolor,i_mtfbearishfvgcolor,_htf)
  80. //Used to remove the gap from its relevant array as a result of it being filled.
  81. f_gapDeletion(_currentgap,_i,_boxholder,_midholder,_labelholder)=>
  82. array.remove(_boxholder,_i)
  83. if i_fillByMid
  84. currentmid=array.get(_midholder,_i)
  85. array.remove(_midholder,_i)
  86. if i_deleteonfill
  87. line.delete(currentmid)
  88. else
  89. line.set_extend(currentmid, extend.none)
  90. line.set_x2(currentmid,bar_index)
  91. if i_deleteonfill
  92. box.delete(_currentgap)
  93. else
  94. box.set_extend(_currentgap,extend.none)
  95. box.set_right(_currentgap,bar_index)
  96. if i_labeltf
  97. currentlabel=array.get(_labelholder,_i)
  98. array.remove(_labelholder,_i)
  99. if i_deleteonfill
  100. label.delete(currentlabel)
  101. //checks if gap has been filled either by 0.5 fill (i_fillByMid) or SHRINKS the gap to reflect the true value gap left.
  102. f_gapCheck(_high,_low)=>
  103. if array.size(bullishgapholder) > 0
  104. for i = array.size(bullishgapholder)-1 to 0
  105. currentgap = array.get(bullishgapholder,i)
  106. currenttop = box.get_top(currentgap)
  107. if i_fillByMid
  108. currentmid = array.get(bullishmidholder,i)
  109. currenttop := line.get_y1(currentmid)
  110. if _high >= currenttop
  111. f_gapDeletion(currentgap,i,bullishgapholder,bullishmidholder,bullishlabelholder)
  112. if _high > box.get_bottom(currentgap) and _high < box.get_top(currentgap)
  113. box.set_bottom(currentgap,_high)
  114. if array.size(bearishgapholder) > 0
  115. for i = array.size(bearishgapholder)-1 to 0
  116. currentgap = array.get(bearishgapholder,i)
  117. currentbottom = box.get_bottom(currentgap)
  118. if i_fillByMid
  119. currentmid = array.get(bearishmidholder,i)
  120. currentbottom := line.get_y1(currentmid)
  121. if _low <= currentbottom
  122. f_gapDeletion(currentgap,i,bearishgapholder,bearishmidholder,bearishlabelholder)
  123. if _low < box.get_top(currentgap) and _low > box.get_bottom(currentgap)
  124. box.set_top(currentgap,_low)
  125. // pine provided function to determine a new bar
  126. is_newbar(res) =>
  127. t = time(res)
  128. not na(t) and (na(t[1]) or t > t[1])
  129. if is_newbar(i_tf)
  130. htfH := open
  131. htfL := open
  132. // }
  133. // User Input, allow MTF data calculations
  134. if is_newbar(i_tf) and (i_mtf == "Current + HTF" or i_mtf == "HTF")
  135. f_gapLogic(sClose,htfH,sHighP2,htfL,sLowP2,sOpen,bar_index,true)
  136. // Use current Timeframe data to provide gap logic
  137. if (i_mtf == "Current + HTF" or i_mtf == "Current TF") and cpFilter
  138. f_gapLogic(close[1],high,high[2],low,low[2],open[1],bar_index,false)
  139. f_gapCheck(high,low)

huangapple
  • 本文由 发表于 2023年4月19日 18:07:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053241.html
匿名

发表评论

匿名网友

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

确定