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

Mine Script 源码,供学习

| 发表于 2025-8-1 18:05:58 | 显示全部楼层 |复制链接

  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. // // }
复制代码














Heikin Ashi 市场偏见:





使用双重 EMA 平滑(默认周期 100 和 100)计算 Heikin Ashi OHLC 值,生成平滑 K线,减少市场噪音。



振荡器(默认周期 7)基于平滑 Heikin Ashi 开收盘价差(100 * (c2 - o2))计算市场偏见,平滑后判断趋势强度。



趋势信号:





强看涨:振荡器 > 0 且大于平滑值(浅绿色,透明度 35)。



弱看涨:振荡器 > 0 但小于平滑值(深绿色,透明度 75)。



强看跌:振荡器 < 0 且小于平滑值(浅红色,透明度 35)。



弱看跌:振荡器 < 0 但大于平滑值(深红色,透明度 75)。



多时间框架支持:允许使用高于图表时间框架的数据(通过 FORCE_CTF 和 f_no_repaint_request 防止重绘)。



警报功能:支持趋势切换(看涨/看跌)、趋势增强和减弱警报,可自定义消息。



G-Channel 趋势检测:





使用 AlexGrover 的 G-Channel 算法计算动态上下轨,基于价格波动的高效极值。



看涨:价格突破上轨,仅绘制上轨到中线(默认绿色)。



看跌:价格跌破下轨,仅绘制下轨到中线(默认红色)。



供需区域:





通过枢轴点识别近期高点(HH/LH,供应区域,默认白色,提示卖出)和低点(HL/LL,需求区域,默认蓝色,提示买入)。



支持调整区域框宽度和历史保留数量。



市场结构与枢轴点:





实时显示波段结构标签(HH:更高高点,HL:更高低点,LH:更低高点,LL:更低低点)和 BOS 线。



BOS 检测价格突破 HH/HL(看涨)或 LH/LL(看跌),提示趋势延续。



显示 Zig Zag 模式,突出市场波动路径。



线性回归趋势分析:





计算短期(默认 20 周期)和长期(默认 50 周期)线性回归线,评估趋势方向和强度。



结合成交量线性回归,验证趋势是否得到支持(高成交量支持提示强劲趋势,低成交量预示反转)。



双范围过滤器(TRF):





基于快线(默认 27 周期 EMA)和慢线(默认 55 周期 EMA)的平均值,生成动态范围边界。



价格突破上轨提示买入,跌破下轨提示卖出,优于传统 ATR 设置。



成交量支持:





分析成交量回归斜率,判断趋势动能。



上升趋势中,正向斜率提示强劲动能;下降趋势中,低成交量提示反转风险。



综合信号:





高概率信号需满足:Heikin Ashi 振荡器看涨/看跌、G-Channel 突破、供需区域、线性回归趋势、TRF 信号和成交量支持一致。



示例:看涨信号需价格突破 G-Channel 上轨、在需求区域、Heikin Ashi 振荡器 > 0(强看涨优先)、短期/长期趋势线向上、TRF 上轨突破、成交量回归斜率正向。



视觉设置:





支持自定义 Heikin Ashi K线颜色(默认绿色看涨,红色看跌)、G-Channel 上下轨、供需区域(供应白色,需求蓝色)、BOS 标签、枢轴点标签、Zig Zag 线和 TRF 线颜色。



可启用/禁用 Heikin Ashi K线、Zig Zag、枢轴点标签、BOS、TRF 信号和市场偏见填充。
如果有帮助,就支持一下我呗
举报

评论 使用道具

精彩评论3

xiaofengdt
DD
| 发表于 2025-8-1 18:11:12 来自手机 | 显示全部楼层
谢谢分享。
举报

点赞 评论 使用道具

yuiran
DDD
| 发表于 2025-8-6 20:13:53 | 显示全部楼层
小峰 发表于 2025-8-1 18:14
谁编译出来了吗来张图片

没有,等个成品
举报

点赞 评论 使用道具

yumin777
D
| 发表于 2025-8-17 17:02:36 | 显示全部楼层
谢谢分享源码
举报

点赞 评论 使用道具

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

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