Download MetaTrader 5

Indicators: Change Timeframes with Hotkeys

To add comments, please log in or register
Automated-Trading
Admin
110788
Automated-Trading  

Change Timeframes with Hotkeys:

This simple indicator will change the timeframes of the current chart with keypress.

Author: mohit marwaha

FractalZoom
13
FractalZoom  
There is a bug in the code; when changing timeframes, drawn objects disappear. Please fix, thanks.

Nevermind, I am a coding noob (day 1)... Marco vd Heijden showed me the way. Thank you both for your contributions/help, respectively.
trademikenr
6
trademikenr  
FractalZoom:
There is a bug in the code; when changing timeframes, drawn objects disappear. Please fix, thanks.

Nevermind, I am a coding noob (day 1)... Marco vd Heijden showed me the way. Thank you both for your contributions/help, respectively.

Hi there, 

 Can you tell me how to fix this? I have the same problem you have - the indi is EXACTLY what I want, but I don't want it to delete objects every time I change time frame.

 Many many thanks!

 Mike :) 

Marco vd Heijden
Moderator
6139
Marco vd Heijden  
trademikenr:

Hi there, 

 Can you tell me how to fix this? I have the same problem you have - the indi is EXACTLY what I want, but I don't want it to delete objects every time I change time frame.

 Many many thanks!

 Mike :) 

Use the

OnChartEvent()

Function with the respectively

ENUM_CHART_EVENT

ID

Description

CHARTEVENT_KEYDOWN

Keystrokes


Plese see: https://www.mql5.com/en/docs/constants/chartconstants/enum_chartevents

And scroll down for example.

In this case it succeeds to remove

ObjectsDeleteAll() 

in

OnDeinit()

Function.

See below

//+------------------------------------------------------------------+
//|                                                      hotKeys.mq4 |
//|                                    Copyright 2015, Mohit Marwaha |
//|                                             marwaha1@gmail.com   |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, Mohit Marwaha"
#property link      "marwaha1@gmail.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property description "marwaha1@gmail.com"
#property description "The indicator will change the timeframe of the chart with key press"
#property description "M - Monthly, W - Weekly, D - Daily, 4 - 4Hr, 1 - 1Hr, 3 - 30Mins, f - 15Mins, 5 - 5Mins"
#define KEY_MONTHLY 57
#define KEY_WEEKLY 56
#define KEY_DAILY 55
#define KEY_4HOUR 54
#define KEY_1HOUR 53
#define KEY_5MIN 50
#define KEY_30MIN 52
#define KEY_15MIN 51
#define KEY_1MIN 49
#define KEY_CHARTFIX 48
#define KEY_P 80

