Download MetaTrader 5

Array Problem!

To add comments, please log in or register
fly7680
636
fly7680  

Hello everyone! I created a simple HeikenAshi pattern and every training I have to insert a horizontal line at the close up and I have to see him in the historic midnight. I can not understand, how can I add a line for each pattern that has already occurred from midnight onwards.
Maybe I should use the Array to fit more lines?
sorry for my English



//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_color1 Blue
#property indicator_style1 0
#property indicator_width1 1

#property indicator_color2 Red
#property indicator_style2 0
#property indicator_width2 2



//--- indicator buffers
double Buffer1[];
double Buffer2[];

double price1;


double myPoint;
bool OK = true;
extern color Colore = clrYellow;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {  
   IndicatorBuffers(2);
   IndicatorDigits(Digits);
  
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle     (0,DRAW_SECTION);
   SetIndexBuffer    (0,Buffer1);
   SetIndexLabel     (0, "Line 1");
  
   SetIndexEmptyValue(1,0.0);
   SetIndexStyle     (1,DRAW_SECTION);
   SetIndexBuffer    (1,Buffer2);
   SetIndexLabel     (1, "Line 2");  
  
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 4 || Digits() == 3 || Digits() == 2)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| 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[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
  
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
      
     }
   else
      limit++;
//----------------------------------------------------------------------------------------------+
//                                Calcolo Shift di MidNight                                     |
//----------------------------------------------------------------------------------------------+
      datetime when     = TimeCurrent();
      datetime midnight = when - (when % 86400); // there are 86400 seconds in a day: 86400
      datetime some_time= midnight;
      int      shift=iBarShift(Symbol(),PERIOD_CURRENT,some_time);// return index MindNight
      //Print("index of the bar for the time ",TimeToStr(some_time)," is ",shift);
//----------------------------------------------------------------------------------------------
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(shift-1, rates_total-1-50)) continue;// Cycle from MindNight
        
      //Calcolo Max delle 3 Candele prima dell'inversione SELL
      int MaxClose = iHighest(Symbol(),PERIOD_CURRENT,MODE_CLOSE,6+i,4+i);      
      
        
      //Pattern
        if(iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 1+i)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 1+i) // Candela Rossa Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 2+i)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 2+i) // Candela Rossa Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 3+i)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 3+i) // Candela Rossa Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 4+i)  < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 4+i) // Candela Verde Heiken
        )
      
        {
         price1 = Close[MaxClose]; // Assign the highest pattern of closing to double price1
        }
       }      
      
       if(OK == true)
      
      {      
       ObjectCreate(Symbol(),"Mezzanotte",OBJ_VLINE,0,some_time,0); // Vertical Line to MindNight
       ObjectSetInteger(Symbol(),"Mezzanotte",OBJPROP_COLOR,Colore);
      }
      
//---Line Max1
  
  
   for(i=0;i<=shift;i++)
     {
      Buffer1[i]=price1; //  Assign the price1 to Buffer1 and cycle the candle to MindNight
     }  
      
    
   return(rates_total);
  }
//+------------------------------------------------------------------+


honest_knave
Moderator
2326
honest_knave  
ObjectCreate(Symbol(),"Mezzanotte",OBJ_VLINE,0,some_time,0); // Vertical Line to MindNight

There can be only one object with the name "Mezzanotte"

If you want more lines, you need more names. 

fly7680
636
fly7680  
sorry for my English. the vertical line must be 1, I want horizontal lines for each of the buffer1 Pattern confirmed
honest_knave
Moderator
2326
honest_knave  

Why don't you create the line when you detect the pattern?

//Pattern
        if(iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 1+i)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 1+i) // Candela Rossa Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 2+i)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 2+i) // Candela Rossa Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 3+i)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 3+i) // Candela Rossa Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 4+i)  < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 4+i) // Candela Verde Heiken
        )
      
        {
         price1 = Close[MaxClose]; // Assign the highest pattern of closing to double price1
         ObjectCreate(...
        }
       }      

 Or have I misunderstood?

fly7680
636
fly7680  
thanks honest_knave, that's right, I could draw a line is true, however I need to create a line buffer that can be used at a later time.
Marco vd Heijden
Moderator
5493
Marco vd Heijden  

You could essentially just use a period 3 or 4 moving average as Heiken Ashi is merely a visualization technique and the moving average is a bit more easy to use then all those iCustom calls.

whroeder1
15902
whroeder1  
  1. datetime when     = TimeCurrent();
    :
    for(int i = limit-1; i >= 0; i--)
    Why are you looking at current time instead of Time[i]?
  2. if (i >= MathMin(shift-1, rates_total-1-50)) continue;// Cycle from MindNight
    Do your lookbacks correctly.
  3. ObjectCreate(Symbol(),"Mezzanotte",OBJ_VLINE,0,some_time,0); // Vertical Line to MindNight
    You can only create one object of a given name. After the first run, your code breaks. Use object name + time to create unique names.
  4. if(iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 1+i)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 1+i) // Candela Rossa Heiken
    && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 2+i)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 2+i) // Candela Rossa Heiken
    && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 3+i)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 3+i) // Candela Rossa Heiken
    && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 4+i)  < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 4+i) // Candela Verde Heiken
    Are your books one column but two feet wide? No because that is unreadable. They are 6 inches, sometimes two columns, so you can read it easily. So should be your code. I'm not going to go scrolling (or moving my eyes) back and forth trying to read it.
  5. You should write a self documenting function instead of calling iCustom directly, see Detailed explanation of iCustom - MQL4 forum
  6. Why are you looking at 1+i .. 4+i? Just look at the current candle 0+i .. 3+i. Don't process bar zero, see #2

  7. if(OK == true){      
           ObjectCreate(Symbol(),"Mezzanotte",OBJ_VLINE,0,some_time,0); // Vertical Line to MindNight
           ObjectSetInteger(Symbol(),"Mezzanotte",OBJPROP_COLOR,Colore);
    }
    //---Line Max1
    for(i=0;i<=shift;i++){
          Buffer1[i]=price1; //  Assign the price1 to Buffer1 and cycle the candle to MindNight
    }
    All this should be inside your for(i) loop, because that is only where price1 is valid, see #1.
  8. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence. If(OK) is meaningless. What is OK?
  9. Why are you using a loop to fill part of an array, instead of just ArrayFill(Buffer1, 0, shift+1. price1)?


fly7680
636
fly7680  

whroeder1 thanks for the answers!

1) Use TimeCurrent because I'm out of the loop, and I do not have the variable declared.
2) This section does not know I have to study it better.
3) The vertical line that's okay ... I just need a line.
4/5/6) Call iCostum () because I need different shift to identify the color of Heiken Ashi.
9) I asked for advice, why I not know well arrays
fly7680
636
fly7680  

I'm back .... I have solved my problem and now, I can draw Buffer correctly using for loops. The only problem with this condition sometimes is not read correctly.

//----------------------------------------------------------------------------------------------+
//                                calculation Shift di MidNight                                 |
//----------------------------------------------------------------------------------------------+
      datetime when     = TimeCurrent();
      datetime midnight = when - (when % 86400); // there are 86400 seconds in a day: 86400
      datetime some_time= midnight;
      int      shift=iBarShift(Symbol(),PERIOD_CURRENT,some_time);
      //Print("index of the bar for the time ",TimeToStr(some_time)," is ",shift);
//----------------------------------------------------------------------------------------------+
//                                Vertical line MidNight (only one)                             |
//----------------------------------------------------------------------------------------------+
     if(OK == true)
      
      {      
       ObjectCreate(Symbol(),"Mezzanotte",OBJ_VLINE,0,some_time,0);
       ObjectSetInteger(Symbol(),"Mezzanotte",OBJPROP_COLOR,Colore);
      }
      
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------+
//                                1° Loop Buffer1                                               |
//----------------------------------------------------------------------------------------------+
  

   //--- main loop
   for(int i = 0; i <= shift; i++) // from 0 to midnight
     {
      
      int MaxClose1 = iHighest(Symbol(),PERIOD_CURRENT,MODE_CLOSE,6+i,4+i);      
      
        
      //Indicator Buffer 1
        if(iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 1+i)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 1+i) // Red Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 2+i)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 2+i) // Red Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 3+i)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 3+i) // Red Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 4+i)  < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 4+i) // Green Heiken
        )
      
        {
         price1 = Close[MaxClose1];
         break;
        }  
       }  
//---Line Max1
  
   for(i=0;i<=shift;i++) // from 0 to midnight
     {
      Buffer1[i]=price1; //drawing buffer
     }    
      
  
  
//----------------------------------------------------------------------------------------------+
//                                2° Loop Buffer2                                               |
//----------------------------------------------------------------------------------------------+  

   //--- main loop
   for(int I = MaxClose1; I <= shift; I++)// from MaxClose1 to midnight
     {
      
      int MaxClose2 = iHighest(Symbol(),PERIOD_CURRENT,MODE_CLOSE,6+I,4+I);      
      
        
      //Indicator Buffer 1
        if(iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 1+I)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 1+I) // Red Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 2+I)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 2+I) // Red Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 3+I)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 3+I) // Red Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 4+I)  < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 4+I) // Green Heiken
        )
      
        {
         price2 = Close[MaxClose2];
         break;
        }
       }  
//---Line Max2
    
   for(I=0;I<=shift;I++) // from 0 to midnight
     {
      Buffer2[I]=price2; //drawing buffer
     }      

    
//----------------------------------------------------------------------------------------------+
//                                3° Loop Buffer3                                               |
//----------------------------------------------------------------------------------------------+  

   //--- main loop
   for(int a = MaxClose2; a <= shift; a++) //from MaxClose2 to midnight
     {
      
      int MaxClose3 = iHighest(Symbol(),PERIOD_CURRENT,MODE_CLOSE,6+a,4+a);      
      
        
      //Indicator Buffer 1
        if(iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 1+a)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 1+a) // Red Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 2+a)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 2+a) // Red Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 3+a)  > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 3+a) // Red Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 4+a)  < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 4+a) // Green Heiken
        )
      
        {
         price3 = Close[MaxClose3];
         break;
        }
       }  
//---Line Max3  

   for(a=0;a<=shift;a++) // from 0 to midnight
     {
      Buffer3[a]=price3; //drawing buffer
     }

  
   return(rates_total);
  }
To add comments, please log in or register