MT5 EA stop processing lines in OnTick()

 
Hi. I'm new to MT5 coding. I migrated from MT4 and found out really quick that MT5 is totally different from MT4. anyways, this is my problem. My codes are below. the code is placed inside OnTick() but it stops after a while.

I'm expecting it to keep looping (every time a tick is received). do 1, 2, 3 and keep looping in 4. I expect to see a message " Stop Order Placed. Waiting for it to be triggered" every time a tick is received.

what happens is that is stops somewhere in the middle after a while. even if i intervene and remove the stop order after 3 is executed, i expect it to loop and start from 1 and place another stop order at 3.

 please help.

// Variables
int i;
ulong ticket;
bool ans;

string   Target = "EURUSD";
int      Direction;
double   Price;
double   Lot;
double   SL;

bool   Stop_Order;
double Lots_Size_to_Pad = 0.01;
MqlTradeResult result = {0};
MqlTradeRequest request = {0};
double Pips_Clearance;
double Spread;
double Trail_Clearance;
MqlTick Tick;

//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Print (" ------------------ START OF EA ------------------ ");     
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+

void OnTick()
   {
   // 1. check for an open position. if there are no open position, do nothing.

   // Get all the Details about the Open Positions. 
//   Target = "EURUSD";
   PositionSelect(Target);                                                 // this line will select EURUS
   Direction = PositionGetInteger(POSITION_TYPE);                          // Get the Direction for the current position. Direction of 0 = BUY, 1 = SELL
   Price = NormalizeDouble(PositionGetDouble(POSITION_PRICE_OPEN),5);      // Get the Open Price
   Lot = PositionGetDouble(POSITION_VOLUME);                               // Get the Volume or Lot Size
   SL = PositionGetDouble(POSITION_SL);                                    // Get the Stop Loss value.
//   Print (" Symbol = ", Target," Direction is = ", Direction," Price is = ", Price," Lot Size is = ", Lot," SL is = ", SL);
   
   // create "Tick" to be able to call bid and ask 
   SymbolInfoTick(Target,Tick);
   
   // invoke codes only if there is an open position.
   if (Lot !=0)   
      {
      // 2. if there is an open position, check for an already exisitng stop order. if a stop order is already open, delete it if it is wrong.
      Stop_Order = false;
      for (i=0; i <= OrdersTotal()-1; i++)
         {      
         if ( Target = OrderGetString(ORDER_SYMBOL) )
            {
            // Pair Pending Order Found!
            Stop_Order = true;                                                      // Marker to identify that a Stop was seen and confirmed correct.
            ans=OrderSelect(OrderGetTicket(i));                                     // Select the order matching the Target.
            if (ans = false) Print ("Error Seen = ", GetLastError());
            // Check if the Pending Order is correct. Correct means:
            // Open Price are the same.
            // Direction is the same. Sell + 4 = Sell STop, Buy + 4 = Buy Stop
            if ( Direction + 4  == OrderGetInteger(ORDER_TYPE) && Price == OrderGetDouble(ORDER_PRICE_OPEN) && Lots_Size_to_Pad == OrderGetDouble(ORDER_VOLUME_CURRENT) )
               {
               Print (" Pending order is correct.");
               }
               else
               {
               Print ("Order seen but it is incorrect. Order Number ", OrderGetTicket(i) );
               // Here is where we remove the old stop order and place a new one. 
               request.order = OrderGetTicket(i);
               request.action = TRADE_ACTION_REMOVE;
               ans=OrderSend(request,result);
               if (ans = false) Print ("Error Seen = ", GetLastError());
               Print (OrderGetTicket(i) , " has seen been deleted.");
               }
            }
         }
      Pips_Clearance = NormalizeDouble(20 * Point(),5); 
      // 3. if there is no stop order, place one.
      if ( (Tick.bid - Pips_Clearance > Price && Direction == 1 && Stop_Order == false) ||  (Price > Tick.ask + Pips_Clearance && Direction == 0 && Stop_Order == false)  )
         {
         // Place a Stop Order here.
         request.action = TRADE_ACTION_PENDING;
         request.symbol = Target;
         request.volume = Lots_Size_to_Pad;
         request.price = Price;
         request.tp = 0;
         request.deviation = 0.5;
         if (Direction == 0) request.type = ORDER_TYPE_BUY_STOP;
         if (Direction == 1) request.type = ORDER_TYPE_SELL_STOP;
         request.type_filling = 0;
         ans=OrderSend(request,result);
         if (ans = false) Print ("Error Seen = ", GetLastError());
         Print ("No Orders seen. Place a Correct one now. Target ", request.symbol, " Price = ", request.price, " SL = ", request.sl);
         Sleep(1000);
         }

      // 4. wait for the stop order to get triggered. NO ACTION REQUIRED.
      if (Stop_Order == true)
         {
         Print (" Stop Order Placed. Waiting for it to be triggered");
         }

      // 5. once a threashold is reached, move sl to be.
      if ( ( (Tick.ask < Price - Pips_Clearance ) && Direction == 1 && SL == 0) || ((Tick.bid > Price + Pips_Clearance) && Direction == 0 && SL == 0)  )
         // if price is 2pips + freeze points into the money, move SL to BE.
         {
         if (Direction == 0) request.sl = Price ;
         if (Direction == 1) request.sl = Price ;
         request.action = TRADE_ACTION_SLTP;
         request.symbol = Target;
         request.tp = 0;
         //ans=OrderSend(request,result);
         //if (ans = false) Print ("Error Seen = ", GetLastError());
         Print (" Move SL to BE. SL = ",request.sl);  
         }
     
      Spread = NormalizeDouble(SymbolInfoInteger(Target,SYMBOL_SPREAD) * 10 * Point(),5);
      Trail_Clearance = NormalizeDouble(70 * Point(),5) ;      
     // 6. once a trailing buffer is reached, move sl forward and trail SL.
      if ( ( (Tick.ask < Price - Trail_Clearance) && Direction == 1 && SL == 0 && SL > Tick.ask + Trail_Clearance ) || ((Tick.bid > Price + Trail_Clearance) && Direction == 0 && SL == 0 && SL < Tick.bid - Trail_Clearance)  )
         {
         // setup all the needed variables.
         if (Direction == 0) request.sl = Tick.bid - Trail_Clearance;
         if (Direction == 1) request.sl = Tick.ask + Trail_Clearance;
         request.action = TRADE_ACTION_SLTP;
         request.symbol = Target;
         request.tp = 0;
         ans = OrderSend(request,result);
         if (ans = false) Print ("Error Seen = ", GetLastError());
         Print ("Target ", request.symbol, " New SL = ", request.sl);
         }
      }
   }

 

 

I didn't check all your code's details, but just some thoughts :

do 1, 2, 3 and keep looping in 4.

  • You don't have a loop in your code, don't expect "keep looping in 4".
  • PositionSelect() is a function which returns a value, you have to check this value.
  • Wrong if conditions, should be '==' :
         if ( Target = OrderGetString(ORDER_SYMBOL) )
if (ans = false)
  • I suggest you to read the documentation of OrderSend(), you didn't check the returned code.

...

 

thanks for the feedback I'll check these out.

 

about " keep looping", what i mean is that every time a tick is received,  the codes will be executed, right? so in essence, these codes will keep looping. 

not checking the return code for OrderSend() means the EA will stop processing and not proceed even after a tick is received ? 

 
Jonatslim:

thanks for the feedback I'll check these out.

 

about " keep looping", what i mean is that every time a tick is received,  the codes will be executed, right? so in essence, these codes will keep looping.

Ok, but that's not looping.


not checking the return code for OrderSend() means the EA will stop processing and not proceed even after a tick is received ? 

That means you can have errors and you need to check for them.
Reason: