Create an Object on Chart

Zarik
20
Zarik  
//+------------------------------------------------------------------+
//| ObjectOnChart                                                    |
//+------------------------------------------------------------------+
datetime    prev_bar  =  0;
int         handle_ma_50, handle_ma_200;
int OnInit()
  {
//---
   prev_bar          =     TimeCurrent();
//---
   handle_ma_50      =     iMA(_Symbol,_Period,50,0,MODE_EMA,PRICE_HIGH);
   handle_ma_200     =     iMA(_Symbol,_Period,200,0,MODE_EMA,PRICE_CLOSE);
//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(handle_ma_50);
   IndicatorRelease(handle_ma_200);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   datetime current_bar =  iTime(_Symbol,_Period,0);
   if(current_bar!=prev_bar)
     {
      prev_bar  =  current_bar;

      double   array_ma_50[]   =  {};
      ArraySetAsSeries(array_ma_50,true);
      CopyBuffer(handle_ma_50,0,0,10,array_ma_50);

      double   array_ma_200[]  =  {};
      ArraySetAsSeries(array_ma_200,true);
      CopyBuffer(handle_ma_200,0,0,10,array_ma_200);

      if(array_ma_50 [1] > array_ma_200[1])
        {
         // PRINT OBJECT ON CHART;}
        }
     }
  }
//+------------------------------------------------------------------+

Hello everyone! 

I am totally new to this community. Please forgive me in case I am missing any rules (which I have tried so hard not to).

I am trying to create an object (arrow, thumbs, etc) in case of an event occurrence over the chart. For instance, if two EMA 50 and 200 crosses each other, it will create and object over the chart (I have attached a screen shot of an example).
I really appreciate it if someone helps me with completing my code or direct me to a post where I can learn it. (except the documentation).

Thank You

Files:
Ernst Van Der Merwe
7633
Ernst Van Der Merwe  
#include <ChartObjects\ChartObjectsArrows.mqh>
#include <Arrays\ArrayObj.mqh>
CArrayObj thumbs;
//+------------------------------------------------------------------+
//| ObjectOnChart                                                    |
//+------------------------------------------------------------------+
datetime    prev_bar  =  0;
int         handle_ma_50, handle_ma_200;
int OnInit()
  {
//---
   prev_bar          =     TimeCurrent();
//---
   handle_ma_50      =     iMA(_Symbol,_Period,50,0,MODE_EMA,PRICE_HIGH);
   handle_ma_200     =     iMA(_Symbol,_Period,200,0,MODE_EMA,PRICE_CLOSE);
//---
   if(MQLInfoInteger(MQL_TESTER))
      thumbs.FreeMode(false);     
//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(handle_ma_50);
   IndicatorRelease(handle_ma_200);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   datetime current_bar =  iTime(_Symbol,_Period,0);
   if(current_bar!=prev_bar)
     {
      prev_bar  =  current_bar;

      double   array_ma_50[];
      ArraySetAsSeries(array_ma_50,true);
      if(CopyBuffer(handle_ma_50,0,1,2,array_ma_50)!=2)
         return;

      double   array_ma_200[];
      ArraySetAsSeries(array_ma_200,true);
      if(CopyBuffer(handle_ma_200,0,1,2,array_ma_200)!=2)
         return;

      if((array_ma_50 [0] > array_ma_200[0]) &&
         (array_ma_50 [1] < array_ma_200[1]))
        {
         ThumbUp(iTime(_Symbol,_Period,0),iLow(_Symbol,_Period,0)-100*_Point);
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool ThumbUp(const datetime time,const double price)
  {
   CChartObjectArrowThumbUp *up;
   if(!CheckPointer(up=new CChartObjectArrowThumbUp) ||
      !up.Create(ChartID(),"up "+TimeToString(time),0,time,price))
     {
      Print("error creating arrow: ",(string)GetLastError());
      return(false);
     }
   up.Color(clrGreen);
   up.SetInteger(OBJPROP_WIDTH,5);
   up.SetInteger(OBJPROP_HIDDEN,false);
   thumbs.Add(up);
   ChartRedraw();
   return(true);
  }
//+------------------------------------------------------------------+
Zarik
20
Zarik  

Thank you so much Ernst! Works perfectly as I was imagining! 
Cheers!