Functions in the loop - page 2

 

   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;
                  }
               }
            }

I think your loop got some bugs . 

   for(int i=OrdersTotal()-1;i>=0;i--)

Try this one . 

 

First thing first,

In your original integrated solution, declare all variables outside For loop and then see if it works??

double LastBuyPrice=0,LastSellPrice=0,LastBuyLot=0,LastSellLot=0; // Should declare them outside For loop

Also, it's a good practice to Take OrdersTotal() value in a variable and use that variable in For loop as Orders count can change as you are doing some operations(placing new orders, closing existing one's) within For loop.

Thanks.

 
Yip Sin Hang:


I think your loop got some bugs . 

Try this one . 

This don't work.
 
Manu Dhanda:

First thing first,

In your original integrated solution, declare all variables outside For loop and then see if it works??

Also, it's a good practice to Take OrdersTotal() value in a variable and use that variable in For loop as Orders count can change as you are doing some operations(placing new orders, closing existing one's) within For loop.

Thanks.

Now this way it open:

1 x 0.01

1 x 0.02

2 x 0.04

4 x 0.08

8 x 0.16

...

   for(int i=OrdersTotal()-1;i>=0;i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         //----------------------------------------- Grid orders ---!
         if(GridSize>0){
            if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()){
               if(OrderOpenPrice()<LastBuyPrice||LastBuyPrice==0){LastBuyPrice=OrderOpenPrice();}
               if(OrderLots()>LastBuyLot){LastBuyLot=OrderLots();}
               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;
                     break;
                     }
                  }
 
Lot value depends on your calculation in your expression for OrderSend() : NormalizeDouble(LastBuyLot*Martingale,2)
Reason: