Download MetaTrader 5

get manual closing order event

To add comments, please log in or register
Mindexperiment
87
Mindexperiment 2014.01.07 10:49 

Hi all,


I need to decrease a variable every time I close an order manually..

the variable to decrease is the number_of_open_buy_order or number_of_open_sell_order


I'm under mql4, any help?

Ubzen
5394
Ubzen 2014.01.07 12:38  
Mindexperiment:

Hi all,


I need to decrease a variable every time I close an order manually..

the variable to decrease is the number_of_open_buy_order or number_of_open_sell_order


I'm under mql4, any help?

Show your code.
Mindexperiment
87
Mindexperiment 2014.01.07 13:36  

My code is not written yet :D


I was using a function "checkEvents" but it's a lot confusing, so I would need a better solution..


here is the function but I don't want to use it..

void checkEvents( int magic = 0 ) {
        static bool first = true;
        int getLastError = 0;
        int ordersTotal = OrdersTotal();
        int nowOrders = 0;
        static int preOrders = 0;

        int nowOrdersArr[][2];
        int nowCurOrder = 0;

        int preCurOrder = 0;
        int nowClsOrdersArr[6][3];
        int nowOpnPendingOrders[4];

        bool orderClosed = true;
        bool pendingOrderOpened = false;

        int ticket = 0, type = -1, closeType = -1;

        OBclsSL = 0;
        OBclsTP = 0;
        OSclsSL = 0;
        OSclsTP = 0;

        ArrayResize( nowOrdersArr, MathMax( ordersTotal, 1 ) );
        ArrayInitialize( nowOrdersArr, 0.0 );
        ArrayInitialize( nowClsOrdersArr, 0.0 );
        ArrayInitialize( nowOpnPendingOrders, 0.0 );

        for ( int z = ordersTotal - 1; z >= 0; z-- ) {
                if ( !OrderSelect( z, SELECT_BY_POS ) ) {
                        Print( "The selection of order: ", z, " thrown error #", GetLastError() );
                        continue;
                }
                if ( OrderMagicNumber() == magic && OrderSymbol() == Symbol() ) {
                        nowOrdersArr[nowOrders][0] = OrderTicket();
                        nowOrdersArr[nowOrders][1] = OrderType();
                        nowOrders++;
                }
        }
        ArrayResize( nowOrdersArr, MathMax( nowOrders, 1 ) );

        for ( preCurOrder = 0; preCurOrder < preOrders; preCurOrder ++ ) {
                // memorize the ticket number and the order type
                ticket = preOrdersArr[preCurOrder][0];
                type   = preOrdersArr[preCurOrder][1];
                // assume that, if it is a position, it has been closed
                orderClosed = true;
                // assume that, if it is a pending order, it has not triggered
                pendingOrderOpened = false;
 
                // search in all positions from the current list of open positions
                for ( nowCurOrder = 0; nowCurOrder < nowOrders; nowCurOrder++ ) {
                        // if there is a position with such a ticket number in the list,
                        if ( ticket == nowOrdersArr[nowCurOrder][0] ) {
                                // it means that the position has not been closed (the order has not been cancelled)
                                orderClosed = false;

                                // if its type has changed,
                                if ( type != nowOrdersArr[nowCurOrder][1] ) {
                                        // it means that it was a pending order and it triggered
                                        pendingOrderOpened = true;
                                }
                                break;
                        }
                }

                // if a position has been closed (an order has been cancelled),
                if ( orderClosed ) {
                        // select it
                        if ( !OrderSelect( ticket, SELECT_BY_TICKET ) ) {
                                Print( "The selection of order: ", ticket, " thrown error #", GetLastError() );
                                continue;
                        }
                        // and check HOW the position has been closed (the order has been cancelled):
                        if ( type < 2 ) {
                                // Buy and Sell: 0 - manually, 1 - by SL, 2 - by TP
                                closeType = 0;
                                if ( StringFind( OrderComment(), "[sl]" ) >= 0 || OrderStopLoss() == OrderClosePrice() ) closeType = 1;
                                if ( StringFind( OrderComment(), "[tp]" ) >= 0 ) closeType = 2;
                                } else {
                                // Pending orders: 0 - manually, 1 - expiration
                                closeType = 0;
                                if ( StringFind( OrderComment(), "expiration" ) >= 0 ) closeType = 1;
                        }
                        // and write in the closed orders array that the order of the type 'type' 
                        // was closed by close_type
                        nowClsOrdersArr[type][closeType]++;
                        continue;
                }
                // if a pending order has triggered,
                if ( pendingOrderOpened ) {
                        // write in the triggered orders array that the order of type 'type' triggered
                        nowOpnPendingOrders[type - 2]++;
                        continue;
                }
        }

        if ( !first ) {
                // search in all elements of the triggered pending orders array
                for ( type = 2; type < 6; type++ ) {
                        // if the element is not empty (an order of the type has not triggered), change the variable value
                        if ( nowOpnPendingOrders[type-2] > 0 ) {
                                setOpenEvent( type );
                        }
                }

                // search in all elements of the closed positions array
                for ( type = 0; type < 6; type++ ) {
                        for ( closeType = 0; closeType < 3; closeType++ )       {
                                // if the element is not empty (a position has been closed), change the variable value
                                if ( nowClsOrdersArr[type][closeType] > 0 ) {
                                        setCloseEvent( type, closeType );
                                }
                        }
                }
                } else {
                first = false;
        }

        //---- save the current positions array in the previous positions array
        ArrayResize( preOrdersArr, MathMax( nowOrders, 1 ) );
        for ( nowCurOrder = 0; nowCurOrder < nowOrders; nowCurOrder++ ) {
                preOrdersArr[nowCurOrder][0] = nowOrdersArr[nowCurOrder][0];
                preOrdersArr[nowCurOrder][1] = nowOrdersArr[nowCurOrder][1];
        }
        preOrders = nowOrders;
}
whroeder1
14737
whroeder1 2014.01.07 14:58  
  1. Mindexperiment: I need to decrease a variable every time I close an order manually..
    the variable to decrease is the number_of_open_buy_order or number_of_open_sell_order
    No you don't. Just (re)count how many buys/sells are still open. It will be one less every time you close an order.
  2. Mindexperiment: here is the function but I don't want to use it..
     if ( StringFind( OrderComment(), "[sl]"
    Some brokers add [sl]/[tp] to comments, some do NOT. Brokers can also change comments, including complete replacement. Never depend on comments.
Mindexperiment
87
Mindexperiment 2014.01.07 15:20  
WHRoeder:
  1. Mindexperiment: I need to decrease a variable every time I close an order manually..
    the variable to decrease is the number_of_open_buy_order or number_of_open_sell_order
    No you don't. Just (re)count how many buys/sells are still open. It will be one less every time you close an order.
  2. Mindexperiment: here is the function but I don't want to use it..
    Some brokers add [sl]/[tp] to comments, some do NOT. Brokers can also change comments, including complete replacement. Never depend on comments.

Thanks WHRoeder


yes I can re-count the total orders, this should be a good advice..


I won't use the function "checkEvents" so no problem for comments..

Mindexperiment
87
Mindexperiment 2014.01.07 15:40  

I can't check on metatrader...


this is conceptually the function any improvement appriciated :)


void checkOrders() {
        int i, tip, totals = OrdersTotal();

        if ( totals == 1 ) {
                for ( i = totals-1; i >= 0; i-- ) {
                        if ( OrderSelect( i, SELECT_BY_POS ) == true ) {
                                tip = OrderType();
                                switch ( tip ) {
                                        case OP_BUY:
                                                if ( orders > 0 ) {
                                                        orders = 0;
                                                }

                                        case OP_SELL:
                                                if ( orderb > 0 ) {
                                                        orderb = 0;
                                                }
                                }
                        }
                }
        }
}
Simon Gniadkowski
Moderator
18002
Simon Gniadkowski 2014.01.07 15:48  
Mindexperiment:

I can't check on metatrader...


this is conceptually the function any improvement appriciated :)

That doesn't do what you described in your first post . . . what are you actually trying to achieve ?
Mindexperiment
87
Mindexperiment 2014.01.07 15:56  

this function count the numbers of total orders and check if there's only 1 order (this is the only situation I can have where I don't know if EA is align to manual trading)

If there's 1 order it check that the "orderb" or "orders" variables MUST be 0 depending on the ordertype.


if total_orders = 1:

If I have 1 buy I can't have also 1 sell

If I have 1 sell I can't have also 1 buy


So my EA use only 1 order at time long/short. I can have only a maximun of 2 orders at the same time, if I manually close one of the order I need the EA to sync the variable to let it open another order.. should be right..

Simon Gniadkowski
Moderator
18002
Simon Gniadkowski 2014.01.07 16:17  
Mindexperiment:

this function count the numbers of total orders and check if there's only 1 order (this is the only situation I can have where I don't know if EA is align to manual trading)

If there's 1 order it check that the "orderb" or "orders" variables MUST be 0 depending on the ordertype.


if total_orders = 1:

If I have 1 buy I can't have also 1 sell

If I have 1 sell I can't have also 1 buy


So my EA use only 1 order at time long/short. I can have only a maximun of 2 orders at the same time, if I manually close one of the order I need the EA to sync the variable to let it open another order.. should be right..

OK, so why not . . .

int CheckOrders() {
   int OrderPosition, OType, OrdersStatus = 0;

   for ( OrderPosition = OrdersTotal() - 1; OrderPosition >= 0; OrderPosition-- ) {
      if ( OrderSelect( OrderPosition, SELECT_BY_POS ) ) {
         OType = OrderType();
         switch ( OType ) {
            case OP_BUY: OrderStatus |= 3; break;
            
            case OP_SELL: OrderStatus |= 5; break;
            }
         }
      }
      
   return(OrderStatus);
   }


the returned value can be checked bitwise, and with 1 and a value > 0 tells you there are orders, and with 2 and a value > 0 tells you there are OP_BUY, and with 4 and a value > 0 tells you there is an OP_SELL

Note: in your proposed solution you need to add the break in the switch expression.

Mindexperiment
87
Mindexperiment 2014.01.07 16:30  
RaptorUK:

OK, so why not . . .


the returned value can be checked bitwise, and with 1 and a value > 0 tells you there are orders, and with 2 and a value > 0 tells you there are OP_BUY, and with 4 and a value > 0 tells you there is an OP_SELL

Note: in your proposed solution you need to add the break in the switch expression.


Your solution add complexity on my start function.. I don't need to worry about internal variables



I don't understand this: |=

I know =, ==, ===, != but never seen |= ...what's that?

Simon Gniadkowski
Moderator
18002
Simon Gniadkowski 2014.01.07 16:38  
Mindexperiment:


I don't understand this: |=

I know =, ==, ===, != but never seen |= ...what's that?

a |= b means a = a | b means a = bitwise OR of a and b

|= Assignment operators

To add comments, please log in or register