Mql4 selllimit

lbranjord

Well-known member
Messages
453
Likes
15
Quick question. Coding my first real EA here.

I want a sell limit that expires after a certain time. Is this possible or do I have to code my own method? Another question, how do I cancel a pending order, or cancel all pending orders?

Here's an example of my pathetic code :)

OrderSend(Symbol(), OP_SELLLIMIT, Lot, Midpoint,Slippage,Stoploss,Target,"Bearish Outside Bar", 0, 0,CLR_NONE);
 
Hi,

You can set expiration time on pending order:

Code:
int OrderSend(string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment=NULL, int magic=0, datetime [B][I]expiration[/I][/B]=0, color arrow_color=CLR_NONE)

From MetaTrader docs:

Applying of pending order expiration time can be disabled in some trade servers. In this case, when a non-zero value is specified in the expiration parameter, the error 147 (ERR_TRADE_EXPIRATION_DENIED) will be generated.

Example:

Code:
datetime [B]extime [/B]= D'2010.01.01 00:00';
OrderSend(Symbol(), OP_SELLLIMIT, Lot, Midpoint, Slippage, Stoploss, Target, "Bearish Outside Bar", 0, [B]extime[/B], CLR_NONE);

Cancelling pending order:

Code:
bool OrderDelete(int ticket, color Color=CLR_NONE)

Cancelling all pending orders:

Code:
void CancelAllPendingOrders()
{
    for(int i = 0; i < OrdersTotal(); i++) 
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            switch(OrderType())
            {
            case OP_BUY:
            case OP_SELL:
                break;
                
            default:
                if(!OrderDelete(OrderTicket())) {
                    Alert("Error: Delete pending order failed!, ", GetLastError());
                }
            }
        }
    }
}

Hope it helps.
 
Quick question. Coding my first real EA here.

I want a sell limit that expires after a certain time. Is this possible or do I have to code my own method? Another question, how do I cancel a pending order, or cancel all pending orders?

Here's an example of my pathetic code :)

OrderSend(Symbol(), OP_SELLLIMIT, Lot, Midpoint,Slippage,Stoploss,Target,"Bearish Outside Bar", 0, 0,CLR_NONE);

It is VERY easy to do what you want. The details of OrderSend are:

int OrderSend( string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment=NULL, int magic=0, datetime expiration=0, color arrow_color=CLR_NONE)

As you can see the second to last parameter is datetime expiration = 0. What this means is that you can set the order to expire at 'expiration' and if you don't put a value there, then the value will be 0 and the order won't expire. The expiraiton is in number of seconds from the time you put the order in.
 
Wow,

Great help here. I didn't expect to see a lot of MQL4 coders on these boards. Thanks all
 
Wow,

Great help here. I didn't expect to see a lot of MQL4 coders on these boards. Thanks all

I ended up calculating my expiration time on each order in seconds. I had different times for each chart time frame

Daily=24*60*60
4hr=60*60*4
etc.

The code to delete all pending orders is great too though. I know I will have use for that in the future.
 
Top