refactor: into a class-based structure

This commit is contained in:
Toh4iem9
2025-08-23 10:49:11 +02:00
parent f5e78ddfbe
commit 53ceacb3f6
+84 -29
View File
@@ -1,17 +1,39 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| SymbolInfoDoubleChecker.mq5 | //| SymbolInfoDoubleChecker.mq5 |
//| Copyright 2025, MetaQuotes Ltd. | //| Copyright 2025, xxxxxxxx |
//| https://www.mql5.com | //| |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd." #property copyright "Copyright 2025, xxxxxxxx"
#property link "https://www.mql5.com" #property link ""
#property version "1.02" // Version updated to reflect fixes #property version "2.00" // Refactored into a class-based structure
#property description "Checks and displays all DOUBLE properties for the current symbol." #property description "Checks and displays all DOUBLE properties for the current symbol."
#include <Trade\SymbolInfo.mqh> #include <Trade\SymbolInfo.mqh>
//+------------------------------------------------------------------+
//| Class to check and display symbol properties |
//+------------------------------------------------------------------+
class CSymbolPropertyChecker
{
private:
CSymbolInfo m_symbol_info;
string m_symbol_name;
//--- Array of all double properties to check. //--- Array of all double properties to check.
const ENUM_SYMBOL_INFO_DOUBLE G_DOUBLE_PROPERTIES[] = static const ENUM_SYMBOL_INFO_DOUBLE S_DOUBLE_PROPERTIES[];
public:
CSymbolPropertyChecker(const string symbol);
bool Run(void);
private:
void PrintHeader(void);
void PrintFooter(void);
void PrintProperty(ENUM_SYMBOL_INFO_DOUBLE property_id);
};
//--- Static member initialization
const ENUM_SYMBOL_INFO_DOUBLE CSymbolPropertyChecker::S_DOUBLE_PROPERTIES[] =
{ {
SYMBOL_BID, SYMBOL_BIDHIGH, SYMBOL_BIDLOW, SYMBOL_ASK, SYMBOL_ASKHIGH, SYMBOL_BID, SYMBOL_BIDHIGH, SYMBOL_BIDLOW, SYMBOL_ASK, SYMBOL_ASKHIGH,
SYMBOL_ASKLOW, SYMBOL_LAST, SYMBOL_LASTHIGH, SYMBOL_LASTLOW, SYMBOL_ASKLOW, SYMBOL_LAST, SYMBOL_LASTHIGH, SYMBOL_LASTLOW,
@@ -36,56 +58,89 @@ const ENUM_SYMBOL_INFO_DOUBLE G_DOUBLE_PROPERTIES[] =
}; };
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Script program start function | //| Constructor |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void OnStart() CSymbolPropertyChecker::CSymbolPropertyChecker(const string symbol) : m_symbol_name(symbol)
{ {
string symbol = _Symbol; // Initialization of m_symbol_info is handled in Run()
// Initialize CSymbolInfo. The constructor is parameterless.
// We must call .Name() to set the symbol.
CSymbolInfo m_symbol_info;
if(!m_symbol_info.Name(symbol))
{
PrintFormat("Error: Symbol '%s' not found or not available in Market Watch.", symbol);
return;
} }
Print("--- Symbol Property Checker Started ---"); //+------------------------------------------------------------------+
PrintFormat("Checking symbol: %s", symbol); //| Runs the property check process |
Print("------------------------------------------------------------------"); //+------------------------------------------------------------------+
bool CSymbolPropertyChecker::Run(void)
{
if(!m_symbol_info.Name(m_symbol_name))
{
PrintFormat("Error: Symbol '%s' not found or not available in Market Watch.", m_symbol_name);
return false;
}
PrintHeader();
for(int i = 0; i < ArraySize(S_DOUBLE_PROPERTIES); i++)
{
PrintProperty(S_DOUBLE_PROPERTIES[i]);
}
PrintFooter();
return true;
}
//+------------------------------------------------------------------+
//| Prints a single property's status and value |
//+------------------------------------------------------------------+
void CSymbolPropertyChecker::PrintProperty(ENUM_SYMBOL_INFO_DOUBLE property_id)
{
double value; double value;
string result_str; string result_str;
string property_name = EnumToString(property_id);
for(int i = 0; i < ArraySize(G_DOUBLE_PROPERTIES); i++)
{
ResetLastError(); ResetLastError();
bool success = m_symbol_info.InfoDouble(G_DOUBLE_PROPERTIES[i], value); bool success = m_symbol_info.InfoDouble(property_id, value);
int error = GetLastError(); int error = GetLastError();
// --- FIX: Using StringFormat with padding for alignment ---
// The "%-35s" specifier pads the string to 35 characters, left-aligned.
string property_name = EnumToString(G_DOUBLE_PROPERTIES[i]);
if(success) if(success)
{ {
// The "%g" specifier provides a general, clean format for double values.
result_str = StringFormat("%-35s | Status: SUPPORTED | Value: %g", result_str = StringFormat("%-35s | Status: SUPPORTED | Value: %g",
property_name, property_name,
value); value);
Print(result_str);
} }
else else
{ {
result_str = StringFormat("%-35s | Status: FAILED | Error: %d", result_str = StringFormat("%-35s | Status: FAILED | Error: %d",
property_name, property_name,
error); error);
}
Print(result_str); Print(result_str);
} }
//+------------------------------------------------------------------+
//| Prints the header for the output |
//+------------------------------------------------------------------+
void CSymbolPropertyChecker::PrintHeader(void)
{
Print("--- Symbol Property Checker Started ---");
PrintFormat("Checking symbol: %s", m_symbol_name);
Print("------------------------------------------------------------------");
} }
//+------------------------------------------------------------------+
//| Prints the footer for the output |
//+------------------------------------------------------------------+
void CSymbolPropertyChecker::PrintFooter(void)
{
Print("------------------------------------------------------------------"); Print("------------------------------------------------------------------");
Print("--- Check Completed ---"); Print("--- Check Completed ---");
} }
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
CSymbolPropertyChecker checker(_Symbol);
checker.Run();
}
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+