OrderSend error 4756 Invalid request
luisfermartinezricardo123:
Please insert the code correctly: when editing a message, press the button I'm sending an order but I get error 4756 with the Invalid request detail, I already tried the code in MetaQuotes it works fine but as soon as I put it in the mt5 of the broker it doesn't work, the code is as follows.
I use the 'CTrade' trading class - this avoids a lot of mistakes. Code example:
//+------------------------------------------------------------------+ //| Stop Loss Take Profit.mq5 | //| Copyright © 2016-2022, Vladimir Karputov | //| https://www.mql5.com/en/users/barabashkakvn | //+------------------------------------------------------------------+ #property copyright "Copyright © 2016-2022, Vladimir Karputov" #property link "https://www.mql5.com/en/users/barabashkakvn" #property version "1.007" #property description "Take Profit and Stop Loss - in Points (1.00055-1.00045=10 points)" #include <Trade\PositionInfo.mqh> #include <Trade\Trade.mqh> #include <Trade\SymbolInfo.mqh> //--- CPositionInfo m_position; // object of CPositionInfo class CTrade m_trade; // object of CTrade class CSymbolInfo m_symbol; // object of CSymbolInfo class //--- input parameters input group "Trading settings" input uint InpStopLoss = 150; // Stop Loss input uint InpTakeProfit = 460; // Take Profit input group "Position size management (lot calculation)" input double InpLots = 0.01; // Lots input group "Additional features" input ulong InpDeviation = 10; // Deviation, in Points (1.00045-1.00055=10 points) input ulong InpMagic = 200; // Magic number //--- double m_stop_loss = 0.0; // Stop Loss -> double double m_take_profit = 0.0; // Take Profit -> double //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- forced initialization of variables m_stop_loss = 0.0; // Stop Loss -> double m_take_profit = 0.0; // Take Profit -> double //--- ResetLastError(); if(!m_symbol.Name(Symbol())) // sets symbol name { Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name"); return(INIT_FAILED); } RefreshRates(); //--- m_trade.SetExpertMagicNumber(InpMagic); m_trade.SetMarginMode(); m_trade.SetTypeFillingBySymbol(m_symbol.Name()); m_trade.SetDeviationInPoints(InpDeviation); //--- m_stop_loss = InpStopLoss * m_symbol.Point(); m_take_profit = InpTakeProfit * m_symbol.Point(); //--- Initialize the generator of random numbers MathSrand(GetTickCount()); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(CalculateAllPositions()==0) { if(!RefreshRates()) return; //--- odd (1) - "BUY", even (2) - "SELL" int math_rand=MathRand(); if(math_rand%2==0) { double sl=(m_stop_loss==0.0)?0.0:m_symbol.Ask()-m_stop_loss; double tp=(m_take_profit==0.0)?0.0:m_symbol.Ask()+m_take_profit; m_trade.Buy(InpLots,m_symbol.Name(),m_symbol.Ask(),m_symbol.NormalizePrice(sl),m_symbol.NormalizePrice(tp)); } else { double sl=(m_stop_loss==0.0)?0.0:m_symbol.Bid()+m_stop_loss; double tp=(m_take_profit==0.0)?0.0:m_symbol.Bid()-m_take_profit; m_trade.Sell(InpLots,m_symbol.Name(),m_symbol.Bid(),m_symbol.NormalizePrice(sl),m_symbol.NormalizePrice(tp));; } } //--- } //+------------------------------------------------------------------+ //| Refreshes the symbol quotes data | //+------------------------------------------------------------------+ bool RefreshRates() { //--- refresh rates if(!m_symbol.RefreshRates()) { Print(__FILE__," ",__FUNCTION__,", ERROR: ","RefreshRates error"); return(false); } //--- protection against the return value of "zero" if(m_symbol.Ask()==0 || m_symbol.Bid()==0) { Print(__FILE__," ",__FUNCTION__,", ERROR: ","Ask == 0.0 OR Bid == 0.0"); return(false); } //--- return(true); } //+------------------------------------------------------------------+ //| Calculate all positions Buy and Sell | //+------------------------------------------------------------------+ int CalculateAllPositions() { int totlal=0; for(int i=PositionsTotal()-1; i>=0; i--) if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic) totlal++; //--- return(totlal); } //+------------------------------------------------------------------+
Result:

Files:
luisfermartinezricardo123 # :
Thank you very much, I managed to solve it. It works great for me, but I have another question, how do I get the number of the ticket that was created?
Thank you very much, I managed to solve it. It works great for me, but I have another question, how do I get the number of the ticket that was created?
Clarify your question - why do you need it?
I use full control of trading orders according to the algorithm described in the article 'An attempt at developing an EA constructor'
An attempt at developing an EA constructor
- www.mql5.com
In this article, I offer my set of trading functions in the form of a ready-made EA. This method allows getting multiple trading strategies by simply adding indicators and changing inputs.
I'm sending an order but I get error 4756 with the Invalid request detail, I already tried the code in MetaQuotes it works fine but as soon as I put it in the mt5 of the broker it doesn't work, the code is as follows.