extern bool showInfo=true;
//+------------------------------------------------------------------+
//| 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[])
  {

   if(showInfo==true)
     {
      ObjectCreate("Obj1",OBJ_LABEL,0,0,0);
      ObjectSetText("Obj1",Symbol()+"-"+IntegerToString(Period())+" Min",12,"Calibri",DodgerBlue);
      ObjectSet("Obj1",OBJPROP_CORNER,0);
      ObjectSet("Obj1",OBJPROP_XDISTANCE,15);
      ObjectSet("Obj1",OBJPROP_YDISTANCE,10);

      ObjectCreate("Obj2",OBJ_LABEL,0,0,0);
      ObjectSetText("Obj2","Ask:"+DoubleToStr(Ask),8,"Verdana",Red);
      ObjectSet("Obj2",OBJPROP_CORNER,0);
      ObjectSet("Obj2",OBJPROP_XDISTANCE,15);
      ObjectSet("Obj2",OBJPROP_YDISTANCE,30);

      ObjectCreate("Obj3",OBJ_LABEL,0,0,0);
      ObjectSetText("Obj3","Bid:"+DoubleToStr(Bid),8,"Verdana",Green);
      ObjectSet("Obj3",OBJPROP_CORNER,0);
      ObjectSet("Obj3",OBJPROP_XDISTANCE,15);
      ObjectSet("Obj3",OBJPROP_YDISTANCE,40);

      ObjectCreate("Obj4",OBJ_LABEL,0,0,0);
      ObjectSetText("Obj4","Spread:"+DoubleToStr(NormalizeDouble((Ask-Bid),5)),8,"Verdana",Gray);
      ObjectSet("Obj4",OBJPROP_CORNER,0);
      ObjectSet("Obj4",OBJPROP_XDISTANCE,15);
      ObjectSet("Obj4",OBJPROP_YDISTANCE,50);
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if(id==CHARTEVENT_KEYDOWN)
     {
      switch(int(lparam))
        {
         case KEY_MONTHLY:ChartSetSymbolPeriod(0,NULL,43200);
         break;
         case KEY_WEEKLY:ChartSetSymbolPeriod(0,NULL,10080);
         break;
         case KEY_DAILY:ChartSetSymbolPeriod(0,NULL,1440);
         break;
         case KEY_4HOUR:ChartSetSymbolPeriod(0,NULL,240);
         break;
         case KEY_1HOUR:ChartSetSymbolPeriod(0,NULL,60);
         break;
         case KEY_5MIN:ChartSetSymbolPeriod(0,NULL,5);
         break;
         case KEY_30MIN:ChartSetSymbolPeriod(0,NULL,30);
         break;
         case KEY_15MIN:ChartSetSymbolPeriod(0,NULL,15);
         break;
         case KEY_1MIN:ChartSetSymbolPeriod(0,NULL,1);
         break;
         case KEY_CHARTFIX:
           {
            bool isFixed=ChartGetInteger(0,CHART_SCALEFIX,true);
            if(!isFixed)
              {
               ChartSetInteger(0,CHART_SCALEFIX,true);
              }
            else  
              {
               ChartSetInteger(0,CHART_SCALEFIX,false);
              }
           }
         break;
         case KEY_P:
           {
            bool isFixed=ChartGetInteger(0,CHART_SHOW_PRICE_SCALE,true);
            if(!isFixed)
              {
               ChartSetInteger(0,CHART_SHOW_PRICE_SCALE,true);
              }
            else  
              {
               ChartSetInteger(0,CHART_SHOW_PRICE_SCALE,false);
              }
           }
         break;
        }
      ChartRedraw();
     }
  }
//+------------------------------------------------------------------+
int deinit()
  {
   //ObjectsDeleteAll(); <<--- Here
   return(0);
  }
//+------------------------------------------------------------------+
Documentation on MQL5: Standard Constants, Enumerations and Structures / Chart Constants / Types of Chart Events
Documentation on MQL5: Standard Constants, Enumerations and Structures / Chart Constants / Types of Chart Events
  • www.mql5.com
Standard Constants, Enumerations and Structures / Chart Constants / Types of Chart Events - Reference on algorithmic/automated trading language for MetaTrader 5
trademikenr
6
trademikenr  
Marco vd Heijden:

Use the

Function with the respectively

ENUM_CHART_EVENT

ID

Description

CHARTEVENT_KEYDOWN

Keystrokes


Plese see: https://www.mql5.com/en/docs/constants/chartconstants/enum_chartevents

And scroll down for example.

In this case it succeeds to remove

in

Function.

See below

Thank you SO much for that :) Like so many things, it's easy when you know how :) 

 Many many thanks again!

 

Mike ;) 

greenpar
38
greenpar  
Marco vd Heijden:

Use the

Function with the respectively

ENUM_CHART_EVENT

ID

Description

CHARTEVENT_KEYDOWN

Keystrokes


Plese see: https://www.mql5.com/en/docs/constants/chartconstants/enum_chartevents

And scroll down for example.

In this case it succeeds to remove

in

Function.

See below


I took Marco's advice and edited this indictor with regard to deleting objects, and also removed the 4 objects it placed on the chart (symbol, ask, bid, spread). Below is the code and I just wanted to check that I had edited it properly.

I also wanted to specifically check that I have used the correct body for 'int OnCalculate' (see highlighted code below).

Thanks for all of your help.


