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

Mine Script如何写量化

| 发表于 2025-8-1 10:35:12 | 显示全部楼层 |复制链接
  1. //@version=5
  2. indicator(title='Market Bias (CEREBR) - 市场偏见 (CEREBR)', shorttitle='市场偏见', overlay=true)
  3. //#region -----> 输入、函数
  4. // 函数
  5. //@function 此函数强制输入时间框架不得低于当前图表时间框架
  6. FORCE_CTF(str_tf) =>
  7.     i_tf_sec = timeframe.in_seconds(str_tf)
  8.     ctf_sec = timeframe.in_seconds('')
  9.    
  10.     // 如果输入时间框架低于当前图表时间框架,返回当前时间框架
  11.     i_tf_sec < ctf_sec ? '' : str_tf
  12. //
  13. // 输入
  14. ha_htf = FORCE_CTF(input.timeframe('', '时间框架', tooltip="此时间框架必须等于或大于图表时间框架", group="Heikin Ashi 市场偏见"))
  15. ha_len = input(100, '周期', group="Heikin Ashi 市场偏见")
  16. ha_len2 = input(100, '平滑', group="Heikin Ashi 市场偏见")
  17. show_ha = input.bool(true, "显示 Heikin Ashi K线", inline='ha/mb display', group='显示设置')
  18. show_mb = input.bool(true, "显示市场偏见", inline='ha/mb display', group='显示设置')
  19. col_bull = input.color(color.lime, '颜色:看涨', inline='bull/bear color', group='显示设置', display=display.none)
  20. col_bear = input.color(color.red, '看跌', inline='bull/bear color', group='显示设置', display=display.none)
  21. // 在导入数据时添加偏移以避免前视偏差
  22. indexHighTF = timeframe.in_seconds(ha_htf) == timeframe.in_seconds('') ? 0 : barstate.isrealtime ? 1 : 0
  23. indexCurrTF = timeframe.in_seconds(ha_htf) == timeframe.in_seconds('') ? 0 : barstate.isrealtime ? 0 : 1
  24. //@function 处理从其他时间框架导入数据,同时防止重绘
  25. //@param _resolution (str) : 要导入数据的目标时间框架
  26. //@param _expression (float | int) : 要导入的数据
  27. f_no_repaint_request(string _resolution, _expression) =>
  28.      request.security(syminfo.tickerid, _resolution, _expression[indexHighTF])[indexCurrTF]
  29. //#endregion
  30. //#region -----> 计算
  31. // 平滑 OHLC 值
  32. o = ta.ema(open, ha_len)
  33. c = ta.ema(close, ha_len)
  34. h = ta.ema(high, ha_len)
  35. l = ta.ema(low, ha_len)
  36. // 计算 Heikin Ashi OHLC 值
  37. haclose = f_no_repaint_request(ha_htf, (o + h + l + c) / 4)
  38. xhaopen = f_no_repaint_request(ha_htf, (o + c) / 2)
  39. haopen = na(xhaopen[1]) ? (o + c) / 2 : (xhaopen[1] + haclose[1]) / 2
  40. hahigh = math.max(h, math.max(haopen, haclose))
  41. halow = math.min(l, math.min(haopen, haclose))
  42. // 平滑 Heikin Ashi K线
  43. o2 = f_no_repaint_request(ha_htf, ta.ema(haopen, ha_len2))
  44. c2 = f_no_repaint_request(ha_htf, ta.ema(haclose, ha_len2))
  45. h2 = f_no_repaint_request(ha_htf, ta.ema(hahigh, ha_len2))
  46. l2 = f_no_repaint_request(ha_htf, ta.ema(halow, ha_len2))
  47. ha_avg = (h2 + l2) / 2
  48. // 振荡器
  49. osc_len = input.int(7, "振荡器周期", group="Heikin Ashi 市场偏见")
  50. osc_bias = 100 * (c2 - o2)
  51. osc_smooth = ta.ema(osc_bias, osc_len)
  52. sigcolor = switch
  53.     (osc_bias > 0) and (osc_bias >= osc_smooth) => color.new(col_bull, 35)
  54.     (osc_bias > 0) and (osc_bias < osc_smooth) => color.new(col_bull, 75)
  55.     (osc_bias < 0) and (osc_bias <= osc_smooth) => color.new(col_bear, 35)
  56.     (osc_bias < 0) and (osc_bias > osc_smooth) => color.new(col_bear, 75)
  57.     => color(na)
  58. //#endregion
  59. //#region -----> 绘图、警报
  60. // 绘图
  61. p_h = plot(h2, "偏见高点", color=color(na), display=display.data_window, editable=false)
  62. p_l = plot(l2, "偏见低点", color=color(na), display=display.data_window, editable=false)
  63. p_avg = plot(ha_avg, "偏见平均", color=color(na), display=display.data_window, editable=false)
  64. fill(p_l, p_h, show_mb ? sigcolor : na)
  65. col = o2 > c2 ? col_bear : col_bull
  66. plotcandle(o2, h2, l2, c2, title='平滑 Heikin Ashi', color=col, display=show_ha ? display.pane : display.data_window, editable=false)
  67. // 警报
  68. // 看涨趋势切换(看跌 -> 看涨)
  69. alertcondition(ta.change(ta.change(math.sign(osc_bias)) > 0),
  70.   '看涨趋势切换(看跌 -> 看涨)', '{{exchange}}:{{ticker}}: 趋势现为看涨。')
  71. // 看涨趋势增强
  72. alertcondition(osc_bias > 0 and ta.change(math.sign(osc_bias - osc_smooth)) > 0,
  73.   '看涨趋势增强', '{{exchange}}:{{ticker}}: 看涨趋势现更强。')
  74. // 看涨趋势减弱
  75. alertcondition(osc_bias > 0 and ta.change(math.sign(osc_bias - osc_smooth)) < 0,
  76.   '看涨趋势减弱', '{{exchange}}:{{ticker}}: 看涨趋势现较弱。')
  77. // 看跌趋势切换(看涨 -> 看跌)
  78. alertcondition(ta.change(ta.change(math.sign(osc_bias)) < 0),
  79.   '看跌趋势切换(看涨 -> 看跌)', '{{exchange}}:{{ticker}}: 趋势现为看跌。')
  80. // 看跌趋势增强
  81. alertcondition(osc_bias < 0 and ta.change(math.sign(osc_bias - osc_smooth)) < 0,
  82.   '看跌趋势增强', '{{exchange}}:{{ticker}}: 看跌趋势现更强。')
  83. // 看跌趋势减弱
  84. alertcondition(osc_bias < 0 and ta.change(math.sign(osc_bias - osc_smooth)) > 0,
  85.   '看跌趋势减弱', '{{exchange}}:{{ticker}}: 看跌趋势现较弱。')
  86. //#endregion
  87. // 自定义警报:要启用此功能,请选择并取消以下代码块的注释(Cmd (或 Ctrl) + /),从下一行开始。
  88. // // {
  89. // use_custom_alerts = input.bool(false, '使用自定义警报消息', group='警报消息', display=display.none)
  90. // i_alert_bull_trend_switch = input.text_area('新看涨趋势', '看涨趋势切换', group='警报消息', display=display.none)
  91. // i_alert_bull_trend_strengthen = input.text_area('新强看涨趋势', '看涨趋势增强', group='警报消息', display=display.none)
  92. // i_alert_bull_trend_weaken = input.text_area('新弱看涨趋势', '看涨趋势减弱', group='警报消息', display=display.none)
  93. // i_alert_bear_trend_switch = input.text_area('新看跌趋势', '看跌趋势切换', group='警报消息', display=display.none)
  94. // i_alert_bear_trend_strengthen = input.text_area('新强看跌趋势', '看跌趋势增强', group='警报消息', display=display.none)
  95. // i_alert_bear_trend_weaken = input.text_area('新弱看跌趋势', '看跌趋势减弱', group='警报消息', display=display.none)
  96. // // 看涨趋势切换(看跌 -> 看涨)
  97. // if (ta.change(ta.change(math.sign(osc_bias)) > 0))
  98. //     alert(i_alert_bull_trend_switch, alert.freq_once_per_bar)
  99. // // 看涨趋势增强
  100. // if (osc_bias > 0 and ta.change(math.sign(osc_bias - osc_smooth)) > 0)
  101. //     alert(i_alert_bull_trend_strengthen, alert.freq_once_per_bar)
  102. // // 看涨趋势减弱
  103. // if (osc_bias > 0 and ta.change(math.sign(osc_bias - osc_smooth)) < 0)
  104. //     alert(i_alert_bull_trend_weaken, alert.freq_once_per_bar)
  105. // // 看跌趋势切换(看涨 -> 看跌)
  106. // if (ta.change(ta.change(math.sign(osc_bias)) < 0))
  107. //     alert(i_alert_bear_trend_switch, alert.freq_once_per_bar)
  108. // // 看跌趋势增强
  109. // if (osc_bias < 0 and ta.change(math.sign(osc_bias - osc_smooth)) < 0)
  110. //     alert(i_alert_bear_trend_strengthen, alert.freq_once_per_bar)
  111. // // 看跌趋势减弱
  112. // if (osc_bias < 0 and ta.change(math.sign(osc_bias - osc_smooth)) > 0)
  113. //     alert(i_alert_bear_trend_weaken, alert.freq_once_per_bar)
  114. // // }
复制代码
如果有帮助,就支持一下我呗
举报

评论 使用道具

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

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