How to color code the direction of an indicator from a higher time frame

 

I am trying to code an indicator (lets say a MA for simplicity) that is calculated in higher time frame data to be displayed in a lower TF chart. For this code, I have recycle several codes available in the code base, but I am falling short to find a way to color code the direction of the line. Say blue when it goes up and red when it goes down. Does this requires to create a new buffer? I am trying to avoid using computer resources, as for some reason this code is very slow.... So are the originals I found.

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1

//--- indicator plots
#property indicator_type1   DRAW_LINE
#property indicator_color1  Red
#property indicator_width1  1
#property indicator_label1  "Average"
#property indicator_style1  STYLE_SOLID

//--- input parameters
input ENUM_TIMEFRAMES      TimeFrame=      PERIOD_H1;                    // Timeframe 2 (TF2) period
input int                  InpMaPeriod=14;                                    // MA Period

//--- indicator buffer
double                     ExtMaBuffer_TF2[];

//--- arrays TF2 - to retrieve TF 2 values of buffers and/or timeseries
double                     ExtMaArray_TF2[];             // intermediate array to hold TF2 ma buffer values

//--- variables
int                        PeriodRatio=1;                // ratio between timeframe 1 (TF1) and timeframe 2 (TF2)
int                        PeriodSeconds_TF1;            // TF1 period in seconds
int                        PeriodSeconds_TF2;            // TF2 period in seconds

//--- indicator handles TF2
int                        IndicatorHandle;              // ma handle TF2

void OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtMaBuffer_TF2,INDICATOR_DATA);

//--- set buffers as series, most recent entry at index [0]
   ArraySetAsSeries(ExtMaBuffer_TF2,true);
//--- set arrays as series, most recent entry at index [0]
   ArraySetAsSeries(ExtMaArray_TF2,true);

//--- calculate at which bar to start drawing indicators
   PeriodSeconds_TF1=PeriodSeconds();
   PeriodSeconds_TF2=PeriodSeconds(TimeFrame);

   if(PeriodSeconds_TF1<PeriodSeconds_TF2)
      PeriodRatio=PeriodSeconds_TF2/PeriodSeconds_TF1;

//---  Handle
     IndicatorHandle=iMA(NULL,TimeFrame,InpMaPeriod,0,MODE_EMA,PRICE_CLOSE);  
  }


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[])
  {
//--- set arrays as series, most recent entry at index [0]
   ArraySetAsSeries(Time,true);

//--- check for data
   int bars_TF2=Bars(NULL,TimeFrame);
   if(bars_TF2<InpMaPeriod) return(0);
      
//--- not all data may be calculated
   int calculated_TF2;
   calculated_TF2=BarsCalculated(IndicatorHandle);
   if(calculated_TF2<bars_TF2) return(0);

//--- set limit for which bars need to be (re)calculated
   int limit;
   if(prev_calculated==0 || prev_calculated<0 || prev_calculated>rates_total)
      limit=rates_total-1;
   else
      limit=rates_total-prev_calculated;

//--- create variable required to convert between TF1 and TF2
   datetime convertedTime;

//--- loop through TF1 bars to set buffer TF1 values
   for(int i=limit;i>=0;i--)
     {
      //--- convert time TF1 to nearest earlier time TF2 for a bar opened on TF2 which is to close during the current TF1 bar
      //--- use this for calculations with PRICE_CLOSE, PRICE_HIGH, PRICE_LOW, PRICE_MEDIAN, PRICE_TYPICAL, PRICE_WEIGHTED
      convertedTime=Time[i]+PeriodSeconds_TF1-PeriodSeconds_TF2;

      //--- check if TF2 data is available at convertedTime
      datetime tempTimeArray_TF2[];
      CopyTime(NULL, TimeFrame,calculated_TF2-1,1,tempTimeArray_TF2);

      //--- get ma buffer values of TF2
      if(CopyBuffer(IndicatorHandle,0,convertedTime,1,ExtMaArray_TF2)<=0)
        {
         return(0);
        }
      //--- set ma TF2 buffer on TF1
      else
         ExtMaBuffer_TF2[i]=ExtMaArray_TF2[0];
     }

//--- return value of rates_total, will be used as prev_calculated in next call
   return(rates_total);
  }

void OnDeinit(const int reason)
{
if(IndicatorHandle !=INVALID_HANDLE)  IndicatorRelease(IndicatorHandle);
}
 

You need to use DRAW_COLOR_LINE instead of DRAW_LINE if you want to achieve that.

DRAW_COLOR_LINE uses two buffers for 1 plot.

#property indicator_buffers 2
#property indicator_plots   1


#property indicator_label1  "ColorLine"
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  clrRed,clrBlue,clrGreen


//--- A buffer for plotting
double         ColorLineBuffer[];
//--- A buffer for storing the line color on each bar
double         ColorLineColors[];
int OnInit()
  {
//--- Binding an array and an indicator buffer
   SetIndexBuffer(0,ColorLineBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ColorLineColors,INDICATOR_COLOR_INDEX);

   return(INIT_SUCCEEDED);
  }


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;i++){

ColorLineBuffer[i] = ....... what data should be passed to the line

if(condition1)
        ColorLineColors[i] = 0; //first color defined in properties

if(condition2)
        ColorLineColors[i] = 1; //second color defined in properties

//and so on

}
 
Nice, thank you.