- //+------------------------------------------------------------------+
- //| 多周期均线指标.mq4 |
- //| Copyright 2024, MetaQuotes Ltd. |
- //| https://www.mql5.com |
- //+------------------------------------------------------------------+
- #property copyright "Copyright 2024, MetaQuotes Ltd."
- #property link "https://www.mql5.com"
- #property version "1.00"
- #property strict
- #property indicator_chart_window
- #property indicator_buffers 3
- #property indicator_plots 3
- //--- plot 大周期
- #property indicator_label1 "大周期"
- #property indicator_type1 DRAW_LINE
- #property indicator_color1 clrRed
- #property indicator_style1 STYLE_SOLID
- #property indicator_width1 1
- //--- plot 中周期
- #property indicator_label2 "中周期"
- #property indicator_type2 DRAW_LINE
- #property indicator_color2 clrMediumSpringGreen
- #property indicator_style2 STYLE_SOLID
- #property indicator_width2 1
- //--- plot 小周期
- #property indicator_label3 "小周期"
- #property indicator_type3 DRAW_LINE
- #property indicator_color3 clrYellow
- #property indicator_style3 STYLE_SOLID
- #property indicator_width3 1
- //--- indicator buffers
- double 大周期Buffer[];
- double 中周期Buffer[];
- double 小周期Buffer[];
-
- input ENUM_TIMEFRAMES 大时间周期设置=PERIOD_D1;
- input ENUM_TIMEFRAMES 中时间周期设置=PERIOD_H4;
- input ENUM_TIMEFRAMES 小时间周期设置=PERIOD_H1;
-
- //+------------------------------------------------------------------+
- //| Custom indicator initialization function |
- //+------------------------------------------------------------------+
- int OnInit()
- {
- //--- indicator buffers mapping
- SetIndexBuffer(0,大周期Buffer);
- SetIndexBuffer(1,中周期Buffer);
- SetIndexBuffer(2,小周期Buffer);
-
- //---
- return(INIT_SUCCEEDED);
- }
- //+------------------------------------------------------------------+
- //| Custom indicator iteration function |
- //+------------------------------------------------------------------+
- int OnCalculate(const int rates_total,
- const int prev_calculated,
- const datetime &time[],
- const double &open[],
- const double &high[],
- const double &low[],
- const double &close[],
- const long &tick_volume[],
- const long &volume[],
- const int &spread[])
- {
- //---
- if(Period()!=小时间周期设置)
- {
- Print("加载时间周期不对,请重试");
- return(rates_total);
- }
- for(int i=0;i<rates_total;i++)
- {
-
- 小周期Buffer=iMA(Symbol(),小时间周期设置,43,0,MODE_EMA,PRICE_CLOSE,i);
- }
- for(int i=0;i<rates_total;i++)
- {
- if(iBarShift(Symbol(),中时间周期设置,iTime(Symbol(),小时间周期设置,i),true)==-1)
-
- {
- break;
- }
- //1小时图表的K线序号对应的4小时图表上的K线序号
- //iBarShift这个函数时通过时间来找到包含这个时间的K线序号
- 中周期Buffer=iMA(Symbol(),中时间周期设置,34,0,
- MODE_EMA,PRICE_CLOSE,iBarShift(Symbol(),中时间周期设置,iTime(Symbol(),小时间周期设置,i),true));
- }
- //把日线的均线指标值映射到1小时图表上显示
- for(int i=0;i<rates_total;i++)
- {
- if(iBarShift(Symbol(),中时间周期设置,iTime(Symbol(),小时间周期设置,i),true)==-1)
-
-
- 中周期Buffer=iMA(Symbol(),中时间周期设置,34,0,
- MODE_EMA,PRICE_CLOSE,iBarShift(Symbol(),中时间周期设置,iTime(Symbol(),小时间周期设置,i),true));
- }
-
- //--- return value of prev_calculated for next call
- return(rates_total);
- }
- //+------------------------------------------------------------------+
复制代码
|