一、键盘实现切换K周期原理

  1. 首先,需要定义一组按键对于一组K线周期,按下1代表M1,按下2代表M5,以此类推。

  2. 接下来,需要编写一个函数来处理键盘快捷键的事件。可以使用OnChartEvent()函数来监听键盘事件。当按下指定的快捷键时,触发相应的事件处理函数。

  3. 在事件处理函数中,根据按下的快捷键来切换周期。可以使用ChartSetSymbolPeriod()函数来设置图表的周期。例如,当按下快捷键1时,切换到1分钟周期

二、键盘切换K线周期的代码实现

//+------------------------------------------------------------------+
//|                                          键盘快速切换K线周期.mq4 |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "VX: mtquant"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property description "1=m1, 2=m5, 3=m15, 4=m30, 5=h1, 6=h4, 7=d1, 8=w1, 9=mn ."

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
    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[])
{
    return(rates_total);
}
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
{
    //---
    if (id == CHARTEVENT_KEYDOWN)
    {
        // 使用此网站查找键盘码
        // https://keycode.info
        switch (int(lparam))
        {
            case 49: // 数字键 1
                ChartSetSymbolPeriod(0,NULL,PERIOD_M1);
                break;
            case 50: // 数字键 2
                ChartSetSymbolPeriod(0,NULL,PERIOD_M5);
                break;
            case 51: // 数字键 3
                ChartSetSymbolPeriod(0,NULL,PERIOD_M15);
                break;
            case 52: // 数字键 4
                ChartSetSymbolPeriod(0,NULL,PERIOD_M30);
                break;
            case 53: // 数字键 5
                ChartSetSymbolPeriod(0,NULL,PERIOD_H1);
                break;
            case 54: // 数字键 6
                ChartSetSymbolPeriod(0,NULL,PERIOD_H4);
                break;
            case 55: // 数字键 7
                ChartSetSymbolPeriod(0,NULL,PERIOD_D1);
                break;
            case 56: // 数字键 8
                ChartSetSymbolPeriod(0,NULL,PERIOD_W1);
                break;
            case 57: // 数字键 9
                ChartSetSymbolPeriod(0,NULL,PERIOD_MN1);
                break;
        }
        //
        ChartRedraw();
    }
}

02-17 10:46