MTF Trend-Magic

 

I tried to convert the trend-magic indicator to a MTF indicator but it does not work correcty.

I know a little about coding EA's but nothing about coding Indicators so I need a little help.

Here is the code below:

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_width1 2
#property indicator_color2 Red
#property indicator_width2 2

//+------------------------------------------------------------------+

extern int CCI = 50;
extern int ATR = 5;
extern ENUM_TIMEFRAMES TF = PERIOD_H1;


//+------------------------------------------------------------------+

double bufferUp[];
double bufferDn[];

//+------------------------------------------------------------------+

int init()
{
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexBuffer(0, bufferUp);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexBuffer(1, bufferDn);
   return (0);
}

//+------------------------------------------------------------------+

int deinit()
{
   return (0);
}

//+------------------------------------------------------------------+

int start()
{
   double thisCCI;
   double lastCCI;

   int counted_bars = IndicatorCounted();
   if (counted_bars < 0) return (-1);
   if (counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;

   for (int shift = limit; shift >= 0; shift--)
   {
      thisCCI = iCCI(NULL, TF, CCI, PRICE_TYPICAL, shift);
      lastCCI = iCCI(NULL, TF, CCI, PRICE_TYPICAL, shift + 1);

      if (thisCCI >= 0 && lastCCI < 0) bufferUp[shift + 1] = bufferDn[shift + 1];
      if (thisCCI <= 0 && lastCCI > 0) bufferDn[shift + 1] = bufferUp[shift + 1];

      if (thisCCI >= 0)
      {
         bufferUp[shift] = Low[shift] - iATR(NULL, TF, ATR, shift);
         if (bufferUp[shift] < bufferUp[shift + 1])
            bufferUp[shift] = bufferUp[shift + 1];
      }
      else
      {
         if (thisCCI <= 0)
         {
            bufferDn[shift] = High[shift] + iATR(NULL, TF, ATR, shift);
            if (bufferDn[shift] > bufferDn[shift + 1])
               bufferDn[shift] = bufferDn[shift + 1];
         }
      }
   }
   

   return (0);
}
 
      thisCCI = iCCI(NULL, TF, CCI, PRICE_TYPICAL, shift);

You are mixing apples and oranges.

 
William Roeder #:

You are mixing apples and oranges.

William,

Could you perhaps be a little more explicit?

I would really appreciate it!

 
Ernest Klokow #:

William,

Could you perhaps be a little more explicit?

I would really appreciate it!

   int y = iBarShift(NULL,timeFrame,Time[shift]);

   double cci = iCCI(NULL,timeFrame,Length,Price,y); 


Using the above example from a multi-timeframe CCI indicator, you need to  substitute the 'shift' variable occurances with 'y' variable to get the chart timeframe offset correctly.

You can research iBarShift function.   It finds the right bar time offset for it to align with another timeframe.

 
maximo #:


Using the above example from a multi-timeframe CCI indicator, you need to  substitute the 'shift' variable occurances with 'y' variable to get the chart timeframe offset correctly.

You can research iBarShift function.   It finds the right bar time offset for it to align with another timeframe.

Thank you so much!

Greatly appreciated!

 
Ernest Klokow #: Could you perhaps be a little more explicit?

Do you see those underlined words? They are links, clicking on them takes you to more information.

 

William sorry about not seeing the links - I guess the oranges and apples words made me think you were only referring to a well known saying.

I have in my ignorant state tried to adapt the code with the info you and  gave me.

Unfortunately I am still doing something wrong.

If you can help me to fix this code properly I am prepared to pay the fee you may charge me in USD. 

This is what the code looks like at the moment:

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_width1 2
#property indicator_color2 Red
#property indicator_width2 2

//+------------------------------------------------------------------+

extern int CCI = 50;
extern int ATR = 5;
extern ENUM_TIMEFRAMES TF = PERIOD_H1;


//+------------------------------------------------------------------+

double bufferUp[];
double bufferDn[];

//+------------------------------------------------------------------+

int init()
{
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexBuffer(0, bufferUp);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexBuffer(1, bufferDn);
   return (0);
}

//+------------------------------------------------------------------+

int deinit()
{
   return (0);
}

//+------------------------------------------------------------------+

int start()
{
   double thisCCI;
   double lastCCI;

   int counted_bars = IndicatorCounted();
   if (counted_bars < 0) return (-1);
   if (counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   
   
   for (int i = limit; i >= 0; i--)
   {
      int y = iBarShift(NULL,TF,Time[i]);

      thisCCI = iCCI(NULL, TF, CCI, PRICE_TYPICAL, y);
      lastCCI = iCCI(NULL, TF, CCI, PRICE_TYPICAL, y + 1);

      if (thisCCI >= 0 && lastCCI < 0) bufferUp[y + 1] = bufferDn[y + 1];
      if (thisCCI <= 0 && lastCCI > 0) bufferDn[y + 1] = bufferUp[y + 1];

      if (thisCCI >= 0)
      {
         bufferUp[y] = Low[y] - iATR(NULL, TF, ATR, y);
         if (bufferUp[y] < bufferUp[y + 1])
            bufferUp[y] = bufferUp[y + 1];
      }
      else
      {
         if (thisCCI <= 0)
         {
            bufferDn[y] = High[y] + iATR(NULL, TF, ATR, y);
            if (bufferDn[y] > bufferDn[y + 1])
               bufferDn[y] = bufferDn[y + 1];
         }
      }
   }
   

   return (0);
}
 
Ernest Klokow #: Unfortunately I am still doing something wrong.
      int y = iBarShift(NULL,TF,Time[i]);

      thisCCI = iCCI(NULL, TF, CCI, PRICE_TYPICAL, y);
      lastCCI = iCCI(NULL, TF, CCI, PRICE_TYPICAL, y + 1);

      if (thisCCI >= 0 && lastCCI < 0) bufferUp[y + 1] = bufferDn[y + 1];
      if (thisCCI <= 0 && lastCCI > 0) bufferDn[y + 1] = bufferUp[y + 1];

      if (thisCCI >= 0)
      {
         bufferUp[y] = Low[y] - iATR(NULL, TF, ATR, y);

"y" is now the other timeframe (TF) index. Your buffers (bufferUp), and predefined arrays (Low) are the chart's timeframe. You are still mixing.

 
William Roeder #:

"y" is now the other timeframe (TF) index. Your buffers (bufferUp), and predefined arrays (Low) are the chart's timeframe. You are still mixing.

I see what you mean William (I think!)

Will this change do the trick? 

      datetime time = Time[i];
      int y = iBarShift(NULL, TF, time);
 

Seems like the code below is still mixing things up! I give up!

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_width1 2
#property indicator_color2 Red
#property indicator_width2 2

//+------------------------------------------------------------------+

extern int CCI = 50;
extern int ATR = 5;
extern ENUM_TIMEFRAMES TF = PERIOD_H1;


//+------------------------------------------------------------------+

double bufferUp[];
double bufferDn[];

//+------------------------------------------------------------------+

int init()
{
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexBuffer(0, bufferUp);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexBuffer(1, bufferDn);
   return (0);
}

//+------------------------------------------------------------------+

int deinit()
{
   return (0);
}

//+------------------------------------------------------------------+

int start()
{
   double thisCCI;
   double lastCCI;

   int counted_bars = IndicatorCounted();
   if (counted_bars < 0) return (-1);
   if (counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   
   
   for (int i = limit; i >= 0; i--)
   {
      datetime time = Time[i];
      int y = iBarShift(NULL, TF, time);

      thisCCI = iCCI(NULL, TF, CCI, PRICE_TYPICAL, y);
      lastCCI = iCCI(NULL, TF, CCI, PRICE_TYPICAL, y + 1);

      if (thisCCI >= 0 && lastCCI < 0) bufferUp[y + 1] = bufferDn[y + 1];
      if (thisCCI <= 0 && lastCCI > 0) bufferDn[y + 1] = bufferUp[y + 1];

      if (thisCCI >= 0)
      {
         bufferUp[y] = Low[y] - iATR(NULL, TF, ATR, y);
         if (bufferUp[y] < bufferUp[y + 1])
            bufferUp[y] = bufferUp[y + 1];
      }
      else
      {
         if (thisCCI <= 0)
         {
            bufferDn[y] = High[y] + iATR(NULL, TF, ATR, y);
            if (bufferDn[y] > bufferDn[y + 1])
               bufferDn[y] = bufferDn[y + 1];
         }
      }
   }
   

   return (0);
}
 
Ernest Klokow #: Will this change do the trick? 
Your new code
      datetime time = Time[i];
      int y = iBarShift(NULL, TF, time);
It is exactly the same as what you had.
It changes nothing.
      int y = iBarShift(NULL,TF,Time[i]);

I previously said “Your buffers (bufferUp), and predefined arrays (Low) are the chart's timeframe.” Did you ignore it, not understand it, or was it not clear? Go back and read the links provided.

Reason: