82 lines
2.3 KiB
Plaintext
82 lines
2.3 KiB
Plaintext
//+------------------------------------------------------------------+
|
|
//| Utilities.mqh - Helper utility functions |
|
|
//| Price normalization, lot normalization, etc. |
|
|
//+------------------------------------------------------------------+
|
|
|
|
#ifndef __UTILITIES_MQH__
|
|
#define __UTILITIES_MQH__
|
|
|
|
class CUtilities
|
|
{
|
|
public:
|
|
// Normalize price to bid/ask
|
|
static double NormalizePrice(const string symbol, double price)
|
|
{
|
|
double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
|
|
int digits = GetDigits(symbol);
|
|
return NormalizeDouble(price, digits);
|
|
}
|
|
|
|
// Normalize lot size to contract size step
|
|
static double NormalizeLot(const string symbol, double lot)
|
|
{
|
|
double min_lot = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MIN);
|
|
double max_lot = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MAX);
|
|
double lot_step = SymbolInfoDouble(symbol, SYMBOL_VOLUME_STEP);
|
|
|
|
// Apply limits
|
|
if(lot < min_lot) lot = min_lot;
|
|
if(lot > max_lot) lot = max_lot;
|
|
|
|
// Round to step
|
|
lot = MathFloor(lot / lot_step) * lot_step;
|
|
|
|
return NormalizeDouble(lot, 2);
|
|
}
|
|
|
|
// Convert points to price distance
|
|
static double PointsToPrice(const string symbol, int points)
|
|
{
|
|
double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
|
|
return (double)points * point;
|
|
}
|
|
|
|
// Convert price distance to points
|
|
static int PriceToPoints(const string symbol, double price_distance)
|
|
{
|
|
double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
|
|
return (int)(price_distance / point);
|
|
}
|
|
|
|
// Get point value
|
|
static double GetPoint(const string symbol)
|
|
{
|
|
return SymbolInfoDouble(symbol, SYMBOL_POINT);
|
|
}
|
|
|
|
// Get digits
|
|
static int GetDigits(const string symbol)
|
|
{
|
|
long digits = SymbolInfoInteger(symbol, SYMBOL_DIGITS);
|
|
if(digits < 0)
|
|
digits = 0;
|
|
if(digits > 255)
|
|
digits = 255;
|
|
return (int)digits;
|
|
}
|
|
|
|
// Safe double comparison with precision
|
|
static bool DoubleEquals(double a, double b, double tolerance = 0.00001)
|
|
{
|
|
return MathAbs(a - b) < tolerance;
|
|
}
|
|
|
|
// Get contract size (lot multiplier)
|
|
static double GetContractSize(const string symbol)
|
|
{
|
|
return SymbolInfoDouble(symbol, SYMBOL_TRADE_CONTRACT_SIZE);
|
|
}
|
|
};
|
|
|
|
#endif //__UTILITIES_MQH__
|