MQL4 Order Send Fine But Nothing Happen, No Order Send. Can be a latency problem?

 

MQL4 Order Send Fine But Nothing Happen, No Order Send. Can be a latency problem?

Hi All, this code sometimes return me nothing after having all condition met for open an order. No error, nothing. Is it possible that in some ms price changed and order automatically discarded? Thanks in advance, I don't know anymore where to search.. How You can see here when PlaceOrder called.. BUY or SELL and nothing happen before.. in the first you can see first an issue with USDCAD, after 9 minutes another signal occurred and ea opened correctly. The placeorder code is free for You



bool PlaceOrder(int type, int magic, int id, bool checkmargin = false)
        {
         Print("PlaceOrder called.. Type : "+TypeToStr(type));
         double entry, lot;
         color  col;
         int typet;
         int i = 0;
         while(i < NumRetries)
           {
            i += 1;
            while(IsTradeContextBusy())
               Sleep(RetryDelayInSec*1000);
            RefreshRates();
            if(type == OP_BUY)
              {
               entry = Ask;
               col   = Blue;
               typet = 2;
              }
            else
              {
               entry = Bid;
               col   = Red;
               typet = 4;
              }
            if(MathAbs(entry-Close[1]) > stoplevel)
               break;  //--Controllo che non ci siano stati spike nel prezzo (es. new impattante)
            if(LotCalculation == FixedLots)
               lot = FixedLotSize;
            else
               lot = GetLotSize(type, entry);
            Print("Try "+i+" : Open "+TypeToStr(type)+" order. Entry : "+DoubleToStr(entry,Digits)+" Lot :"+lot+" Magic :"+magic);
            if(checkmargin)
              {
               double margin_required = 2 * MarketInfo(Symbol(),MODE_MARGINREQUIRED)*lot;
               double initloss = MathCeil((InitialPL() + CommissionPerLot)*lot);
               Print("margin_required to open 2 trades : "+margin_required+" initloss : "+initloss+" AccountFreeMargin : "+AccountFreeMargin());
               if(AccountFreeMargin() <= margin_required+initloss)
                 {
                  Print("Unable to place order due to insufficient margin");
                  return(false);
                 }
              }       
            int ticket = OrderSend(Symbol(),type,lot,entry,Slippage,0,0,TradeComment,magic,0,col);     
            if(ticket <= 0)
              {
               Print("ERROR opening market order. ErrorCode:"+GetLastError());
               if(i == NumRetries)
                 {
                  Print("*** Final retry to OPEN ORDER failed ***");
                  return(false);
                 }
              }
            else
              {
               SetSLandTP(ticket, id);
               i = NumRetries;
               return(true);
              }
           }
         return(false);
        }
 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

What is the variable typet for, you don't use it for anything?

            if(MathAbs(entry-Close[1]) > stoplevel)
               break;  //--Controllo che non ci siano stati spike nel prezzo (es. new impattante)

What is the variable stoplevel? What does the difference between entry and Bar[1] close price have to do with anything? 

This can prevent a trade being opened, but you don't print anything to report that.

This was probably what stopped the order being executed as this print didn't happen

            Print("Try "+i+" : Open "+TypeToStr(type)+" order. Entry : "+DoubleToStr(entry,Digits)+" Lot :"+lot+" Magic :"+magic);
 

Sorry Keith, didn't see that was mql5 forum. 

double stoplevel = MarketInfo(Symbol(), MODE_STOPLEVEL)*Point + MarketInfo(Symbol(),MODE_SPREAD)*Point;  //-- return min distance for tp/sl


entry and Bar[1] close can be really distant if we have a news release or something that makes moves market really fast, so I want check that my rates are distant at max my stoplevel (otherwise I can also put number of pips to check).

and you are right, I'll put an Alert if this will be true. Thanks

if(MathAbs(entry-Close[1]) > stoplevel)
 *typet is used for limit orders but it's not implemented at the moment
 
Luca Bruseghini: MQL4 Order Send Fine But Nothing Happen, No Order Send. Can be a latency problem?

You have a print statement if the OrderSend fails. Since the log does not contain that, you are not calling OrderSend.

Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?

 

William, I really like your copy pasted insults :)

I was asking "Can be a latency problem?" the code was here only for your information. I'm new to algo trading and some tricks and things that only experience can give I havent't yet.. thanks anyway for the work you do on the forum

 
Luca Bruseghini: William, I really like your copy pasted insults :) I was asking "Can be a latency problem?" the code was here only for your information. I'm new to algo trading and some tricks and things that only experience can give I havent't yet.. thanks anyway for the work you do on the forum

He was not insulting you and gave you valuable guidance, which you have decided to ignore because your ego was bruised.

From your log it can clearly be seen that your function "PlaceOrder" is called but the OrderSend function is NOT! I repeat, it is NOT called.

He was telling you track the problem by checking the flow of you code logic (use prints at every outcome if possible), and to check the Last Error codes, and to debug to find out why that part of the code was never reached.

EDIT: Just to clarify as William told you, your print (see below) never occurred, so most probably the loop never executed either and NumRetries is actualy 0 or less maybe. So "debug" and find out why!

Print("Try "+i+" : Open "+TypeToStr(type)+" order. Entry : "+DoubleToStr(entry,Digits)+" Lot :"+lot+" Magic :"+magic);
 
Fernando Carreiro:

EDIT: Just to clarify as William told you, your print (see below) never occurred, so most probably the loop never executed either and NumRetries is actualy 0 or less maybe. So "debug" and find out why!

Print("Try "+i+" : Open "+TypeToStr(type)+" order. Entry : "+DoubleToStr(entry,Digits)+" Lot :"+lot+" Magic :"+magic);

 As I had already said in #1

Keith Watford:

What is the variable stoplevel? What does the difference between entry and Bar[1] close price have to do with anything? 

This can prevent a trade being opened, but you don't print anything to report that.

This was probably what stopped the order being executed as this print didn't happen

            Print("Try "+i+" : Open "+TypeToStr(type)+" order. Entry : "+DoubleToStr(entry,Digits)+" Lot :"+lot+" Magic :"+magic);
 
Keith Watford As I had already said in #1

Yes, you are correct! Sorry, Keith! I was so angry at the OP for being short-sighted that I did not even notice your post!

 
 if(MathAbs(entry-Close[1]) > stoplevel)
               break;

The "problem" was here, now I've added this print statement:

if(MathAbs(entry-Close[1]) > stoplevel){
         Print("Skipped Entry on " + Symbol() + " Due To 'StopLevel > GAP of Close - Act Price");
         break;  //--Controllo che non ci siano stati spike nel prezzo (es. new impattante)

It's "unusual" because i don't use Japanese candlestick and some ms of latency makes the difference with my scripts. Sorry to all if You felt disrespected

Reason: