MA Cross Over Doesn't Work Properly HELP!

 
//+------------------------------------------------------------------+
//|                                                       test01.mq4 |
//|                                                    helmy gabriel |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "helmy gabriel"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int fastMa = 9;
input int slowMa = 21;
input int fastMaShift = 0;
input int fastMaMode = 0;
input int fastMaPrice = 0;
input int slowMaShift = 0;
input int slowMaMode = 0;
input int slowMaPrice = 0;
input double lotSize = 0.01;
input int stopLose = 15;
input int takeProfit = 100;
double pips;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
 double tickSize = MarketInfo(Symbol(),MODE_TICKSIZE);
 if(tickSize == 0.00001 || Point == 0.001)
 pips = tickSize*10;
 else pips = tickSize;
 
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
 double fastMaV0 = iMA(NULL,0,fastMa,fastMaShift,fastMaMode,fastMaPrice,0);
 double fastMaV1 = iMA(NULL,0,fastMa,fastMaShift,fastMaMode,fastMaPrice,1);
 double fastMaV2 = iMA(NULL,0,fastMa,fastMaShift,fastMaMode,fastMaPrice,2);
 double fastMaV3 = iMA(NULL,0,fastMa,fastMaShift,fastMaMode,fastMaPrice,3);
 double fastMaV4 = iMA(NULL,0,fastMa,fastMaShift,fastMaMode,fastMaPrice,4);
 /////////////////////////////////////////////////////////////////////////////
 double slowMaV0 = iMA(NULL,0,slowMa,slowMaShift,slowMaMode,slowMaPrice,0);
 double slowMaV1 = iMA(NULL,0,slowMa,slowMaShift,slowMaMode,slowMaPrice,1);
 double slowMaV2 = iMA(NULL,0,slowMa,slowMaShift,slowMaMode,slowMaPrice,2);
 double slowMaV3 = iMA(NULL,0,slowMa,slowMaShift,slowMaMode,slowMaPrice,3);
 double slowMaV4 = iMA(NULL,0,slowMa,slowMaShift,slowMaMode,slowMaPrice,4);
 ///////////////////////////////////////////////////////////////////////////// SELL
 if(fastMaV2>slowMaV2&&  fastMaV1<slowMaV1)
 if(OrdersTotal()==0)
     OrderSend(Symbol(),1,lotSize,Bid,3,Bid+(stopLose*pips),Bid-(takeProfit*pips),NULL,0,0,Red);
 ////////////////////////////////////////////////////////////////////////////////////////////BUY
  if(fastMaV2<slowMaV2&&  fastMaV1>slowMaV1)
  if(OrdersTotal()==0)
     OrderSend(Symbol(),0,lotSize,Ask,3,Ask-(stopLose*pips),Ask+(takeProfit*pips),NULL,0,0,Green);


  
  
  
  
  }
//+------------------------------------------------------------------+

HI, I tried this EA but it doesn't buy or sell at the MA Cross Over what is the issue here and it gives me (return value of 'OrderSend' should be checked) Error.  when I put return nothing happened and still have the same Error HELP guys.

Thanks. 

 
Anyone :) .
 
  1. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?

  2.  OrderSend(Symbol(),0,lotSize,Ask,3,Ask-(stopLose*pips),Ask+(takeProfit*pips),NULL,0,0,Green);

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to 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.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

 
William Roeder:
  1. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?

  2. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to 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.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

hey bro why so aggressive with me ? I asked for help I didn't find it in the documentary you don't have to debug it for me just point me to a book or article I cant find one .  

 
themasterx7: hey bro why so aggressive with me ? I asked for help I didn't find it in the documentary you don't have to debug it for me just point me to a book or article I cant find one .  

You can't find anything in the documentation because you don't know what's wrong. Debug your code.

Poor little snowflake — steaming at the slightest heat.

 
William Roeder:

You can't find anything in the documentation because you don't know what's wrong. Debug your code.

Poor little snowflake — steaming at the slightest he

Actually I debugged my code and I know what's wrong I told you above !  Is there any example of CrossOver MAs that works and I'll studied as well ? 

 
themasterx7:

HI, I tried this EA but it doesn't buy or sell at the MA Cross Over what is the issue here and it gives me (return value of 'OrderSend' should be checked) Error.  when I put return nothing happened and still have the same Error HELP guys.

Thanks. 

The code should work,

Check your return code to know where the problems come from


 

    if(fastMaV2<slowMaV2 && fastMaV1>slowMaV1)
  if(OrdersTotal()==0)
    {
    int tic =   OrderSend(Symbol(),0,lotSize,Ask,3,Ask-(stopLose*pips),Ask+    (takeProfit*pips),NULL,0,0,Green);
    if(tic < 0 )
      {
      Print( " ERROR BUY  err N° ",(string)GetLastError() );     
      }
   else Print( " BUY  ticket N° ",(string)tic );     
    }  
 

Check the journal.

if it print something, problem come from ordersend, else from the entry condition

 
themasterx7:
Anyone :) .

corrected.

corr
//+------------------------------------------------------------------+
//|                                                       test01.mq4 |
//|                                                    helmy gabriel |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "helmy gabriel"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int fastMa = 9;
input int slowMa = 21;
input int fastMaShift = 0;
input int fastMaMode = 0;
input int fastMaPrice = 0;
input int slowMaShift = 0;
input int slowMaMode = 0;
input int slowMaPrice = 0;
input double lotSize = 0.01;
input int stopLose = 150;
input int takeProfit = 100;
double pips;
bool ordersendresult;
int ticket;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   double tickSize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if(tickSize == 0.00001 || Point == 0.001)
      pips = tickSize*10;
   else
      pips = tickSize;

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   bool rst;

   double fastMaV0 = iMA(NULL,0,fastMa,fastMaShift,fastMaMode,fastMaPrice,0);
   double fastMaV1 = iMA(NULL,0,fastMa,fastMaShift,fastMaMode,fastMaPrice,1);

//double fastMaV2 = iMA(NULL,0,fastMa,fastMaShift,fastMaMode,fastMaPrice,2);
//double fastMaV3 = iMA(NULL,0,fastMa,fastMaShift,fastMaMode,fastMaPrice,3);
//double fastMaV4 = iMA(NULL,0,fastMa,fastMaShift,fastMaMode,fastMaPrice,4);
/////////////////////////////////////////////////////////////////////////////
   double slowMaV0 = iMA(NULL,0,slowMa,slowMaShift,slowMaMode,slowMaPrice,0);
   double slowMaV1 = iMA(NULL,0,slowMa,slowMaShift,slowMaMode,slowMaPrice,1);
//double slowMaV2 = iMA(NULL,0,slowMa,slowMaShift,slowMaMode,slowMaPrice,2);
//double slowMaV3 = iMA(NULL,0,slowMa,slowMaShift,slowMaMode,slowMaPrice,3);
//double slowMaV4 = iMA(NULL,0,slowMa,slowMaShift,slowMaMode,slowMaPrice,4);
///////////////////////////////////////////////////////////////////////////// SELL
   if(fastMaV0>slowMaV0 &&  fastMaV1<slowMaV1 &&OrdersTotal()==0)
     {
      ticket= OrderSend(Symbol(),OP_BUY,lotSize,Ask,3,0,0,NULL,0,0,Green);
      if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
         rst=OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice()-(stopLose*Point),OrderOpenPrice()+(takeProfit*Point),0,CLR_NONE);
     }

   if(fastMaV0<slowMaV0 &&  fastMaV1>slowMaV1 && OrdersTotal()==0)
     {
      ticket= OrderSend(Symbol(),OP_SELL,lotSize,Bid,3,0,0,NULL,0,0,Red);
      if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
         rst=OrderModify(ticket,OrderOpenPrice(),
         NormalizeDouble((OrderOpenPrice()+(stopLose*Point)),MODE_DIGITS),
         NormalizeDouble((OrderOpenPrice()-(takeProfit*Point)),MODE_DIGITS),
         0,
         CLR_NONE);
         
          }

////////////////////////////////////////////////////////////////////////////////////////////BUY






  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
 
Mehmet Bastem:

corrected.

THANK YOU, can you recommend for me a book or article to learn more about mql4.

 
ffoorr:

The code should work,

Check your return code to know where the problems come from


Check the journal.

if it print something, problem come from ordersend, else from the entry condition

THANK YOU :)

 
themasterx7:

THANK YOU, can you recommend for me a book or article to learn more about mql4.

Here are the old program of MT4.

https://book.mql4.com/appendix/examples

The include file, down the page,  show the good way to code an EA in MT4

List of Programs - Appendixes - MQL4 Tutorial
List of Programs - Appendixes - MQL4 Tutorial
  • book.mql4.com
Expert Advisors:
Reason: