My own expert advisor is not run the order

 

Hello everybody, I am newbie . I really need help for my first Expert Advisor ever.

I am run it on M1 timeframe. And it do nothing although the logic of condition is okay. Help me please.

Here is my order code:

int CountSellPosition(){



   int NumberOfSellPositions= 0;

   for(int i=OrdersTotal()-1;i >=0; i--)

   {

   // select an open trade

   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

   // get the currency pair

   string CurrencyPair=OrderSymbol();

   if(_Symbol== CurrencyPair)

   if(OrderType()==OP_SELL)

   {

   NumberOfSellPositions=NumberOfSellPositions+1;

   }

  }

  return NumberOfSellPositions;

}

int CountBuyPosition(){



   int NumberOfBuyPositions= 0;

   for(int i=OrdersTotal()-1;i >=0; i--)

   {

   // select an open trade

   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

   // get the currency pair

   string CurrencyPair=OrderSymbol();

   if(_Symbol== CurrencyPair)

   if(OrderType()==OP_BUY)

   {

   NumberOfBuyPositions=NumberOfBuyPositions+1;

   }

  }

  return NumberOfBuyPositions;

}

int start(){

 double MyMovingAverage1 = iMA(_Symbol,_Period,PeriodEMA1,0,MODE_EMA, PRICE_OPEN,0);

 double MyMovingAverage2 = iMA(_Symbol,_Period,PeriodEMA2,0,MODE_EMA, PRICE_OPEN,0);

 double MyMovingAverage1H1 = iMA(_Symbol,PERIOD_H1,PeriodEMA1,0,MODE_EMA, PRICE_CLOSE,0);

 double MyMovingAverage2H1 = iMA(_Symbol,PERIOD_H1,PeriodEMA2,0,MODE_EMA, PRICE_CLOSE,0);

 double MyMovingAverage1H4 = iMA(_Symbol,PERIOD_H4,PeriodEMA1,0,MODE_EMA, PRICE_CLOSE,0);

 double MyMovingAverage2H4 = iMA(_Symbol,PERIOD_H4,PeriodEMA2,0,MODE_EMA, PRICE_CLOSE,0);

 int ticket;

 //Buy position with H4 trends

 if(MyMovingAverage1<Bid & MyMovingAverage1 > MyMovingAverage2 & MyMovingAverage1H4<Bid & MyMovingAverage1H4 > MyMovingAverage2H4 & CountBuyPosition() <1){

        ticket = OrderSend(Symbol(), OP_BUY, lots,Ask,5, 0, 0, "Raymond's 1.1 bot Buy H4 trend",Magic_Number,0,Blue);

        if(ticket > 0){

          if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))

            Print("BUY Order Opened: ", OrderOpenPrice(), " SL:", 0, " TP: ", 0);

          }

          else

            Print("Error Opening BUY  Order: ", GetLastError());

            return(0);

        }

    // Sell - Short position with H4 trends

    if(MyMovingAverage1>Ask & MyMovingAverage1 < MyMovingAverage2 & MyMovingAverage1H4 < MyMovingAverage2H4 & CountSellPosition() <1){

      ticket = OrderSend(Symbol(), OP_SELL, lots,Bid,5, 0, 0, "Raymond's 1.1 bot Sell H4 trend",Magic_Number,0,Red);

      if(ticket > 0){

        if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))

          Print("SELL Order Opened: ", OrderOpenPrice(), " SL:", 0, " TP: ", 0);

        }

        else

          Print("Error Opening SELL Order: ", GetLastError());

          return(0);

      }

 //Buy position with H1 trends

 if(MyMovingAverage1<Bid & MyMovingAverage1 > MyMovingAverage2 & MyMovingAverage1H1 > MyMovingAverage2H1 & CountBuyPosition() <1){

        ticket = OrderSend(Symbol(), OP_BUY, lots,Ask,5, 0, 0, "Raymond's 1.1 bot Buy H1 trend",Magic_Number,0,Blue);

        if(ticket > 0){

          if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))

            Print("BUY Order Opened: ", OrderOpenPrice(), " SL:", 0, " TP: ", 0);

          }

          else

            Print("Error Opening BUY  Order: ", GetLastError());

            return(0);

        }

    // Sell - Short position with H1 trends

    if(MyMovingAverage1>Ask & MyMovingAverage1 < MyMovingAverage2 & MyMovingAverage1H1 < MyMovingAverage2H1 & CountSellPosition() <1){

      ticket = OrderSend(Symbol(), OP_SELL, lots,Bid,5, 0, 0, "Raymond's 1.1 bot Sell H1 trend",Magic_Number,0,Red);

      if(ticket > 0){

        if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))

          Print("SELL Order Opened: ", OrderOpenPrice(), " SL:", 0, " TP: ", 0);

        }

        else

          Print("Error Opening SELL Order: ", GetLastError());

          return(0);

      }

return(0);

}
 
  1. Don't say “although the logic of condition is okay.” It is almost always your code.

  2. Your code uses bit operations.
    if(MyMovingAverage1<Bid & MyMovingAverage1 > MyMovingAverage2 & MyMovingAverage1H1 > MyMovingAverage2H1 & CountBuyPosition() <1){
    Correction, use logical operations.
    if(MyMovingAverage1<Bid && MyMovingAverage1 > MyMovingAverage2 && MyMovingAverage1H1 > MyMovingAverage2H1 && CountBuyPosition() <1){
 
William Roeder:
  1. Don't say “although the logic of condition is okay.” It is almost always your code.

  2. Your code uses bit operations.
    Correction, use logical operations.
I already check it when i use comment to show it is on the chart and it still can change from number 0 to 1 when price meet the condition 
 
Bui Thanh Phat: I already check it when i use comment to show it is on the chart and it still can change from number 0 to 1 when price meet the condition 

@William Roeder just told you that you have many bugs in your code! Yet you respond by saying you have already checked?

Fix the bugs he has pointed out first! Then resubmit your code for analysis, with the new conditions you are facing!

 
Fernando Carreiro:

@William Roeder just told you that you have many bugs in your code! Yet you respond by saying you have already checked?

Fix the bugs he has pointed out first! Then resubmit your code for analysis, with the new conditions you are facing!

int start(){
 double MyMovingAverage1 = iMA(_Symbol,_Period,PeriodEMA1,0,MODE_EMA, PRICE_OPEN,0);
 double MyMovingAverage2 = iMA(_Symbol,_Period,PeriodEMA2,0,MODE_EMA, PRICE_OPEN,0);
 double MyMovingAverage1H1 = iMA(_Symbol,PERIOD_H1,PeriodEMA1,0,MODE_EMA, PRICE_CLOSE,0);
 double MyMovingAverage2H1 = iMA(_Symbol,PERIOD_H1,PeriodEMA2,0,MODE_EMA, PRICE_CLOSE,0);
 double MyMovingAverage1H4 = iMA(_Symbol,PERIOD_H4,PeriodEMA1,0,MODE_EMA, PRICE_CLOSE,0);
 double MyMovingAverage2H4 = iMA(_Symbol,PERIOD_H4,PeriodEMA2,0,MODE_EMA, PRICE_CLOSE,0);
 int ticket;
 //Buy position with H4 trends
 if(MyMovingAverage1<Bid && MyMovingAverage1 > MyMovingAverage2 && MyMovingAverage1H4<Bid && MyMovingAverage1H4 > MyMovingAverage2H4 && CountBuyPosition() <1){
        ticket = OrderSend(Symbol(), OP_BUY, lots,Ask,5, 0, 0, "Raymond's 1.1 bot Buy H4 trend",Magic_Number,0,Blue);
        if(ticket > 0){
          if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
            Print("BUY Order Opened: ", OrderOpenPrice(), " SL:", 0, " TP: ", 0);
          }
          else
            Print("Error Opening BUY  Order: ", GetLastError());
            return(0);
        }
    // Sell - Short position with H4 trends
    if(MyMovingAverage1>Ask && MyMovingAverage1 < MyMovingAverage2 && MyMovingAverage1H4 < MyMovingAverage2H4 && CountSellPosition() <1){
      ticket = OrderSend(Symbol(), OP_SELL, lots,Bid,5, 0, 0, "Raymond's 1.1 bot Sell H4 trend",Magic_Number,0,Red);
      if(ticket > 0){
        if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
          Print("SELL Order Opened: ", OrderOpenPrice(), " SL:", 0, " TP: ", 0);
        }
        else
          Print("Error Opening SELL Order: ", GetLastError());
          return(0);
      }
 
 //Buy position with H1 trends
 if(MyMovingAverage1<Bid && MyMovingAverage1 > MyMovingAverage2 && MyMovingAverage1H1 > MyMovingAverage2H1 && CountBuyPosition() <1){
        ticket = OrderSend(Symbol(), OP_BUY, lots,Ask,5, 0, 0, "Raymond's 1.1 bot Buy H1 trend",Magic_Number,0,Blue);
        if(ticket > 0){
          if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
            Print("BUY Order Opened: ", OrderOpenPrice(), " SL:", 0, " TP: ", 0);
          }
          else
            Print("Error Opening BUY  Order: ", GetLastError());
            return(0);
        }
    // Sell - Short position with H1 trends
    if(MyMovingAverage1>Ask && MyMovingAverage1 < MyMovingAverage2 && MyMovingAverage1H1 < MyMovingAverage2H1 && CountSellPosition() <1){
      ticket = OrderSend(Symbol(), OP_SELL, lots,Bid,5, 0, 0, "Raymond's 1.1 bot Sell H1 trend",Magic_Number,0,Red);
      if(ticket > 0){
        if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
          Print("SELL Order Opened: ", OrderOpenPrice(), " SL:", 0, " TP: ", 0);
        }
        else
          Print("Error Opening SELL Order: ", GetLastError());
          return(0);
      }
return(0);
}

Already fix it. It still not open a trade

 
Bui Thanh Phat: Already fix it. It still not open a trade

We are "blind"! We cannot see your screen or know what you are doing!

Show a screenshot and describe to us your environment!

What symbol are you putting it on? Are you putting it on a chart or are you running it in the Strategy Tester?

You have several "Prints" in your code. Show us the output of those prints! Show us what is in the log!

If you don't help us understand what you are doing, we will not be able to help you.

 
Fernando Carreiro:

We are "blind"! We cannot see your screen or know what you are doing!

Show a screenshot and describe to us your environment!

What symbol are you putting it on? Are you putting it on a chart or are you running it in the Strategy Tester?

You have several "Prints" in your code. Show us the output of those prints! Show us what is in the log!

If you don't help us understand what you are doing, we will not be able to help you.

Here is my tull source code. It has no error in log... i dont know how to fix...

//+------------------------------------------------------------------+
//|                                                   EMA Signal.mq4 |
//|                                                      Raymond Bui |
//|                   https://www.facebook.com/raymond.buithanhphat/ |
//+------------------------------------------------------------------+
#property copyright "Raymond Bui"
#property link      "https://www.facebook.com/raymond.buithanhphat/"
#property version   "1.00"
#property strict
#define NL                 "\n" 
input int Magic_Number =   8071994; //Input the magic number
input int PeriodEMA1        = 19; //Input the Period 1:
input int PeriodEMA2       = 39; //Input the Period 2:
input double lots             = 0.01; //Input start lots:
int OnInit(){

   return(INIT_SUCCEEDED);
}
int CountSellPosition(){

   int NumberOfSellPositions= 0;
   for(int i=OrdersTotal()-1;i >=0; i--){
   // select an open trade
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      // get the currency pair
      string CurrencyPair=OrderSymbol();
      if(_Symbol== CurrencyPair)
         {if(OrderType()==OP_SELL){
      NumberOfSellPositions=NumberOfSellPositions+1;
   }}
  }
  return NumberOfSellPositions;
}
int CountBuyPosition(){

   int NumberOfBuyPositions= 0;
   for(int i=OrdersTotal()-1;i >=0; i--)
   {
   // select an open trade
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   // get the currency pair
   string CurrencyPair=OrderSymbol();
   if(_Symbol== CurrencyPair)
   if(OrderType()==OP_BUY)
   {
   NumberOfBuyPositions=NumberOfBuyPositions+1;
   }
  }
  return NumberOfBuyPositions;
}
void OnTick()
  {
  //We create a string variable for the signal
  string signal ="";
  string signalM15 ="";
  string signalH1 ="";
  string signalH4 ="";
  string signalDaily ="";
  
  //We calculate a simple moving average for PeriodEMA candles:
  double MyMovingAverage1 = iMA(_Symbol,_Period,PeriodEMA1,0,MODE_EMA, PRICE_CLOSE,0);
  double MyMovingAverage2 = iMA(_Symbol,_Period,PeriodEMA2,0,MODE_EMA, PRICE_CLOSE,0);
  double MyMovingAverage1M15 = iMA(_Symbol,PERIOD_M15,PeriodEMA1,0,MODE_EMA, PRICE_CLOSE,0);
  double MyMovingAverage2M15 = iMA(_Symbol,PERIOD_M15,PeriodEMA2,0,MODE_EMA, PRICE_CLOSE,0);
  double MyMovingAverage1H1 = iMA(_Symbol,PERIOD_H1,PeriodEMA1,0,MODE_EMA, PRICE_CLOSE,0);
  double MyMovingAverage2H1 = iMA(_Symbol,PERIOD_H1,PeriodEMA2,0,MODE_EMA, PRICE_CLOSE,0);
  double MyMovingAverage1H4 = iMA(_Symbol,PERIOD_H4,PeriodEMA1,0,MODE_EMA, PRICE_CLOSE,0);
  double MyMovingAverage2H4 = iMA(_Symbol,PERIOD_H4,PeriodEMA2,0,MODE_EMA, PRICE_CLOSE,0);
  double MyMovingAverage1Daily = iMA(_Symbol,PERIOD_D1,PeriodEMA1,0,MODE_EMA, PRICE_CLOSE,0);
  double MyMovingAverage2Daily = iMA(_Symbol,PERIOD_D1,PeriodEMA2,0,MODE_EMA, PRICE_CLOSE,0);
  
  //If the EMA is below the bid price
  if(MyMovingAverage1<Bid && MyMovingAverage1 > MyMovingAverage2){
      //set the signal to buy
      signal = "buy";
  }
  //If the EMA is above the Ask price:
  if(MyMovingAverage1>Ask && MyMovingAverage1 < MyMovingAverage2){
      //set the signal to sell
      signal = "sell";
  }
  //M15 TF
  if(MyMovingAverage1M15<Bid && MyMovingAverage1M15 > MyMovingAverage2M15){
      //set the signal to buy
      signalM15 = "buy";
  }
  //If the EMA is above the Ask price:M15
  if(MyMovingAverage1M15>Ask && MyMovingAverage1M15 < MyMovingAverage2M15){
      //set the signal to sell
      signalM15 = "sell";
  }
  //H1 TF
  if(MyMovingAverage1H1<Bid && MyMovingAverage1H1 > MyMovingAverage2H1){
      //set the signal to buy
      signalH1 = "buy";
  }
  //If the EMA is above the Ask price:
  if(MyMovingAverage1H1>Ask && MyMovingAverage1H1 < MyMovingAverage2H1){
      //set the signal to sell
      signalH1 = "sell";
  }
  //H4 TF
  if(MyMovingAverage1H4<Bid && MyMovingAverage1H4 > MyMovingAverage2H4){
      //set the signal to buy
      signalH4 = "buy";
  }
  //If the EMA is above the Ask price:
  if(MyMovingAverage1H4>Ask && MyMovingAverage1H4 < MyMovingAverage2H4){
      //set the signal to sell
      signalH4 = "sell";
  }
  
  //Daily TF
  if(MyMovingAverage1Daily<Bid && MyMovingAverage1Daily > MyMovingAverage2Daily){
      //set the signal to buy
      signalDaily = "buy";
  }
  //If the EMA is above the Ask price:
  if(MyMovingAverage1Daily>Ask && MyMovingAverage1Daily < MyMovingAverage2Daily){
      //set the signal to sell
      signalDaily = "sell";
  }
  //Output the signal on the chart
  Comment ("The current TimeFrame signal is: ",signal, NL, NL,
  "The M15 signal is: ",signalM15, NL, NL,
  "The H1 signal is: ",signalH1, NL, NL,
  "The H4 TimeFrame signal is: ",signalH4, NL, NL,
  "The Daily TimeFrame signal is: ",signalDaily, NL, NL,
  "Number of Buy Positions are: ",CountBuyPosition(), NL, NL,
  "Number of Sell Positions are: ",CountSellPosition(), NL, NL,
  "Current Unrealized Profit/Loss on Order: ", OrderProfit(),NL, NL,
  "Test result: ", MyMovingAverage1<Bid && MyMovingAverage1 > MyMovingAverage2 && MyMovingAverage1H4<Bid && MyMovingAverage1H4 > MyMovingAverage2H4 && CountBuyPosition() <1);
  }
int start(){
 double MyMovingAverage1 = iMA(_Symbol,_Period,PeriodEMA1,0,MODE_EMA, PRICE_CLOSE,0);
 double MyMovingAverage2 = iMA(_Symbol,_Period,PeriodEMA2,0,MODE_EMA, PRICE_CLOSE,0);
 double MyMovingAverage1H1 = iMA(_Symbol,PERIOD_H1,PeriodEMA1,0,MODE_EMA, PRICE_CLOSE,0);
 double MyMovingAverage2H1 = iMA(_Symbol,PERIOD_H1,PeriodEMA2,0,MODE_EMA, PRICE_CLOSE,0);
 double MyMovingAverage1H4 = iMA(_Symbol,PERIOD_H4,PeriodEMA1,0,MODE_EMA, PRICE_CLOSE,0);
 double MyMovingAverage2H4 = iMA(_Symbol,PERIOD_H4,PeriodEMA2,0,MODE_EMA, PRICE_CLOSE,0);
 int ticket;
 int countbuy = CountBuyPosition();
 int countsell = CountSellPosition();
 
 //Buy position with H4 trends
 if(MyMovingAverage1<Bid && MyMovingAverage1 > MyMovingAverage2 && MyMovingAverage1H4<Bid && MyMovingAverage1H4 > MyMovingAverage2H4 && countbuy <1)
 {
        ticket = OrderSend(Symbol(), OP_BUY, lots,Ask,5, 0, 0, "Raymond's 1.1 bot Buy H4 trend",Magic_Number,0,Blue);
        if(ticket > 0){
          if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
            Print("BUY Order Opened: ", OrderOpenPrice(), " SL:", 0, " TP: ", 0);
          }
          else
            Print("Error Opening BUY  Order: ", GetLastError());
            return(0);
        }
    // Sell - Short position with H4 trends
    if(MyMovingAverage1>Ask && MyMovingAverage1 < MyMovingAverage2 && MyMovingAverage1H4>Ask && MyMovingAverage1H4 < MyMovingAverage2H4 && countsell <1){
      ticket = OrderSend(Symbol(), OP_SELL, lots,Bid,5, 0, 0, "Raymond's 1.1 bot Sell H4 trend",Magic_Number,0,Red);
      if(ticket > 0){
        if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
          Print("SELL Order Opened: ", OrderOpenPrice(), " SL:", 0, " TP: ", 0);
        }
        else
          Print("Error Opening SELL Order: ", GetLastError());
          return(0);
      }
 
 //Buy position with H1 trends
 if(MyMovingAverage1<Bid && MyMovingAverage1 > MyMovingAverage2 && MyMovingAverage1H1<Bid && MyMovingAverage1H1 > MyMovingAverage2H1 && CountBuyPosition() <1){
        ticket = OrderSend(Symbol(), OP_BUY, lots,Ask,5, 0, 0, "Raymond's 1.1 bot Buy H1 trend",Magic_Number,0,Blue);
        if(ticket > 0){
          if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
            Print("BUY Order Opened: ", OrderOpenPrice(), " SL:", 0, " TP: ", 0);
          }
          else
            Print("Error Opening BUY  Order: ", GetLastError());
            return(0);
        }
    // Sell - Short position with H1 trends
    if(MyMovingAverage1>Ask && MyMovingAverage1 < MyMovingAverage2 && MyMovingAverage1H1>Ask && MyMovingAverage1H1 < MyMovingAverage2H1 && CountSellPosition() <1){
      ticket = OrderSend(Symbol(), OP_SELL, lots,Bid,5, 0, 0, "Raymond's 1.1 bot Sell H1 trend",Magic_Number,0,Red);
      if(ticket > 0){
        if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
          Print("SELL Order Opened: ", OrderOpenPrice(), " SL:", 0, " TP: ", 0);
        }
        else
          Print("Error Opening SELL Order: ", GetLastError());
          return(0);
      }
return(0);
}
//+------------------------------------------------------------------+
 
Fernando Carreiro:

We are "blind"! We cannot see your screen or know what you are doing!

Show a screenshot and describe to us your environment!

What symbol are you putting it on? Are you putting it on a chart or are you running it in the Strategy Tester?

You have several "Prints" in your code. Show us the output of those prints! Show us what is in the log!

If you don't help us understand what you are doing, we will not be able to help you.

the issue is the expert advisor don't throw out any error. And it not put any order although all the condition is fullfill

 

You have OnTick() and start() functions.

Expert entry point is OnTick() functioni.

I don't see where you call start()

Besides that, "start" is a reserved word in mql so you should probably change the name in order not to confuse the compiler.

 
Start() is the old event handler. OnTick() is the new event handler. You can't have both.
 
William Roeder:
Start() is the old event handler. OnTick() is the new event handler. You can't have both.
Thank you william.