Code: Select all
input double DollarPerPip = 1;
double LotSize(string sym) {
// Retrieve minimum, maximum, and step size for lot calculation
double minLot = SymbolInfoDouble(sym, SYMBOL_VOLUME_MIN);
double maxLot = SymbolInfoDouble(sym, SYMBOL_VOLUME_MAX);
double lotStep = SymbolInfoDouble(sym, SYMBOL_VOLUME_STEP);
// Ensure SymbolInfoDouble() calls succeeded
if (minLot <= 0 || maxLot <= 0 || lotStep <= 0) {
Print("Error retrieving lot size information for symbol: ", sym);
return 0;
}
// Retrieve symbol properties
double tickSize = SymbolInfoDouble(sym, SYMBOL_TRADE_TICK_SIZE); // Minimum price movement
double tickValue = SymbolInfoDouble(sym, SYMBOL_TRADE_TICK_VALUE); // Value of the tick size
double contractSize = SymbolInfoDouble(sym, SYMBOL_TRADE_CONTRACT_SIZE); // Contract size
// Validate retrieved values
if (tickSize <= 0 || tickValue <= 0 || contractSize <= 0) {
Print("Error retrieving symbol properties for: ", sym);
return 0;
}
// Ensure DollarPerPip is valid
if (DollarPerPip <= 0) {
Print("Invalid DollarPerPip value: ", DollarPerPip);
return 0;
}
// Calculate pip value for 1 lot
double pipValue = (tickValue / tickSize);
// Calculate the required lot size to achieve DollarPerPip
double lot = DollarPerPip / pipValue;
// Normalize lot size to the nearest step size
lot = NormalizeDouble(MathFloor(lot / lotStep) * lotStep, (int) SymbolInfoInteger(sym, SYMBOL_DIGITS));
Print("Normalized lot size: ", lot);
// Ensure the lot size is within the allowable range
if (lot < minLot) lot = minLot;
if (lot > maxLot) lot = maxLot;
return lot;
}