Download MetaTrader 5
To add comments, please log in or register
Documentation is available in CHM and PDF. Download it in any convenient form!
kensho
40
kensho 2014.03.02 14:19 

Hi all,

first of all, English is not my native language so I hope you won't get angry, if some misunderstandings occur in this thread ;-). Furthermore I want to give you a realistic view about my programming capabilities - well there are almost none. I can print "Hello world" in Python and know about "if-else" or the "for" operators, but there is not much more.....

I hope you can help me to find a solution for my problem.

I'm tired of switching through the charts with my mouse and switching with "CTRL+TAB" doesn't work for me, since the order of the "Chart-Tabs" gets messed up as soon as you go to another tab, that is not the next one.

So I thought that a script could help me achieving that. This script I wanted to bind to a hot-key combination, that I could use via a macro-key on my keyboard. So I was looking around for such a script and found this: https://forum.mql4.com/31024#297786

Now I tried to customize the code for my needs. Therefore I used an array containing the symbols of my charts in the right order, together with some other things, this came up:

#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
#property show_inputs
#import "user32.dll"
int GetParent(int hWnd);
int SendMessageA(int hWnd, int Msg, int wParam, int lParam);
#import

#define WM_MDIACTIVATE 0x222
 
string charts[13] = {"EURUSD", "GBPUSD", "USDCHF", "USDJPY", "EURJPY", "GBPJPY", "AUDUSD", "AUDJPY", "NZDUSD", "USDCAD", "GBPCHF", "EURGBP", "EURCHF"};

int Window.activate(int hwnd) 
   {
   int p = GetParent(hwnd);
   SendMessageA(GetParent(p), WM_MDIACTIVATE, p, 0);
   }

int Window.getHandle() 
   {
   int i, hwnd;
   string chart = Symbol();
      
   if (chart==charts[ArraySize(charts)-1])
      {
      hwnd=WindowHandle(charts[0], 0);
      }else
      {
      for (i=0; chart != charts[i]; i++) 
         {
         hwnd = WindowHandle(charts[i+1], 0);
         if (hwnd != 0) break;
         }
      }
   return (hwnd);
   }

void start()
   {
   int hwnd = Window.getHandle();
   Window.activate(hwnd);
   }

The problem is, when starting this script, I get this notification:

When clicking "OK", nothing happens.....

So my questions are:

1. What do I have to do to avoid this notification? I later want to simply push my macro-button and the chart switches to the next one. (Later I also want to make a script where I can jump to the chart before)

2. Are there any other mistakes in "my code"?

Maybe it is important to know, that the charts often times have different time frames, so it should be no problem to switch from a 1 min. chart to a 5 min. chart and vice versa...


Any help is highly appreciated!

Thanks and best regards

kensho

qjol
3243
qjol 2014.03.03 00:06  
from the new version B600 > Winapi is no need to use for that use CHART_BRING_TO_TOP
kensho
40
kensho 2014.03.03 07:12  

Hi gjol,

thank you very much for your input. I'll try implement it this evening.

Thanks

kensho

kensho
40
kensho 2014.03.03 16:27  

Hi again,

with the tip from gjol I wrote a new script that works.

But i'd like to increase its responsiveness. Because sometimes it needs 3-4 seconds to switch to the next chart.... maybe you have some tips.

Are there ways to improve the loading, initialization and deinitialization of the script? Maybe there is the possibility to let the script open/running for the time MetaTrader is running and implement the hot-key directly in the code?

Also other tips to improve the code are highly appreciated.

So here is my code:

//--- array of open charts in right order
string charts[13] = {"EURUSD", "GBPUSD", "USDCHF", "USDJPY", "EURJPY", "GBPJPY", "AUDUSD", "AUDJPY", "NZDUSD", "USDCAD", "GBPCHF", "EURGBP", "EURCHF"};

int chartsSize = ArraySize(charts);
string tSymbol;

//--- search for the symbol of the targeted chart
string zielsymbol()
  {  
   string aktuell = Symbol();
   int i;
  
   if(aktuell == charts[chartsSize - 1])
     {
      tSymbol = charts[0];
     }
   else
     {
      for(i = 0; i <= (chartsSize - 1); i++)
        {
         if(aktuell == charts[i])
           {
            tSymbol = charts[i+1];
            break;
           }
        }
     }
   return tSymbol;
  }

//--- search of ChartId of the chart with the targeted symbol
long sucheChartID()
  {
  int i;
  long altID;
  long aktID = ChartFirst();
  
  for(i = 0; i <= (chartsSize - 1); i++)
    {
     if(ChartSymbol(aktID) != tSymbol)
       {
        altID = aktID;
        aktID = ChartNext(altID);
       }
     else
       {
        break;
       }
    }
   return aktID; 
  }

 
void OnStart()
  {
   zielsymbol();
   ChartSetInteger(sucheChartID(), CHART_BRING_TO_TOP, 0, true);
  }
kensho
40
kensho 2014.03.03 22:34  

So a little and probably last update in this thread:

I think I have found the reason for the lack of responsiveness that occurred sometimes. I believe for switching a new tick for the targeted chart was necessary. Depending on the times of the day, this can take some good time.

So a ChartRedraw() was inserted, that made the script switch a lot faster.

Here is my final code:

#property copyright "kensho"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+

//--- array of open charts in right order
string charts[13] = {"EURUSD", "GBPUSD", "USDCHF", "USDJPY", "EURJPY", "GBPJPY", "AUDUSD", "AUDJPY", "NZDUSD", "USDCAD", "GBPCHF", "EURGBP", "EURCHF"};

int chartsSize = ArraySize(charts);
string tSymbol;
long zielID;

//--- search for the symbol of the targeted chart
string zielsymbol()
  {  
   string aktuell = Symbol();
   int i;
  
   if(aktuell == charts[chartsSize - 1])
     {
      tSymbol = charts[0];
     }
   else
     {
      for(i = 0; i <= (chartsSize - 1); i++)
        {
         if(aktuell == charts[i])
           {
            tSymbol = charts[i+1];
            break;
           }
        }
     }
   return tSymbol;
  }

//--- search of ChartId of the chart with the targeted symbol
long sucheChartID()
  {
  int i;
  long altID;
  long aktID = ChartFirst();
  
  for(i = 0; i <= (chartsSize - 1); i++)
    {
     if(ChartSymbol(aktID) != tSymbol)
       {
        altID = aktID;
        aktID = ChartNext(altID);
       }
     else
       {
        break;
       }
    }
   return aktID; 
  }

 
void OnStart()
  {
   zielsymbol();
   zielID = sucheChartID();
   ChartSetInteger(zielID, CHART_BRING_TO_TOP, 0, true);
   ChartRedraw(zielID);
  }

Best regards

kensho

Roberto
7
Roberto 2014.03.03 23:42  

Hello kensho,

You can found solution for your problem here: https://www.mql5.com/en/code/11060?source=terminal4_codebase

/
To add comments, please log in or register