Looking for direction with custom indicator

 

I have been working on this great custom indicator that detects ranging boxes. In its barest form, it detects double, triple and so on inside bars and then draws a box  around them.  Since the same price action forms all time frames, the code can detect a double inside bar on the hourly chart on a lower timeframe giving the viewer a clearer picture on price action and detecting a break soon than other techniques.  I've developed it to assist my trading on 50 tick charts but backtesting shows it has great potential as a day trading breakout strategy on the smaller time frames.

How ever I have a couple of issues with it. And because I'm not a programmer (I learn by trial and error) I looking for help to refine the code. So the main issue I have is that I run two versions of this indicator. I have attached the two full codes at the end. One I use to test  with, one I use to trade with. A picture so you can see what the indicator does and what I'm talking about

 

 

  

The first (left) is from my "tester" code. Now this indicator repaints. But it has to by definition. So to achieve this, I assigned a new object name each time the indicator detected a "box" using the following logic

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int box_no;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
   box_no=1;

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
datetime initTime;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

   if(valid && bc>=5)
      {
        drawBox("B"+IntegerToString(box_no)+"",Time[i+2],outH,Time[0],outL,clrRed);
        box_no++;
        break;
      }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void drawBox(string name,datetime time1,double price1,datetime time2,double price2,color clr,bool background=false)
  {
   ObjectDelete(name);
   ObjectCreate(name,OBJ_RECTANGLE,0,time1,price1,time2,price2);
   ObjectSet(name,OBJPROP_COLOR,clr);
   ObjectSet(name,OBJPROP_WIDTH,2);
   ObjectSet(name,OBJPROP_BACK,0);
  }

 Now I don't know if this works for the right reasons or wrong, but it works. It captures all boxes detected. However it leaves a very clutter chart. Too cluttered to trade from. To solve this, I assigned each specific box type a name thus as each new box of that type was formed the previous manifest was deleted. As the image on the right shows it's much clearer to trade from.

            if(valid && bc>=34)
              {
               drawBox("B12",Time[i+13],outH,Time[0],outL,clrDarkViolet);
               break;
              }
  

 Also, by assigning each box type a name, I can use the ObjectFind() function to detect it on my chart inside an EA and automate my trading, the ultimate goal. The problem with this version but is I can't use it during backtesting. It deletes the previous manifest of the box each time a new box forms. So unless a got a snapshot at the time, one can go back over the charts to compare structure the box was formed in. 

 

So what I want to do is get the indicator to met in the common ground. One that clears the clutter from the tester version, but leaves the final manifest of the box after it forms, not delete's it as it has too in the trading version.

 

And on a final note, please don't laugh too hard when you view the whole code. I play and dabble in coding. My reference is the mql4 site and my teachers have been the advice from members that have shared their knowledge here. I hope I can learn some more.