Results 1 to 3 of 3
 0 Attachment(s)    

Thread: Help Proper use of IsTradeContextBusy()

  1. #1

    Default Help Proper use of IsTradeContextBusy()

    I'd like advice.... I'm experiencing Trade Context is Busy errors, very occasionally ...(but always unwelcome) when Closing Multiple Orders. I've tried

    Sleep(XXXX) but this has two problems..

    First) It wastes time when there is nothing wrong..

    Two) It doesn't always cure Trade Context Busy errors.

    I'd like to check for NO TradeContextBusy BEFORE trying to close a position.

    In the first "Loop code"... will it check before EACH try to OrderClose...or only check once.... AND..... Should there also be a second if(!IsTradeContectBusy())

    AFTER the Buy OrderClose() but before the Sell OrderClose()??

    In the second code...."Straight through" one time close code...is the Syntax correct??

    ADDITIONALLY......

    Are ALL the RefreshRates() in these two codes a waste of time because I'm specifying MarketInfo(OrderSymbol()?? I think they may be....

    Can I remove the Sleep() if I use the check for TradeContextBusy?? I see NO need for them.....

    Thanks for your help...Bill


    //---------LOOP CODE---------------
    if (Closenow==True)
    {
    for(i=OrdersTotal()-1;i>=0;i--)
    {
    OrderSelect(i, SELECT_BY_POS);
    if(!IsTradeContextBusy())
    int type = OrderType();
    bool result = false;
    switch(type)
    {
    //Close opened long positions
    case OP_BUY : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(Or derSymbol(),MODE_BID),Slippage,Pink);
    break;
    Sleep(69);
    RefreshRates();
    //Close opened short positions
    case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(Or derSymbol(),MODE_ASK),Slippage,Pink);
    Sleep(69);
    RefreshRates();
    }
    if(result == false)
    {
    Sleep(3000);
    }
    }
    }

    //---------------------------------------------------------------------------------


    //----------ONCE THROUGH CODE-------------------------
    if (Closenow == True)
    if (OT>0)
    {
    RefreshRates();
    OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
    if(OrderType()==OP_BUY&&!IsTradeContextBusy())
    OrderClose(OrderTicket(),OrderLots(),MarketInfo(Or derSymbol(),MODE_BID),5,Violet);
    RefreshRates();
    if(OrderType()==OP_SELL&&!IsTradeContextBusy())
    OrderClose(OrderTicket(),OrderLots(),MarketInfo(Or derSymbol(),MODE_ASK),5,Violet);
    RefreshRates();
    if(OrderType()>OP_SELL&&!IsTradeContextBusy()) //pending orders
    OrderDelete(OrderTicket());
    Sleep(60);
    }
    Last edited by BillR; 11-28-2009 at 14:38.

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

    Default

    Hi BillR,

    You can use a code that will check if an order has been opened since the last signal. If no, the EA continues to try to enter the wanted order.

    You just need to set a datetime when you get a signal and to compare it to the order open time of the current trades.

  3. #3

    Default

    Hi Funyoo..Thanks...The problem I was having is in closing the Orders...If I had 5 to close and got a TradeContextBusy error...the loop would pass it and not close that one.... I have a check for OrdersTotal=0 to reset all the variables at the end of my program....so having that one order not close was leaving the EA stuck.... The way around it I think is IsTradeAllowed() as a condition of the close order loop.....what do you think?? Will it work?? It works in Backtesting...and if I change it to &&!IsTradeAllowed() none close as expected...so the test for the condition seems valid....

    if (Closenow==True)
    {
    for(i=OrdersTotal()-1;i>=0&&IsTradeAllowed();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(Or derSymbol(),MODE_BID),Slippage,Pink);
    break;

    //Close opened short positions
    case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(Or derSymbol(),MODE_ASK),Slippage,Pink);
    }
    if(result == false)
    {
    Sleep(30);
    }
    }
    }
    Last edited by BillR; 11-29-2009 at 19:53.

Similar Threads

  1. How to do a proper simulation with renko chart
    By fxtester in forum MQL programming
    Replies: 0
    Last Post: 09-06-2014, 10:34

Tags for this Thread

backtesting, change, closing, color, signal, slippage, time

Posting Permissions

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