mq5 timeframes hotkeys with zoom in and out

 

Hi guys,


Trying to figure out and cant make it happened. 

Need easier access to different timeframes on hotkeys. The only thing I am missing is when coming back from 1min TF it doesnt zooms in twice automatically, something overwrites that.

Ideal scenario is when moving to 1 - 1min TF it zooms out twice and moving to other hotkeys 2,3,4,5 it zooms in. Anyone has ideas ? 


Thanks in advance

#property copyright "Darvin"
#property version   "1.05"
#property description "The indicator will change the timeframe of the chart with key press"
#property indicator_chart_window

#define KEY_DAILY   '6'
#define KEY_4HOUR   '5'
#define KEY_1HOUR   '4'
#define KEY_30MIN   '3'
#define KEY_15MIN   '2'
#define KEY_1MIN    '1'

#define KEY_CHARTFIX 48
#define KEY_P        80

#define ZOOM_IN_FACTOR 1.15
#define MIN_SCALE 1

//+------------------------------------------------------------------+
int OnStart()
{
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
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);
}

//+------------------------------------------------------------------+
void ZoomIn(int times)
{
    long scale = ChartGetInteger(0, CHART_SCALE, 0);
    if(scale <= 0) scale = 100;

    for(int i = 0; i < times; i++)
    {
        scale = (long)(scale / ZOOM_IN_FACTOR);
        if(scale < MIN_SCALE)
            scale = MIN_SCALE;

        ChartSetInteger(0, CHART_SCALE, scale);
        ChartRedraw();
        Sleep(50);
    }
}

//+------------------------------------------------------------------+
void ChangeTFAndZoom(ENUM_TIMEFRAMES tf, int zoomTimes)
{
    ChartSetSymbolPeriod(0, NULL, tf);
    ChartRedraw();
    Sleep(150);   // allow MT5 to finish TF switch

    ZoomIn(zoomTimes);
}

//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
    if(id != CHARTEVENT_KEYDOWN)
        return;

    switch((int)lparam)
    {
        case KEY_1MIN:
            ChangeTFAndZoom(PERIOD_M1, 2);
            break;

        case KEY_15MIN:
            ChangeTFAndZoom(PERIOD_M15, 0);
            break;

        case KEY_30MIN:
            ChangeTFAndZoom(PERIOD_M30, 0);
            break;

        case KEY_1HOUR:
            ChangeTFAndZoom(PERIOD_H1, 0);
            break;

        case KEY_4HOUR:
            ChangeTFAndZoom(PERIOD_H4, 0);
            break;

        case KEY_DAILY:
            ChangeTFAndZoom(PERIOD_D1, 0);
            break;

        case KEY_CHARTFIX:
        {
            bool isFixed = ChartGetInteger(0, CHART_SCALEFIX, true);
            ChartSetInteger(0, CHART_SCALEFIX, !isFixed);
        }
        break;

        case KEY_P:
        {
            bool isShown = ChartGetInteger(0, CHART_SHOW_PRICE_SCALE, true);
            ChartSetInteger(0, CHART_SHOW_PRICE_SCALE, !isShown);
        }
        break;
    }

    ChartRedraw();
}
Files:
ddd2.mq5  4 kb
 
DD DD:

Hi guys,


Trying to figure out and cant make it happened. 

Need easier access to different timeframes on hotkeys. The only thing I am missing is when coming back from 1min TF it doesnt zooms in twice automatically, something overwrites that.

Ideal scenario is when moving to 1 - 1min TF it zooms out twice and moving to other hotkeys 2,3,4,5 it zooms in. Anyone has ideas ? 


Thanks in advance

The issue is not MT5 but the zoom logic.
MT5 keeps the previous chart scale when switching timeframes unless you explicitly reset it.
In your code, zoom is only applied on M1, while other TFs use
zoomTimes = 0, so the old scale is preserved.
Also, your
ZoomIn() function actually zooms out (scale division), which makes the behavior confusing.

The fix is to:

  1. Separate real zoom-in and zoom-out functions
  2. Always apply zoom after ChartSetSymbolPeriod()
  3. Explicitly zoom out on M1 and zoom in on higher TFs

Once zoom is forced after every TF switch, the behavior becomes consistent.