Page 1 of 4 123 ... LastLast
Results 1 to 10 of 34
 2 Attachment(s)    

Thread: Closing Previous Orders in a Martingale Sequence

  1. #1
    Member
    Join Date
    Jan 2009
    Location
    Austria
    Posts
    325

    Default Closing Previous Orders in a Martingale Sequence

    Hi all,

    my personal martingale coding is reaching the end, for security i would code a option which closes previous orders in a martingale sequence:

    example:
    we buy, but the market is going down, so we are in negative profit.
    max trades are 10, all previous orders should be closed except the last two orders. If order #3 is open, order #1 should be closed with a loss, leaving orders #2 + #3 open, if order #4 is open, order #2 should be closed with loss, leaving orders #3 + #4 open - this gives you a loss but saves your equity. at max trades 10, trade 9 + 10 schould be open, orders 1 till 8 should be closed

    my code gives me always the error: "invalid ticket in ordersend function"
    any hints very welcome!

    extern bool ClosePrevOrder = true; //EA should works only with last two opened orders. Closing previous, realizing its lost
    int OrdersCanceled = 0

    if(ClosePrevOrder==true)
    {
    if(_ExpertOrdersTotal>=2)
    {
    OrderSelect(_OrderTicket[counter+2],SELECT_BY_TICKET);
    OrderClose(OrderTicket(),OrderLots(),OrderClosePri ce(),slippage,Lime);
    OrdersCanceled++;
    }
    }


    Attached a detailed demo statement without this security option, which i would activate at friday noon to monday, to secure the account during the weekend
    As you can see, the ea trades at 5 timeframes at the same time, why? easy hedging: TF5 (Magic 99995) shows buy, TF240 (Magic 9999240) shows sell, you hedge your martingale - double profits :-)
    Used indis: Stoch and RVI
    Profit this week from 3/3/2009 to 3/6/2009 (3.30 GMT) 4.500 (Starting 5000) Account now 9500 in 5 days / Profit Factor 5.66

    Hope someone could help me with this error

    Thanks

    IX
    Last edited by ixbone; 03-06-2009 at 03:40.

  2. #2

    Default

    I too have been a big advocate of closing positions that will never be profitable...BEFORE they get to be too large. I'll take a look at your code..and try to understand how you propose to close them....
    I have also had much luck using stop loss to close positions effectively....I also find that when using martingale style it is a very good idea to close ALL positions once a day to reset everything to a neutral bias...thus starting every day with a clean slate....You will take a lose when doing this but if you have your parameters set properly...your pluses will be much greater than the loses from once a day close em all....Bill

  3. #3

    Default

    One additional thought....make sure your code is not dependent on multiple open orders to determine the next orders progressive lotsi....that's not the error you're seeing but could be your next problem.....also you do see the extra space in pri ce ??? Bill
    Last edited by BillR; 03-06-2009 at 04:24.

  4. #4

    Default

    One more additional thought.....If you do use ONLY the previous order to determine the lotsi of the next....be also aware of properly setting the TP to adequately account for the closed losing positions....Bill

  5. #5
    Member
    Join Date
    Jan 2009
    Location
    Austria
    Posts
    325

    Default

    Quote Originally Posted by BillR View Post
    I too have been a big advocate of closing positions that will never be profitable...BEFORE they get to be too large. I'll take a look at your code..and try to understand how you propose to close them....
    I have also had much luck using stop loss to close positions effectively....I also find that when using martingale style it is a very good idea to close ALL positions once a day to reset everything to a neutral bias...thus starting every day with a clean slate....You will take a lose when doing this but if you have your parameters set properly...your pluses will be much greater than the loses from once a day close em all....Bill
    Bill

    For closing i use % of account balance (close all), TS if the % of balance in profit not reached (closing the orders which in profit, example: 4 orders, #3+#4 in profit = close = realize profit, leaving #1 +#2 further open

    To secure profit and account, the % of account balance is also the ratio to close all, realizing loss example if % = 3 of account balance, your take profit is 3% (with TS option) or 3% max loss in a sequence

    With this values i don't need stoploss on regular days, with my option "closepreviousorders" I'll put my account in the secure area over the weekend.

    Starting every day (clean state) is not possible, because my ea trades 24 hours and have open orders all the time. But i have the option to research the market on saturday, if regular let it run, if irregular reset and start with new magic number on monday

    IX

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

    Default

    Quote Originally Posted by BillR View Post
    One additional thought....make sure your code is not dependent on multiple open orders to determine the next orders progressive lotsi....that's not the error you're seeing but could be your next problem.....also you do see the extra space in pri ce ??? Bill
    Bill

    well, the ea is nearly finished, as you can see at the detailed statement.
    only some small security options needs to be coded

    pri ce: typo in forum, i checked the code, its ok - thanks

    IX

  7. #7
    Member
    Join Date
    Jan 2009
    Location
    Austria
    Posts
    325

    Default

    Quote Originally Posted by BillR View Post
    One more additional thought.....If you do use ONLY the previous order to determine the lotsi of the next....be also aware of properly setting the TP to adequately account for the closed losing positions....Bill
    Bill thanks for your thoughts, as i described earlier, i use % of account to set the TP on all orders or TS is TP not reached

    Determining the TP:
    ProfitTarget = NormalizeDouble(AccountBalance()*SecureProfit/100,2);

    Closing if the TP is reached:
    if(_ExpertProfitTotal<0 && ProfitTarget<0)
    {
    if(_ExpertProfitTotal<=ProfitTarget)
    {
    Sequex(); (close all function, we are in profit)
    ContinueOpening=False; (stop trading and start again with starting lot size)
    myOrderType = 6; (buy=1, sell=2, 6=clean the array)
    MaxTrades = 0;(reset the max trades and start sequence again)
    }
    }

    Well, my ea is really high risk, without the closeprevorder function its only for advanced forex traders. (profit approx. 1000 bucks a day=>detailed statement) , with big account and calmness - personal using ea and maybe on managed big accounts in the future
    With the closeprevorder it will be more secure for beginners and gives you approx. 25-100 bucks a day in a secure way, also you can't get wiped if you have less money in the pocket/account - this version will be free to the community

    IX

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

    Default

    Hi ixbone,

    Thanks for sharing your ideas,

    Try this :

    Code:
    if(ClosePrevOrder==true)ClosePrevOrders();
    function

    Code:
    void ClosePrevOrders()
    {
      int total = OrdersTotal();
      for(int i=total-2;i>=0;i--)
      {
        OrderSelect(i, SELECT_BY_POS);
        int type   = OrderType();
        bool result = false;
        
        switch(type)
        {
          //Close opened long positions
          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;
    
          //Close pending orders
          case OP_BUYLIMIT  :
          case OP_BUYSTOP   : result = OrderDelete( OrderTicket() );
          case OP_SELLLIMIT :
          case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
        }
        if(result == false)
        {
          Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
          Sleep(3000);
        }
      }
      return(0);
    }

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

    Default

    Quote Originally Posted by funyoo View Post
    Hi ixbone,

    Thanks for sharing your ideas,

    Try this :

    Code:
    if(ClosePrevOrder==true)ClosePrevOrders();
    function

    Code:
    void ClosePrevOrders()
    {
      int total = OrdersTotal();
      for(int i=total-2;i>=0;i--)
      {
        OrderSelect(i, SELECT_BY_POS);
        int type   = OrderType();
        bool result = false;
        
        switch(type)
        {
          //Close opened long positions
          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;
    
          //Close pending orders
          case OP_BUYLIMIT  :
          case OP_BUYSTOP   : result = OrderDelete( OrderTicket() );
          case OP_SELLLIMIT :
          case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
        }
        if(result == false)
        {
          Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
          Sleep(3000);
        }
      }
      return(0);
    }
    hi funyoo
    sounds great, i'll give a try, reporting back to you

    IX

  10. #10
    Member
    Join Date
    Jan 2009
    Location
    Austria
    Posts
    325

    Default

    Quote Originally Posted by ixbone View Post
    hi funyoo
    sounds great, i'll give a try, reporting back to you

    IX
    hi funyoo,

    thanks for your code, but it closes no orders, also no errors are logged, so it seems the code is in generally mt4 rules right.

    The one and only error occurs, if the ea/terminal will open the first order, but the trade context is busy (alpari), and after 5 retries and data refresh my ea can't open the first order, your code produces the information/error:

    2009.03.06 16:56:17 AUDNZD,M5: Order 0 failed to close. Error:4105

    this is correct, because there is no order on which the function closeprevorder can operate - so its not really an function error, but now i know the code is activated, if the first and follwing orders accepted, no Order0 error occurs - but it closes no orders, example: if we have 3 orders (0.1, 0.2, 0.4) only the last two orders should be remaining, order #1 should be closed at the moment if order #3 is traded, therefore function closeprevorder must be the last running function - I know you know this and what i mean.

    thanks for suggestions

    IX
    Last edited by ixbone; 03-06-2009 at 18:18.

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
  •