Indicator not displaying horizontal lines (ObjectCreate/ObjectSetInteger)

 

Greets,


Would appreciate any comment on where I might be going wrong here. Creating an indicator works as expected. However, I would like to parameterize the two horizontal levels (eg, 80% and 20%) instead of using the usual #property indicator_level1 lines which are hard coded).


Here's the sample code:

#property strict
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 2
#property indicator_plots   2

// This works as expected:
//#property indicator_level1     20.0
//#property indicator_level2     80.0
//#property indicator_levelcolor clrSilver
//#property indicator_levelstyle STYLE_DOT

input int top = 80;
input int bottom = 20;

int OnInit()
{
   IndicatorSetInteger(INDICATOR_DIGITS,0);

   // Following displays nothing:
   
   // Create a horizontal line for the top level
   if (!ObjectCreate(0, "top", OBJ_HLINE, 0, 0, top))
      Print("Error creating top: ", GetLastError());

   ObjectSetInteger(0, "top", OBJPROP_COLOR, clrBlack);
   ObjectSetInteger(0, "top", OBJPROP_STYLE, STYLE_DASHDOT);
   ObjectSetInteger(0, "top", OBJPROP_WIDTH, 1);

   // Create a horizontal line for the bottom level
   if (!ObjectCreate(0, "bottom", OBJ_HLINE, 0, 0, bottom))
      Print("Error creating bottom: ", GetLastError());

   ObjectSetInteger(0, "bottom", OBJPROP_COLOR, clrRed);
   ObjectSetInteger(0, "bottom", OBJPROP_STYLE, STYLE_DASHDOT);
   ObjectSetInteger(0, "bottom", OBJPROP_WIDTH, 1);
   
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason) {
        
}
//+------------------------------------------------------------------+
//| 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[])
{
   return 50;
}
Documentation on MQL5: Language Basics / Preprocessor / Program Properties (#property)
Documentation on MQL5: Language Basics / Preprocessor / Program Properties (#property)
  • www.mql5.com
Every mql5-program allows to specify additional specific parameters named #property that help client terminal in proper servicing for programs...
 
Henka Combs: Would appreciate any comment on where I might be going wrong here. Creating an indicator works as expected. However, I would like to parameterize the two horizontal levels (eg, 80% and 20%) instead of using the usual #property indicator_level1 lines which are hard coded).

To set the levels programmatically at runtime, instead of at compile-time with #property use the following ...

IndicatorSetDouble

Sets the value of an indicator property of the double type

IndicatorSetInteger

Sets the value of an indicator property of the int type

IndicatorSetString

Sets the value of an indicator property of the string type

ENUM_CUSTOMIND_PROPERTY_INTEGER

ID

Description

Property type

INDICATOR_LEVELS

Number of levels in the indicator window

int

INDICATOR_LEVELCOLOR

Color of the level line

color                      modifier = level number

INDICATOR_LEVELSTYLE

Style of the level line

ENUM_LINE_STYLE  modifier = level number

INDICATOR_LEVELWIDTH

Thickness of the level line

int                         modifier = level number


ENUM_CUSTOMIND_PROPERTY_DOUBLE

ID

Description

Property type

INDICATOR_LEVELVALUE

Level value

double                    modifier = level number


ENUM_CUSTOMIND_PROPERTY_STRING

ID

Description

Property type

INDICATOR_LEVELTEXT

Level description

string                    modifier = level number

 
Fernando Carreiro #:

To set the levels programmatically at runtime, instead of at compile-time with #property use the following ...

IndicatorSetDouble

Sets the value of an indicator property of the double type

IndicatorSetInteger

Sets the value of an indicator property of the int type

IndicatorSetString

Sets the value of an indicator property of the string type

ENUM_CUSTOMIND_PROPERTY_INTEGER

ID

Description

Property type

INDICATOR_LEVELS

Number of levels in the indicator window

int

INDICATOR_LEVELCOLOR

Color of the level line

color                      modifier = level number

INDICATOR_LEVELSTYLE

Style of the level line

ENUM_LINE_STYLE  modifier = level number

INDICATOR_LEVELWIDTH

Thickness of the level line

int                         modifier = level number


ENUM_CUSTOMIND_PROPERTY_DOUBLE

ID

Description

Property type

INDICATOR_LEVELVALUE

Level value

double                    modifier = level number


ENUM_CUSTOMIND_PROPERTY_STRING

ID

Description

Property type

INDICATOR_LEVELTEXT

Level description

string                    modifier = level number


Great! Thanks Fernando.


   IndicatorSetInteger(INDICATOR_DIGITS, 0);
   IndicatorSetInteger(INDICATOR_LEVELS, 2);

   IndicatorSetDouble(INDICATOR_MAXIMUM, 100);
   IndicatorSetDouble(INDICATOR_MINIMUM, 0);
   IndicatorSetDouble(INDICATOR_LEVELVALUE, 0, bottom);
   IndicatorSetDouble(INDICATOR_LEVELVALUE, 1, top);