How to make the Indicator write in the DESCRIPTION portion of the horizontal line it drew

 

Hi guys,


I have this code called Sweetspot gold that draws lines at every specific interval like 500 pips etc


It writes a Name at every horizontal line it draws


string linename= "[SweetSpot] " + text + " Line",


[SweetSpot] 1.6500 Line


Can this be written in the DESCRIPTION too?



//+------------------------------------------------------------------+
//| SweetSpots.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright Shimodax"
#property link "http://www.strategybuilderfx.com"

#property indicator_chart_window

/* Introduction:

This indicator shows lines at sweet spots (50 and 100
pips levels). It is recommended to turn off the grid.

Enjoy!

Markus
*/

extern int NumLinesAboveBelow= 50;
extern int SweetSpotMainLevels= 500;
extern color LineColorMain= Gold;
extern int LineStyleMain= STYLE_DASHDOT;
extern bool ShowSubLevels= true;
extern int sublevels= 100;
extern color LineColorSub= MidnightBlue;
extern int LineStyleSub= STYLE_DASHDOT;



//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}

int deinit()
{
int obj_total= ObjectsTotal();

for (int i= obj_total; i>=0; i--) {
string name= ObjectName(i);

if (StringSubstr(name,0,11)=="[SweetSpot]")
ObjectDelete(name);
}

return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
static datetime timelastupdate= 0;
static datetime lasttimeframe= 0;


// no need to update these buggers too often
if (CurTime()-timelastupdate < 600 && Period()==lasttimeframe)
return (0);

deinit(); // delete all previous lines

int i, ssp1, style, ssp, thickness; //sublevels= 50;
double ds1;
color linecolor;

if (!ShowSubLevels)
sublevels*= 2;

ssp1= Bid / Point;
ssp1= ssp1 - ssp1%sublevels;

for (i= -NumLinesAboveBelow; i<NumLinesAboveBelow; i++) {

ssp= ssp1+(i*sublevels);

if (ssp%SweetSpotMainLevels==0) {
style= LineStyleMain;
linecolor= LineColorMain;
}
else {
style= LineStyleSub;
linecolor= LineColorSub;
}

thickness= 1;

if (ssp%(SweetSpotMainLevels*10)==0) {
thickness= 2;
}

if (ssp%(SweetSpotMainLevels*100)==0) {
thickness= 3;
}

ds1= ssp*Point;
SetLevel(DoubleToStr(ds1,Digits), ds1, linecolor, style, thickness, Time[10]);
}

return(0);
}


//+------------------------------------------------------------------+
//| Helper |
//+------------------------------------------------------------------+
void SetLevel(string text, double level, color col1, int linestyle, int thickness, datetime startofday)
{
int digits= Digits;
string linename= "[SweetSpot] " + text + " Line",
pricelabel;

// create or move the horizontal line
if (ObjectFind(linename) != 0) {
ObjectCreate(linename, OBJ_HLINE, 0, 0, level);
ObjectSet(linename, OBJPROP_STYLE, linestyle);
ObjectSet(linename, OBJPROP_COLOR, col1);
ObjectSet(linename, OBJPROP_WIDTH, thickness);

ObjectSet(linename, OBJPROP_BACK, True);
}
else {
ObjectMove(linename, 0, Time[0], level);
}
}

 

Hi

You can use the function ObjectSetText(). Refer to:

https://docs.mql4.com/objects/ObjectSetText

Cheers

Jellybean

 

I tried adding in this indicator Objectsettext but it does not work. Where should I place it?


//+------------------------------------------------------------------+

//| PPZ.mq4
//| By fxsheep[forex factory]
//|
//| Description:
//| - Draws number of ppz lines specified by user. (num_ppz_show)
//| - Draws ppz with highest number of confulence first.
//| confulence is calcuated by how close to high/low of a bar (tolerance)
//| - PPz levels are at certain pips away from each other (ppz_spacing)
//| - programs runs at startup or at first 2 minute of time frame
//| work best at 4H & daily charts
//|
//| Mar 18, 2009 fxsheep - first cut
//|
//|
//+------------------------------------------------------------------+
#property indicator_chart_window

//---- input parameters
extern int BarCount = 50, // Number of bars to look back on the chart
Tolerance = 3, // Tolerance in pips counting confluence
ppz_spacing = 15, // spacing requirement between ppz levels in pips
num_ppz_show = 10; // number of ppz lines to show


extern color Color = DeepSkyBlue;

//-- internal global variables
int con[]; //confluence at each price point
int price_range = 0; // range between highest and lowest prices
int ll_pip; // highest price in 10pip unit
int hh_pip;
int TimeFrame = 0; // Time frame to analyze bars from {0=current chart, 1=M1, 5=M5, ..., 1440=D1, 10080=W1, 43200=MN1}
double hh; // highest price
double ll; // lowest price

bool timeToCheck=false;
bool startup = false; // variable to signal problem start
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void init()
{
IndicatorShortName("PPZ");
DeleteLines();
startup = true;
if(TimeFrame==0)
TimeFrame=Period();
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void deinit()
{
DeleteLines();
startup = false;
}

//+------------------------------------------------------------------+
void DrawLevel(double price, int conf)
{
price = NormalizeDouble(price,Digits);
string name = DoubleToStr(price,Digits)+" "+conf+" PPZ Level "+" confulence ";

if(ObjectFind(name)==-1)
ObjectCreate(name,OBJ_HLINE,0,0,price);

ObjectSet(name,OBJPROP_COLOR,Color);
ObjectSetText("OBJ_HLINE", "Hello world!", 10, "Times New Roman", Green);
}

//+------------------------------------------------------------------+
// draws ppz lines up to the number specified by user
// draws ppz with highest confulence first
// filter out any lines within ppz already drawn on chart
void DrawPPZ()
{
int con_sorted[];
int drawn_array[];
int drawn = 0;
int i;
bool far_enough = true;

//-- before drawing, delete existing ppz lines
DeleteLines();

ArrayResize(con_sorted,ArrayDimension(con));

ArrayCopy(con_sorted,con);
ArraySort(con_sorted,WHOLE_ARRAY,0,MODE_DESCEND);
//Print("non", con[0]," ", con[1], " ",con[2]," ",con[3]," ",con[4]);
//Print("sorted", con_sorted[0]," ", con_sorted[1], " ",con_sorted[2]," ",con_sorted[3]," ",con_sorted[4]);

ArrayResize(drawn_array,num_ppz_show);

int j = 0;
//while (j < num_ppz_show)
while (con_sorted[j] > 1)
{
for(i = 0; i < price_range; i++)
{
// match highest confluence for the price i
if(con[i] == con_sorted[j])
{

//-- if haven't drawn enough ppz and ppz level yet
if ((drawn < num_ppz_show)){
//-- make sure price is far enough from ppz already drawn
far_enough = true;
for (int k=0;k<drawn;k++) {
//Print ("i is ",i," drawn_array has ",drawn_array[k]);
if (MathAbs(i - drawn_array[k]) < ppz_spacing/1) {
far_enough = false;
}
} //-- end for drawn

if (far_enough) {
drawn_array[drawn] = i;
//Print ("calculation based on price ", ll, " to ",hh);
// Print ("drawn# ",drawn," ppz: ",ll_pip+i, " w/ confulence of ", con[i]);
//-- use Point*10 cuz base unit is 10pip
DrawLevel( (ll_pip+i)*Point*1, con[i]);
drawn++;
} //-- end if far_enough
}//-- end if drawn
} //-- end if con
} //-- end for i
j++;
} //--end while
} //-- end procedure

//+------------------------------------------------------------------+
// delete any horizontal lines with name "PPZ" in it
void DeleteLines()
{
string name;
for (int m =0; m< ObjectsTotal(); m++)
{
//if first 3 char eq ppz
name = ObjectName(m);
//Print ("name is ",name);
int found = StringFind(name, "PPZ");
//Print ("index ",found);
if (found >= 0) // if not found, index returned is -1
{
//Print("delete");
ObjectDelete(name);
}
//delete it
}
}

//+-------------------------------------------------------
// if program just startup, return true,
// if in the first minute of each time frame, return true
void CheckTime(bool&check)
{

switch (Period())
{

case PERIOD_M1:
Print ("PPZ not supported in M1");
break;
case PERIOD_M5:
Print ("PPZ not supported in M5");
break;
case PERIOD_M15:
Print ("PPZ not supported in M15");
break;
case PERIOD_M30:
if ((Minute()>28 && Seconds()>50) || startup) check=true; else check=false;
break;
case PERIOD_H1:
//Print("I am in H1!");
if ((Minute()<2 && Seconds()>50) || startup) check=true; else check=false;
//Print("check=", check);
break;
case PERIOD_H4:
if ((MathMod(Hour(),4)==0 && Minute()<2 && Seconds()>50) || startup) check=true; else check=false;
break;
case PERIOD_D1:
if ((Hour()==0 && Minute()<2 && Seconds()>50) || startup) check=true; else check=false;
//Print("check=",check);
break;
case PERIOD_W1:
if ((DayOfWeek()==0 && Hour()==0 && Minute()<2 && Seconds()>50) || startup) check=true; else check=false;
break;
case PERIOD_MN1:
if ((Day()==0 && Hour()==0 && Minute()<2 && Seconds()>50) || startup) check=true; else check=false;
break;
default:
Print("I am in default!");
check=false;
break;
}//-- end switch
} //-- end CheckTime

//+------------------------------------------------------------------+
void start()
{
//-- run checkTime routine to figure out if it's time to run
CheckTime(timeToCheck);
// Print ("timeToCheck",timeToCheck);
if (timeToCheck == true)
{
hh = iHigh(NULL,TimeFrame,iHighest(NULL,TimeFrame,MODE_HIGH,BarCount,0));
ll = iLow (NULL,TimeFrame,iLowest (NULL,TimeFrame,MODE_LOW, BarCount,0));

// use 10pips as basic unit, instead of 1 pip
hh_pip = hh/(Point*1);
ll_pip = ll/(Point*1);

price_range = hh_pip -ll_pip + 1; //price range in pips

//Print ("hh_pip ",hh_pip, " ll_pip ", ll_pip, " price range: ",price_range);

ArrayResize(con,price_range);
ArrayInitialize(con,0);

int price;
// i=1: start with last bar
for(int i = 1; i < BarCount; i++)
{
int high = iHigh(NULL,TimeFrame,i) / (Point *1);
int low = iLow(NULL,TimeFrame,i) / (Point *1);
// each price++ is 10 pips
for(price = ll_pip; price <= hh_pip; price++)
{
if ((MathAbs(price - high) < Tolerance/1) || (MathAbs(price - low) < Tolerance/1))
{
int index = price - ll_pip; //move index to store in array
con[index]++;
//Print ("ppz price level(x10pips) ",price," at index [",index,"] has confulence of ", con[index]);
} // end if
} // end for
} // end for
//-- call drawPPZ function
DrawPPZ();
}//-- end if CheckTime

startup = false;
} //-- end start
 

Hi

The first parameter of ObjectSetText() needs to be the name of the object, so

ObjectSetText("OBJ_HLINE", "Hello world!", 10, "Times New Roman", Green);

should be

ObjectSetText(name, "Hello world!", 10, "Times New Roman", Green);

Cheers

Jellybean

Reason: