Why Does MQL5 Behave Unexpectedly

 

I have a script in MQL5 derived from MQL4 with the appropriate libraries to make it 'work' on MQL5. It does work... but only when it feels like it. Here is the piece of code in MQL5 derived from MQL4.

   string _Apri_Primario(string oPen,double lott, int slipp,double STp,double TKp,string Commento){
       int Result=0;
      if (oPen=="BUY"){
             Result = OrderSend(_Symbol,OP_BUY,lott,MarketInfo(_Symbol, MODE_ASK),slipp,STp,TKp,IntegerToString(Magik)+Commento+IntegerToString(Enteglement),Magik,0,clrGreen);
               if ( Result ==-1){
               Print("op_BUY "+Commento ,GetLastError());
               }
         }else if (oPen=="SELL"){
             Result = OrderSend(_Symbol,OP_SELL,lott,MarketInfo(_Symbol, MODE_BID),slipp,STp,TKp,IntegerToString(Magik)+Commento+IntegerToString(Enteglement),Magik,0,clrGreen);
               if ( Result ==-1){
               Print("op_SELL "+Commento ,GetLastError());
               }
          }                       
         return IntegerToString(Result);
   }


   string _Apri_Correlato(string oPen,double lott, int slipp,double STp,double TKp,string Commento){
      int Result=0;
        Print("RISULTATO "+Cross_Correlato+oPen);
      if (oPen=="BUY"){
             Result = OrderSend(Cross_Correlato,OP_BUY,lott,MarketInfo(_Symbol, MODE_ASK),slipp,STp,TKp,IntegerToString(Magik)+Commento+IntegerToString(Enteglement),Magik,0,clrGreen);
               if ( Result ==-1){
               Print("op_BUY "+Commento+"_Result_"+Result ,GetLastError());
               }
         }else if (oPen=="SELL"){
             Result = OrderSend(Cross_Correlato,OP_SELL,lott,MarketInfo(_Symbol, MODE_BID),slipp,STp,TKp,IntegerToString(Magik)+Commento+IntegerToString(Enteglement),Magik,0,clrGreen);
               Print("RISULTATO "+Result+"_"+Cross_Correlato);
               if ( Result ==-1){
               Print("op_SELL "+Commento ,GetLastError());
               }
          }                       
         return IntegerToString( Result);
   }
   
If in MQL5 I launch Apri_Primario where I have _Symbol in OrderSend like OrderSend(_Symbol, OP_BUY, lott, MarketInfo(_Symbol, MODE_ASK), slipp, STp, TKp, IntegerToString(Magik) + Commento + IntegerToString(Enteglement), Magik, 0, clrGreen); , it works fine. However, if I replace _Symbol with the string containing the cross I need, Cross_Correlato , it doesn’t work. I have a few questions: 1. Is it possible to make it work by forcing it in some way?
 

you have not shown code that defines Cross_Correlo so how do you expect a response?

I recommend that you use the search field at top of this page. There are many articles and forum threads discussing multi symbol trading.

EDIT: could it be because you replaced _Symbol with Cross_Correlo, but still have _Symbol in the MarketInfo?

 

⚠️ Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893


⚠️You have been informed multiple times! Please do not disregard!

 
faustf:

I have a script in MQL5 derived from MQL4 with the appropriate libraries to make it 'work' on MQL5. It does work... but only when it feels like it. Here is the piece of code in MQL5 derived from MQL4.

If in MQL5 I launch Apri_Primario where I have _Symbol in OrderSend like OrderSend(_Symbol, OP_BUY, lott, MarketInfo(_Symbol, MODE_ASK), slipp, STp, TKp, IntegerToString(Magik) + Commento + IntegerToString(Enteglement), Magik, 0, clrGreen); , it works fine. However, if I replace _Symbol with the string containing the cross I need, Cross_Correlato , it doesn’t work. I have a few questions: 1. Is it possible to make it work by forcing it in some way?

You are requesting _Symbol in Market Info but are trading Correlatio 

Correlatio means correlation probably so make sure your data points for the calculation of correlation are synced across pairs
 
Michael Charles Schefe #:

you have not shown code that defines Cross_Correlo so how do you expect a response?

I recommend that you use the search field at top of this page. There are many articles and forum threads discussing multi symbol trading.

EDIT: could it be because you replaced _Symbol with Cross_Correlo, but still have _Symbol in the MarketInfo?

input string   Cross_Correlato="NZDUSD";          // CROSS CORRELATO

 
Lorentzos Roussos #:

You are requesting _Symbol in Market Info but are trading Correlatio 

Correlatio means correlation probably so make sure your data points for the calculation of correlation are synced across pairs

thanks  yes i fix it 

string _Apri_Correlato(string oPen,double lott, int slipp,double STp,double TKp,string Commento){
      int Result=0;
        Print("RISULTATO "+Cross_Correlato+oPen);
      if (oPen=="BUY"){
       Print (Cross_Correlato+"_"+OP_SELL+"_"+lott+"_"+Cross_Correlato+"_"+ MODE_ASK+"_"+slipp+"_"+STp+"_"+TKp+"_"+IntegerToString(Magik)+"_"+Commento+"_"+IntegerToString(Enteglement)+"_"+Magik+"_"+0+"_"+clrGreen);
       
             Result = OrderSend(Cross_Correlato,OP_BUY,lott,MarketInfo(Cross_Correlato, MODE_ASK),slipp,STp,TKp,IntegerToString(Magik)+Commento+IntegerToString(Enteglement),Magik,0,clrGreen);
               if ( Result ==-1){
               Print("op_BUY "+Commento+"_Result_"+Result ,GetLastError());
               }
         }else if (oPen=="SELL"){
           Print (Cross_Correlato+"_"+OP_SELL+"_"+lott+"_"+Cross_Correlato+"_"+ MODE_ASK+"_"+slipp+"_"+STp+"_"+TKp+"_"+IntegerToString(Magik)+"_"+Commento+"_"+IntegerToString(Enteglement)+"_"+Magik+"_"+0+"_"+clrGreen);
               Result = OrderSend(Cross_Correlato,OP_SELL,lott,MarketInfo(Cross_Correlato, MODE_BID),slipp,STp,TKp,IntegerToString(Magik)+Commento+IntegerToString(Enteglement),Magik,0,clrGreen);
               Print("RISULTATO "+Result+"_"+Cross_Correlato);
               if ( Result ==-1){
               Print("op_SELL "+Commento ,GetLastError());
               }
          }                       
         return IntegerToString( Result);
   }
   
but  return the same effect , i notice the info debug
 Print("RISULTATO "+Cross_Correlato+oPen);
write correctly 2024.10.28 09:34:52.572    SPREAD_ROBOT (AUDJPY,H1)    RISULTATO NZDUSDBUY   and  this 
 Print (Cross_Correlato+"_"+OP_SELL+"_"+lott+"_"+Cross_Correlato+"_"+ MODE_ASK+"_"+slipp+"_"+STp+"_"+TKp+"_"+IntegerToString(Magik)+"_"+Commento+"_"+IntegerToString(Enteglement)+"_"+Magik+"_"+0+"_"+clrGreen);

return 2024.10.28 09:34:52.572    SPREAD_ROBOT (AUDJPY,H1)    NZDUSD_1_1.0_NZDUSD_10_2_0.0_0.0_123456__SPREAD_0003__4_123456_0_clrGreen

therefore i pass correctly info to ordersend  , the  question is _Symbol  return a string  ?  because NZDUSD  that i passed is a string i declare in top of my code " input string   Cross_Correlato="NZDUSD";          // CROSS CORRELATO "

 
io notice also if i use  the expert  over crypto with BTCUSD  and open correlation ETHUSD  work , but if i did do with forex  not  work why ? in the sae broker in mt4  work forex  and  all o_O
 

without seeing your calculations of the functions inputs. we can only make assumptions. These may be correct, but might not be. We can only guess that your calculations are wrong for either or both the price and/or the trading lot. Maybe you are not switching to the other trading symbols. How do you expect anyone to give you answers if you only provide the last bit of code above?

EDIT: I noted this...

