AntRobot/utils/Utils.mqh
super.admin 9174d587ba convert
2025-05-30 14:40:14 +02:00

105 lines
9 KiB
MQL5

//+------------------------------------------------------------------+
//| Utils.mqh |
//| Copyright 2021, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
class Utils {
private:
static string GetRelativeProgramPathWithFileName() {
int pos2;
//--- obtenemos la ruta absoluta hacia la aplicación
string path=MQLInfoString(MQL_PROGRAM_PATH);
//--- buscamos la posición de la subcadena "\MQL5\"
int pos =StringFind(path,"\\MQL5\\");
//--- subcadena no encontrada - error
if(pos<0)
return(NULL);
//--- saltamos la carpeta "\MQL5"
pos+=5;
//--- saltamos los símbolos '\' que sobran
while(StringGetCharacter(path,pos+1)=='\\')
pos++;
//--- si es un recurso, devolvemos la ruta respecto al directorio MQL5
if(StringFind(path,"::",pos)>=0)
return(StringSubstr(path,pos));
//--- buscamos el divisor para el primer subdirectorio en MQL5 (por ejemplo, MQL5\Indicators)
//--- si no hay, devolvemos la ruta respecto al directorio MQL5
if((pos2=StringFind(path,"\\",pos+1))<0)
return(StringSubstr(path,pos));
//--- devolvemos la ruta respecto al subdirectorio (por ejemplo, MQL5\Indicators)
return (StringSubstr(path,pos2+1));
}
public:
Utils();
~Utils();
static string GetRelativeProgramPath() {
string path = GetRelativeProgramPathWithFileName();
//path = StringSubstr(path,0,StringLen(path) - 1);
int length = StringLen(path);
int lastPositionI = length - 1;
for(int i=0; i<length -1 ; i++) {
if(StringGetCharacter(path, i) == '\\')
lastPositionI = i;
}
string result = StringSubstr(path,0,lastPositionI);
return result;
}
static int checkIfNewBar() {
MqlRates rates[1];
ResetLastError();
if(CopyRates(Symbol(),Period(),0,1,rates)!=1) {
Print("CopyRates error de copia, Code = ",GetLastError());
return false;
}
if(rates[0].tick_volume>1) {
return false;
}
return true;
}
double absDiffInPercent(const double initExtreme, const double endExtreme) {
return MathAbs(((endExtreme - initExtreme) * 100) / initExtreme);
}
double betterLotage(double maxRiskByTradeInPercent, double stopLossLevel, bool isLong, bool completeWithMinLot, bool completeWithMaxLot) {
double riskInMoney = NormalizeDouble(AccountInfoDouble(ACCOUNT_MARGIN_FREE)*(maxRiskByTradeInPercent/100),_Digits);
double deltaValuePerLot = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) / SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double lotStep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
double maxLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
double diffPrice = 0;
double lotage = 0;
if(isLong) {
double ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
diffPrice = MathAbs(ask - stopLossLevel);
} else {
double bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
diffPrice = MathAbs(bid - stopLossLevel);
}
//riskInMoney = Orderlost * diffPrice * DeltaPorLot + (comissio)
double lotageTmp = riskInMoney/(diffPrice*deltaValuePerLot);
lotage = NormalizeDouble(NormalizeDouble(lotageTmp/lotStep,_Digits)*lotStep, 2);
if(lotage < minLot && completeWithMinLot)
lotage = minLot;
if(lotage > maxLot && completeWithMaxLot)
lotage = maxLot;
return lotage;
}
bool isLong (int signal) {
return signal > 0;
}
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Utils::Utils() {
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Utils::~Utils() {
}
//+------------------------------------------------------------------+