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);
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. |
|
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.) |

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.