Why this mql4 code does not draw color candles in MT4

 

I want to draw color candles in MT4. but the bellow code doesn't do anything. The code doesn't return any error but not work as expected. Can someone tell me what the issue is? I tried with ChatGPT to find an answer, it doesn't give any correct answer for this.  


//+------------------------------------------------------------------+
//|                                                Color Candles.mq4 |
//|                                          Copyright, H A T LAKMAL |
//|                                           https://t.me/Lakmal846 |
//+------------------------------------------------------------------+
#property copyright "Copyright, H A T LAKMAL"
#property link      "https://t.me/Lakmal846"
#property version   "1.00"
#property strict
#property indicator_chart_window

#property indicator_buffers 5

#property indicator_type1 DRAW_COLORCANDLE
#property indicator_label1 "UP DOWN"
#property indicator_color1 clrLimeGreen

//Color Candle Buffers
double OpenClr [];
double HighClr [];
double LowClr [];
double CloseClr[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   //Color Candels
   SetIndexBuffer(2,OpenClr);
   SetIndexStyle(2,DRAW_COLORCANDLE,STYLE_SOLID,3,clrLimeGreen);
   SetIndexBuffer(3,CloseClr);
   SetIndexStyle(3,DRAW_COLORCANDLE,STYLE_SOLID,3,clrLimeGreen);
   
   SetIndexBuffer(4,HighClr);
   SetIndexStyle(4,DRAW_COLORCANDLE,STYLE_SOLID,1,clrLimeGreen);
   SetIndexBuffer(5,LowClr);
   SetIndexStyle(5,DRAW_COLORCANDLE,STYLE_SOLID,1,clrLimeGreen);   
//---
   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[])
  {
  
   //Resize arrays for colors
   ArrayResize(OpenClr,rates_total);
   ArrayResize(HighClr,rates_total);
   ArrayResize(LowClr,rates_total);
   ArrayResize(CloseClr,rates_total);
  
//---
  int pos = prev_calculated;
  
  //Calculate for all available bars
  for(int i=pos; i<rates_total; i++)
    {
      //Resize arrays for colors
      ArrayResize(OpenClr,rates_total);
      ArrayResize(HighClr,rates_total);
      ArrayResize(LowClr,rates_total);
      ArrayResize(CloseClr,rates_total);
    
      HighClr[i] = high [i];
      LowClr [i] = low  [i];
      OpenClr [i] = open [i];
      CloseClr [i] = close [i];     
    }
  
  
  
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }

 
Hapu Arachchilage Tharindu Lakmal:

I want to draw color candles in MT4. but the bellow code doesn't do anything. The code doesn't return any error but not work as expected. Can someone tell me what the issue is? I tried with ChatGPT to find an answer, it doesn't give any correct answer for this.  


it does not work for mt4 

here is the code for mt5

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots 1 

double buf_open[], buf_high[], buf_low[], buf_close[];
double buf_color_line[];  
int OnInit()
{
    // Assign buffers
    SetIndexBuffer(0, buf_open, INDICATOR_DATA);
    SetIndexBuffer(1, buf_high, INDICATOR_DATA);
    SetIndexBuffer(2, buf_low, INDICATOR_DATA);
    SetIndexBuffer(3, buf_close, INDICATOR_DATA);
    SetIndexBuffer(4, buf_color_line, INDICATOR_COLOR_INDEX);
    PlotIndexSetInteger(0, PLOT_DRAW_TYPE,DRAW_COLOR_CANDLES);
    PlotIndexSetInteger(0, PLOT_COLOR_INDEXES, 2); 

    PlotIndexSetInteger(0, PLOT_LINE_COLOR, 0, clrBlue);
    PlotIndexSetInteger(0, PLOT_LINE_COLOR, 1, clrRed);


    return (INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| OnCalculate 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[])
{
   for(int i=prev_calculated;i<=rates_total-1;i++) {
      buf_open[i]  = open[i];
      buf_high[i]  = high[i];
      buf_low[i]   = low[i];
      buf_close[i] = close[i];
      if(close[i]>=open[i]){
        buf_color_line[i]=0;
        }else{
        buf_color_line[i]=1;
        }
    }

    return (rates_total);
}
 
Lorentzos Roussos #:

it does not work for mt4 

here is the code for mt5

Are there any other method to draw color candles in mt4 except drawing them as "histograms"?.

 
Hapu Arachchilage Tharindu Lakmal #:

Are there any other method to draw color candles in mt4 except drawing them as "histograms"?.

with the canvas , but its a lot more code

 
Hapu Arachchilage Tharindu Lakmal: s? I tried with ChatGPT to find an answer, it doesn't give any correct answer for this.  

There is no DRAW_COLORCANDLE in MT4.

ChatGPT (the worst), “Bots Builder”, “EA builder”, “EA Builder Pro”, EATree, “Etasoft forex generator”, “Forex Strategy Builder”, ForexEAdvisor (aka. ForexEAdvisor STRATEGY BUILDER, and Online Forex Expert Advisor Generator), ForexRobotAcademy.com, forexsb, “FX EA Builder”, fxDreema, Forex Generator, FxPro, “LP-MOBI”, Molanis, “Octa-FX Meta Editor”, Strategy Builder FX, “Strategy Quant”, “Visual Trader Studio”, “MQL5 Wizard”, etc., are all the same. You will get something quick, but then you will spend a much longer time trying to get it right, than if you learned the language up front, and then just wrote it.

Since you haven't learned MQL4/5, therefor there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.

We are willing to HELP you when you post your attempt (using Code button) and state the nature of your problem, but we are not going to debug your hundreds of lines of code. You are essentially going to be on your own.

ChatGPT
  1. Even it says do not use it for coding. * 
  2. Mixing MT4 and MT5 code together.
  3. Creating multiple OnCalculate/OnTick functions.
  4. OnCalculate returning a double.
  5. Filling buffers with zero in OnInit (they have no size yet). Setting buffer elements to zero but not setting Empty Value to correspond.
  6. Calling undefined functions.
  7. Calling MT4 functions in MT5 code.
  8. Sometimes, not using strict (MT4 code).
  9. Code that will not compile.
  10. Creating code outside of functions. * 
  11. Creating incomplete code. * 
  12. Initialization of Global variables with non-constants. * 
  13. Assigning a MT5 handle to a double or missing the buffer and bar indexes in a MT4 call. * 
  14. Useing MT4 Trade Functions without first selecting an order. * 
  15. Uses NULL in OrderSend (MT4). * 
bot builder Creating two OnInit() functions. * 
EA builder
  1. Counting up while closing multiple orders.
  2. Not useing time in new bar detection.
  3. Not adjusting for 4/5 digit brokers, TP/SL and slippage. * 
  4. Not checking return codes.
EATree Uses objects on chart to save values — not persistent storage (files or GV+Flush.) No recovery (crash/power failure.)
ForexEAdvisor
  1. Non-updateing global variables.
  2. Compilation errors.
  3. Not checking return codes.
  4. Not reporting errors.
FX EA Builder
  1. Not checking return codes.
  2. Loosing open tickets on terminal restart. No recovery (crash/power failure.)
  3. Not adjusting stops for the spread. * 
  4. Using OrdersTotal directly.
  5. Using the old event handlers.