Author: Copyright 2016, MetaQuotes Software Corp.
opamm

This script is designed to automate the process of retrieving and viewing historical trading data for a specific PAMM account from an external web source. Instead of manually searching for and downloading files, it handles the communication and file management internally to present the data as a viewable chart.

Core Functionality

1. Automated Data Retrieval The script connects to a specific web URL to fetch historical price data (candles). It dynamically builds this link based on the PAMM account number and the timeframe you select (either Hourly or Daily). It also allows for a specific date range to be defined, ensuring the tool grabs exactly the period of history you are interested in.

2. File Management & Storage Once the script fetches the data from the internet:

  • It creates a specialized history file on your computer.
  • It cleans up existing temporary files for the account to ensure that the data you see is current and not cluttered by old records.
  • It parses the raw web data (converting it from a text format into usable numbers like Open, High, Low, and Close prices) and writes it into a format that MetaTrader can recognize as a historical chart.

3. Automated Chart Navigation A unique feature of this script is how it displays the results. Rather than just saving a file to a folder, it interacts with the MetaTrader interface itself. It automatically opens the "Offline Charts" list and uses simulated keyboard commands (like pressing the "Down" arrow) to navigate to the correct file and select it. This allows the user to see the retrieved data almost instantly on their screen.

Key Workflow Summary

  • Inputs: You provide the PAMM account number, the desired timeframe, and the date range.
  • Execution: The script fetches the data from the web, saves it as a local history file, and then "types" the commands into MetaTrader to open that specific chart for you.
  • User Experience: The goal is to remove the manual steps of downloading CSV files, importing them into the platform, and searching through menus to find them.
Miscellaneous
It writes information to fileIt writes information to fileIt writes information to fileIt opens Message Boxes to the userIt issuies visual alerts to the screenIt writes information to file
7 Views
0 Downloads
0 Favorites
opamm
//+------------------------------------------------------------------+
//|                                               opamm_manbeast.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                           ic.icreator@gmail.com (äëÿ ñïàì áîòîâ) |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "0.10"
#property strict
#property show_inputs
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
enum TF 
  {
   H1 = PERIOD_H1,// H1 (íóæíî óêàçàòü äàòó)
   D1 = PERIOD_D1 // D1 (âñÿ èñòîðèÿ)
  };

input int pamm = 331296; // Íîìåð ÏÀÌÌ-ñ÷åòà
input TF timeframe = D1; // ÒÔ
input datetime dateStart=0; // Íà÷àëüíàÿ äàòà
input datetime dateEnd=0; // Êîíå÷íàÿ äàòà

#import "user32.dll"
int PostMessageA(int hWnd,int Msg,int wParam,int lParam);
int GetAncestor(int hWnd,int gaFlags);
int GetLastActivePopup(int hWnd);
int GetDlgItem(int hDlg,int nIDDlgItem);
#import

#import "kernel32.dll"
int FindFirstFileW(string Path,ushort &Answer[]);
bool FindNextFileW(int handle,ushort &Answer[]);
bool FindClose(int handle);
int DeleteFileW(string file);
#import

#define WM_COMMAND 0x0111
#define WM_KEYDOWN 0x0100
#define VK_DOWN 0x28
#define BM_CLICK 0x00F5
#define GA_ROOT 2
#define PAUSE 100
#define BASE 100.0
#define STD_OTMAZA "Íå òî ÷òîáû ÷òî-òî ïîøëî íå òàê, íî ÷òî-òî íå ðàáîòàåò."

string pammStr;
string tfStr;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart() 
  {
   bool isOk=true;
   string headers;
   char post[],result[];
   int timeout=3000;
   int time;
   double open,low,high,close,volume=1;
   int hwnd=0,cnt=0;
   int unused[13];
   pammStr=IntegerToString(pamm);
   tfStr=IntegerToString(timeframe);
   string name=pammStr+tfStr+".hst";
   if(GetChartPos(pammStr+"1440.hst")>-1)
      DeleteFileW(TerminalInfoString(TERMINAL_DATA_PATH)+"\\history\\"+AccountInfoString(ACCOUNT_SERVER)+"\\"+pammStr+"1440.hst");
   if(GetChartPos(pammStr+"60.hst")>-1)
      DeleteFileW(TerminalInfoString(TERMINAL_DATA_PATH)+"\\history\\"+AccountInfoString(ACCOUNT_SERVER)+"\\"+pammStr+"60.hst");
   int handle=FileOpenHistory(name,FILE_BIN|FILE_WRITE);
   if(handle<0) 
     {
      Print(STD_OTMAZA);
      return;
     }
   FileWriteInteger(handle,400,LONG_VALUE);
   FileWriteString(handle,"(C)opyright 2003, MetaQuotes Software Corp.",64);
   FileWriteString(handle,pammStr,12);
   FileWriteInteger(handle,timeframe,LONG_VALUE);
   FileWriteInteger(handle,_Digits,LONG_VALUE);
   FileWriteInteger(handle,0,LONG_VALUE);
   FileWriteInteger(handle,0,LONG_VALUE);
   FileWriteArray(handle,unused,0,13);

   int res=WebRequest("GET",getURL(timeframe),NULL,NULL,timeout,post,0,result,headers);
   if(res==-1) 
     {
      MessageBox("Ê ñîæàëåíèþ, äàííûé ðåñóðñ íåäîñòóïåí íà òåððèòîðèè ÐÔ.","Ðåñóðñ çàáëîêèðîâàí",MB_ICONERROR);
      isOk=false;
        } else {
      //PrintFormat("The file has been successfully loaded, File size =%d bytes.",ArraySize(result));

      string data= CharArrayToString(result);
      string str = "";
      int prev=0;
      int n=StringFind(data,"\n");
      string items[];
      while(n>0) 
        {
         str=StringSubstr(data,prev,n-prev);
         StringSplit(str,';',items);
         if(ArraySize(items)<10) 
           {
            Alert("Êàæåòñÿ, ïåðèîä ñëèøêîì áîëüøîé.");
            isOk=false;
            break;
           }
         StringReplace(items[0],"-",".");
         if(timeframe == D1)
            items[0] = items[0] + " 00:00";
         time=(int) StringToTime(items[0]);
         open= BASE+StringToDouble(items[1]);
         low = BASE+StringToDouble(items[2]);
         high= BASE+StringToDouble(items[3]);
         close=BASE+StringToDouble(items[4]);

         FileWriteInteger(handle,time,LONG_VALUE);
         FileWriteDouble(handle,open,DOUBLE_VALUE);
         FileWriteDouble(handle,low,DOUBLE_VALUE);
         FileWriteDouble(handle,high,DOUBLE_VALUE);
         FileWriteDouble(handle,close,DOUBLE_VALUE);
         FileWriteDouble(handle,1,DOUBLE_VALUE);
         FileFlush(handle);

         prev=n+1;
         n=StringFind(data,"\n",prev);
        }
      FileClose(handle);
     }
   if(isOk) 
     {
      int p=GetChartPos(name);
      if(p>-1) 
        {
         OpenOfflineChartbyNum(p);
           } else {
         MessageBox("Ãðàôèê íå íàéäåí.","Îøèáêà",MB_ICONINFORMATION);
        }
     }
  }
// Âîçâðàùàåò àäðåñ äëÿ ñêà÷èâàíèÿ
string getURL(int tf) 
  {
   MqlDateTime dtS,dtE;
   string date;

   TimeToStruct(dateStart,dtS);
   TimeToStruct(dateEnd,dtE);

   date=StringFormat("?start=%02i-%02i-%02i&end=%02i-%02i-%02i",
                     dtS.year,
                     dtS.mon,
                     dtS.day,
                     dtE.year,
                     dtE.mon,
                     dtE.day);

   switch(tf) 
     {
      case H1: return "http://www.alpari.ru/ru/investor/pamm/" + pammStr + "/monitoring/hourly_all_candle.csv" + date;
      default:
         case D1: return "http://www.alpari.ru/ru/investor/pamm/" + pammStr + "/monitoring/daily_all_candle.csv";
     }
  }
// Âîçâðàùàåò ïîçèöèþ ãðàôèêà â ñïèñêå.
int GetChartPos(string FileName) 
  {
   ushort Buffer[300];
   int Pos=-1;
   string path= TerminalInfoString(TERMINAL_DATA_PATH) + "\\history\\" + AccountInfoString(ACCOUNT_SERVER) + "\\*.hst";
   int handle = FindFirstFileW(path, Buffer);
   string name= ShortArrayToString(Buffer,22,152);
   Pos++;
   if(name!=FileName) 
     {
      ArrayInitialize(Buffer,0);
      while(FindNextFileW(handle,Buffer))
        {
         name=ShortArrayToString(Buffer,22,152);
         Pos++;
         if(name==FileName) 
           {
            break;
           }
         ArrayInitialize(Buffer,0);
        }
     }

   if(handle>0)
      FindClose(handle);

   return(Pos);
  }
// Îòêðûâàåò ñïèñîê Offline-ãðàôèêîâ. Âîçâðàùàåò õýíäë îêíà ñïèñêà.
int OpenOfflineList() 
  {
   int hwnd=WindowHandle(Symbol(),Period());

   hwnd=GetAncestor(hwnd,GA_ROOT);

   PostMessageA(hwnd,WM_COMMAND,33053,0);
   Sleep(PAUSE);

   hwnd=GetLastActivePopup(hwnd);

   return(hwnd);
  }
// Îòêðûâàåò Offline-ãðàôèê ïî íîìåðó â ñïèñêå.
void OpenOfflineChartbyNum(int ChartPos) 
  {
   int hwnd1 = OpenOfflineList();
   int hwnd2 = GetDlgItem(hwnd1, 1);

   hwnd1=GetDlgItem(hwnd1,0x487);

   while(ChartPos>=0) 
     {
      PostMessageA(hwnd1,WM_KEYDOWN,VK_DOWN,0);
      ChartPos--;
     }

   Sleep(PAUSE);

   PostMessageA(hwnd2,BM_CLICK,0,0);
  }
//+------------------------------------------------------------------+

Comments