MQL programming -

 

Hi

I keep received a repeating "Buy hits 5" notification. How to adjust the programming to only SendNotification only 1 time ?


int OnInit()
  {
 
   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
   Comment ("number of Buy Position: ",CountBuyPositions());

        }

int CountBuyPositions()

{
int NumberOfBuyPositions=0;

for(int i=OrdersTotal()-1; i >= 0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

string CurrencyPair=OrderSymbol();

if(_Symbol==CurrencyPair)

if(OrderType()==OP_BUY)
   {
   NumberOfBuyPositions=NumberOfBuyPositions+1;
   }

}  
         if(NumberOfBuyPositions==5)
      SendNotification("BUY hits 5");  
return NumberOfBuyPositions;

}
Documentation on MQL5: Network Functions / SendNotification
Documentation on MQL5: Network Functions / SendNotification
  • www.mql5.com
true if a notification has been successfully sent from the terminal; in case of failure returns false. When checking after a failed push of notification, GetLastError () may return one of the following errors: Strict use restrictions are set for...
 
ken46rex:

Hi

I keep received a repeating "Buy hits 5" notification. How to adjust the programming to only SendNotification only 1 time ?

don't you want to sort your buy orders by magic number also and not just symbol?...plus i corrected return value of OrderSelect()

The way you have coded it all you will receive notifications as long as the count returns 5 and that is not how you want things right...?

Code it to sen a notification along with OrderSend() once, when order 5 sent, you get a notification...well that's how i should have done it....

This counter counts the total current open orders and you need to count a different way than this one....don't you want a count that only tells you when a 5th order been sent...?

extern int MagicNumber=123456;

int CountBuyPositions()
  {
   int NumberOfBuyPositions=0;
   for(int i=OrdersTotal()-1; i >= 0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         break;
      if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
        {
         if(OrderType()==OP_BUY)
            NumberOfBuyPositions++;
        }
     }
   return (NumberOfBuyPositions);
  }
 

try something like this, a different counting example just counting how many orders been sent, not how many 'total' you have open as the other did...try it

extern int MagicNumber=123456;
int ordercnt=0;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   SendBuy();
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SendBuy()
  {
   ticket = OrderSend(...);
   if(ticket<1)
     {
      Print("error");
      return;
     }
   else
     {
      Print("Buy order sent");
      ordercnt++;
      if(ordercnt==5)
      SendNotification("BUY hits 5");
     }
  }
 
ken46rex:

Hi

I keep received a repeating "Buy hits 5" notification. How to adjust the programming to only SendNotification only 1 time ?



you are calling sendnotification inside the loop..that's the root cause..

1. create a condition if the loop return any expected behavior/target

2. before returning NumberOfBuyPositions, you could send the notification if all desired condition is met

 

made a simple functional example for you with a buy order....as i understand you want the total open orders you have on current chart commented out plus you want a notification sent about when the 5th order been executed. However my example does not reset the counter, only deal with the 5th order and keep counting up.

Wish you all the best ;)

extern int MagicNumber=123456;
int ordercnt=0;
int take=100;
int stop=100;
int ticket=0;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   Comment("Total open : ",CountBuyPositions());
   if(Pos()==0)
      SendBuy();
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SendBuy()
  {
   ticket = OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Ask-stop*Point,Ask+take*Point,NULL,123456,0,Green);
   if(ticket<1)
     {
      Print("error");
      return;
     }
   else
     {
      Print("Buy order sent");
      ordercnt++;
      if(ordercnt==5)
         SendNotification("BUY hits 5");
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CountBuyPositions()
  {
   int NumberOfBuyPositions=0;
   for(int i=OrdersTotal()-1; i >= 0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         break;
      if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
        {
         if(OrderType()==OP_BUY)
            NumberOfBuyPositions++;
        }
     }
   return (NumberOfBuyPositions);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int Pos()
  {
   int poss=0;
   for(int k = OrdersTotal() - 1; k >= 0; k--)
     {
      if(!OrderSelect(k, SELECT_BY_POS))
         break;
      if(OrderSymbol()!=Symbol() && OrderMagicNumber()!=MagicNumber)
         continue;
      if(OrderCloseTime() == 0 && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         if(OrderType() == OP_BUY)
            poss = 1; 
         if(OrderType() == OP_SELL)
            poss = -1; 
        }
     }
   return(poss);
  }
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
roshjardine:

you are calling sendnotification inside the loop..that's the root cause..

1. create a condition if the loop return any expected behavior/target

2. before returning NumberOfBuyPositions, you could send the notification if all desired condition is met


hi Roshjardine

noted. how could i amend it? i m stuck on how to do it. ( honestly, i m newbie in the MQL programming). Appreciate if can amend from my code.

 
Kenneth Parling:

made a simple functional example for you with a buy order....as i understand you want the total open orders you have on current chart commented out plus you want a notification sent about when the 5th order been executed. However my example does not reset the counter, only deal with the 5th order and keep counting up.

Wish you all the best ;)

hi Kenneth.

thanks. that is my intention for my trade as I'd like to manage how many trade trigger from my EA and send notification + display in MT4 panel.

Thanks for the code too...appreciate it... i will try it out..