My EA for MQL 4 does not work !!

 

Hello All !


I have been studying on coding my EA for 2 months. But I cant even code a simple EA for Buy Market Order. Eventhough it seems i did everything correctly, it does not work and apparently i am missing sth. I really appreciate your help and time. Thanks in advance for your sparing time and help.


Here is the code!

//+------------------------------------------------------------------+
//|                                          TO THE LAST MOHICAN.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+


extern double StopLoss = 15;//50;
extern double TakeProfit = 75;//100;

extern int TrailingStop = 100;
extern int MinimumProfit = 50;

extern int Slippage = 10;

extern int MagicNumber = 123;

extern int  1 ;//RED
extern int TENmaPERIOD = 10 ;//PINK
extern int TWENTY7maPERIOD = 27 ;//YELLOW
extern int FIFTYmaPERIOD = 50 ;//ORANGE
extern int maSHIFT= 0 ;



   //GLOOBAAAAALLL VARS
int BuyTicket;
int SellTicket;
bool Closed;
bool Deleted;




double UsePoint;
int UseSlippage;




int OnInit()
  {
//---
   UsePoint = PipPoint(Symbol());
   UseSlippage = GetSlippage(Symbol(), Slippage);
   
   //---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
                  ////////////////// 1MA PERIODS
      double  iMA(Symbol(),PERIOD_H1,ONEmaPERIOD,maSHIFT,MODE_EMA,PRICE_CLOSE,0);
      double  iMA(Symbol(),PERIOD_H1,ONEmaPERIOD,maSHIFT,MODE_EMA,PRICE_CLOSE,1);

                  ////////////////// 10MA PERIODS*/
      double TEN_FASTma = iMA(Symbol(),PERIOD_H1,TENmaPERIOD,maSHIFT,MODE_EMA,PRICE_CLOSE,0);
      double TEN_FASTma_pre1 = iMA(Symbol(),PERIOD_H1,TENmaPERIOD,maSHIFT,MODE_EMA,PRICE_CLOSE,1);


      
      if( ONE_FASTma_pre1 < ONE_FASTma_pre1 && ONE_FASTma > TEN_FASTma)
         {
         
         bool Select = OrderSelect(SellTicket,SELECT_BY_TICKET);
            if ( OrderCloseTime() == 0 && SellTicket >0 )
               {
               
               double CloseLots = OrderLots();
               RefreshRates();
               double ClosePrice = Ask;
               Closed = OrderClose(SellTicket,CloseLots,ClosePrice, UseSlippage,clrRed);
               
               }
               
               RefreshRates();
               double OpenPrice = Ask;
               double BuyTP;
               double BuySL;
                  
                   BuySL = OpenPrice - (StopLoss*UsePoint);
                   BuyTP = OpenPrice + (TakeProfit * UsePoint);
                  
                  //OPEN BUY MARKET ORDER
                  BuyTicket = OrderSend (Symbol(),OP_BUY, LotSize, OpenPrice, UseSlippage, BuySL, BuyTP, " BUYMARKET ORDER ", MagicNumber,0,clrBlue);
               
         
         }
         
      //else if ( ONE_FASTma_pre1 > ONE_FASTma_pre1 && ONE_FASTma < TEN_FASTma)
  }
//+------------------------------------------------------------------+


//  to be used inside the function of INT INIT
int GetSlippage (string Currency, int SlippagePips)

   {
     double CalcSlippage=0;
     double CalcDigits = MarketInfo(Currency,MODE_DIGITS);
     if( CalcDigits == 2 || CalcDigits == 4 ) CalcSlippage = SlippagePips;
     else if (CalcDigits == 3 || CalcDigits == 5) CalcSlippage = SlippagePips*10;
     return (CalcSlippage); 
   }

//+------------------------------------------------------------------+

// Pip Point Function
double PipPoint(string Currency)
   {
      double CalcPoint=0 ;
      double CalcDigits = MarketInfo(Currency,MODE_DIGITS);
      if(CalcDigits == 2 || CalcDigits == 3) CalcPoint = 0.01;
      else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
      return(CalcPoint);

   }
 
emsahii:

Hello All !

I have been studying on coding my EA for 2 months. But I cant even code a simple EA for Buy Market Order. Eventhough it seems i did everything correctly, it does not work and apparently i am missing sth. I really appreciate your help and time. Thanks in advance for your sparing time and help.

Here is the code!

Seems you did everything correctly?  The compiler says otherwise... solve the compilation errors first.

 
emsahii:

Hello All !


I have been studying on coding my EA for 2 months. But I cant even code a simple EA for Buy Market Order. Eventhough it seems i did everything correctly, it does not work and apparently i am missing sth. I really appreciate your help and time. Thanks in advance for your sparing time and help.


Here is the code!

Hi emsahii.

If there's a struggle, its a good idea to get back to basics. Just write a simple EA without all the bells and whistles. And make sure you understand the debugging tools; the errors log in the editor, and the EA log and journal tabs on the MT4 terminal....

As Seng says, there are compilation errors, and even if you fixed them, you might not be learning for next time.....

If you ran only this code, that will at least create a buy order for you, with a SL and TP - if the Ask price is above the MA. It won't place an order until trading opens of course.


#property strict
void OnInit()
{
 OnTick();  // code runs on the weekend when there are no ticks!
}

void OnTick()
  {
  if(OrdersTotal()>1)return;  // just one order
   if(Ask>iMA(_Symbol,0,15,0,MODE_EMA,PRICE_CLOSE,0)){
     double lots=.01;
     double orderprice=Ask;
     double sl=Bid-15*Point;
     double tp=Ask+50*Point;  
    int ticket=OrderSend(_Symbol,OP_BUY,lots,Ask,3,sl,tp,"Comment",0,0,clrNONE);
    }
  }

Then, you can look at expanding the code, adding criteria, magic numbers etc, running the code every x minutes etc.

 
     double orderprice=Ask;
     double sl=Bid-15*Point;
     double tp=Ask+50*Point; 
You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.
  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP would be longer. Don't you want the same/specified amount for either direction?
  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25
  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (Control-O) → charts → Show ask line.)
 
William Roeder:
You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.
  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP would be longer. Don't you want the same/specified amount for either direction?
  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25
  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (Control-O) → charts → Show ask line.)
Thank you ! I have a long way to go !!!
 
andrew:

Hi emsahii.

If there's a struggle, its a good idea to get back to basics. Just write a simple EA without all the bells and whistles. And make sure you understand the debugging tools; the errors log in the editor, and the EA log and journal tabs on the MT4 terminal....

As Seng says, there are compilation errors, and even if you fixed them, you might not be learning for next time.....

If you ran only this code, that will at least create a buy order for you, with a SL and TP - if the Ask price is above the MA. It won't place an order until trading opens of course.


Then, you can look at expanding the code, adding criteria, magic numbers etc, running the code every x minutes etc.

Thank you sir!
 
Seng Joo Thio:

Seems you did everything correctly?  The compiler says otherwise... solve the compilation errors first.

Thank you !
Reason: