Please help with this simple EA

 

Hi all, I am really new to mql4, coming from NT world.

I downloaded a custom indicator which plots 2 lines (white and blue).

All I want my EA to do is to go long when white crosses blue and go short when blue crosses white. However, I want it to be done only if cross happens after the one-day candle is formed (if cross happens while candle is forming it does not count, cross must be there on the chart).

I wrote this simple EA, but it gives me totally random signals, not even close to indicator line crosses. I tried to figure out all day long today, but no luck. Hope you can help.

Below are the codes for indicator and for EA. They are also attached:

INDICATOR:

//+------------------------------------------------------------------+
//|                                                 trendxplorer.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Peeter Paan"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   3
//--- plot first
#property indicator_label1  "first"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrWhite
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot second
#property indicator_label2  "second"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

//--- indicator buffers
double         firstBuffer[];
double         secondBuffer[];
input int closePeriod   =5;   //Close period
input int lowPeriod     =13;  //Low period
input int highPeriod    =34;  //High period
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,firstBuffer);
   SetIndexBuffer(1,secondBuffer);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   for(int i=0; i<Bars; i++)
     {

      double emac=iMA(NULL,0,closePeriod,0,1,0,i);  //Takes EMA with Close Price
      double emal=iMA(NULL,0,lowPeriod,0,1,3,i);  //Takes EMA with Low Price
      double emah=iMA(NULL,0,highPeriod,0,1,2,i);  //Takes EMA with High Price

      firstBuffer[i]=emac-emah;
      secondBuffer[i]=emal-emac;

     }
   return(rates_total);
  }
//+------------------------------------------------------------------+

EA:

double uplinenow;
double uplinebefore;
double downlinenow;
double downlinebefore;
input int closePeriod   =5;   //Close period
input int lowPeriod     =13;  //Low period
input int highPeriod    =34;  //High period

void OnTick()
{
      //Getting values from indicator
      uplinenow = iCustom(NULL,PERIOD_D1,"HLCTrend",closePeriod,lowPeriod,highPeriod,0,0); 
      uplinebefore = iCustom(NULL,PERIOD_D1,"HLCTrend",closePeriod,lowPeriod,highPeriod,0,1);
   
      downlinenow = iCustom(NULL,PERIOD_D1,"HLCTrend",closePeriod,lowPeriod,highPeriod,1,0);
      downlinebefore = iCustom(NULL,PERIOD_D1,"HLCTrend",closePeriod,lowPeriod,highPeriod,1,1);
   
   
      string signal= "";
      
      //checking for cross up
      if (uplinenow > downlinenow)
      if (uplinebefore < downlinebefore)
   
      {
         signal="buy";
      }
       //checking for cross down
      if (uplinenow < downlinenow)
      if (uplinebefore > downlinebefore)
   
      {
         signal="sell";
      }
   
      //Entering the trades based on up or down condition
      if (signal=="buy")
      {
         OrderSend (_Symbol,OP_BUY,1,Close[1],3,0,0,NULL,0,0,Green);
      }
   
      if (signal=="sell")
      {
         OrderSend (_Symbol,OP_SELL,1,Close[1],3,0,0,NULL,0,0,Red);
      }

}
Files:
 
  1. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Stop looking at bar zero. (multiple crosses.)

  3. Look at the start of a new bar.

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum

  4. Why are you looking at the D1 instead of the current TF.

Reason: