Download MetaTrader 5

Pending Orders Loop

To add comments, please log in or register
Do you have idle PCs? Connect them to MQL5 Cloud Network and earn money!
Tom Maryland
77
Tom Maryland 2013.12.01 16:41 

Hi,can you please explain to me how I can do the following:

When a pending order is hit(turned into a market order),how to place another pending order in a fixed pip range away from it?

for an example,if a buystop is hit,place a sellstop order 20 pips below it,when the sellstop is hit place a buystop 20 pips above it,and so on.

I've been struggling with it the whole day,if you can help,it would be of great help,thank you.

whroeder1
14519
whroeder1 2013.12.01 17:37  
OrderSelect loop, find the open order, Get the OrderOpenPrice, open another.
Tom Maryland
77
Tom Maryland 2013.12.01 18:53  
OrderSelect loop, find the open order, Get the OrderOpenPrice, open another.

Thank you for your reply,WHRoeder.

Maybe you have a simpler version of this,because its very "messy"(no offense intended).

Tom Maryland
77
Tom Maryland 2013.12.01 19:26  

This is what I wrote,but it doesn't seem to open orders,please advise.

Only the first pending order is placed, but it doesnt repeat..

void Loop()
{
  for(int s=OrdersTotal()-1;s>=0;s--)
  {
    if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
      if(OrderMagicNumber()==9)
        if(OrderSymbol()==Symbol())
          if(OrderType()==OP_SELL && InitSwitch == true)
          {
            InitPrice = OrderOpenPrice();
            InitSwitch = false;
            sell_loop = true;
            OrderSend(Symbol(),OP_BUYSTOP,0.20,Ask+20*Poin,3,0,0,NULL,9,0,Green);
          }
  }
  
  for(int b=OrdersTotal()-1;b>=0;b--)
  {
    if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      if(OrderMagicNumber()==9)
        if(OrderSymbol()==Symbol())
          if(OrderType()==OP_BUY && InitSwitch == true)
          {
            InitPrice = OrderOpenPrice();
            InitSwitch = false;
            buy_loop = true;
            OrderSend(Symbol(),OP_SELLSTOP,0.20,Bid-20*Poin,3,0,0,NULL,9,0,Red);
          }

  }
  
  for(int spo=OrdersTotal()-1;spo>=0;spo--)
  {
    if(OrderSelect(spo,SELECT_BY_POS,MODE_TRADES))
      if(OrderMagicNumber()==9)
        if(OrderSymbol()==Symbol())
          if(OrderType()==OP_SELLSTOP)
          {
            SPOTarget = OrderOpenPrice();
            buy_loop_target = InitPrice-SPOTarget;
            double CurrentBIDe = MarketInfo(Symbol(),MODE_BID);
            if(CurrentBIDe<SPOTarget && buy_loop==true)
            {
              buy_loop = false;
              sell_loop = true;
              OrderSend(Symbol(),OP_BUYSTOP,0.20,Ask+buy_loop_target,3,0,0,NULL,9,0,Green);
            }
          }        
  }
  
  for(int bpo=OrdersTotal()-1;bpo>=0;bpo--)
  {
    if(OrderSelect(bpo,SELECT_BY_POS,MODE_TRADES))
      if(OrderMagicNumber()==9)
        if(OrderSymbol()==Symbol())
          if(OrderType()==OP_BUYSTOP)
          {
            BPOTarget = OrderOpenPrice();
            sell_loop_target = BPOTarget-InitPrice;
            double CurrentASKe = MarketInfo(Symbol(),MODE_ASK);
            if(CurrentASKe>BPOTarget && sell_loop==true)
            {
              sell_loop = false;
              buy_loop = true;
              OrderSend(Symbol(),OP_SELLSTOP,0.20,Bid-sell_loop_target,3,0,0,NULL,9,0,Green);
            }
          }  
  }
}
Tjipke de Vries
6316
Tjipke de Vries 2013.12.01 20:34  
Redael777:

This is what I wrote,but it doesn't seem to open orders,please advise.

Only the first pending order is placed, but it doesnt repeat..


why do you wanna open new trades before you have checked all your open trades ??

use one loop to check all open trades of your EA and you be able to know if you have to open new one

Tom Maryland
77
Tom Maryland 2013.12.02 14:11  

How do I stop this broken loop of opening buystops and sellstops without end,and only have 1 sellstop or 1 buystop until its hit and the oposite is placed..Please help.

void orderloop()
{
  for(int s=OrdersTotal()-1;s>=0;s--)
  {
    if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
      if(OrderMagicNumber()==9)
        if(OrderSymbol()==Symbol())
          if(OrderType()==OP_SELL && InitSwitch == true)
          {
            InitSwitch = false;
            sell_loop = true;
            OrderSend(Symbol(),OP_BUYSTOP,0.20,Ask+20*Poin,3,0,0,NULL,9,0,Green);
          }
          else if(OrderType()==OP_BUY && sell_loop == true)
          {
            sell_loop = false;
            buy_loop = true;
            OrderSend(Symbol(),OP_SELLSTOP,0.20,Bid-20*Poin,3,0,0,NULL,9,0,Red);
          }
  }
  
  for(int b=OrdersTotal()-1;b>=0;b--)
  {
    if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      if(OrderMagicNumber()==9)
        if(OrderSymbol()==Symbol())
          if(OrderType()==OP_BUY && InitSwitch == true)
          {
            InitSwitch = false;
            buy_loop = true;
            OrderSend(Symbol(),OP_SELLSTOP,0.20,Bid-20*Poin,3,0,0,NULL,9,0,Red);
          }
          else if(OrderType()==OP_SELL && buy_loop == true)
          {
            buy_loop = false;
            sell_loop = true;
            OrderSend(Symbol(),OP_BUYSTOP,0.20,Ask+20*Poin,3,0,0,NULL,9,0,Green);
          }
  }
}
whroeder1
14519
whroeder1 2013.12.02 15:21  
Redael777: How do I stop this broken loop of opening buystops and sellstops without end,and only have 1 sellstop or 1 buystop until its hit and the oposite is placed..Please help.
Why didn't do do what deVries suggested?
Tom Maryland
77
Tom Maryland 2013.12.02 15:23  
maybe you could give an example,it seems that i dont understand
Tom Maryland
77
Tom Maryland 2013.12.02 15:53  
Can someone help me or not? Give some examples maybe?
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.02 16:01  
Redael777:
Can someone help me or not? Give some examples maybe?
There is example code here: Loops and Closing or Deleting Orders it doesn't do exactly what you want but the principles are the same, read it, understand it and learn.
To add comments, please log in or register