Looking for Advise to improve my Code

 

Hi, i am trying to improve my current Indicator but something doesnt work the way I want ^^

#property indicator_chart_window

#property indicator_buffers 3
#property indicator_color1 LightSeaGreen
#property indicator_color2 LightSeaGreen
#property indicator_color3 LightSeaGreen

extern int    UpperBandPeriod       = 34;
extern int    UpperBandShift        = 0;
extern double UpperBandDeviation    = 2.0;

extern int    MA_Period              = 21;
extern int    MA_Shift               = 0;

extern int    LowerBandPeriod       = 34;
extern int    LowerBandShift        = 0;
extern double LowerBandDeviation    = 2.0;

extern int    LowerBandPierced      = 26; //Anzahl Candles, die vergangan sind, seit das Lower Band unterschritten wurde
extern int    UpperBandPierced      = 26;

//============================= Dummies ======================
double Multiplier;
double AdjustedUpperBand;
double AdjustedMA;
double AdjustedLowerBand;
datetime TimeOld;
double BandDiff   = 0;
bool SetArrowUp   = false;
bool SetArrowDown = false;
int x             = 0;
//============================== Parameters ==================
double dDiff=0;
bool   bArrowPlacedLong  = false;
bool   bArrowPlacedShort = false;
int    MinClose = 0;
int    MaxClose = 0;
int    counterUP = 1;
// ================================= Arrays =====================
double MA[];
double Lower_Band[];

//============================= Place Arrow Up ==== ORIGINAL ======================================
void PlaceArrowUp(int i){
   

   
   if(    (Open[i]    > iMA(NULL,0,21,MA_Shift,MODE_SMA,PRICE_CLOSE,i)) 
       && (Open[i+1]  < iMA(NULL,0,21,MA_Shift,MODE_SMA,PRICE_CLOSE,i+1))
       && (Close[i+1] > iMA(NULL,0,21,MA_Shift,MODE_SMA,PRICE_CLOSE,i+1)) ){
      for(int k=1;k<=26;k++){
         if( Low[i+k] < (iMA(NULL,0,21,MA_Shift,MODE_SMA,PRICE_CLOSE,i+k)-0.02) ){
               x++;
               ObjectCreate("BUY" + x,OBJ_ARROW,0,Time[i],Low[MinClose]);
               ObjectSet   ("BUY" + x,OBJPROP_COLOR,Blue); 
               ObjectSet   ("BUY" + x,OBJPROP_ARROWCODE,SYMBOL_ARROWUP); 
               ObjectSet   ("BUY" + x,OBJPROP_WIDTH,5); 
               ObjectSet   ("BUY" + x,OBJPROP_BACK,true);

               break;
         }
      }
   
   }
}

//================================================ INIT =============================================
int init(){
   int period = Period();

   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MA);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,Lower_Band);

   SetIndexDrawBegin(1,AdjustedMA+MA_Shift);
   SetIndexDrawBegin(2,AdjustedLowerBand+LowerBandShift+LowerBandPierced);

   return(0);
}
//===================================== ON TICK ===============================================
int start(){
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int uncounted_bars=Bars-counted_bars;

   for(int i=0;i<uncounted_bars;i++){
      if( Time[i] != TimeOld ){
         MA[i]         = iMA(NULL,0,21,MA_Shift,MODE_SMA,PRICE_CLOSE,i);
         Lower_Band[i] = iMA(NULL,0,21,MA_Shift,MODE_SMA,PRICE_CLOSE,i)-0.02;
         MinClose      = iLowest (NULL,0,MODE_CLOSE,6,i);//Kleinster Schlusskurs  

         
         PlaceArrowUp(i);

      }

   }

   return(0);
}
//===================================== DEINIT =================================================
int deinit(){
   ObjectsDeleteAll(); // delete all objects from chart
   return 0;
}

This  is how the indicator works:

1. When the actual OPENING is above the upper MA,

2. and the last Candles OPENING was UNDER the upper MA and its CLOSE was above the upper MA

3. look, if the LOWER MA was pierced during the last 26 candles

4. Place an Arrow, if the conditions are fulfilled

 

And NOW i want the Code to improve in a way, that there wont be another Arrow, befor the LOWER MA isnt pierced again.

I tried to set a Flag:

Implement a global variable that gets the value 0, when the LOWER MA is pierced once, and it gets the value 1, when the UPPER MA is pierced.

Befor the variable isnt set back to 0 the indicator shall not be allowed to set another arrow.

But my implementation doesnt work, because somehow the indicator waits till the UPPER MA is pierced twice before the arrow is placed.

This is the NEW CODE:

#property indicator_chart_window

