Search code examples
algorithmic-tradingmql4metatrader4mql5

MQL4 - ERROR 4111 when calling OrderSend() method


I am trying to place order, but my call to OrderSend() method ( https://docs.mql4.com/trading/ordersend )
is failing:

2016.08.01 00:51:09.710 2016.07.01 01:00 s EURUSD,M1: OrderSend error 4111

void OnTick() {
    if (  OrdersTotal() == 0 ){
          int   result =  OrderSend( NULL, OP_SELL, 0.01, Bid, 5, 0, Bid - 0.002, NULL, 0, 0, clrGreen );
          if (  result <  0 ) Print( "Order failed #", GetLastError() );
          else                Print( "Order success" );
    }
} 

Do you know what am I doing wrong please?


Solution

  • Let's dis-assemble the OrderSend() call first:

    int result = OrderSend( NULL,             // string:      _Symbol,
                            OP_SELL,          // int:         OP_SELL,
                            0.01,             // double:      NormalizeLOTs( nLOTs ),
                            Bid,              // double:      NormalizeDouble( Bid, Digits ),
                            5,                // int:         slippagePOINTs,
                            0,                // double:      {       0 | NormalizeDouble( aSlPriceTARGET, Digits ) },
                            Bid-0.002,        // double:      {       0 | NormalizeDouble( aTpPriceTARGET, Digits ) },
                            NULL,             // string:      {    NULL | aBrokerUnguaranteedStringCOMMENT },
                            0,                // int:         {       0 | aMagicNUMBER },
                            0,                // datetime:    {       0 | aPendingOrderEXPIRATION },
                            clrGreen          // color:       { clrNONE | aMarkerCOLOR }
                            );
    

    For one's further peace-of-mind, one ought always normalise all the values, that have some restrictive handling on the MQL4-side ( prices + lot ( quantised ) values -- as these are not continuous values in R domain, but rather quantum-stepped:

    prices: having a 0.00001 or 0.0001 or 0.001 or 0.01 or 0.1 or 1.0 etc. stepping,

    lot-volumes: being more restricted by the Broker-specific settings, per instrument, of three key values, all allowable volume sizes have to meet:
    [aMinLOTs<=, +aMinLotSTEP, <=aMaxLOTs] + a proper digit normalisation
    ~ thus a double NormalizeLOTs( aProposedVOLUME ) {...} is a handy tool for a seamless implementation of both parts of this need.


    Error 4111:

    There are a few other barriers, that prevent your MetaTrader Terminal 4 from running your code smooth:

    4111
    ERR_SHORTS_NOT_ALLOWED
    Shorts are not allowed. Check the Expert Advisor properties

     if (  !TerminalInfoInteger( TERMINAL_TRADE_ALLOWED ) ) 
            Alert( "Check if automated trading is allowed in the terminal settings!" ); 
     else  if (  !MQLInfoInteger( MQL_TRADE_ALLOWED ) )
                 Alert( "Automated trading is forbidden in the program settings for ",
                        __FILE__
                        );
    

    This instructs user to revise MetaTrader Terminal 4 settings,
    under MT4 -> Tools -> Options -> ExpertAdvisor Tab
    and Broker-side Trading Instrument conditions, where shorting for some instruments may be restricted in general, or just for certain Account type(s).

     if (  !AccountInfoInteger( ACCOUNT_TRADE_EXPERT ) )
            Alert( "Automated trading is forbidden for the account",
                    AccountInfoInteger( ACCOUNT_LOGIN ),
                   " at the trade server side. Contact Broker's Customer Care Dept."
                   );
    

    For more details, printScreens and demonstrated programmatic handling of this group of both Terminal-side / Broker-side barriers: ref.-> MQL4 Reference / MQL4 programs / Trade Permission