Download MetaTrader 5

hi; a question about time define in mql5 .

To add comments, please log in or register
Mehrdad Shiri
4596
Mehrdad Shiri  

hi ;

i want to check market by candles are in special time,

for example:

day_of_week==2
Hour_Start=20;
Minute_Start=0;
Hour_End=23;
Minute_End=59;

with using :MqlDateTime

     MqlDateTime time_now;
//   TimeCurrent(time_now);
     if( time_now.day_of_week==2 && 
        (time_now.hour>=Hour_Start &&  time_now.min>=Minute_Start ) && 
        (time_now.hour<=Hour_End &&  time_now.min<=Minute_End )        )

can you explain to fixed.

thanx

Slava
Moderator
6961
Slava  

Why do you comment TimeCurrent function call?

You need to use some time function to get time. Try TimeTradeServer

Mehrdad Shiri
4596
Mehrdad Shiri  

thank you.

i correct it but ; still has a problem;

//+------------------------------------------------------------------+
//|                                  Z4_ColorCandles_Daily_Hours.mq5 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window

int Day_End=4;
int Hour_Start=6;
int Minute_Start=0;
int Hour_End=23;
int Minute_End=59;

input color Color_Chart_Line_0=Magenta;
input color Color_Candle_Bull_0=SkyBlue;
input color Color_Candle_Bear_0=LightPink;
input color Color_Bar_Up_0=PowderBlue;
input color Color_Bar_Down_0=Bisque;
//---
input color Color_Chart_Line_1=Red;
input color Color_Candle_Bull_1=Red;
input color Color_Candle_Bear_1=Red;
input color Color_Bar_Up_1=Red;
input color Color_Bar_Down_1=Red;
//------------------------------------------------------------------------------  color for line chart and Doji candles                  
    color  GetColorChartLine() { return((color)ChartGetInteger(0,CHART_COLOR_CHART_LINE)); }
    void  SetColorChartLine(color Color_Chart_Line) { ChartSetInteger(0,CHART_COLOR_CHART_LINE,Color_Chart_Line); } 
//--------------------------------------------------------------------------------------  body color of the bull candle           
    color  GetColorCandleBull() { return((color)ChartGetInteger(0,CHART_COLOR_CHART_UP)); }
    void  SetColorCandleBull(color Color_Candle_Bull) { ChartSetInteger(0,CHART_COLOR_CHART_UP,Color_Candle_Bull); }
//--------------------------------------------------------------------------------------  body color of the bear candle           
    color  GetColorCandleBear() { return((color)ChartGetInteger(0,CHART_COLOR_CHART_DOWN)); }
    void  SetColorCandleBear(color Color_Candle_Bear) { ChartSetInteger(0,CHART_COLOR_CHART_DOWN,Color_Candle_Bear); }   
//-----------------------------------------------------------------------------------  Body color of a bull candlestick     
    color  GetColorBarUp() { return((color)ChartGetInteger(0,CHART_COLOR_CANDLE_BULL)); }
    void  SetColorBarUp(color Color_Bar_Up) { ChartSetInteger(0,CHART_COLOR_CANDLE_BULL,Color_Bar_Up); }     
//-----------------------------------------------------------------------------------  Body color of a bull candlestick     
    color  GetColorBarDown() { return((color)ChartGetInteger(0,CHART_COLOR_CANDLE_BEAR)); }
    void  SetColorBarDown(color Color_Bar_Down) { ChartSetInteger(0,CHART_COLOR_CANDLE_BEAR,Color_Bar_Down); }      
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
   ChartSetInteger(0,CHART_COLOR_CHART_LINE,Color_Chart_Line_0);
   ChartSetInteger(0,CHART_COLOR_CHART_UP,Color_Candle_Bull_0);   
   ChartSetInteger(0,CHART_COLOR_CHART_DOWN,Color_Candle_Bear_0);   
   ChartSetInteger(0,CHART_COLOR_CANDLE_BULL,Color_Bar_Up_0);  
   ChartSetInteger(0,CHART_COLOR_CANDLE_BEAR,Color_Bar_Down_0);
   
   MqlDateTime time_now;
   datetime candle_time=TimeTradeServer(time_now);
   

   if( time_now.day_of_week==Day_End && time_now.hour>=Hour_End &&  time_now.min>=Minute_End ) 
   {
   ChartSetInteger(0,CHART_COLOR_CHART_LINE,Color_Chart_Line_1);
   ChartSetInteger(0,CHART_COLOR_CHART_UP,Color_Candle_Bull_1);   
   ChartSetInteger(0,CHART_COLOR_CHART_DOWN,Color_Candle_Bear_1);   
   ChartSetInteger(0,CHART_COLOR_CANDLE_BULL,Color_Bar_Up_1);  
   ChartSetInteger(0,CHART_COLOR_CANDLE_BEAR,Color_Bar_Down_1);
   }
//+------------------------------------------------------------------+   
   Comment(
           "\ntime_now=",candle_time
           ,"\ntime_now_year=",time_now.year,"   time_now_mon=",time_now.mon    
           ,"\ntime_now_day=",time_now.day,"   time_now_hour=",time_now.hour,"   time_now_min=",time_now.min
           ,"   time_now_sec=",time_now.sec);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+     
void OnDeinit(const int reason)
  {
   Comment("");
//----
//----
  }
//+------------------------------------------------------------------+

there is no change in color for  :

day_of_week==4
Hour_Start=6;
Minute_Start=0;
Hour_End=23;
Minute_End=59;

pic. :







Gabor Torma
2816
Gabor Torma  

The time is never larger then 23:59...

day_of_week==4
Hour_Start=6;
Minute_Start=0;
Hour_End=23;
Minute_End=59;
if( time_now.day_of_week==Day_End && time_now.hour>=Hour_End &&  time_now.min>=Minute_End ) 

It means:

if( time_now.day_of_week==4 && time_now.hour>=23 &&  time_now.min>=59) 

this condition true only 1 minute at day4 23:59. after 1 minute the time will be day 5 0:00, therefore 5 !=time_now.day_of_week and 0!>=time_now.hour and 0!>=time_now.time

Gabor Torma
2816
Gabor Torma  

Please write when do you change the color.
phi nuts
2181
phi nuts  

TIMisthebest, I'm not so sure what you are trying to achieve, are you trying to change candle color for specific time ?. If yes, then you can not use ChartSetInteger() function, because this function will change the color off all candle. You also have to change your code. Below is modified code, base on Heikin Ashi.

 

 

//+------------------------------------------------------------------+
//|                                                  Heiken_Ashi.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                  Z4_ColorCandles_Daily_Hours.mq5 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property description "TIMisthebest : https://www.mql5.com/en/forum/9142"

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   1
#property indicator_type1   DRAW_COLOR_CANDLES                 //--- to change color
#property indicator_color1  Yellow, OrangeRed                  //--- to change color
#property indicator_label1  "Z4 Open;Z4 High;Z4 Low;Z4 Close"

//--- indicator input 
enum Day_Of_Week
  {
   Sunday,
   Monday,
   Tuesday,
   Wednesday,
   Thursday,
   Friday,
   Saturday,
  };
  
//--- input parameters
input Day_Of_Week Day_Start    = Sunday;
input int Hour_Start           = 6;
input int Minute_Start         = 0;
input Day_Of_Week Day_End      = Saturday;
input int Hour_End             = 20;
input int Minute_End           = 59;

//--- indicator buffers
double ExtOBuffer[];
double ExtHBuffer[];
double ExtLBuffer[];
double ExtCBuffer[];
double ExtColorBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtOBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtHBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,ExtLBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,ExtCBuffer,INDICATOR_DATA);
   SetIndexBuffer(4,ExtColorBuffer,INDICATOR_COLOR_INDEX);
//---
   
//--- sets first bar from what index will be drawn
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- sets text for indicator
   IndicatorSetString(INDICATOR_SHORTNAME,"Z4_ColorCandles_Daily_Hours");
//--- sets drawing line empty value
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
//--- initialization done
  }
