It returns an enumeration. You never need to know the value. Don't use 60 or 16385; use the proper enumeration (PERIOD_H1).
please... when I use Period() on h1 on mt5 why is the value different and not the same as mt4... on mt4 period_h1 is 60 while on mt5 it is 16385... please help
The difference in the values returned by Period() function between MetaTrader 4 (MT4) and MetaTrader 5 (MT5) is due to how each platform assigns numerical values to different timeframes.
In MT4:
The value returned for the H1 timeframe by Period() function is indeed 60.
In MT5:
The value returned for the H1 timeframe by Period() function is 16385.
This discrepancy occurs because MT5 uses a different numerical representation for timeframes compared to MT4. While the actual timeframe remains the same (H1 in both cases), the numerical values assigned to timeframes differ between the two platforms.
To adapt your scripts or expert advisors to work seamlessly across both MT4 and MT5 platforms, you can create conditional statements that account for the differences in timeframe values. Here is an example i made for you:
// Check if the current platform is MT4 #ifdef __MQL4__ int periodH1 = 60; #else int periodH1 = 16385; #endif
So, by using conditional compilation directives like #ifdef __MQL4__ , you can differentiate between MT4 and MT5 environments and assign the appropriate values accordingly.
This way, your scripts or expert advisors can handle timeframe-related calculations consistently across both MetaTrader platforms.
Conversely, i would recommend you using the proper way by using the Enum Period_H1, or Period_D1 etc. Whichever period you are using
I am usingto display as follows and get this strange number - but as stated doesn't generate any problems
IntegerToString(PERIOD_H1)
"Conversely, i would recommend you using the proper way by using the Enum Period_H1, or Period_D1 etc. Whichever period you are using"
How do I display using Enum?
Obviously could add "if H1 then use 60" but 'messy' and is there a simple way to display any time period using the same code
Who can help me to solve 10 errors and 2 warnings in this code :
#property strict input double RiskPercent = 2.0; // Risque par trade (% du capital) input double ATR_Multiplier = 1.5; // Multiplicateur ATR pour définir SL input double ADX_Period = 14; // Période de l'ADX input double LotSize = 0.1; // Taille du lot fixe (peut être ajustée par la gestion du risque) double ATR, EMA50, EMA200, RSI, MACD_Main, MACD_Signal, ADX; //+------------------------------------------------------------------+ //| Initialisation des indicateurs | //+------------------------------------------------------------------+ int OnInit() { // Initialisation des indicateurs EMA50 = iMA(_Symbol, PERIOD_M15, 50, 0, MODE_EMA, PRICE_CLOSE, 0); EMA200 = iMA(_Symbol, PERIOD_M15, 200, 0, MODE_EMA, PRICE_CLOSE, 0); RSI = iRSI(_Symbol, PERIOD_M15, 14, 0); // Déclaration des tableaux pour macd double macd_main[], macd_signal[]; // Appel de la fonction iMACD pour récupérer les valeurs iMACD(_Symbol, PERIOD_M15, 12, 26, 9, PRICE_CLOSE, macd_main, macd_signal); MACD_Main = macd_main[0]; // Assigner la valeur principale du MACD MACD_Signal = macd_signal[0]; // Assigner la valeur du signal du MACD ATR = iATR(_Symbol, PERIOD_M15, 14, 0); ADX = iADX(_Symbol, PERIOD_M15, 14, 0); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Fonction principale : OnTick | //+------------------------------------------------------------------+ void OnTick() { double price = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Récupérer les valeurs des indicateurs EMA50 = iMA(_Symbol, PERIOD_M15, 50, 0, MODE_EMA, PRICE_CLOSE, 0); EMA200 = iMA(_Symbol, PERIOD_M15, 200, 0, MODE_EMA, PRICE_CLOSE, 0); RSI = iRSI(_Symbol, PERIOD_M15, 14, 0); // Déclaration des tableaux pour macd double macd_main[], macd_signal[]; // Appel de la fonction iMACD pour récupérer les valeurs iMACD(_Symbol, PERIOD_M15, 12, 26, 9, PRICE_CLOSE, macd_main, macd_signal); MACD_Main = macd_main[0]; // Assigner la valeur principale du MACD MACD_Signal = macd_signal[0]; // Assigner la valeur du signal du MACD ATR = iATR(_Symbol, PERIOD_M15, 14, 0); ADX = iADX(_Symbol, PERIOD_M15, 14, 0); // Calcul de la taille du lot en fonction du risque double SL = ATR * ATR_Multiplier; double TP = SL * 2; // Calcul de la taille du lot selon le risque double AccountBalance = AccountInfoDouble(ACCOUNT_BALANCE); double LotSizeCalculated = CalculateLotSize(RiskPercent, SL, AccountBalance); // Vérifier les conditions pour un achat if (EMA50 > EMA200 && RSI > 30 && RSI < 50 && MACD_Main > MACD_Signal && ADX > 25) { OpenTrade(ORDER_TYPE_BUY, LotSizeCalculated, price, price - SL, price + TP); } // Vérifier les conditions pour une vente else if (EMA50 < EMA200 && RSI > 50 && RSI < 70 && MACD_Main < MACD_Signal && ADX > 25) { OpenTrade(ORDER_TYPE_SELL, LotSizeCalculated, price, price + SL, price - TP); } } //+------------------------------------------------------------------+ //| Fonction pour calculer la taille du lot en fonction du risque | //+------------------------------------------------------------------+ double CalculateLotSize(double riskPercent, double stopLoss, double accountBalance) { double riskAmount = accountBalance * (riskPercent / 100); double lotSize = riskAmount / stopLoss; return NormalizeDouble(lotSize, 2); // Limiter à 2 décimales } //+------------------------------------------------------------------+ //| Fonction pour ouvrir une position | //+------------------------------------------------------------------+ void OpenTrade(int type, double lot, double price, double sl, double tp) { MqlTradeRequest request; MqlTradeResult result; request.action = TRADE_ACTION_DEAL; request.type = type; request.symbol = _Symbol; request.volume = lot; request.price = price; request.sl = sl; request.tp = tp; request.deviation = 10; request.magic = 123456; request.comment = "XAU/EUR Bot"; request.type_filling = ORDER_FILLING_FOK; request.type_time = ORDER_TIME_GTC; if(OrderSend(request, result) < 0) { Print("Erreur d'ouverture de position. Code d'erreur: ", GetLastError()); } else { Print("Position ouverte avec succès !"); } }