Drawing multiple lines with object name in global array

 

Hello everyone, I have an indicator that define 2 market condition: uptrend (status =1) downtrend (status = 0 ). When trend change I want to draw n. lines with a fixed distance from a chosen price level. The problem would be that until the trend has changed current lines must be "extended" to the right so that they advance with price. Here's what I did:

//Buffers
double ...,
...,
status[];

// Gloabal variables
string current_lines[];
int uptrend_start; //Index of uptrend start
int dwtrend_start; //Index of downtrend start
//--- Event handling functions

// Initialisation event handler
int OnInit(void)
  {
// Set number of significant digits (precision)
   IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
   ArrayResize(current_lines,14);
// Set Indicator buffers
   SetIndexBuffer(0, ..., INDICATOR_DATA);
   SetIndexBuffer(1, ..., INDICATOR_DATA);
   SetIndexBuffer(2, ..., INDICATOR_DATA);
   SetIndexBuffer(3, ..., INDICATOR_DATA);
   SetIndexBuffer(4, status, INDICATOR_DATA);
   ;
//Set indicator style
   SetIndexStyle(0,DRAW_NONE,0,3,m_col);
   SetIndexStyle(1,DRAW_NONE,0,3,m_col);

   SetIndexStyle(2,DRAW_LINE,0,3,m_col);
   SetIndexStyle(3,DRAW_LINE,0,3,m_col);
   SetIndexStyle(4,DRAW_NONE,0,3,m_col);

   return INIT_SUCCEEDED;  // Successful initialisation of indicator
  };

// Calculation event handler
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[]
)
  {
// Define maximum index for the buffer array

   int lookback = 100; //trying to display 1D open on last 100 1h candles
// Replacing loop function with the one you pointed out
   for(int iBar = Bars-1-MathMax(lookback, prev_calculated); iBar >= 0; --iBar)
     {
                ...
                ...
                ...     
        
        int reverse_ = status[iBar] == 0 ? uptrend_start  : dwtrend_start ;
      double level[14]  = {0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5};
      if(status[iBar] == 1 && status[iBar+1] == 0)
        {

         for(int i=0; i<=ArraySize(level); i++)
           {
            string obj2name = StringConcatenate("grid"+DoubleToString(i)+" "+time_day(Time[iBar]));  //Time day is a function that return a string "DD-MM-YYYY HH.mm"
            current_lines[i] = obj2name;
            double price = status[iBar] == 0 ? ...[iBar]+...*level[i] : ...[iBar]-...*level[i];
            ObjectCreate(0,obj2name,OBJ_TREND,0,Time[reverse_],price,Time[iBar],price);
            ObjectSetInteger(0,obj2name,OBJPROP_COLOR,clrOrange);
            ObjectSetInteger(0,obj2name,OBJPROP_WIDTH,2);
            ObjectSetInteger(0,obj2name,OBJPROP_RAY,false);
            ObjectSetInteger(0,obj2name,OBJPROP_SELECTABLE,false);
           }
        }
      else
        {
         for(int i=0; i<=ArraySize(level); i++)
           {
            ObjectSetInteger(0,current_lines[i],OBJPROP_TIME2,Time[iBar]);
           }
        }

     }
   return rates_total-1; // Recalculate current bar next tick.
// Return value of prev_calculated for next call
  };

I hope the code is clear enough... Basically it is:

Condition = true ? -> Draw NEW n lines 

Contion = false ? -> Extend current lines to the current prices


Thank you

 
ironhak:

Hello everyone, I have an indicator that define 2 market condition: uptrend (status =1) downtrend (status = 0 ). When trend change I want to draw n. lines with a fixed distance from a chosen price level. The problem would be that until the trend has changed current lines must be "extended" to the right so that they advance with price. Here's what I did:

I hope the code is clear enough... Basically it is:

Condition = true ? -> Draw NEW n lines 

Contion = false ? -> Extend current lines to the current prices


Thank you

I've managed to fix the code, before it was not working at all giving no error message. Now it gives error "array out of range", but I declared the array with the right size, why it does not work?

 
ironhak #:

I've managed to fix the code, before it was not working at all giving no error message. Now it gives error "array out of range", but I declared the array with the right size, why it does not work?

Solution: 

      if(status[iBar] == 1 && status[iBar+1] == 0)
        {

         for(int i=0; i<=ArraySize(level)-1; i++)
           {
            string obj2name = StringConcatenate("grid"+DoubleToString(i)+" "+time_day(Time[iBar]));  //Time day is a function that return a string "DD-MM-YYYY HH.mm"
            current_lines[i] = obj2name;
            double price = status[iBar] == 0 ? ...[iBar]+...*level[i] : ...[iBar]-...*level[i];
            ObjectCreate(0,obj2name,OBJ_TREND,0,Time[reverse_],price,Time[iBar],price);
            ObjectSetInteger(0,obj2name,OBJPROP_COLOR,clrOrange);
            ObjectSetInteger(0,obj2name,OBJPROP_WIDTH,2);
            ObjectSetInteger(0,obj2name,OBJPROP_RAY,false);
            ObjectSetInteger(0,obj2name,OBJPROP_SELECTABLE,false);
           }
        }
      else
        {
         for(int i=0; i<=ArraySize(level); i++)
           {
            ObjectSetInteger(0,current_lines[i],OBJPROP_TIME2,Time[iBar]);
           }
        }

I had to add "-1". 

 
ironhak #: I had to add "-1". 
         for(int i=0; i<=ArraySize(level); i++){
            ObjectSetInteger(0,current_lines[i],OBJPROP_TIME2,Time[iBar]);
  1. You could have just used i < Arraysize.
  2. You missed the other one.
 
William Roeder #:
  1. You could have just used i < Arraysize.
  2. You missed the other one.

You right, thank you. 

Reason: