Create HLines every set amount

 

Hi there,

I'm trying to create HLines for every specified amount, for example every .0025 pips there should be another HLine.

I've tried to do this on EURUSD with a script on and a custom indicator but no luck.

This is the script version

string objectName = "HL at: ";
long chartID = ChartID();
double increment = .0025;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   for(double i=0;i<=.2010;i+= NormalizeDouble(increment,4)){
      objectName +=  DoubleToStr(i);
      Print("i: ", DoubleToStr(i,4));
      if(!ObjectCreate(objectName,OBJ_HLINE,0,0,i)){
         Print("Error: can't create HLINE! code #",GetLastError());
      }// end if
      ObjectSet(objectName,OBJPROP_COLOR,clrRed);
   }// end for
   //ObjectSetInteger(chartID,objectName,OBJPROP_COLOR,clrRed);
   ChartRedraw(0);
   //Sleep(60);
  }
//+------------------------------------------------------------------+

It prints the right numbers and the error code 4207 i.e. Some error in object operation.


And this is the Custom Indicator version.

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red

double levels[];
int count;
double boxSize = .0025;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,levels);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
//---
   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(double l = 0; l < 2.001; l += boxSize)
      {
      levels[count] = l;
      count++;
      }// end for
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


Thanks.

 

Script has problem with object names. I presume that names have some limit because your code appends value at the end of object name and it gets longer with each iteration: 

objectName +=  DoubleToStr(i);

 

It will work If you replace that line with:  

objectName = "HL at: " + DoubleToStr(i);
 
brad:

Hi there,

I'm trying to create HLines for every specified amount, for example every .0025 pips there should be another HLine.

I've tried to do this on EURUSD with a script on and a custom indicator but no luck.

This is the script version

It prints the right numbers and the error code 4207 i.e. Some error in object operation.


And this is the Custom Indicator version.


Thanks.


  may be so?


//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
#property show_confirm
string objectName="HL at: ";
double min=1.3100;        // Mininimum price of the HLine.
double increment=.0025;   // Increment, or step.
int quantity=40;          // Quantity of HLines.
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   long chartID=ChartID();
   double digits=Digits;
   double y=-increment;
   double z;
   bool x;
   int i,ret;
   string nemo[],noname;
   ArrayResize(nemo,quantity,quantity);

   for(i=0;i<quantity;i++)
     {
      y+=increment;
      z=NormalizeDouble(min+y,digits);
      noname=objectName+DoubleToStr(z,digits);
      nemo[i]=noname;
      if(ObjectFind(noname)==-1)
        {
         Sleep(18);
         x=ObjectCreate(noname,OBJ_HLINE,0,0,z);
         if(x==false)
           {
            Print("Can't create "+noname+"! Error description: "+ErrorDescription(GetLastError())+"");
           }
         ObjectSet(noname,OBJPROP_COLOR,clrRed);
        }
      ObjectMove(noname,0,0,z);
      ChartRedraw(chartID);
     }

//----Delete all HLines

   ret=MessageBox("Delete all HLines?","Question",MB_YESNO|MB_ICONQUESTION|MB_TOPMOST);
   if(ret==IDNO)return;

   for(i=quantity-1;i>=0;i--)
     {
      Sleep(18);
      ObjectDelete(nemo[i]);
      ChartRedraw(chartID);
     }
  }
//+------------------------------------------------------------------+

 
brad: error code 4207 i.e. Some error in object operation.
  1. string objectName = "HL at: ";
          objectName +=  DoubleToStr(i);  objname = "HL at: 0"
                                          objname = "HL at: 00.0025"
                                          objname = "HL at: 00.00250.0050"
                                          objname = "HL at: 00.00250.00500.0075"
                                          :
    Like drazen64 said
  2. 4207

    ERR_SOME_OBJECT_ERROR

    Graphical object error

    because your object names are IIIIINNNNFFFFFIIIIIITTTTTTLLLLLLYYYYYYYLLLLLLLLLLLLLOOOOOOOONNNNNNGGGGGG
  3. void OnStart(){
       for(double i=0;i<=.2010;i+= NormalizeDouble(increment,4)){
          objectName+=  DoubleToStr(i);
          if(!ObjectCreate(objectName,OBJ_HLINE,0,0,i)){
    
    After the first tick you will be trying to create the same objects again and will be getting

    4200

    ERR_OBJECT_ALREADY_EXISTS

    Object already exists

  4. Your indicator version won't work as you need 0.2010/0.0025=80 buffers
 

Thanks for your responses.

I have updated my code as below and still receive the 4200 error "Object already exists".  I thought this wouldn't occur as the code runs only once and each object name is stored until script end in the array.

long chartID = ChartID();
double increment = .0025;
bool actioned;
double levels[];
string objectNames[];

int arraySize;
int arrayCount;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
      while (!actioned)
      {
         // calculate the number of levels
         for(double i=0;i<=.2010;i+= NormalizeDouble(increment,4))
         {
            arraySize++;
         }// end for
         
         ArrayResize(levels,arraySize);
         ArrayResize(objectNames,arraySize);
         
         // populate arrays for the levels and their object names
         for(double level=0;level<=.2010;level+= NormalizeDouble(increment,4))
         {
            levels[arrayCount] = level;
            objectNames[arrayCount] = "HL at: " + DoubleToStr(level);
            arrayCount++;
         }
         
         // create the HLine objects
         for(int i = 0; i < arraySize; i++)
         {
            if(!ObjectCreate(objectNames[i],OBJ_HLINE,0,0,levels[i])){
               Print("Error: can't create HLINE! code #",GetLastError());
            }// end if
            
            ObjectSet(objectNames[i],OBJPROP_COLOR,clrRed);
         }// end for
         ChartRedraw(0);
         
         actioned = true;
      }//end while
  }

How can I get the HLines to display?

Thanks.

 

You can't have two objects with the same name on one chart. First time you run this code it will probably work, but next time you will already have lines with same names and script will halt when it encounters first error.

Either delete all objects with ObjectDelete()/ObjectsDeleteAll()  or search for object with ObjectFind() and create it if it does not exists.

Look at example code for ObjectDelete()  and ObjectsDeleteAll()

 
brad: I thought this wouldn't occur as the code runs only once
bool actioned;
Maybe you should set it to false initially, instead of a random value. (Any non-zero value is true and your code never runs.)