Can you access objects manually added to the chart using EA?

Hey all,

Been looking at using a MA or support resistance indicator to using in my EA but none are as good as me drawing in key support resistance lines.

So, is it possible to get an EA to recognize horizontal lines you have manually drawn onto the chart, and what would they be named as standard, and would they all be named the same or would MT4 name them HLine1, HLine2, HLinen for example?

Thanks for any help!!

  1. Add lines to chart

  2. Right-click

  3. Object List

  4. You will see the lines you drew with the names given to them.

Can an EA detect these? Probably. I haven’t tried it myself, but this thread looks applicable.

Thanks!!! …

It can be done as with any object - as long as you know the exact name of the object you are looking for

you can filter any object by name, type, color, date of creation, you name it, this is powerful !

Here’s an example of how you could add HLines dropped on the chart as S/R in your EA

//+------------------------------------------------------------------+
//|                                           CustomSR-DragNDrop.mq4 |
//|                                      Copyright 2017, nicholishen |
//|                         https://www.forexfactory.com/nicholishen |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, nicholishen"
#property link "https://www.forexfactory.com/nicholishen"
#property version "1.00"
#property strict

#include <ChartObjects\ChartObjectsLines.mqh>
#include <Arrays\ArrayObj.mqh>
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CSR : public CChartObjectHLine
{
 public:
   bool Attach(string name) { return Attach(ChartID(), name, 0, 1); }
};
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CSRCollection : public CArrayObj
{
 public:
   CSR *operator[](int i) { return At(i); }
   bool Add(string name)
   {
      if (MessageBox("Would you like to add " + name + " as S/R?", NULL, MB_YESNO) == IDYES)
      {
         CSR *line = new CSR;
         if(!line.Attach(name))
	    return false;
         line.Color(clrDarkOrange);
         line.Style(STYLE_DOT);
         line.Description("S/R");
         return CArrayObj::Add(line);
      }
      return false;
   }
   bool Delete(string name)
   {
      for (int i = Total() - 1; i >= 0; i--)
         if (this[i].Name() == name)
            return Delete(i);
      return false;
   }
   void ToComment()
   {
      string res = "";
      for (int i = 0; i < Total(); i++)
         res += (string)this[i].CreateTime() + " -- " + DoubleToString(this[i].Price(0), _Digits) + "\r\n";
      Comment(res);
   }
};

CSRCollection sr_collection;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
{
   ChartSetInteger(0, CHART_EVENT_OBJECT_CREATE, true);
   ChartSetInteger(0, CHART_EVENT_OBJECT_DELETE, true);
   ChartSetInteger(0, CHART_SHOW_OBJECT_DESCR, true);
   return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
{
   sr_collection.ToComment();
}
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
{
}
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   if (id == CHARTEVENT_OBJECT_CREATE && ObjectGetInteger(0, sparam, OBJPROP_TYPE) == OBJ_HLINE)
   {
      if (sr_collection.Add(sparam))
         sr_collection.ToComment();
   }
   else if (id == CHARTEVENT_OBJECT_DELETE)
   {
      if (sr_collection.Delete(sparam))
         sr_collection.ToComment();
   }
}
//+------------------------------------------------------------------+