//+------------------------------------------------------------------+
//| Heiken Ashi                                                      |
//+------------------------------------------------------------------+
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 &TickVolume[],
                const long &Volume[],
                const int &Spread[])
  {
   int i,limit;
   MqlDateTime time_now;
   datetime candle_time;
   
//--- preliminary calculations
   if(prev_calculated == 0)
     {
      //--- set first candle
      ExtLBuffer[0] = Low[0];
      ExtHBuffer[0] = High[0];
      ExtOBuffer[0] = Open[0];
      ExtCBuffer[0] = Close[0];
      limit = 1;
     }
   else limit = prev_calculated - 1;

//--- the main loop of calculations
   for(i = limit;i < rates_total && !IsStopped(); i++)
     {
      double haOpen  = Open[i];
      double haClose = Close[i];
      double haHigh  = High[i];
      double haLow   = Low[i];

      ExtLBuffer[i] = haLow;
      ExtHBuffer[i] = haHigh;
      ExtOBuffer[i] = haOpen;
      ExtCBuffer[i] = haClose;

      //--- set candle color
      candle_time = Time [i];
      TimeToStruct(candle_time, time_now);
      
    if(  //time_now.day_of_week  >= Day_Start &&    //--- i'm not using this
         time_now.hour >= Hour_Start && 
         time_now.min >= Minute_Start && 
         
         //time_now.day_of_week <= Day_End &&       //--- i'm not using this
         time_now.hour <= Hour_End   && 
         time_now.min <= Minute_End  )
        
       ExtColorBuffer[i] = 0.0; // set color DodgerBlue
      else               
       ExtColorBuffer[i] = 1.0; // set color Red
 
     }
//--- done
   return(rates_total);
  }
//+------------------------------------------------------------------+

 

Mehrdad Shiri
4596
Mehrdad Shiri  

with thank to phi.nuts ;( & how helped;)

thank help.

i want to draw candle in chart with my favorite color ; assume:

1)  in case normal:

input color Color_Chart_Line_0=Magenta;
input color Color_Candle_Bull_0=SkyBlue;
input color Color_Candle_Bear_0=LightPink;
input color Color_Bar_Up_0=PowderBlue;
input color Color_Bar_Down_0=Bisque;

then for special time ; for example : (  " 3hour before market closing " or " 3hour after market opening " , { to see in tester;,,,, and with expert use this  indicator ; the market reaction ,)

for simple this : assume;

2) when time between :

int Day_Start=4;
int Hour_Start=6;
int Minute_Start=0;
int Day_End=4; // 
int Hour_End=20;
int Minute_End=59;

the color change to :

input color Color_Chart_Line_1=Red;
input color Color_Candle_Bull_1=Red;
input color Color_Candle_Bear_1=Red;
input color Color_Bar_Up_1=Red;
input color Color_Bar_Down_1=Red;

make it visible on the CHART with different color.

in this code:

but still has problem ; can you please check it

//+------------------------------------------------------------------------------+            
#property indicator_chart_window              
#property  version "1.0"                                   
#property indicator_buffers 6
#property indicator_label1 "Open;High;Low;Close"
#property indicator_plots 1                     
#property indicator_type1 DRAW_COLOR_CANDLES   
#property indicator_width1 3                    
input int Day_Start=4;
input int Hour_Start=6;
input int Minute_Start=0;
input int Day_End=4;
input int Hour_End=20;
input int Minute_End=59;
input color Color_Bar_Up_1=clrPowderBlue;
input color Color_Bar_Down_1=clrBisque; 
input color Color_Bar_Up_0=clrGreen;
input color Color_Bar_Down_0=clrRed;                                                
double buffer_open[],buffer_high[],buffer_low[],buffer_close[]; 
double buffer_color_line[]; 
double buffer_tmp[1];       

int OnInit()
  {
   SetIndexBuffer(0,buffer_open,INDICATOR_DATA);
   SetIndexBuffer(1,buffer_high,INDICATOR_DATA);
   SetIndexBuffer(2,buffer_low,INDICATOR_DATA);
   SetIndexBuffer(3,buffer_close,INDICATOR_DATA);
   SetIndexBuffer(4,buffer_color_line,INDICATOR_COLOR_INDEX);

   PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,4);
   
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,Color_Bar_Up_0);  
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,Color_Bar_Down_0); 
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,2,Color_Bar_Up_1);
 PlotIndexSetInteger(0,PLOT_LINE_COLOR,3,Color_Bar_Down_1);               
   return(0);
  }

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[])
  {
  MqlDateTime time_now;
   for(int i=prev_calculated;i<=rates_total-1;i++)
     {
      buffer_open[i]=open[i]; 
      buffer_high[i]=high[i];  
      buffer_low[i]=low[i];   
      buffer_close[i]=close[i];
   datetime candle_time=time[i];
       TimeToStruct(time[i],time_now);
       if( (time_now.day_of_week==Day_Start && time_now.hour>=Hour_Start && time_now.min>=Minute_Start)
     &&( time_now.day_of_week==Day_End && time_now.hour<=Hour_End  && time_now.min<=Minute_End )   )
        { 
        if ( open[i]>= close[i])
            buffer_color_line[i]=3;
        else
           buffer_color_line[i]=2;
           }
    else
     { 
      if ( open[i]>= close[i])
            buffer_color_line[i]=1;
        else
           buffer_color_line[i]=0;
           }         
     }
   return(rates_total-1); 
  }
//+------------------------------------------------------------------+

phi nuts
2181
phi nuts  

You don't have this

#property indicator_color1  Yellow, DodgerBlue, Red, Green

in property.

This is my modified code from above - I'm not so sure if it is correct

//+------------------------------------------------------------------+
//|                                                  Heiken_Ashi.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                  Z4_ColorCandles_Daily_Hours.mq5 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property description "TIMisthebest : https://www.mql5.com/en/forum/9142"

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   1
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  Yellow, DodgerBlue, Red, Green
#property indicator_label1  "Z4 Open;Z4 High;Z4 Low;Z4 Close"

//--- indicator input 
enum Day_Of_Week    //--- use name of the day in your language :
  {
   Day_0,  //--- Sunday ...
   Day_1,
   Day_2,
   Day_3,
   Day_4,
   Day_5,
   Day_6,  //--- ... Saturday
  };
  
//--- input parameters
input Day_Of_Week Day_Start    = Day_4;
input int Hour_Start           = 6;
input int Minute_Start         = 0;
input Day_Of_Week Day_End      = Day_4;
input int Hour_End             = 20;
input int Minute_End           = 59;

//--- indicator buffers
double ExtOBuffer[];
double ExtHBuffer[];
double ExtLBuffer[];
double ExtCBuffer[];
double ExtColorBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtOBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtHBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,ExtLBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,ExtCBuffer,INDICATOR_DATA);
   SetIndexBuffer(4,ExtColorBuffer,INDICATOR_COLOR_INDEX);
//---
   
//--- sets first bar from what index will be drawn
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- sets text for indicator
   IndicatorSetString(INDICATOR_SHORTNAME,"Z4_ColorCandles_Daily_Hours");
//--- sets drawing line empty value
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
//--- initialization done
  }
//+------------------------------------------------------------------+
//| Heiken Ashi                                                      |
//+------------------------------------------------------------------+
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 &TickVolume[],
                const long &Volume[],
                const int &Spread[])
  {
   int i,limit;
   MqlDateTime time_now;
   datetime candle_time;
   
//--- preliminary calculations
   if(prev_calculated == 0)
     {
      //--- set first candle
      ExtLBuffer[0] = Low[0];
      ExtHBuffer[0] = High[0];
      ExtOBuffer[0] = Open[0];
      ExtCBuffer[0] = Close[0];
      limit = 1;
     }
   else limit = prev_calculated - 1;

//--- the main loop of calculations
   for(i = limit;i < rates_total && !IsStopped(); i++)
     {
      double haOpen  = Open[i];
      double haClose = Close[i];
      double haHigh  = High[i];
      double haLow   = Low[i];

      ExtLBuffer[i] = haLow;
      ExtHBuffer[i] = haHigh;
      ExtOBuffer[i] = haOpen;
      ExtCBuffer[i] = haClose;

      //--- set candle color
      candle_time = Time [i];
      TimeToStruct(candle_time, time_now);
      
    if(  time_now.day_of_week  >= Day_Start && 
         time_now.hour >= Hour_Start && 
         time_now.min >= Minute_Start && 
         
         time_now.day_of_week <= Day_End &&   
         time_now.hour <= Hour_End   && 
         time_now.min <= Minute_End  )
       { 
       ExtColorBuffer[i] = 2.0; // set color to Red
       }
      else
      {
      if (haOpen > haClose)
          ExtColorBuffer[i] = 0.0; // set color Lime
          else
          ExtColorBuffer[i] = 1.0; // set color Aqua
      }
     }
//--- done
   return(rates_total);
  }
//+------------------------------------------------------------------+
Mehrdad Shiri
4596
Mehrdad Shiri  

no it is not correct;

with change in day and time ;

the candles dose not change in color

phi nuts
2181
phi nuts  
TIMisthebest:

no it is not correct;

with change in day and time ;

the candles dose not change in color

Good then, you can begin re-writing it, and I can correct it if you want.

After all you're the only person who knows what you want.

To add comments, please log in or register