# Thread: Closing Previous Orders in a Martingale Sequence

1. Ok, I have made some modifications :

The line to close the orders.

Code:
`if(ClosePrevOrder==true&&(CountOrders(OP_BUY,Magic)+CountOrders(OP_SELL,Magic))>2)ClosePrevOrders(Magic);`
The count orders function.

Code:
```int CountOrders(int Type,int Magic)
{
int _CountOrd;
_CountOrd=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSymbol()==Symbol())
{
if((OrderType()==Type&&(OrderMagicNumber()==Magic)||Magic==0))_CountOrd++;
}
}
return(_CountOrd);
}```
The close orders function.

Code:
```int ClosePrevOrders(int Magic)
{
int total=OrdersTotal();

for (int cnt=total-3;cnt>=0;cnt--)
{
if(OrderMagicNumber()==Magic)
{
int type=OrderType();
bool result=false;
switch(type)
{
break;
break;
case OP_SELLLIMIT :
case OP_SELLSTOP  : result=OrderDelete( OrderTicket() );
}
if(result==false)
{
Alert("Order ",OrderTicket()," failed to close. Error:",GetLastError());
Sleep(3000);
}
}
}
return(0);
}```
I have tried it in a simple martingale code and it works perfectly.

2. Originally Posted by funyoo
Ok, I have made some modifications :

The line to close the orders.

Code:
`if(ClosePrevOrder==true&&(CountOrders(OP_BUY,Magic)+CountOrders(OP_SELL,Magic))>2)ClosePrevOrders(Magic);`
The count orders function.

Code:
```int CountOrders(int Type,int Magic)
{
int _CountOrd;
_CountOrd=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSymbol()==Symbol())
{
if((OrderType()==Type&&(OrderMagicNumber()==Magic)||Magic==0))_CountOrd++;
}
}
return(_CountOrd);
}```
The close orders function.

Code:
```int ClosePrevOrders(int Magic)
{
int total=OrdersTotal();

for (int cnt=total-3;cnt>=0;cnt--)
{
if(OrderMagicNumber()==Magic)
{
int type=OrderType();
bool result=false;
switch(type)
{
break;
break;
case OP_SELLLIMIT :
case OP_SELLSTOP  : result=OrderDelete( OrderTicket() );
}
if(result==false)
{
Alert("Order ",OrderTicket()," failed to close. Error:",GetLastError());
Sleep(3000);
}
}
}
return(0);
}```
I have tried it in a simple martingale code and it works perfectly.

thanks again funyoo

if(result==true)
{
Print("Order ",OrderTicket()," Previous order closed. Error:",GetLastError());

code seems to be perfect working, maybe we are in a loop,
maybe a strategy tester problem:
settings: maxtrade=5 / so we have 0.01 till 0.16 lot size
if the sequence reaches the maxlot 0.16 it opens and closes always the last order in the sequence
so i'll test it further, maybe my mistake, tester problem

don't cut your sunday! reporting to you on monday

IX

2009.03.07 11:08:47 2007.01.02 09:31 EURUSD,M5: open #13 sell 0.16 EURUSD at 1.32796 tp: 1.31545 ok
2009.03.07 11:08:47 2007.01.02 09:31 EURUSD,M5: Ticket Opened by EA :12
2009.03.07 11:08:47 2007.01.02 09:31 EURUSD,M5: open #12 sell 0.16 EURUSD at 1.32796 tp: 1.31545 ok
2009.03.07 11:08:47 2007.01.02 08:49 EURUSD,M5: Order 8 Previous order closed. Error:0
2009.03.07 11:08:47 2007.01.02 08:49 EURUSD,M5: close #8 sell 0.16 EURUSD at 1.32580 tp: 1.31330 at price 1.32713
2009.03.07 11:08:47 2007.01.02 08:49 EURUSD,M5: Order 9 Previous order closed. Error:0
2009.03.07 11:08:47 2007.01.02 08:49 EURUSD,M5: close #9 sell 0.16 EURUSD at 1.32580 tp: 1.31330 at price 1.32713
2009.03.07 11:08:47 2007.01.02 08:49 EURUSD,M5: Ticket Opened by EA :11
2009.03.07 11:08:47 2007.01.02 08:49 EURUSD,M5: open #11 sell 0.16 EURUSD at 1.32695 tp: 1.31430 ok
2009.03.07 11:08:47 2007.01.02 08:49 EURUSD,M5: Ticket Opened by EA :10
2009.03.07 11:08:47 2007.01.02 08:49 EURUSD,M5: open #10 sell 0.16 EURUSD at 1.32695 tp: 1.31430 ok
2009.03.07 11:08:47 2007.01.02 08:35 EURUSD,M5: Order 6 Previous order closed. Error:0
2009.03.07 11:08:47 2007.01.02 08:35 EURUSD,M5: close #6 sell 0.16 EURUSD at 1.32480 tp: 1.31210 at price 1.32598
2009.03.07 11:08:47 2007.01.02 08:35 EURUSD,M5: Order 7 Previous order closed. Error:0
2009.03.07 11:08:47 2007.01.02 08:35 EURUSD,M5: close #7 sell 0.16 EURUSD at 1.32480 tp: 1.31210 at price 1.32598
2009.03.07 11:08:47 2007.01.02 08:35 EURUSD,M5: Ticket Opened by EA :9
2009.03.07 11:08:47 2007.01.02 01:26 EURUSD,M5: close #2 sell 0.02 EURUSD at 1.32050 tp: 1.30740 at price 1.32274
2009.03.07 11:08:47 2007.01.02 01:26 EURUSD,M5: Ticket Opened by EA :4
2009.03.07 11:08:47 2007.01.02 01:26 EURUSD,M5: open #4 sell 0.08 EURUSD at 1.32256 tp: 1.31001 ok
2009.03.07 11:08:47 2007.01.02 00:49 EURUSD,M5: Order 1 Previous order closed. Error:0
2009.03.07 11:08:47 2007.01.02 00:49 EURUSD,M5: close #1 sell 0.01 EURUSD at 1.31890 tp: 1.30690 at price 1.32169
2009.03.07 11:08:47 2007.01.02 00:49 EURUSD,M5: Ticket Opened by EA :3
2009.03.07 11:08:47 2007.01.02 00:49 EURUSD,M5: open #3 sell 0.04 EURUSD at 1.32151 tp: 1.30900 ok
2009.03.07 11:08:47 2007.01.02 00:00 EURUSD,M5: Ticket Opened by EA :2
2009.03.07 11:08:47 2007.01.02 00:00 EURUSD,M5: open #2 sell 0.02 EURUSD at 1.32050 tp: 1.30740 ok
2009.03.07 11:08:47 2007.01.01 00:05 EURUSD,M5: Ticket Opened by EA :1
2009.03.07 11:08:47 2007.01.01 00:05 EURUSD,M5: open #1 sell 0.01 EURUSD at 1.31890 tp: 1.30690 ok

3. You can exclude the last price and the last previous price to avoid this :

Last price and last prev price function :

Code:
```double LastPrice()
{
double oldorderopenprice=0,orderprice;
int cnt,oldticketnumber=0,ticketnumber;
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
ticketnumber=OrderTicket();
if(ticketnumber>oldticketnumber)
{
orderprice=OrderOpenPrice();
oldorderopenprice=orderprice;
oldticketnumber=ticketnumber;
}
}
}
return(orderprice);
}

double LastPrevPrice()
{
double oldorderopenprice=0,orderprice;
int cnt,oldticketnumber=0,ticketnumber;
for(cnt=OrdersTotal()-2;cnt>=0;cnt--)
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
ticketnumber=OrderTicket();
if(ticketnumber>oldticketnumber)
{
orderprice=OrderOpenPrice();
oldorderopenprice=orderprice;
oldticketnumber=ticketnumber;
}
}
}
return(orderprice);
}```
In the close orders function, replace this line :

Code:
`if(OrderMagicNumber()==Magic)`
By :

Code:
`if(OrderMagicNumber()==Magic && OrderOpendPrice()!=LastPrice() && OrderOpendPrice()!=LastPrevPrice())`

4. Originally Posted by funyoo
You can exclude the last price and the last previous price to avoid this :

Last price and last prev price function :

Code:
```double LastPrice()
{
double oldorderopenprice=0,orderprice;
int cnt,oldticketnumber=0,ticketnumber;
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
ticketnumber=OrderTicket();
if(ticketnumber>oldticketnumber)
{
orderprice=OrderOpenPrice();
oldorderopenprice=orderprice;
oldticketnumber=ticketnumber;
}
}
}
return(orderprice);
}

double LastPrevPrice()
{
double oldorderopenprice=0,orderprice;
int cnt,oldticketnumber=0,ticketnumber;
for(cnt=OrdersTotal()-2;cnt>=0;cnt--)
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
ticketnumber=OrderTicket();
if(ticketnumber>oldticketnumber)
{
orderprice=OrderOpenPrice();
oldorderopenprice=orderprice;
oldticketnumber=ticketnumber;
}
}
}
return(orderprice);
}```
In the close orders function, replace this line :

Code:
`if(OrderMagicNumber()==Magic)`
By :

Code:
`if(OrderMagicNumber()==Magic && OrderOpendPrice()!=LastPrice() && OrderOpendPrice()!=LastPrevPrice())`

funyoo, you are my hero of the day :-)
I'lltry it

5. Hi IX and Funyoo... My coding skills do not come close to yours yet...I am very happy to learn from your discussion. I do trade using "Martingale" (both directions at the same time) and as you know making the money is the easy part....keeping it from Blowout is the harder part...If you make parameters very conservative to avoid blow out you don't make enough between blowouts... I've found that taking money out of the account daily gets it away from blow out.....Also I know I mentioned this before and you said it wasn't possible....but I too trade 24 hours a day....and find that closing daily after NY close at a \$500/\$1000.00 loss is cheap insurance from blowout.. Don't close it when you have a large order ready to turn around and profit....reset when you have only four or five open ones.....Try this and you'll see....I usually backtest parameters going back to Oct 5, 2008 Test your parameters on a weekly basis and see your results....then go back and check them from day to day to day to day etc....you'll see by resetting the new day you don't carry any bias or pre-stress into the new trading day....It works much better than week to week. I'd like to see your MQ4 when done...the one I'm using is here in MQL programming area.

6. Originally Posted by BillR
Hi IX and Funyoo... My coding skills do not come close to yours yet...I am very happy to learn from your discussion. I do trade using "Martingale" (both directions at the same time) and as you know making the money is the easy part....keeping it from Blowout is the harder part...If you make parameters very conservative to avoid blow out you don't make enough between blowouts... I've found that taking money out of the account daily gets it away from blow out.....Also I know I mentioned this before and you said it wasn't possible....but I too trade 24 hours a day....and find that closing daily after NY close at a \$500/\$1000.00 loss is cheap insurance from blowout.. Don't close it when you have a large order ready to turn around and profit....reset when you have only four or five open ones.....Try this and you'll see....I usually backtest parameters going back to Oct 5, 2008 Test your parameters on a weekly basis and see your results....then go back and check them from day to day to day to day etc....you'll see by resetting the new day you don't carry any bias or pre-stress into the new trading day....I works much better than week to week. I'd like to see your MQ4 when done...the one I'm using is here in MQL programming area.
HI Bill,

thank you for your comment, but martingale trading in booth directions is the wrong way- or maybe i misunderstood your system, i know it, i trade martingale over 10 years

The only martingale which works:
Account 5000 bucks
Profit per week 5000 bucks
Second deposit account
Withdrawl you profits (5000) every week to the second account, its faster then all other transfer methods and then to your bank account
With this method you must only staying one week alive to break even in a month - this is real martingale mm and it is not real to loose 4 weeks in a row!

just my two cents

IX

march 1st week 3-6.3.2009 mo-fr

7. Thanks IX Both directions means turn on EA and it opens 2 orders...one Buy and One Sell... If the currency goes UP the Buy Order closes at a Profit, a new lowest lot Buy order is then placed AND a Second Sell order is placed at the next increased lots size in the Martingale style....If it keeps going up the Buy closes at a profit AND another lowest lot Buy order is placed AND another larger Martingale multiple sell is entered....When the currency starts to go down the routine is the same....only working in reverse. This way Martingale is working in both directions...you make TWICE as much \$\$'s...

8. Here is one week....One is Long Only the other Long and Short

9. Originally Posted by BillR
Thanks IX Both directions means turn on EA and it opens 2 orders...one Buy and One Sell... If the currency goes UP the Buy Order closes at a Profit, a new lowest lot Buy order is then placed AND a Second Sell order is placed at the next increased lots size in the Martingale style....If it keeps going up the Buy closes at a profit AND another lowest lot Buy order is placed AND another larger Martingale multiple sell is entered....When the currency starts to go down the routine is the same....only working in reverse. This way Martingale is working in both directions...you make TWICE as much \$\$'s...
Bill, maybe i missunderstood this twice :-)

You trade a double reverse martingale!?
You win by setting correct reverses (TP, SL, next entries), so if the market is ranging and makes a massive one time gap you overtrade or wipe depending on your funds

as noted, maybe i missunderstood, do you have a live statement?

IX

10. IX....Simple Martingale trading both ways...

I'm having trouble getting the results to UPLOAD...don't I just attach the file?? When I upload the results...only the
chart gif show up not the results .htm which display fine on my computer...Says Upload failed when I try...the htm