设为首页 收藏本站 切换语言

基于RSI的EA,Pine转mq5

| 发表于 2024-10-5 15:25:55 | 显示全部楼层 |复制链接
任务编号:125557 悬赏任务20H币 悬赏任务 : 按雇主需求进行任务后提交稿件,被采纳后即获得佣金。

距截止: 任务已圆满完成 雇主已托管赏金:20 H币
雇主发布需求24.10.05 雇主托管佣金24.10.05 投稿者投稿 雇主开始选稿 任务完成24.10.15

任务大厅共需1个合格投稿 | 每稿将获得20 H币 | 每人交稿次数不限 可多次任务

投诉举报 联系Ta 我来承接 已有0 个投稿 | 已采纳0 稿 | 拒绝0 稿 | 还需要1 稿

任务需求:
不需要实现指标和图表物件,只需要实现下单功能,实现EA。

  1. //@version=5

  2. const int maxDistanceToLastBar = 5000
  3. const int labelCooldown = 8
  4. const int KDELimit = 300

  5. strategy("RSI短线", shorttitle = "RSI短线",overlay = true, max_labels_count = 500)

  6. rsiLengthInput = input.int(14, minval = 1, title="RSI Length", group="RSI Settings")
  7. rsiSourceInput = input.source(close, "Source", group="RSI Settings")

  8. highPivotLen = input.int(21,  "High Pivot Length", minval = 1, group = "Pivots", display = display.none)
  9. lowPivotLen = input.int(21, "Low Pivot Length", minval = 1, group = "Pivots", display = display.none)

  10. activationThresholdStr = input.string("Medium", "Activation Threshold", options = ["High", "Medium", "Low"], group = "KDE", tooltip = "Determines the amount of arrows shown. Higher options will result in more arrows being rendered.")
  11. string KDEKernel = input.string("Gaussian", "Kernel", options=['Uniform', 'Gaussian', 'Sigmoid'], group = "KDE", tooltip = "The kernel function for KDE calculation. Gaussian is a commonly used kernel and is based on normal distribution.")
  12. float KDEBandwidth = input.float(2.71828, "Bandwidth", group = "KDE", tooltip = "This setting sets the smoothness of the KDE function output.")
  13. int KDEStep = input.int(100, "Nº Bins", minval = 1, group = "KDE", tooltip = "The number of elements the KDE Probability array will have. Higher settings will result in greater precision.")
  14. activationThreshold = (activationThresholdStr == "High" ? 0.4 : activationThresholdStr == "Medium" ? 0.25 : 0.15)
  15. probMode = "Sum"
  16. minPadding =  false

  17. bearishColor = input.color(#f23646, "High Pivots", group = "Style", inline = "col", display = display.none)
  18. neutralColor = input.color(color.gray, "Neutral", group = "Style", inline = "col", display = display.none)
  19. bullishColor = input.color(#089981, "Low Pivots", group = "Style", inline = "col", display = display.none)
  20. RSILabelsEnabled = input.bool(true, "RSI Labels", group = "Style")
  21. KDELabelsEnabled = input.bool(true, "KDE Labels", group = "Style")

  22. rsi = ta.rsi(rsiSourceInput, rsiLengthInput)


  23. //#region KDE
  24. gaussian (float distance, float bandwidth = 1.0) => 1.0 / math.sqrt(2.0 * math.pi) * math.pow(math.e, -0.5 * math.pow(distance / bandwidth, 2.0))
  25. uniform (float distance, float bandwidth = 1.0) => (math.abs(distance) > bandwidth) ? 0.0 : 0.5
  26. sigmoid (float distance, float bandwidth = 1.0) => 2.0 / math.pi * (1.0 / (math.pow(math.e, (distance / bandwidth)) + math.pow(math.e, -(distance / bandwidth))))

  27. kde (array<float> arr, string kernel, float bandwidth, int steps) =>
  28.     arrSize = arr.size()
  29.     arrRange = arr.range()
  30.     arrMin = arr.min() - (minPadding ? (arrRange / 2.0) : 0)
  31.     stepCount = arrRange / steps
  32.    
  33.     densityRange = array.new<float>(steps * 2)
  34.     for i = 0 to (steps * 2) - 1
  35.         densityRange.set(i, arrMin + i * stepCount)
  36.    
  37.     xArr = array.new<float>()
  38.     yArr = array.new<float>()
  39.     for i = 0 to densityRange.size() - 1
  40.         float temp = 0
  41.         for j = 0 to arr.size() - 1
  42.             temp += gaussian(densityRange.get(i) - arr.get(j), 1.0 / bandwidth)

  43.         
  44.         xArr.push(densityRange.get(i))
  45.         yArr.push(1.0 / arrSize * temp)
  46.     [xArr, yArr]
  47. //#endregion

  48. //#region Pivots

  49. prefixSum (array<float> arr, int l, int r) =>
  50.     arr.get(r) - (l == 0 ? 0 : arr.get(l - 1))

  51. float MidKDEHigh = na
  52. float MidKDELow = na

  53. var array<float> KDEHighX = na
  54. var array<float> KDEHighY = na
  55. var array<float> KDEHighYSum = array.new<float>()

  56. var array<float> KDELowX = na
  57. var array<float> KDELowY = na
  58. var array<float> KDELowYSum = array.new<float>()

  59. highPivot = ta.pivothigh(highPivotLen, highPivotLen)
  60. lowPivot = ta.pivotlow(lowPivotLen, lowPivotLen)

  61. var highPivotRSIs = array.new<float>()
  62. var lowPivotRSIs = array.new<float>()

  63. if not na(highPivot)
  64.     if highPivotRSIs.size() > KDELimit
  65.         highPivotRSIs.remove(0)
  66.     highPivotRSIs.push(rsi[highPivotLen])

  67.     [KDEHighX1, KDEHighY1] = kde(highPivotRSIs, KDEKernel, KDEBandwidth, KDEStep)
  68.     KDEHighX := KDEHighX1
  69.     KDEHighY := KDEHighY1
  70.    
  71.     KDEHighYSum.clear()
  72.     temp = 0.0
  73.     for i = 0 to KDEHighY.size() - 1
  74.         temp += KDEHighY.get(i)
  75.         KDEHighYSum.push(temp)

  76.     MidKDEHigh := array.get(KDEHighX, array.indexof(KDEHighY, array.max(KDEHighY)))

  77. if not na(lowPivot)
  78.     if lowPivotRSIs.size() > KDELimit
  79.         lowPivotRSIs.remove(0)
  80.     lowPivotRSIs.push(rsi[lowPivotLen])

  81.     [KDELowX1, KDELowY1] = kde(lowPivotRSIs, KDEKernel, KDEBandwidth, KDEStep)
  82.     KDELowX := KDELowX1
  83.     KDELowY := KDELowY1

  84.     KDELowYSum.clear()
  85.     temp = 0.0
  86.     for i = 0 to KDELowY.size() - 1
  87.         temp += KDELowY.get(i)
  88.         KDELowYSum.push(temp)

  89.     MidKDELow := array.get(KDELowX, array.indexof(KDELowY, array.max(KDELowY)))
  90. //#endregion


  91. float lowProb = na
  92. float maxLowProb = na
  93. float highProb = na
  94. float maxHighProb = na

  95. if last_bar_index - maxDistanceToLastBar < bar_index
  96.     if highPivotRSIs.size() > 0
  97.         highXIndexL = array.binary_search_leftmost(KDEHighX, rsi)
  98.         highXIndexR = math.min(array.binary_search_rightmost(KDEHighX, rsi), KDEHighX.size() - 1)
  99.         nearestIndex = (math.abs(rsi - KDEHighX.get(highXIndexL)) < math.abs(rsi - KDEHighX.get(highXIndexR))) ? highXIndexL : highXIndexR
  100.         highProb := prefixSum(KDEHighYSum, 0, nearestIndex)
  101.    
  102.     if lowPivotRSIs.size() > 0
  103.         lowXIndexL = array.binary_search_leftmost(KDELowX, rsi)
  104.         lowXIndexR = math.min(array.binary_search_rightmost(KDELowX, rsi), KDELowX.size() - 1)
  105.         nearestIndex = (math.abs(rsi - KDELowX.get(lowXIndexL)) < math.abs(rsi - KDELowX.get(lowXIndexR))) ? lowXIndexL : lowXIndexR
  106.         lowProb := prefixSum(KDELowYSum, nearestIndex, KDELowYSum.size() - 1)


  107. //#region Draw Pivots
  108. color curColor = na
  109. if (not na(KDELowY)) and (not na(KDEHighY))
  110.     if lowProb > KDELowY.sum() * (1.0 - activationThreshold)
  111.         curColor := bullishColor
  112.     else if highProb > KDEHighY.sum() * (1.0 - activationThreshold)
  113.         curColor := bearishColor


  114. long = na(curColor) and curColor[1] == bullishColor and barstate.isconfirmed
  115. short = na(curColor) and curColor[1] == bearishColor and barstate.isconfirmed


  116. if long
  117.     strategy.entry("Long", strategy.long)
  118. if short
  119.     strategy.entry("Short", strategy.short)   
复制代码
举报

评论 使用道具

发新帖
EA交易
您需要登录后才可以评论 登录 | 立即注册

简体中文
繁體中文
English(英语)
日本語(日语)
Deutsch(德语)
Русский язык(俄语)
بالعربية(阿拉伯语)
Türkçe(土耳其语)
Português(葡萄牙语)
ภาษาไทย(泰国语)
한어(朝鲜语/韩语)
Français(法语)