Functions in the loop

To add comments, please log in or register
David Diez
1434
David Diez  

To simplify the code, I was trying to integrate functions in the loop this way:

   for(int i=0;i<OrdersTotal();i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES&&OrderSymbol()==Symbol())){
         double LastBuyPrice=0,LastSellPrice=0,LastBuyLot=0,LastSellLot=0;
         //----------------------------------------- Grid orders ---!
         if(GridSize>0){
            if(OrderType()==OP_BUY){
               if(OrderOpenPrice()<LastBuyPrice||LastBuyPrice==0){LastBuyPrice=OrderOpenPrice();} // For getting LastBuyPrice
               if(OrderLots()>LastBuyLot){LastBuyLot=OrderLots();} // For getting LastBuyLot
               if(Ask<=LastBuyPrice-(GridSize*10)*Point){
                  if(!OrderSend(Symbol(),OP_BUY,NormalizeDouble(LastBuyLot*Martingale,2),Ask,3,0,0,BOTNAME,MagicNumber,0,clrBlue)){
                     Print(Symbol()," OrderSend error ",GetLastError());
                     return;
                     }
                  }
               }

Original functions that worked were:

double LastBuy(){
   double Price=0;
   for(int i=0;i<OrdersTotal();i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderType()==OP_BUY){
            if(OrderOpenPrice()<Price||Price==0){
               Price=OrderOpenPrice();
               }
            }
         }
      }
   return(Price);
   }

double LastBuyLot(){
   double Lot=0;
   for(int i=0;i<OrdersTotal();i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderType()==OP_BUY){
            if(OrderLots()>Lot){
               Lot=OrderLots();
               }
            }
         }
      }
   return(Lot);
   }

As the loop works in the same way that's why I tried to integrate, but no success, just take values from the first order.

I hope someone can help, thank you in advance.

Lorentzos Roussos
19087
Lorentzos Roussos  
David Diez:

To simplify the code, I was trying to integrate functions in the loop this way:

Original functions that worked were:

As the loop works in the same way that's why I tried to integrate, but no success, just take values from the first order.

I hope someone can help, thank you in advance.

Loop the other way from OrdersTotal-1 to 0 -- ,when you find the order you seek break the loop

David Diez
1434
David Diez  
Lorentzos Roussos:

Loop the other way from OrdersTotal-1 to 0 -- ,when you find the order you seek break the loop

Can you explain better? I don't know much about loops, always used this one.
Lorentzos Roussos
19087
Lorentzos Roussos  
David Diez:
Can you explain better? I don't know much about loops, always used this one.

Sure . 


for(int i=0;i<OrdersTotal();i++)
  {
  //this will run through orders from 0 to last
  }

for(int i=OrdersTotal()-1;i>=0;i--)
  {
  //this will run through orders from last to 0 
  //break;//<--this will stop the loop process
  //continue;//<--this will skip all following code in the loop and move to the next loop run (i) 
  }
David Diez
1434
David Diez  
Lorentzos Roussos:

Sure . 


That doesn't work.

Loop Works correct in function calls, so why it doesn't in the main sequence?

Lorentzos Roussos
19087
Lorentzos Roussos  
David Diez:

That doesn't work.

Loop Works correct in function calls, so why it doesn't in the main sequence?

Ow . it was implied these should be used in a function and not on the global scope

David Diez
1434
David Diez  
Lorentzos Roussos:

Ow . it was implied these should be used in a function and not on the global scope

I'm trying now a loop for buy and another loop for sell:

   //-------------------------------------------------- Buy loop ---!
   for(int i=BuyOrders()-1;i>=0;i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         //----------------------------------------- Grid orders ---!
         if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()){
            if(Ask<=OrderOpenPrice()-(GridSize*10)*Point){ // OpenPrice of the last order.
               if(!OrderSend(Symbol(),OP_BUY,NormalizeDouble(OrderLots()*Martingale,2),Ask,3,0,0,BOTNAME,MagicNumber,0,clrBlue)){ // OrderLots of the last order
                  Print(Symbol()," OrderSend error ",GetLastError());
                  return;
                  }
               }
            }

But doesn't match the values :/

Open new orders with the same lot above the grid distance till reach that distance, then opens the correct lot.
David Diez
1434
David Diez  


Now this Works, but doesn't open new grids from the third order:

   for(int i=BuyOrders()-1;i==0;i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         //----------------------------------------- Grid orders ---!
         if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()){
            Print("Grid distance is ",(GridSize*10)*Point);
            Print("New grid at ",OrderOpenPrice()-(GridSize*10)*Point);
            if(Ask<=OrderOpenPrice()-(GridSize*10)*Point){
               if(!OrderSend(Symbol(),OP_BUY,NormalizeDouble(OrderLots()*Martingale,2),Ask,3,0,0,BOTNAME,MagicNumber,0,clrBlue)){
                  Print(Symbol()," OrderSend error ",GetLastError());
                  return;
                  }
               }
            }
Lorentzos Roussos
19087
Lorentzos Roussos  
You have to use OrdersTotal() in the loop header , BuyOrders will lead to a wrong index reference since you are selecting by position
William Roeder
20248
William Roeder  
David Diez: Can you explain better? I don't know much about loops, always used this one.
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
  1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
              Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
    For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
  2. and check OrderSelect in case earlier positions were deleted.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().
David Diez
1434
David Diez  
William Roeder:
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
  1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
              Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
    For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
  2. and check OrderSelect in case earlier positions were deleted.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().
Worths nothing partner.
12
To add comments, please log in or register