#property indicator_buffers 3
#property indicator_color1 LightSeaGreen
#property indicator_color2 LightSeaGreen
#property indicator_color3 LightSeaGreen

extern int    UpperBandPeriod       = 34;
extern int    UpperBandShift        = 0;
extern double UpperBandDeviation    = 2.0;

extern int    MA_Period              = 21;
extern int    MA_Shift               = 0;

extern int    LowerBandPeriod       = 34;
extern int    LowerBandShift        = 0;
extern double LowerBandDeviation    = 2.0;

extern int    LowerBandPierced      = 26; //Anzahl Candles, die vergangan sind, seit das Lower Band unterschritten wurde
extern int    UpperBandPierced      = 26;

//============================= Dummies ======================
double Multiplier;
double AdjustedUpperBand;
double AdjustedMA;
double AdjustedLowerBand;
datetime TimeOld;
double BandDiff   = 0;
bool SetArrowUp   = false;
bool SetArrowDown = false;
int x             = 0;
//============================== Parameters ==================
double dDiff=0;
bool   bArrowPlacedLong  = false;
bool   bArrowPlacedShort = false;
int    MinClose = 0;
int    MaxClose = 0;
int    counterUP = 1;
// ================================= Arrays =====================
double MA[];
double Lower_Band[];

//============================= Place Arrow Up ==== ORIGINAL ======================================
void PlaceArrowUp(int i){
   
   if(counterUP == 0){                                                           //   NEU
   
   if(    (Open[i]    > iMA(NULL,0,21,MA_Shift,MODE_SMA,PRICE_CLOSE,i)) 
       && (Open[i+1]  < iMA(NULL,0,21,MA_Shift,MODE_SMA,PRICE_CLOSE,i+1))
       && (Close[i+1] > iMA(NULL,0,21,MA_Shift,MODE_SMA,PRICE_CLOSE,i+1)) ){
      for(int k=1;k<=26;k++){
         if( Low[i+k] < (iMA(NULL,0,21,MA_Shift,MODE_SMA,PRICE_CLOSE,i+k)-0.02) ){
               x++;
               ObjectCreate("BUY" + x,OBJ_ARROW,0,Time[i],Low[MinClose]);
               ObjectSet   ("BUY" + x,OBJPROP_COLOR,Blue); 
               ObjectSet   ("BUY" + x,OBJPROP_ARROWCODE,SYMBOL_ARROWUP); 
               ObjectSet   ("BUY" + x,OBJPROP_WIDTH,5); 
               ObjectSet   ("BUY" + x,OBJPROP_BACK,true);
               counterUP = 1;                                                    //   NEU
               break;
         }
      }
   }
   }               
}

//================================================ INIT =============================================
int init(){
   int period = Period();

   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MA);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,Lower_Band);

   SetIndexDrawBegin(1,AdjustedMA+MA_Shift);
   SetIndexDrawBegin(2,AdjustedLowerBand+LowerBandShift+LowerBandPierced);

   return(0);
}
//===================================== ON TICK ===============================================
int start(){
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int uncounted_bars=Bars-counted_bars;

   for(int i=0;i<uncounted_bars;i++){
      if( Time[i] != TimeOld ){
         MA[i]         = iMA(NULL,0,21,MA_Shift,MODE_SMA,PRICE_CLOSE,i);
         Lower_Band[i] = iMA(NULL,0,21,MA_Shift,MODE_SMA,PRICE_CLOSE,i)-0.02;
         MinClose      = iLowest (NULL,0,MODE_CLOSE,6,i);//Kleinster Schlusskurs  
         
         if(Low[i] < Lower_Band[i]) {counterUP = 0;}          //   NEU
         
         PlaceArrowUp(i);

      }

   }

   return(0);
}
//===================================== DEINIT =================================================
int deinit(){
   ObjectsDeleteAll(); // delete all objects from chart
   return 0;
}
 

This is the result of my first Code

BUT THIS IS THE RESULT OF MY SECOND CODE:


 

Why does the arrow only appear, when the UPPER MA it pierced twice ?

I dont get it....

 
BlazeRed:

Why does the arrow only appear, when the UPPER MA it pierced twice ?

I dont get it....

I haven't checked it well, but if you use that kind of Flag then the code should go through from the oldest bar to the newest (from uncounted_bars to 0) your code does the opposite.
 
Laszlo Tormasi:
I haven't checked it well, but if you use that kind of Flag then the code should go through from the oldest bar to the newest (from uncounted_bars to 0) your code does the opposite.

Hey, thanks for the hint.

You are right, my "i" was running from right to left, I corrected it and now everything is fine ^^