How build a semaphore based on EMA crossover?

 

Hi MQL5 Experts,

I am a newcomer to MQL programming, read also several tutorial articles about custom indicators, alert functions and indicator integrations. But I am now stuck in my way of customizing the DRAW_Indicator (https://www.mql5.com/en/docs/customind/indicators_examples/draw_color_arrow) example.

What is my goal with this customized indicator? I would like to have a signal triggered (semaphore calls the Alert() Function and draws arrow_up in red on the chart, if EMA(Period 48) crosses EMA(Period 28). Plain and simple. Here is the code. Important segments are highlighted in green and yellow. My problem is now: I have initalized the two EMA's via iMA, but how can I check during OnCalculate(), if after the last close price the EMAs have been crossed or not? How do I do this correctly? 

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots   1
//--- plot ColorArrow
#property indicator_label1  "ColorArrow"
#property indicator_type1   DRAW_COLOR_ARROW
//--- Define 8 colors for coloring the histogram (they are stored in the special array)
#property indicator_color1  clrRed //,clrBlue,clrSeaGreen,clrGold,clrDarkOrange,clrMagenta,clrYellowGreen,clrChocolate
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
  
//--- An indicator buffer for the plot
double         ColorArrowBuffer[];
//--- A buffer to store color indexes
double         ColorArrowColors[];
// Declaration of 2 EMAs
double MA48[];
int MA48_handle;

double MA28[];
int MA28_handle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ColorArrowBuffer,INDICATOR_DATA);
//--- Define the symbol code for drawing in PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,233);
//--- Set the vertical shift of arrows in pixels
   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,20);
   PlotIndexSetInteger(0,PLOT_LINE_COLOR, clrRed);
   PlotIndexSetInteger(0,PLOT_LINE_WIDTH, 5);
//--- Set as an empty value 0
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);    
  
//---
   MA48_handle = iMA(NULL,0,48,0,MODE_EMA,PRICE_CLOSE);
   MA28_handle = iMA(NULL,0,28,0,MODE_EMA,PRICE_CLOSE);
  
   CopyBuffer(MA48_handle,0,0,100,MA48);
   ArraySetAsSeries(MA48,true);
   CopyBuffer(MA28_handle,0,0,100,MA28);
   ArraySetAsSeries(MA28,true);
  
  
   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[])
  {
  //--- Block for calculating indicator values
   int start=1;
   if(prev_calculated>0) start=prev_calculated-1;
//--- Calculation loop
   for(int i=1;i<rates_total;i++)
     {
      //--- If the current Close price is higher than the previous one, draw an arrow
      //if(close[i]>=close[i-1]){
      

//Call Signal if MA28 crosses MA48
   if(MA28[0]>MA48[0] && MA28[1]==MA48[1]){
      Alert("CALL Signal at: "TimeTradeServer(), MA28[0], " ", MA48[0]);

   }  

//draw an arrow 

         ColorArrowBuffer[i]=close[i]; 

      }

//--- Otherwise specify the null value

      else
         ColorArrowBuffer[i]=0;
      //--- Arrow color
     }
  
     
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }

My second questions is a problem with the color of the ARROW_Indicators. I have set the color to red, but after each 2nd compilation the indicators behaves strange: it only shows red arrows which is correct, but after the next compilation it only shows the arrows in red and black alternating... Why?


Alternating ARROW_Color

 

After next compilation... 


 

Any help is well appreciated! 


Cheers,

Burny 

Documentation on MQL5: Custom Indicators / Indicator Styles in Examples / DRAW_COLOR_ARROW
Documentation on MQL5: Custom Indicators / Indicator Styles in Examples / DRAW_COLOR_ARROW
  • www.mql5.com
Custom Indicators / Indicator Styles in Examples / DRAW_COLOR_ARROW - Reference on algorithmic/automated trading language for MetaTrader 5