MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal

Source code library - Expert Advisors, Indicators and Scripts

MetaTrader Market: we have 650 Expert Advisors for algorithmic trading!
To post a new code, please log in or register

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2011.02.24 08:06
Programmatic modification of Tester "From:" and "To:" fields with user32.dll

Programmatic modification of Tester "From:" and "To:" fields with user32.dll - script for MetaTrader 4

| English Spanish Portuguese

Views:
1634
Rating:
votes: 2
Files:
SetDate.mq4 (6.47 KB)view
\MQL4\Include\
WinUser32.mqh (62.68 KB)view

For this script to run properly you must have the strategy tester window open.

I made this for the purpose of creating a distributed automated optimization package.
I was unsatisfied with the available automated optimization code because it necessitates modification of expert advisor code.

This was created and tested on Windows 7 64 bit. It is possible that the #define statements need to be changed to point to the proper IDs for your operating system.

I was unable to directly send a string to the date field because its control, SysDateTimePick32, is difficult to interface with MQL4.

Instead I used a somewhat round-about method of parsing the control's text and sending UP, DOWN, LEFT, and RIGHT keystrokes to adjust the date.

I posted this script because I found it very time consuming to find the necessary information to achieve this (especially for a Windows API newbie like myself). Hopefully this will help others.

Last comments | Go to discussion (1)
Robert Simon
Symr | 27 Jun 2011 at 19:08
i think better way to use WriteProcessMemory via Virtual memory.... checked this out:

//+------------------------------------------------------------------+
//|                                                      SetDate.mq4 |
//|                                                     Version: 2.0 |
//+------------------------------------------------------------------+
#property copyright "Copyright (c) 2011, Symr"
#import "user32.dll"
    int GetAncestor(int hWnd, int gaFlags);
    int GetDlgItem(int hDlg, int nIDDlgItem);
    int GetWindow(int hWnd, int uCmd);
    int GetClassNameA(int hWnd, string lpClassname, int nMaxCount);
    int GetWindowThreadProcessId(int hWnd, int& dwProcessId[]);
    int SendMessageA(int hWnd, int Msg, int wParam, int lParam);    
#import
#import "kernel32.dll"
    int OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
    int VirtualAllocEx(int hProcess, int& addr, int size, int dwType, int dwProtect);
   bool VirtualFreeEx(int hProcess, int& addr, int size, int dwType);
   bool WriteProcessMemory(int hProcess, int addr, int buffer[], int size, int& bytes[]);
   void GetSystemTime(int& TimeArray[]);
#import
//+------------------------------------------------------------------+
#define GW_HWNDNEXT         2
#define GW_CHILD            5
#define ID_TERMINAL         0xE81E
#define ID_TESTER           0x53
#define ID_PANEL            0x81BF
#define PROCESS_ALL_ACCESS  0x1F0FFF
#define MEM_COMMIT          0x1000
#define MEM_RELEASE         0x8000
#define PAGE_READWRITE      4
#define DTM_FIRST           0x1000
#define DTM_SETSYSTEMTIME   0x1002
#define GDT_VALID           0
//+------------------------------------------------------------------+
#property show_inputs
#property show_confirm
extern string from_date = "2003.08.11";
extern string to_date   = "2012.05.05";
void start() {
  ChangeDates(0,from_date);
  ChangeDates(1,to_date);
  return (0);
}
//+------------------------------------------------------------------+
int FindControlByID(int hParentWnd, string className, int id) {
  if (id < 0 || hParentWnd <=0) return;
  int cnt = 0;
  int child = GetWindow(hParentWnd,GW_CHILD);
  while (child != 0) {
    child = GetWindow(child,GW_HWNDNEXT);
    string name = "                                                  ";
    int res = GetClassNameA(child,name,50);
    if (name == className || className == "")
      if (cnt == id) break; else cnt++;
  }
  return (child);
}
//+------------------------------------------------------------------+
void SetControlDateTime(int hWnd, datetime time) {
  int hProcessId[1];
  if (GetWindowThreadProcessId(hWnd, hProcessId) == 0) return;
  int hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, hProcessId[0]);
  int addr = 0;
  int p = VirtualAllocEx(hProcess, addr, 2710, MEM_COMMIT, PAGE_READWRITE); 
  int TimeArray[4];
  GetSystemTime(TimeArray);
  int bytes[1]; 
  TimeArray[0] = TimeYear(time) + (TimeMonth(time) * 0x10000);
  TimeArray[1] = TimeDay(time) * 0x10000;
  WriteProcessMemory(hProcess, p, TimeArray, 16, bytes);
  SendMessageA(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, p);
  VirtualFreeEx(hProcess, p, 0, MEM_RELEASE);
}
//+------------------------------------------------------------------+
void ChangeDates (int fieldID, string date) {
  int hMetaTrader = GetAncestor(WindowHandle(Symbol(),Period()),2);
  int hTerminal = GetDlgItem(hMetaTrader, ID_TERMINAL);
  int hTester = GetDlgItem(hTerminal, ID_TESTER);
  int hInner = GetDlgItem(hTester, ID_PANEL);
  int hDtp = FindControlByID(hInner, "SysDateTimePick32", fieldID);
  if (hDtp > 0) SetControlDateTime(hDtp, StrToTime(date));
  return(0);
}
//+------------------------------------------------------------------+