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

03 指标框架多周期显示在一个图表上

| 发表于 2025-12-22 15:12:10 | 显示全部楼层 |复制链接
  1. //+------------------------------------------------------------------+
  2. //|                                                      多周期均线指标.mq4 |
  3. //|                                  Copyright 2024, MetaQuotes Ltd. |
  4. //|                                             https://www.mql5.com |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright 2024, MetaQuotes Ltd."
  7. #property link      "https://www.mql5.com"
  8. #property version   "1.00"
  9. #property strict
  10. #property indicator_chart_window
  11. #property indicator_buffers 3
  12. #property indicator_plots   3
  13. //--- plot 大周期
  14. #property indicator_label1  "大周期"
  15. #property indicator_type1   DRAW_LINE
  16. #property indicator_color1  clrRed
  17. #property indicator_style1  STYLE_SOLID
  18. #property indicator_width1  1
  19. //--- plot 中周期
  20. #property indicator_label2  "中周期"
  21. #property indicator_type2   DRAW_LINE
  22. #property indicator_color2  clrMediumSpringGreen
  23. #property indicator_style2  STYLE_SOLID
  24. #property indicator_width2  1
  25. //--- plot 小周期
  26. #property indicator_label3  "小周期"
  27. #property indicator_type3   DRAW_LINE
  28. #property indicator_color3  clrYellow
  29. #property indicator_style3  STYLE_SOLID
  30. #property indicator_width3  1
  31. //--- indicator buffers
  32. double         大周期Buffer[];
  33. double         中周期Buffer[];
  34. double         小周期Buffer[];
  35. input ENUM_TIMEFRAMES 大时间周期设置=PERIOD_D1;
  36. input ENUM_TIMEFRAMES 中时间周期设置=PERIOD_H4;
  37. input ENUM_TIMEFRAMES 小时间周期设置=PERIOD_H1;
  38. //+------------------------------------------------------------------+
  39. //| Custom indicator initialization function                         |
  40. //+------------------------------------------------------------------+
  41. int OnInit()
  42.   {
  43. //--- indicator buffers mapping
  44.    SetIndexBuffer(0,大周期Buffer);
  45.    SetIndexBuffer(1,中周期Buffer);
  46.    SetIndexBuffer(2,小周期Buffer);
  47. //---
  48.    return(INIT_SUCCEEDED);
  49.   }
  50. //+------------------------------------------------------------------+
  51. //| Custom indicator iteration function                              |
  52. //+------------------------------------------------------------------+
  53. int OnCalculate(const int rates_total,
  54.                 const int prev_calculated,
  55.                 const datetime &time[],
  56.                 const double &open[],
  57.                 const double &high[],
  58.                 const double &low[],
  59.                 const double &close[],
  60.                 const long &tick_volume[],
  61.                 const long &volume[],
  62.                 const int &spread[])
  63.   {
  64. //---
  65. if(Period()!=小时间周期设置)
  66. {
  67. Print("加载时间周期不对,请重试");
  68. return(rates_total);
  69. }
  70.   for(int i=0;i<rates_total;i++)
  71.     {
  72.      小周期Buffer=iMA(Symbol(),小时间周期设置,43,0,MODE_EMA,PRICE_CLOSE,i);
  73.     }
  74.   for(int i=0;i<rates_total;i++)
  75.     {
  76.      if(iBarShift(Symbol(),中时间周期设置,iTime(Symbol(),小时间周期设置,i),true)==-1)
  77.        {
  78.         break;
  79.        }
  80.        //1小时图表的K线序号对应的4小时图表上的K线序号
  81.        //iBarShift这个函数时通过时间来找到包含这个时间的K线序号
  82.       中周期Buffer=iMA(Symbol(),中时间周期设置,34,0,
  83.       MODE_EMA,PRICE_CLOSE,iBarShift(Symbol(),中时间周期设置,iTime(Symbol(),小时间周期设置,i),true));   
  84.     }
  85.     //把日线的均线指标值映射到1小时图表上显示
  86.     for(int i=0;i<rates_total;i++)
  87.     {
  88.      if(iBarShift(Symbol(),中时间周期设置,iTime(Symbol(),小时间周期设置,i),true)==-1)
  89.       中周期Buffer=iMA(Symbol(),中时间周期设置,34,0,
  90.       MODE_EMA,PRICE_CLOSE,iBarShift(Symbol(),中时间周期设置,iTime(Symbol(),小时间周期设置,i),true));   
  91.     }
  92. //--- return value of prev_calculated for next call
  93.    return(rates_total);
  94.   }
  95. //+------------------------------------------------------------------+
复制代码
举报

评论 使用道具

精彩评论1

痴墨鼠
D
| 发表于 7 天前 | 显示全部楼层
感谢楼主分享,这个思路不错
举报

点赞 评论 使用道具

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

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