Jacket
Jacket

Reputation: 93

My First MQL4 EA does not generate any Orders. Why?

I'm trying to build a first EA, code below, but it doesn't execute any trades.

I know it is very simple, but logically, I think this should Buy and Sell.

I'm trying to only use a code that I understand.

I'd appreciate it if anyone had any feedback!

  //
  extern int sma_short    =    10;
  extern int sma_long     =    20;
  extern double fakeout   =     0.0005 ;
  extern double stoploss  =   150;
  extern double risk      =     1; 
  extern int slippage     =     5;
  extern int magicnumber  = 12345;
  extern bool SignalMail  =     false;
  extern bool UseTrailingStop = true;
  extern int TrailingStop =   150;

  double sma_short_t3; 
  double sma_short_t0; 
  double sma_long_t3; 
  double sma_long_t0;
  double sma_diff_t3;
  double sma_diff_t0;
  double lots;
  double stoplosslevel;

  int P = 1;
  int ticket, ticket2;
  int total = OrdersTotal();

  bool OpenLong   = false;
  bool OpenShort  = false;
  bool CloseLong  = false;
  bool CloseShort = false;
  bool isYenPair  = false;
  bool OpenOrder  = false;


  int OnInit()
  {
     if (  Digits == 5 || Digits == 3 || Digits == 1 ) P = 10; else P = 1; // To account for 5 digit brokers
     if (  Digits == 3 || Digits == 2 ) isYenPair = true; // Adjust for YenPair
     return( INIT_SUCCEEDED );
     }
  //+------------------------------------------------------------------+
  //| Expert deinitialization function                                 |
  //+------------------------------------------------------------------+
  void OnDeinit( const int reason )
  {
    }
  //+------------------------------------------------------------------+
  //| Expert tick function                                             |
  //+------------------------------------------------------------------+
  void Start()
  {
     sma_short_t3 = iMA( NULL, 0, sma_short, 0, MODE_SMA, PRICE_CLOSE, 3 );
     sma_short_t0 = iMA( NULL, 0, sma_short, 0, MODE_SMA, PRICE_CLOSE, 0 );
     sma_long_t3  = iMA( NULL, 0, sma_long,  0, MODE_SMA, PRICE_CLOSE, 3 );
     sma_long_t0  = iMA( NULL, 0, sma_long,  0, MODE_SMA, PRICE_CLOSE, 0 );

     sma_diff_t3  = sma_long_t3 - sma_short_t3;
     sma_diff_t0  = sma_long_t0 - sma_short_t0;

     if (  OpenOrder )
     {
           if (  CloseLong || CloseShort )
           {
                 OrderClose( OrderTicket(), OrderLots(), Bid, slippage, MediumSeaGreen );

                 OpenOrder  = False;
                 CloseLong  = False;
                 CloseShort = False;
                 }
           }
     if (  sma_diff_t3 < 0 && sma_diff_t0 > fakeout )
     {     
           OpenLong   = True ;
           CloseShort = True;
           }         
     if (  sma_diff_t3 > 0 && sma_diff_t0 < -fakeout )
     {     
           OpenShort  = True;  
           CloseLong  = True;  
           }

     lots = risk * 0.01 * AccountBalance() / ( MarketInfo( Symbol(), MODE_LOTSIZE ) * stoploss * P * Point ); // Sizing Algo based on account size
     if (  isYenPair == true ) lots = lots * 100; // Adjust for Yen Pairs
     lots = NormalizeDouble( lots, 2 );

     if (  OpenLong )
     {     
           stoplosslevel = Ask - stoploss * Point * P;
           OrderSend( Symbol(), OP_BUY, lots, Ask, slippage, stoplosslevel, 0, "Buy(#" + magicnumber + ")", magicnumber, 0, DodgerBlue );
           OpenOrder  = True;
           }
     if (  OpenShort )
     {     
           stoplosslevel = Bid + stoploss * Point * P;
           OrderSend( Symbol(), OP_SELL, lots, Ask, slippage, stoplosslevel, 0, "Buy(#" + magicnumber + ")", magicnumber, 0, DodgerBlue );
           OpenOrder = True ; 
           }
     }
  //+------------------------------------------------------------------+

Upvotes: 1

Views: 195

Answers (2)

user3666197
user3666197

Reputation: 1

Few things in MQL4 to rather get used to:

All PriceDOMAIN data has to be NormalizeDouble() before sending to MetaTrader 4 Server.

All EquityDOMAIN data has to follow a set of discrete values,
having MathMin( aMinLOT_SIZE + N * aMinLOT_STEP, aMaxLOT_SIZE ). Normalisation of EquityDOMAIN data is broker-specificand instrument-specific, so need not be always 2.

For XTO, OrderSend(), OrderMOdify(), OrderClose(), one ought follow something like this:

if (   OpenLong )
 {     stoplosslevel = NormalizeDouble( Ask - stoploss * Point * P, _Digits );  // ALWAYS NormalizeDouble()
       int RetCODE   = OrderSend( _Symbol,
                                  OP_BUY,
                                  lots,
                                  Ask,
                                  slippage,
                                  stoplosslevel,
                                  0,
                                  "Buy(#" + magicnumber + ")",
                                  magicnumber,
                                  0,
                                  DodgerBlue
                                  );
        if (  RetCODE < 0 )
        {     Print( "EXC: Tried to go LONG, OrderSend() failed to get confirmed ( Errno: ", GetLastError(), " )" );
              }
        else
        {     OpenOrder  = True;
              ...
              }
        ...
        }

Upvotes: 1

Daniel Kniaz
Daniel Kniaz

Reputation: 4681

and why do you use (MarketInfo(Symbol(),MODE_LOTSIZE)? what is the idea of that? first try with double lots = 1.00; and if problem still exists - please add a line telling about the reason why ea failed to send. sth like int ticket = OrderSend(***); if(ticket<0)Print("error=",GetLastError()); or more complex telling about the actual prices, lots, stoploss etc.

Upvotes: 1

Related Questions