#property description   "The indicator will change the timeframe of the chart with key press"
#property description   "1 - M1, 2 - M5, 3 - M15, 4 - M30, 5 - H1, 6 - H4, 7 - D1, 8 - W1, 9 - MN"
#property strict

#property indicator_chart_window

#define KEY_MONTHLY 57
#define KEY_WEEKLY 56
#define KEY_DAILY 55
#define KEY_4HOUR 54
#define KEY_1HOUR 53
#define KEY_5MIN 50
#define KEY_30MIN 52
#define KEY_15MIN 51
#define KEY_1MIN 49
#define KEY_CHARTFIX 48
#define KEY_P 80

//+------------------------------------------------------------------+
// 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)
        {
        switch(int(lparam))
                {
                case KEY_MONTHLY:ChartSetSymbolPeriod(0,NULL,43200);
                break;
                case KEY_WEEKLY:ChartSetSymbolPeriod(0,NULL,10080);
                break;
                case KEY_DAILY:ChartSetSymbolPeriod(0,NULL,1440);
                break;
                case KEY_4HOUR:ChartSetSymbolPeriod(0,NULL,240);
                break;
                case KEY_1HOUR:ChartSetSymbolPeriod(0,NULL,60);
                break;
                case KEY_5MIN:ChartSetSymbolPeriod(0,NULL,5);
                break;
                case KEY_30MIN:ChartSetSymbolPeriod(0,NULL,30);
                break;
                case KEY_15MIN:ChartSetSymbolPeriod(0,NULL,15);
                break;
                case KEY_1MIN:ChartSetSymbolPeriod(0,NULL,1);
                break;
                case KEY_CHARTFIX:
                        {
                        bool isFixed=ChartGetInteger(0,CHART_SCALEFIX,true);
                        if(!isFixed)
                                {
                                ChartSetInteger(0,CHART_SCALEFIX,true);
                                }
                        else
                                {
                                ChartSetInteger(0,CHART_SCALEFIX,false);
                                }
                        }
                break;
                case KEY_P:
                        {
                        bool isFixed=ChartGetInteger(0,CHART_SHOW_PRICE_SCALE,true);
                        if(!isFixed)
                                {
                                ChartSetInteger(0,CHART_SHOW_PRICE_SCALE,true);
                                }
                        else
                                {
                                ChartSetInteger(0,CHART_SHOW_PRICE_SCALE,false);
                                }
                        }
                break;
                }
        ChartRedraw();
        }
}
greenpar
38
greenpar  
greenpar:

I took Marco's advice and edited this indictor with regard to deleting objects, and also removed the 4 objects it placed on the chart (symbol, ask, bid, spread). Below is the code and I just wanted to check that I had edited it properly.

I also wanted to specifically check that I have used the correct body for 'int OnCalculate' (see highlighted code below).

Thanks for all of your help.



Any help/comments would be appreciated. Thank you.

Thierry Ramaniraka
170
Thierry Ramaniraka  

Merci.
God bless.

Thierry Ramaniraka
170
Thierry Ramaniraka  

Hi there,
Me again to ask your help.

And, i want to add a hotkey to hide subwindows.
But i failed ...

What i have to do and As SIMPLE as possible please ?

Regards.

case KEY_HIDESUB:
           {
            bool isFixed=ChartGetInteger(0,CHART_WINDOW_IS_VISIBLE,true);
            if(!isFixed)
              {
               ChartSetInteger(0,CHART_WINDOW_IS_VISIBLE,false);
              }
            else  
              {
               ChartSetInteger(0,CHART_WINDOW_IS_VISIBLE,false);
              }
           }
         break;
Change Timeframes with Hotkeys
Change Timeframes with Hotkeys
  • votes: 16
  • 2015.08.04
  • Mohit Marwaha
  • www.mql5.com
This simple indicator will change the timeframes of the current chart with keypress. I have added yet another functionality to it to fix/unfix chart scale as well as show/hide price scale. 1 = 1 minute 2 = 5 minutes 3 = 15 minutes 4 = 30 minutes 5 = 60 minutes (1 hour) 6 = 240 minutes...
To add comments, please log in or register