PROBLEM with candlesticks plot

 

Hi Guys , 

Does anybody know what's wrong with this code ? 

Daily MQL5 Swear
//+------------------------------------------------------------------+
//|                                                Chart_Candles.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_plots   1
//--- plot Candlesticks
#property indicator_label1  "Candle"
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  clrCrimson,clrMediumSeaGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- indicator buffers
double         Candle_Open[];
double         Candle_Close[];
double         Candle_High[];
double         Candle_Low[];
double         Candle_Color[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Candle_Open,INDICATOR_DATA);
   SetIndexBuffer(1,Candle_Close,INDICATOR_DATA);
   SetIndexBuffer(2,Candle_High,INDICATOR_DATA);
   SetIndexBuffer(3,Candle_Low,INDICATOR_DATA);
   SetIndexBuffer(4,Candle_Color,INDICATOR_COLOR_INDEX);
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//---
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   Print(reason," ",GetLastError());
  }


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[])
  {
//---
      
      if(prev_calculated == 0)
        {
         Print(GetLastError());
        }
        
      int total = rates_total;
      for(int i=0 ; i < total && !IsStopped() ; i++)
        {
         Candle_Open[i]  = open[i];
         Candle_High[i]  = high[i];
         Candle_Low[i]   = low[i];
         Candle_Close[i] = close[i];
         
         if(Candle_Open[i] < Candle_Close[i])
           {
            Candle_Color[i] = 1.0;
           }
         else
           {
            Candle_Color[i] = 0.0;
           }
        }
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
claudio.zeccolella :

Hi Guys , 

Does anybody know what's wrong with this code ? 

Wrong code: at every tick, it redraws bars throughout the entire HISTORY.

 
Vladimir Karputov:

Wrong code: at every tick, it redraws bars throughout the entire HISTORY.

Thanks for your reply. So what's the right code ? :)

 
claudio.zeccolella :

Thanks for your reply. So what's the right code ? :)

You can't count the WHOLE HISTORY AT EVERY TICK. You need to use a lean algorithm.

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
 
Vladimir Karputov:

You cannot stop ALL bars at every tick. Read the help. Look for articles on "Zero Bar Recalculation".

Vladimir , I'm reading the help but it's 11 years old guide made out of horrible short explanations. If the ref guide where clear enough there were no need to post thousands of threads and articles.

I just don't understand why Metaquotes don't give a fuck about that...


Anyway I'm not able to find that article. Could you please link it ? :) 


Tks

 
claudio.zeccolella :


You are showing ignorance and disrespect for MetaQuotes. The company invests enormous resources in training. It should be respectful of such a titanic work. Open any indicator from the standard delivery - this is an example of economical recalculation on a zero bar.

 

Sorry Vladimir , 

I could run a petition to let you know how many people think the same thing. I have expressed my idea whether you like it or not. The ref guide is briefly written and there are dozens of articles and threads to read in order to learn just one simple thing. Do you really think that learning mql5 in this way it's fun and productive ? Anyway thank you for your reply I'll look another way to fix the problem by myself before switching to jforex again. Tks

 
claudio.zeccolella :

Sorry Vladimir , 

I could run a petition to let you know how many people think the same thing. I have expressed my idea whether you like it or not. The ref guide is briefly written and there are dozens of articles and threads to read in order to learn just one simple thing. Do you really think that learning mql5 in this way it's fun and productive ? Anyway thank you for your reply I'll look another way to fix the problem by myself before switching to jforex again. Tks

I will now prepare an example ...

 
Vladimir Karputov:

I will now prepare an example ...

Thank you very much Vladimir. Nothing against you of course. But believe me , approaching MQL5 for a beginner it's an absolute mess and you give up after a few trials. That's what I'm trying to say. 

:) 

 

Code:

//+------------------------------------------------------------------+
//|                                           DRAW_COLOR_CANDLES.mq5 |
//|                        Copyright 2011, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2011, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property description "An indicator to demonstrate DRAW_COLOR_CANDLES."
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_plots   1
//--- plot ColorCandles
#property indicator_label1  "ColorCandles"
#property indicator_type1   DRAW_COLOR_CANDLES
//--- Define 8 colors for coloring candlesticks (they are stored in the special array)
#property indicator_color1  clrRed,clrBlue,clrGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
//---
//--- Indicator buffers
double   BufferOpen[];
double   BufferHigh[];
double   BufferLow[];
double   BufferClose[];
double   BufferColors[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,BufferOpen,INDICATOR_DATA);
   SetIndexBuffer(1,BufferHigh,INDICATOR_DATA);
   SetIndexBuffer(2,BufferLow,INDICATOR_DATA);
   SetIndexBuffer(3,BufferClose,INDICATOR_DATA);
   SetIndexBuffer(4,BufferColors,INDICATOR_COLOR_INDEX);
//--- An empty value
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
//--- The name of the symbol, for which the bars are drawn
   string symbol=Symbol();
//--- Set the display of the symbol
   PlotIndexSetString(0,PLOT_LABEL,symbol+" Open;"+symbol+" High;"+symbol+" Low;"+symbol+" Close");
   IndicatorSetString(INDICATOR_SHORTNAME,"DRAW_COLOR_CANDLES("+symbol+")");
//---
   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=prev_calculated-1;
   if(prev_calculated==0)
      limit=0;
   for(int i=limit; i<rates_total; i++)
     {
      BufferOpen[i]=open[i];
      BufferHigh[i]=high[i];
      BufferLow[i]=low[i];
      BufferClose[i]=close[i];
      if(open[i]<close[i])
         BufferColors[i]=0.0;
      else
         if(open[i]>close[i])
            BufferColors[i]=1.0;
         else
            BufferColors[i]=2.0;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


Result:

DRAW_COLOR_CANDLES

Files:
 
I do really appreciate your help. Thank you very much indeed. :)