# Pending Orders Loop

77

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.

17074

OrderSelect loop, find the open order, Get the OrderOpenPrice, open another.
77

`OrderSelect loop, find the open order, Get the OrderOpenPrice, open another.`

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

77

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(OrderMagicNumber()==9)
if(OrderSymbol()==Symbol())
if(OrderType()==OP_SELL && InitSwitch == true)
{
InitPrice = OrderOpenPrice();
InitSwitch = false;
sell_loop = true;
}
}

for(int b=OrdersTotal()-1;b>=0;b--)
{
if(OrderMagicNumber()==9)
if(OrderSymbol()==Symbol())
{
InitPrice = OrderOpenPrice();
InitSwitch = false;
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(OrderMagicNumber()==9)
if(OrderSymbol()==Symbol())
if(OrderType()==OP_SELLSTOP)
{
SPOTarget = OrderOpenPrice();
double CurrentBIDe = MarketInfo(Symbol(),MODE_BID);
{
sell_loop = true;
}
}
}

for(int bpo=OrdersTotal()-1;bpo>=0;bpo--)
{
if(OrderMagicNumber()==9)
if(OrderSymbol()==Symbol())
{
BPOTarget = OrderOpenPrice();
sell_loop_target = BPOTarget-InitPrice;
{
sell_loop = false;
OrderSend(Symbol(),OP_SELLSTOP,0.20,Bid-sell_loop_target,3,0,0,NULL,9,0,Green);
}
}
}
}```
6433

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..

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

77

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(OrderMagicNumber()==9)
if(OrderSymbol()==Symbol())
if(OrderType()==OP_SELL && InitSwitch == true)
{
InitSwitch = false;
sell_loop = true;
}
else if(OrderType()==OP_BUY && sell_loop == true)
{
sell_loop = false;
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(OrderMagicNumber()==9)
if(OrderSymbol()==Symbol())
{
InitSwitch = false;
OrderSend(Symbol(),OP_SELLSTOP,0.20,Bid-20*Poin,3,0,0,NULL,9,0,Red);
}
else if(OrderType()==OP_SELL && buy_loop == true)
{
sell_loop = true;
}
}
}```
17074

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?
77

maybe you could give an example,it seems that i dont understand
77

Can someone help me or not? Give some examples maybe?
Moderator
18056

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.