How to place multiple trades X pips apart

 

Hi guys

I'm struggling and hope that someone can please point me in the right direction.  Once a trade is open, I would like to open another buy trade if the price moves 10 pips up or another sell trade once the price hits 10 pips below the OpenOrderPrice.  For some reason my code isn't working.  It seems as if the buy code is working, but when the price drops it will first hit the stoploss of 100 pips before a sell order is placed which should have happened every 10 pips as the price was dropping.

//Previous to this some variables have been defined and the original buy and sell conditions for the initial trade

//+------------------------------------------------------------------+
//| Additional                                                       |
//+------------------------------------------------------------------+
void Additional(){
   double Lots=NormalizeDouble(AccountEquity()*Percentage*Pt/Tickvalue,2);
   double Move=Pipmove*Pip;
   
   for(int i=OrdersTotal()-1; i>=0; i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderSymbol()==Symbol()){               
            if(OrderType()==OP_BUY){
               if(OrderOpenPrice()+Move>Ask){ //Price is supposed to move 10 pips up from last Open Order Price and open another trade
                     ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Original",0,0,Lime);
                    }
                  }
            if(OrderType()==OP_SELL){
               if(OrderOpenPrice()-Move<Bid){
                     ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"Original",0,0,Red);
                    }
                  }
                }
              }
            }
          }
 
trader3000: but when the price drops it will first hit the stoploss of 100 pips before a sell order is placed which should have happened every 10 pips as the price was dropping.
  1. Do you have a sell order open? Not Pending orders.
  2. OOP-n < Bid opens sell orders when the bid rises; not dropping. OOP+n > Ask opens buy orders when the Ask lowers.
  3. Once you fix № 2 Your loop will continuously open orders after the first. Find the lowest sell/highest buy and test that before opening more.
 
William Roeder:
  1. Do you have a sell order open? Not Pending orders.
  2. OOP-n < Bid opens sell orders when the bid rises; not dropping. OOP+n > Ask opens buy orders when the Ask lowers.
  3. Once you fix № 2 Your loop will continuously open orders after the first. Find the lowest sell/highest buy and test that before opening more.

Thank you very much.  Your guidance helped me to figure it out.  I wanted to use last order rather than lowest sell/ highest buy, so I found code that you posted long ago and incorporated that.  Thanks

//+------------------------------------------------------------------+
//| Additional                                                       |
//+------------------------------------------------------------------+
void Additional(){
   double Lots=NormalizeDouble(AccountEquity()*Percentage*Pt/Tickvalue,2);
   double Move=Pipmove*Pip;
   datetime lastTime=0;
   int lastTicket = -1;
       
   for(int i=OrdersTotal()-1; i>=0; i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderSymbol()==Symbol()){               
            if(OrderType()==OP_BUY){
               if(OrderOpenTime()>=lastTime &&  OrderTicket()>lastTicket){
               lastTime   = OrderOpenTime();
               lastTicket = OrderTicket();
               
               if(lastTime && lastTicket && OrderOpenPrice()+Move<Ask){
                     ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stop_Loss*Pip,Ask+Take_Profit*Pip,"Original",0,0,Lime);
                    }
                  }
                }
            if(OrderType()==OP_SELL){
               if(OrderOpenTime()>=lastTime &&  OrderTicket()>lastTicket){
               lastTime   = OrderOpenTime();
               lastTicket = OrderTicket();
               
               if(lastTime && lastTicket && OrderOpenPrice()-Move>Bid){
                     ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+Stop_Loss*Pip,Bid-Take_Profit*Pip,"Original",0,0,Red);
                    }
                  }
                }
              }
            }
          } 
        }
 
trader3000:

Thank you very much.  Your guidance helped me to figure it out.  I wanted to use last order rather than lowest sell/ highest buy, so I found code that you posted long ago and incorporated that.  Thanks

Your code makes no sense.

   datetime lastTime=0;
   int lastTicket = -1;
   //
               lastTime   = OrderOpenTime();
               lastTicket = OrderTicket();
               
               if(lastTime && lastTicket && OrderOpenPrice()+Move<Ask){
                     ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stop_Loss*Pip,Ask+Take_Profit*Pip,"Original",0,0,Lime);
                    }

Neither of the highlighted variables are bools.

You are placing orders inside the loop that is checking for the last order.

Orders should be placed after the loop is finished.

 
Keith Watford:

Your code makes no sense.

Neither of the highlighted variables are bools.

You are placing orders inside the loop that is checking for the last order.

Orders should be placed after the loop is finished.

Thank you for the advice.  I now have this which seems to work, but every now and then a few years will go by without any new trades being opened

//+------------------------------------------------------------------+
//| Additional                                                       |
//+------------------------------------------------------------------+
void Additional(){
   double Lots=NormalizeDouble(AccountEquity()*Percentage*Pt/Tickvalue,2);
   double Move=Pipmove*Pip;
   static datetime last=0;
       
   for(int i=OrdersTotal()-1; i>=0; i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderSymbol()==Symbol()){               
            if(OrderType()==OP_BUY){
               if(OrderOpenTime()>last){
                 if(OrderOpenPrice()+Move<Ask){
                     ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stop_Loss*Pip,Ask+Take_Profit*Pip,"Original",0,0,Lime);
                     last=OrderOpenTime();
                    }
                  }
                }
            if(OrderType()==OP_SELL){
               if(OrderOpenTime()>last){
                 if(OrderOpenPrice()-Move>Bid){
                     ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+Stop_Loss*Pip,Bid-Take_Profit*Pip,"Original",0,0,Red);
                     last=OrderOpenTime();
                    }
                  }
                }
              }
            }
          } 
        }
 
I think I discovered the problem with my code above.  During the OrderSelect loop, it will select the last trade opened as it should.  If that particular trade is then closed because either the TP or SL was hit before the Pipmove was reached, it seems to hang in limbo for a few years and then suddenly start putting trades in again.  This might be the case if a Buy_Order was selected but there was a very long downtrend.  Instead of selecting the last Order Opened (which might close during the loop), how can I rather select the Last order opened that is still open?  Thank you
Reason: