SymbolsLib
Download scriptThis script serves as a specialized data management tool designed to interact with the financial instruments (such as currency pairs or assets) available within your trading terminal. Instead of just showing you what is on the screen, it acts as a bridge to extract, organize, and identify specific details from the underlying database files of your platform.
The logic is broken down into three primary capabilities:
1. Instrument Listing
The script can generate a comprehensive list of all available trading symbols. It distinguishes between "selected" items (those you are currently focusing on) and "raw" data (the entire available database). It automatically calculates how many instruments exist in the files, allocates the necessary memory to hold that information, and populates a clean list for use in other parts of your trading environment.
2. Detailed Descriptions
Beyond just providing a ticker symbol (like "EURUSD"), the script has the ability to retrieve expanded descriptions. When provided with a specific symbol name, it scans the raw data files to find a matching entry and pulls out the associated descriptive text. This is useful for displaying more informative names or labels in a user interface rather than just short codes.
3. Asset Categorization (Grouping)
The script includes logic to identify which "Group" an instrument belongs to. It performs a two-step lookup:
- First, it locates the specific instrument in the database to find its unique group ID number.
- Second, it uses that ID to refer to a separate group definition file.
By doing this, the script can automatically determine if a symbol belongs to a specific category (such as "Forex," "Indices," or "Commodities"), allowing for organized sorting and filtered views of your trading portfolio.
//+------------------------------------------------------------------+
//| SymbolsLib.mq4 |
//| Copyright © 2009, Ilnur |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
// Áèáëèîòåêà ôóíêöèé äëÿ ðàáîòû ñ ôèíàíñîâûìè èíñòðóìåíòàìè,
// çàãðóæåííûìè â òîðãîâûé òåðìèíàë.
#property copyright "Copyright © 2009, Ilnur"
#property link "http://www.metaquotes.net"
#property library
//+------------------------------------------------------------------+
//| Ôóíêöèÿ âîçâðàùàåò ñïèñîê äîñòóïíûõ ñèìâîëîâ |
//+------------------------------------------------------------------+
int SymbolsList(string &Symbols[], bool Selected)
{
string SymbolsFileName;
int Offset, SymbolsNumber;
if(Selected) SymbolsFileName = "symbols.sel";
else SymbolsFileName = "symbols.raw";
// Îòêðûâàåì ôàéë ñ îïèñàíèåì ñèìâîëîâ
int hFile = FileOpenHistory(SymbolsFileName, FILE_BIN|FILE_READ);
if(hFile < 0) return(-1);
// Îïðåäåëÿåì êîëè÷åñòâî ñèìâîëîâ, çàðåãèñòðèðîâàííûõ â ôàéëå
if(Selected) { SymbolsNumber = (FileSize(hFile) - 4) / 128; Offset = 116; }
else { SymbolsNumber = FileSize(hFile) / 1936; Offset = 1924; }
ArrayResize(Symbols, SymbolsNumber);
// Ñ÷èòûâàåì ñèìâîëû èç ôàéëà
if(Selected) FileSeek(hFile, 4, SEEK_SET);
for(int i = 0; i < SymbolsNumber; i++)
{
Symbols[i] = FileReadString(hFile, 12);
FileSeek(hFile, Offset, SEEK_CUR);
}
FileClose(hFile);
// Âîçâðàùàåì êîëè÷åñòâî ñ÷èòàííûõ èíñòðóìåíòîâ
return(SymbolsNumber);
}
//+------------------------------------------------------------------+
//| Ôóíêöèÿ âîçâðàùàåò ðàñøèôðîâàííîå íàçâàíèå ñèìâîëà |
//+------------------------------------------------------------------+
string SymbolDescription(string SymbolName)
{
string SymbolDescription = "";
// Îòêðûâàåì ôàéë ñ îïèñàíèåì ñèìâîëîâ
int hFile = FileOpenHistory("symbols.raw", FILE_BIN|FILE_READ);
if(hFile < 0) return("");
// Îïðåäåëÿåì êîëè÷åñòâî ñèìâîëîâ, çàðåãèñòðèðîâàííûõ â ôàéëå
int SymbolsNumber = FileSize(hFile) / 1936;
// Èùåì ðàñøèôðîâêó ñèìâîëà â ôàéëå
for(int i = 0; i < SymbolsNumber; i++)
{
if(FileReadString(hFile, 12) == SymbolName)
{
SymbolDescription = FileReadString(hFile, 64);
break;
}
FileSeek(hFile, 1924, SEEK_CUR);
}
FileClose(hFile);
return(SymbolDescription);
}
//+------------------------------------------------------------------+
//| Ôóíêöèÿ îïðåäåëÿåò òèï èíñòðóìåíòà |
//+------------------------------------------------------------------+
string SymbolType(string SymbolName)
{
int GroupNumber = -1;
string SymbolGroup = "";
// Îòêðûâàåì ôàéë ñ îïèñàíèåì ñèìâîëîâ
int hFile = FileOpenHistory("symbols.raw", FILE_BIN|FILE_READ);
if(hFile < 0) return("");
// Îïðåäåëÿåì êîëè÷åñòâî ñèìâîëîâ, çàðåãèñòðèðîâàííûõ â ôàéëå
int SymbolsNumber = FileSize(hFile) / 1936;
// Èùåì ñèìâîë â ôàéëå
for(int i = 0; i < SymbolsNumber; i++)
{
if(FileReadString(hFile, 12) == SymbolName)
{
// Îïðåäåëÿåì íîìåð ãðóïïû
FileSeek(hFile, 1936*i + 100, SEEK_SET);
GroupNumber = FileReadInteger(hFile);
break;
}
FileSeek(hFile, 1924, SEEK_CUR);
}
FileClose(hFile);
if(GroupNumber < 0) return("");
// Îòêðûâàåì ôàéë ñ îïèñàíèåì ãðóïï
hFile = FileOpenHistory("symgroups.raw", FILE_BIN|FILE_READ);
if(hFile < 0) return("");
FileSeek(hFile, 80*GroupNumber, SEEK_SET);
SymbolGroup = FileReadString(hFile, 16);
FileClose(hFile);
return(SymbolGroup);
}
Comments