if (oPen=="BUY"){
       Print (Cross_Correlato+"_"+OP_SELL+"_"+lott+"_"+Cross_Correlato+"_"+ MODE_ASK+"_"+slipp+"_"+ST

if it is a buy. then shouldnt the print msg say OP_BUY and MarketInfo(Cross_Correlato,MODE_ASK) ??? replace the MODE_ASK with the MarketInfo func.

 
Michael Charles Schefe #:

without seeing your calculations of the functions inputs. we can only make assumptions. These may be correct, but might not be. We can only guess that your calculations are wrong for either or both the price and/or the trading lot. Maybe you are not switching to the other trading symbols. How do you expect anyone to give you answers if you only provide the last bit of code above?

EDIT: I noted this...

if it is a buy. then shouldnt the print msg say OP_BUY and MarketInfo(Cross_Correlato,MODE_ASK) ??? replace the MODE_ASK with the MarketInfo func.

i change  my function and i use a mql5 syntax

string _Apri_Correlato(string oPen, double lott, int slipp, double STp, double TKp, string Commento) {

    MqlTradeRequest request;
    MqlTradeResult result;
    int errorCode;

    // Inizializza la struttura della richiesta di trading
    ZeroMemory(request);
    request.action = TRADE_ACTION_DEAL;
    request.volume = lott;
    request.symbol = Cross_Correlato;
    request.deviation = slipp;  // Gestione dello slippage
    request.comment = IntegerToString(Magik) + Commento + IntegerToString(Enteglement);
    request.magic = Magik;
    request.tp = 0;  // Nessun take profit
    request.sl = 0;  // Nessuno stop loss

    // Stampa di debug per verificare i valori iniziali
    Print("DEBUG: Inizio funzione _Apri_Correlato con parametri:");
    Print("oPen: ", oPen, ", lott: ", lott, ", slipp: ", slipp, ", Commento: ", Commento);
    Print("Cross_Correlato: ", Cross_Correlato, ", Magik: ", Magik, ", Enteglement: ", Enteglement);

    // Ottieni il prezzo corrente per il simbolo
    double askPrice = SymbolInfoDouble(Cross_Correlato, SYMBOL_ASK);
    double bidPrice = SymbolInfoDouble(Cross_Correlato, SYMBOL_BID);

    // Controlla che i prezzi siano validi
    if (askPrice <= 0 || bidPrice <= 0) {
        Print("DEBUG ERROR: Prezzi non validi - Ask: ", askPrice, ", Bid: ", bidPrice);
        return "-1";
    }
    Print("DEBUG: Prezzi ottenuti - Ask: ", askPrice, ", Bid: ", bidPrice);

    // Calcola lo spread corrente
    double spread = askPrice - bidPrice;
    Print("DEBUG: Spread corrente: ", DoubleToString(spread, _Digits));

    // Ottieni il livello minimo di stop richiesto dal broker (in punti)
    double minStopLevelPoints = SymbolInfoInteger(Cross_Correlato, SYMBOL_TRADE_STOPS_LEVEL) * SymbolInfoDouble(Cross_Correlato, SYMBOL_POINT);
    Print("DEBUG: Livello minimo di stop richiesto (punti): ", minStopLevelPoints);

    // Verifica se lo spread è troppo alto rispetto al livello minimo di stop
    if (spread >= minStopLevelPoints) {
        Print("DEBUG ERROR: Lo spread corrente è troppo ampio per inviare l'ordine. Spread: ", spread, ", MinStopLevel: ", minStopLevelPoints);
        return "-1";  // Esci dalla funzione senza inviare l'ordine
    }

    // Determina se è un ordine di acquisto o vendita e imposta il prezzo
    if (oPen == "BUY") {
        request.type = ORDER_TYPE_BUY;
        request.price = askPrice;
        Print("DEBUG: Tipo ordine BUY, prezzo impostato: ", request.price);
    } else if (oPen == "SELL") {
        request.type = ORDER_TYPE_SELL;
        request.price = bidPrice;
        Print("DEBUG: Tipo ordine SELL, prezzo impostato: ", request.price);
    } else {
        Print("DEBUG ERROR: Tipo di ordine non valido: ", oPen);
        return "-1";
    }

    // Stampa di debug per verificare il prezzo di apertura
    Print("DEBUG: Prezzo di apertura per l'ordine: ", request.price);

    // Stampa di debug prima di inviare l'ordine
    Print("DEBUG: Tentativo di invio ordine - Symbol: ", request.symbol, ", Volume: ", request.volume);
    Print("DEBUG: Azione: ", request.action, ", Tipo: ", request.type, ", Prezzo: ", request.price);
    Print("DEBUG: Deviation: ", request.deviation, ", Commento: ", request.comment, ", Magic: ", request.magic);

    // Invia la richiesta di trading
    if (!OrderSend(request, result)) {
        errorCode = GetLastError();
        Print("DEBUG ERROR: OrderSend fallito con errore: ", errorCode);
        return "-1";
    } else {
        Print("DEBUG: OrderSend riuscito, risultato dell'ordine: ", result.retcode);
        if (result.retcode != TRADE_RETCODE_DONE) {
            Print("DEBUG WARNING: Trade non completato, codice di ritorno: ", result.retcode);
            return "-1";
        }
    }

    // Stampa di debug per confermare il successo dell'operazione
    Print("DEBUG: Ordine eseguito con successo, Ticket: ", result.order);

    // Restituisce il ticket dell'ordine se l'operazione è stata eseguita con successo
    return IntegerToString(result.order);
}

now  return me this error, some test ago return also

2024.10.28 11:53:35.228 SPREAD_ROBOT (AUDJPY,H1) DEBUG ERROR: OrderSend fallito con errore: 4756  
2024.10.28 12:23:49.788 SPREAD_ROBOT (AUDJPY,H1)        DEBUG: Inizio funzione _Apri_Correlato con parametri:
2024.10.28 12:23:49.788 SPREAD_ROBOT (AUDJPY,H1)        oPen: BUY, lott: 1.0, slipp: 2, Commento: _SPREAD_0003_
2024.10.28 12:23:49.788 SPREAD_ROBOT (AUDJPY,H1)        Cross_Correlato: NZDUSD, Magik: 123456, Enteglement: 2
2024.10.28 12:23:49.788 SPREAD_ROBOT (AUDJPY,H1)        DEBUG: Prezzi ottenuti - Ask NZDUSD: 0.5985, Bid NZDUSD: 0.59847
2024.10.28 12:23:49.788 SPREAD_ROBOT (AUDJPY,H1)        DEBUG: Spread corrente: 0.000
2024.10.28 12:23:49.788 SPREAD_ROBOT (AUDJPY,H1)        DEBUG: Livello minimo di stop richiesto (punti): 0.0
2024.10.28 12:23:49.788 SPREAD_ROBOT (AUDJPY,H1)        DEBUG ERROR: Lo spread corrente è troppo ampio per inviare l'ordine. Spread: 0.000030000000000085514, MinStopLevel: 0.0
 
How many follow-ups, I finally found the Arcano. Basically, if you are on one chart and want to open another cross, if you haven't opened any trades on that cross before, it won't let you open it. If you open a trade manually once and then use an expert advisor, it works. However, I don't understand if this limitation is intrinsic to MetaTrader 5 or if my script is causing it unintentionally. I tend to lean towards the first option, especially since in MetaTrader 4 it has always allowed trades to be opened even if the cross had never been worked on before. Do you happen to have any insights? Thanks.

 
faustf #:
How many follow-ups, I finally found the Arcano. Basically, if you are on one chart and want to open another cross, if you haven't opened any trades on that cross before, it won't let you open it. If you open a trade manually once and then use an expert advisor, it works. However, I don't understand if this limitation is intrinsic to MetaTrader 5 or if my script is causing it unintentionally. I tend to lean towards the first option, especially since in MetaTrader 4 it has always allowed trades to be opened even if the cross had never been worked on before. Do you happen to have any insights? Thanks.

That is not correct it is a symptom of you not ensuring that the data for your non-chart symbol is updated before trying to access it
There are multiple threads on how to deal with the problem so do a search