OrdersTotal in a specif pair of currencies

 
Hi guys!

I have another problem…

I want make this:

if(the number of orders in EURGBP < 2){do it…}

I know the function OrdersTotal(), but I don’t want to know the total, I want to know the total in EURGBP! So, how can I do it?

Thanks again!!
 
JHenry:
Hi guys!

I have another problem…

I want make this:

if(the number of orders in EURGBP < 2){do it…}

I know the function OrdersTotal(), but I don’t want to know the total, I want to know the total in EURGBP! So, how can I do it?

Thanks again!!

You can do it like this

   int totalForCurrentSymbol = 0;
   for (int i = OrdersTotal()-1; i>=0; i--)
   {
      if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if (OrderSymbol()      != "EURGBP")            continue;
         totalForCurrentSymbol++;
   }         
And after that the totalForSymbol will contain the exact number of orders currently opened for EURGBP. Then it is just a matter of comparison
 
mladen:

You can do it like this

And after that the totalForSymbol will contain the exact number of orders currently opened for EURGBP. Then it is just a matter of comparison
Didn't work! :/

Now, the EA is opening SEVERAL orders, inifinite orders...
 
mladen:

You can do it like this

And after that the totalForSymbol will contain the exact number of orders currently opened for EURGBP. Then it is just a matter of comparison
Mladen!?!? Where are you!? :(((
 
JHenry:
Didn't work! :/

Now, the EA is opening SEVERAL orders, inifinite orders...

If you check the following :

if (totalForCurrentSymbol==0) ... // then you can open new order for EURGBP in that example

it will work and there is no way that then you can open infinite orders for EURGBP. The code I posted counts open orders for the symbol you need. I am assuming that you can use the values stored in the totalForCurrent symbol in the rest of your code (of which I have no idea what are you doing with it)

 
mladen:

If you check the following :

it will work and there is no way that then you can open infinite orders for EURGBP. The code I posted counts open orders for the symbol you need. I am assuming that you can use the values stored in the totalForCurrent symbol in the rest of your code (of which I have no idea what are you doing with it)

Thank you very much again! :)

But, I'm not getting the wished result...

But, the EA continues opening orders and more orders and more orders... 

Look the code after your tip:

//+------------------------------------------------------------------+
//|                                               Ben_trail_stop.mq4 |
//|                            Copyright © 2010, www.compu-forex.com |
//|                                              www.compu-forex.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, www.compu-forex.com"
#property link      "www.compu-forex.com"

//---- input parameters
extern int        MagicNumber     = 0;
extern double     Trail_From     = 10.0;
extern double     Trail_Max      = 50.0;
extern double     Trail_Percent  = 50;
extern int        Equity_Cut     =10;

int totalForCurrentSymbol = 0;

int   
   D_Factor = 1;
double   
   Pip;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{


for (int i = OrdersTotal()-1; i>=0; i--)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderSymbol()      != "BTCEUR")            continue;
         totalForCurrentSymbol++;
   }         


   Pip = Point;
   if (Digits == 3||Digits == 5){Pip *= 10; D_Factor = 10;}
   Trail_From *= Pip;
   Trail_Max  *= Pip;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   if(totalForCurrentSymbol==0){
      OrderSend(Symbol(), 0, 0.01, Ask, 3, 0 ,0 ,0, 123, 0,CLR_NONE);
      OrderSend(Symbol(), 1, 0.01, Bid, 3, 0 ,0 ,0, 123, 0,CLR_NONE);
   }
bool
   mod;
double
   My_Profit,
   My_Trail;
//----
   for (int i = 0; i < OrdersTotal(); i++){
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderMagicNumber() == MagicNumber || MagicNumber == 0){
            RefreshRates();
            switch(OrderType()){
               case OP_BUY :  My_Profit = MarketInfo(OrderSymbol(), MODE_BID) - OrderOpenPrice();
                              My_Trail = MathMin(My_Profit * Trail_Percent/100,Trail_Max);
                              if(My_Profit > Trail_From){
                                 if(OrderStopLoss() == 0)mod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+My_Trail,Digits),OrderTakeProfit(),0, Red);
                                 if(OrderStopLoss() < OrderOpenPrice() + My_Trail){
                                    mod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+My_Trail,Digits),OrderTakeProfit(),0, Red);
                                    if(!mod && GetLastError() > 1)Print("Error entering Trailing Stop - Error " + GetLastError());
                                 }
                              }
                              break;
                        
               case OP_SELL : My_Profit = OrderOpenPrice() - MarketInfo(OrderSymbol(), MODE_ASK);
                              My_Trail = MathMin(My_Profit * Trail_Percent/100,Trail_Max);
                              if(My_Profit > Trail_From){
                                 if(OrderStopLoss() == 0)mod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-My_Trail,Digits),OrderTakeProfit(),0,Red);
                                 if(OrderStopLoss() > OrderOpenPrice() - My_Trail){
                                    mod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-My_Trail,Digits),OrderTakeProfit(),0,Red);
                                    if(!mod && GetLastError() > 1)Print("Error entering Trailing Stop - Error " + GetLastError());
                                 }
                              }
                              break;
               }
         }
      }
      else Print("Error selecting order");
   } 
//----
   return(0);
  }
//+------------------------------------------------------------------+


 

 
JHenry:

Thank you very much again! :)

But, I'm not getting the wished result...

But, the EA continues opening orders and more orders and more orders... 

Look the code after your tip:


 

Move that code from init() to start() (and execute it on each and every tick before checking the

   if(totalForCurrentSymbol==0){
      OrderSend(Symbol(), 0, 0.01, Ask, 3, 0 ,0 ,0, 123, 0,CLR_NONE);
      OrderSend(Symbol(), 1, 0.01, Bid, 3, 0 ,0 ,0, 123, 0,CLR_NONE);
   }

And move the

int totalForCurrentSymbol = 0;

declaration to start() too

 
mladen:

Move that code from init() to start() (and execute it on each and every tick before checking the

And move the

declaration to start() too

Didn’t work... Something is wrong... I don’t know what…

I receive this tip too:

But, didn’t work too

=((((((((((((

 
JHenry:
Didn’t work... Something is wrong... I don’t know what…

I receive this tip too:

But, didn’t work too

=((((((((((((

Try like this

//+------------------------------------------------------------------+
//|                                               Ben_trail_stop.mq4 |
//|                            Copyright © 2010, www.compu-forex.com |
//|                                              www.compu-forex.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, www.compu-forex.com"
#property link      "www.compu-forex.com"

//---- input parameters
extern int        MagicNumber     = 0;
extern double     Trail_From     = 10.0;
extern double     Trail_Max      = 50.0;
extern double     Trail_Percent  = 50;
extern int        Equity_Cut     =10;


int   
   D_Factor = 1;
double   
   Pip;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{



   Pip = Point;
   if (Digits == 3||Digits == 5){Pip *= 10; D_Factor = 10;}
   Trail_From *= Pip;
   Trail_Max  *= Pip;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{

bool
   mod;
double
   My_Profit,
   My_Trail;
   int totalForCurrentSymbol = 0;
   for (int i = OrdersTotal()-1; i>=0; i--)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderSymbol()      != "BTCEUR")            continue;
         totalForCurrentSymbol++;
   }         

   if(totalForCurrentSymbol==0){
      OrderSend(Symbol(), 0, 0.01, Ask, 3, 0 ,0 ,0, 123, 0,CLR_NONE);
      OrderSend(Symbol(), 1, 0.01, Bid, 3, 0 ,0 ,0, 123, 0,CLR_NONE);
   }
//----
   for (int i = 0; i < OrdersTotal(); i++){
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderMagicNumber() == MagicNumber || MagicNumber == 0){
            RefreshRates();
            switch(OrderType()){
               case OP_BUY :  My_Profit = MarketInfo(OrderSymbol(), MODE_BID) - OrderOpenPrice();
                              My_Trail = MathMin(My_Profit * Trail_Percent/100,Trail_Max);
                              if(My_Profit > Trail_From){
                                 if(OrderStopLoss() == 0)mod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+My_Trail,Digits),OrderTakeProfit(),0, Red);
                                 if(OrderStopLoss() < OrderOpenPrice() + My_Trail){
                                    mod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+My_Trail,Digits),OrderTakeProfit(),0, Red);
                                    if(!mod && GetLastError() > 1)Print("Error entering Trailing Stop - Error " + GetLastError());
                                 }
                              }
                              break;
                        
               case OP_SELL : My_Profit = OrderOpenPrice() - MarketInfo(OrderSymbol(), MODE_ASK);
                              My_Trail = MathMin(My_Profit * Trail_Percent/100,Trail_Max);
                              if(My_Profit > Trail_From){
                                 if(OrderStopLoss() == 0)mod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-My_Trail,Digits),OrderTakeProfit(),0,Red);
                                 if(OrderStopLoss() > OrderOpenPrice() - My_Trail){
                                    mod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-My_Trail,Digits),OrderTakeProfit(),0,Red);
                                    if(!mod && GetLastError() > 1)Print("Error entering Trailing Stop - Error " + GetLastError());
                                 }
                              }
                              break;
               }
         }
      }
      else Print("Error selecting order");
   } 
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
PS: the way you coded it, that EA can work ONLY on "BTCEUR" (on other symbols it will not work correctly)
 
I don’t know, guy… I’m not felling safe with this code…

The function not should return the number of open orders in EURGBP or ABCXYZ!?!? So why you wrote: 

 if(totalForCurrentSymbol==0){OrderSend…}

After, exist other line that use the function OrdersTotal()

for (int i = 0; i < OrdersTotal(); i++){papapa…)

This function should be substituied by totalForCurrentSymbol.

I will put my money in this EA…

The worse is that I don’t understand of programming and MQL4 and I don’t speak English very well, so, I can’t express correctly and I can’t understand your ideia….