How do i change this indicator code to make the HTF candle color based on bull or bear candle?

 

https://www.mql5.com/en/code/39683

this is an awesome indi for my strat as i can use these HTF candles over the top of my LTF chart
However i need to know whether the HTF candle is Bull or Bear ... and this just paints it all red

i think i need to change these but im not sure what exactly i need to change.....

Full code is below with author's details


//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed;clrLime
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//+------------------------------------------------------------------+
//| Create rectangle by the given coordinates                        |
//+------------------------------------------------------------------+
bool RectangleCreate(const long            chart_ID=0,        // chart's ID
                     const string          name="Rectangle",  // rectangle name
                     const int             sub_window=0,      // subwindow index
                     datetime              time1=0,           // first point time
                     double                price1=0,          // first point price
                     datetime              time2=0,           // second point time
                     double                price2=0,          // second point price
                     const color           clr=clrRed,        // rectangle color
                     const ENUM_LINE_STYLE style=STYLE_SOLID, // style of rectangle lines
                     const int             width=1,           // width of rectangle lines
                     const bool            fill=false,        // filling rectangle with color
                     const bool            back=false,        // in the background
                     const bool            selection=false,   // highlight to move
                     const bool            hidden=true,       // hidden in the object list
                     const long            z_order=0)         // priority for mouse click
  {
//--- set anchor points' coordinates if they are not set
   ChangeRectangleEmptyPoints(time1,price1,time2,price2);
//--- reset the error value
   ResetLastError();
//--- create a rectangle by the given coordinates
   if(!ObjectCreate(chart_ID,name,OBJ_RECTANGLE,sub_window,time1,price1,time2,price2))
     {
      Print(__FUNCTION__,
            ": failed to create a rectangle! Error code = ",GetLastError());
      return(false);
     }
//--- set rectangle color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set the style of rectangle lines
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- set width of the rectangle lines
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
//--- enable (true) or disable (false) the mode of filling the rectangle
   ObjectSetInteger(chart_ID,name,OBJPROP_FILL,fill);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of highlighting the rectangle for moving
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,true);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);
  }




//+------------------------------------------------------------------+
//|                                Three Candles Other TimeFrame.mq5 |
//|                              Copyright © 2022, Vladimir Karputov |
//|                      https://www.mql5.com/en/users/barabashkakvn |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2022, Vladimir Karputov"
#property link      "https://www.mql5.com/en/users/barabashkakvn"
#property version   "1.000"
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input    ENUM_TIMEFRAMES   Inp_period_other  = PERIOD_M5;   // Other timeframe
//---
datetime m_prev_bars    = 0;        // "0" -> D'1970.01.01 00:00';
string   m_prefix       = "TCOTF ";
bool     m_init_error   = false;    // error on InInit
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(Inp_period_other==PERIOD_CURRENT || Inp_period_other<Period())
     {
      string err_text="'Other timeframe' cannot be less or equal ('<=') of the current timeframe!";
      if(MQLInfoInteger(MQL_TESTER)) // when testing, we will only output to the log about incorrect input parameters
         Print(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);
      else // if the Expert Advisor is run on the chart, tell the user about the error
         Alert(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);
      //---
      m_init_error=true;
      return(INIT_SUCCEEDED);
     }
   long chart_id=ChartID();
   RectangleCreate(chart_id,m_prefix+"#0");
   RectangleCreate(chart_id,m_prefix+"#1");
   RectangleCreate(chart_id,m_prefix+"#2");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Indicator deinitialization function                              |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   ObjectsDeleteAll(ChartID(),m_prefix,0,OBJ_RECTANGLE);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
   if(m_init_error)
      return(0);
//---
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int start_pos=0,count=3;
   if(CopyRates(Symbol(),Inp_period_other,start_pos,count,rates)!=count)
      return(0);
//--- main loop
   long chart_id=ChartID();
   datetime time_0=time[rates_total-1];
   if(time_0==m_prev_bars)
     {
      //--- move candle #0
      RectanglePointChange(chart_id,m_prefix+"#0",0,rates[0].time,rates[0].high);
      RectanglePointChange(chart_id,m_prefix+"#0",1,time[rates_total-1],rates[0].low);
      //---
      return(rates_total);
     }
   m_prev_bars=time_0;
//--- move candle #0, #1 and #2
   RectanglePointChange(chart_id,m_prefix+"#0",0,rates[0].time,rates[0].high);
   RectanglePointChange(chart_id,m_prefix+"#0",1,time[rates_total-1],rates[0].low);
  
   RectanglePointChange(chart_id,m_prefix+"#1",0,rates[1].time,rates[1].high);
   RectanglePointChange(chart_id,m_prefix+"#1",1,rates[0].time-PeriodSeconds(),rates[1].low);
  
   RectanglePointChange(chart_id,m_prefix+"#2",0,rates[2].time,rates[2].high);
   RectanglePointChange(chart_id,m_prefix+"#2",1,rates[1].time-PeriodSeconds(),rates[2].low);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Create rectangle by the given coordinates                        |
//+------------------------------------------------------------------+
bool RectangleCreate(const long            chart_ID=0,        // chart's ID
                     const string          name="Rectangle",  // rectangle name
                     const int             sub_window=0,      // subwindow index
                     datetime              time1=0,           // first point time
                     double                price1=0,          // first point price
                     datetime              time2=0,           // second point time
                     double                price2=0,          // second point price
                     const color           clr=clrRed,        // rectangle color
                     const ENUM_LINE_STYLE style=STYLE_SOLID, // style of rectangle lines
                     const int             width=1,           // width of rectangle lines
                     const bool            fill=false,        // filling rectangle with color
                     const bool            back=false,        // in the background
                     const bool            selection=false,   // highlight to move
                     const bool            hidden=true,       // hidden in the object list
                     const long            z_order=0)         // priority for mouse click
  {
//--- set anchor points' coordinates if they are not set
   ChangeRectangleEmptyPoints(time1,price1,time2,price2);
//--- reset the error value
   ResetLastError();
//--- create a rectangle by the given coordinates
   if(!ObjectCreate(chart_ID,name,OBJ_RECTANGLE,sub_window,time1,price1,time2,price2))
     {
      Print(__FUNCTION__,
            ": failed to create a rectangle! Error code = ",GetLastError());
      return(false);
     }
//--- set rectangle color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set the style of rectangle lines
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- set width of the rectangle lines
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
//--- enable (true) or disable (false) the mode of filling the rectangle
   ObjectSetInteger(chart_ID,name,OBJPROP_FILL,fill);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of highlighting the rectangle for moving
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,true);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);
  }
//+------------------------------------------------------------------+
//| Move the rectangle anchor point                                  |
//+------------------------------------------------------------------+
bool RectanglePointChange(const long   chart_ID=0,       // chart's ID
                          const string name="Rectangle", // rectangle name
                          const int    point_index=0,    // anchor point index
                          datetime     time=0,           // anchor point time coordinate
                          double       price=0)          // anchor point price coordinate
  {
//--- if point position is not set, move it to the current bar having Bid price
   if(!time)
      time=TimeCurrent();
   if(!price)
      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);
//--- reset the error value
   ResetLastError();
//--- move the anchor point
   if(!ObjectMove(chart_ID,name,point_index,time,price))
     {
      Print(__FUNCTION__,
            ": failed to move the anchor point! Error code = ",GetLastError());
      return(false);
     }
//--- successful execution
   return(true);
  }

//+------------------------------------------------------------------+
//| Check the values of rectangle's anchor points and set default    |
//| values for empty ones                                            |
//+------------------------------------------------------------------+
void ChangeRectangleEmptyPoints(datetime &time1,double &price1,
                                datetime &time2,double &price2)
  {
//--- if the first point's time is not set, it will be on the current bar
   if(!time1)
      time1=TimeCurrent();
//--- if the first point's price is not set, it will have Bid value
   if(!price1)
      price1=SymbolInfoDouble(Symbol(),SYMBOL_BID);
//--- if the second point's time is not set, it is located 9 bars left from the second one
   if(!time2)
     {
      //--- array for receiving the open time of the last 10 bars
      datetime temp[10];
      CopyTime(Symbol(),Period(),time1,10,temp);
      //--- set the second point 9 bars left from the first one
      time2=temp[0];
     }
//--- if the second point's price is not set, move it 300 points lower than the first one
   if(!price2)
      price2=price1-300*SymbolInfoDouble(Symbol(),SYMBOL_POINT);
  }
//+------------------------------------------------------------------+
Three Candles Other TimeFrame
Three Candles Other TimeFrame
  • www.mql5.com
Three candles from another timeframe are drawn using graphical objects
 
Your topic has been moved to the section: Technical Indicators — In the future, please consider which section is most appropriate for your query.
 
How did you get it to work, am I missing something?
 
Tobias Johannes Zimmer #:
How did you get it to work, am I missing something?

what are you missing?
I was asking how to change the indicator color
i have no idea what i am doing ha ha
That code is about 5 levels too high for me to understand...
The indi works great... i can copy code and compile it
I just wanted the HTF candles to reflect the color of the direction they are
Eg.. the 2nd one here to be Green not red

 

 
pebs85 #:

what are you missing?
I was asking how to change the indicator color
i have no idea what i am doing ha ha
That code is about 5 levels too high for me to understand...
The indi works great... i can copy code and compile it
I just wanted the HTF candles to reflect the color of the direction they are
Eg.. the 2nd one here to be Green not red

 

Okay, does it show the rectangles right away or only after some time?

Edit: You would need a logic for open and close to set the color according to the rise and fall of the candle. Right now all you have are high and low. 

But you can see where in the code the color is set. For starters you can set fill to true so you have a colored areas.


Edit: Oh I get it now, it seems to work only in TF 1Min...

 
Tobias Johannes Zimmer #:
Okay, does it show the rectangles right away or only after some time?

Edit: You would need a logic for open and close to set the color according to the rise and fall of the candle. Right now all you have are high and low. 

But you can see where in the code the color is set. For starters you can set fill to true so you have a colored areas.


Edit: Oh I get it now, it seems to work only in TF 1Min...

if does show up as its going and as the new candles are being printed.
i think u need to be in a lower timeframe than the HTF setting you choose...
Eg. u need to be in 1m or 5min chart to show 15min HTF candles...
or u need to be in 15min or 1hr chart to show daily HTF candles.

I see what u mean by the logic..
so it needs to know open > close is red rectangle and close > open is green rectangle
as high and low is always the same regardless of whether the candle is bullish or bearish.. a high is always a high and a low is always a low

thanks

 
pebs85 #:


I see what u mean by the logic..
so it needs to know open > close is red rectangle and close > open is green rectangle
as high and low is always the same regardless of whether the candle is bullish or bearish.. a high is always a high and a low is always a low

thanks

My pleasure
Reason: