Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 34
 2 Attachment(s)    

Thread: Closing Previous Orders in a Martingale Sequence

  1. #11
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    7,003

    Default

    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++)
       {
          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
          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--)
       {
          OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
          if(OrderMagicNumber()==Magic)
          {
             int type=OrderType();
             bool result=false;
             switch(type)
             {
                case OP_BUY       : result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Red);
                                    break;
                case OP_SELL      : result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Red);
                                    break;
                case OP_BUYLIMIT  :
                case OP_BUYSTOP   :
                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. #12
    Member
    Join Date
    Jan 2009
    Location
    Austria
    Posts
    325

    Default

    Quote Originally Posted by funyoo View Post
    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++)
       {
          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
          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--)
       {
          OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
          if(OrderMagicNumber()==Magic)
          {
             int type=OrderType();
             bool result=false;
             switch(type)
             {
                case OP_BUY       : result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Red);
                                    break;
                case OP_SELL      : result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Red);
                                    break;
                case OP_BUYLIMIT  :
                case OP_BUYSTOP   :
                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

    made some extension to get more info logged

    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
    Last edited by ixbone; 03-07-2009 at 10:15.

  3. #13
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    7,003

    Default

    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--)
       {
          OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
          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--)
       {
          OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
          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. #14
    Member
    Join Date
    Jan 2009
    Location
    Austria
    Posts
    325

    Default

    Quote Originally Posted by funyoo View Post
    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--)
       {
          OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
          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--)
       {
          OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
          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. #15

    Default

    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.
    Last edited by BillR; 03-07-2009 at 14:38.

  6. #16
    Member
    Join Date
    Jan 2009
    Location
    Austria
    Posts
    325

    Default

    Quote Originally Posted by BillR View Post
    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
    Last edited by ixbone; 03-07-2009 at 14:50.

  7. #17

    Default

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

    Default

    Here is one week....One is Long Only the other Long and Short
    Attached Images Attached Images Closing Previous Orders in a Martingale Sequence-long-only-gif Closing Previous Orders in a Martingale Sequence-long-and-short-gif 
    Last edited by BillR; 03-07-2009 at 15:13.

  9. #19
    Member
    Join Date
    Jan 2009
    Location
    Austria
    Posts
    325

    Default

    Quote Originally Posted by BillR View Post
    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. #20

    Default

    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
    Last edited by BillR; 03-07-2009 at 15:43.

Similar Threads

  1. Closing all orders on all charts
    By tal1n in forum MQL programming
    Replies: 3
    Last Post: 06-17-2011, 10:21
  2. Closing orders
    By szmvscla in forum MQL programming
    Replies: 0
    Last Post: 10-20-2009, 05:40
  3. Previous Bar
    By altoronto in forum MQL programming
    Replies: 1
    Last Post: 09-16-2009, 19:26
  4. Martingale with SL (closing previous orders)?
    By ixbone in forum MQL programming
    Replies: 1
    Last Post: 02-23-2009, 04:40

Tags for this Thread

100, alpari, average, backtest, breakeven, broker, closing, coding, demo, eurusd, forex, forex trading, free, gbpusd, gold, hedge, hedging, high, historical, learn, live, low, magic, magic number, managed, manual, martingale, mq4, mql, mt4, news, nzd, price action, profit, profit factor, profitable, programming, real, research, scripts, security, sells, short term, simple, slippage, stoch, stop, stoploss, strategy, system, thank you, time, trading, trailing, usd

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •