stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

ATR with MA
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=511
Page 1 of 1
Author:  dynel14 [ Thu Apr 26, 2012 12:20 am ]
Post subject:  ATR with MA

Hi community,

hope you all are doing well...!?
I have a request: Is it possible to make a histogram version of this indicator?
ATR > MA = green
ATR = MA = yellow
ATR < MA = red

I would really appreciate your help!!!
Thank you in advance
Author:  dynel14 [ Sun Apr 29, 2012 8:54 pm ]
Post subject:  Re: ATR with MA

Hmm.. :(

I tried to do it, but it doesn't work. Maybe someone can fix it?

THANKS!

Code: Select all

//+------------------------------------------------------------------+
//|                                                  ATR with MA.mq4 |
//|                                                       MetaQuotes |
//|                                                                  |
//+------------------------------------------------------------------+


#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 LimeGreen
#property indicator_color2 Crimson
#property indicator_color3 Yellow
#property indicator_width1 4
#property indicator_width2 4
#property indicator_width3 4
//---- input parameters
extern int       ATRper=14;
extern int       MAPeriod=14;
extern string    c="Method: 0=SMA, 1=EMA, 2=SMMA, 3=LWMA";
extern int       MAMethod=1;

//---- buffers
double TrendUP[];
double TrendDown[];
double TrendFlat[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   string type;
   switch(MAMethod)
   {
   case MODE_EMA: type="EMA";break;
   case MODE_SMMA: type="SMMA";break;
   case MODE_LWMA: type="LWMA";break;
   default: type="SMA";MAMethod=0;break; //
   }

   IndicatorBuffers(5);
   IndicatorDigits(Digits);
   SetIndexBuffer(0, TrendUP);
   SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexBuffer(1, TrendDown);
   SetIndexStyle(1, DRAW_HISTOGRAM);
   SetIndexBuffer(2, TrendFlat);
   SetIndexStyle(2, DRAW_HISTOGRAM);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int i, iNewBars, iCountedBars;
   double ExtMapBuffer1[];
   double ExtMapBuffer2[];

   // Get unprocessed ticks
   iCountedBars = IndicatorCounted();
   if(iCountedBars < 0) return(-1);
   if(iCountedBars > 0) iCountedBars--;
   iNewBars = Bars - iCountedBars; 

   for(i = 0; i < iNewBars ; i++) {
     ExtMapBuffer1=iATR(NULL,0,ATRper,i);
     ExtMapBuffer2=iMAOnArray(ExtMapBuffer1,0,MAPeriod,0,MAMethod,i);


   if( ExtMapBuffer1 < ExtMapBuffer2 ) {
         TrendUP[i] = 1;
         TrendDown[i] = 0;
         TrendFlat[i] = 0;
     }
     else if( ExtMapBuffer1 > ExtMapBuffer2 ) {
         TrendUP[i] = 0;
         TrendDown[i] = 1;
         TrendFlat[i] = 0;
     }
     else {
         TrendUP[i] = 0;
         TrendDown[i] = 0;
         TrendFlat[i] = 1;
     }
   return(0);
 }
}
//+------------------------------------------------------------------+
Author:  garyfritz [ Sun Apr 29, 2012 11:52 pm ]
Post subject:  Re: ATR with MA

You have a couple of errors. You declare ExtMapBuffer1/2 as arrays, but not pre-allocated arrays. Display buffers are automagically allocated by Empty4. For your own buffers, you have to allocate the space. You can either allocate a fixed array, e.g. "double ExtMapBuffer1[2048];", or you can resize the array to the size you want: "double ExtMapBuffer1[]; ArrayResize(ExtMapBuffer1, Bars);" or similar.

Then, although you declared them as arrays, you assigned into them like a single value, without an indexer.

It's also not necessary for ExtMapBuffer2 to be an array. You need the ExtMapBuffer1 array because you take the iMAOnArray of it, but the MA result gets used and discarded. So it can be a simple double.

You also made a very understandable mistake -- because Empty4's definition is braindead. In bar data, the [0] bar is the newest, rightmost bar. So if you do an iMA on it, it includes bar i and the bars with indexes GREATER THAN i. (A 3-bar SMA would include bar i, i+1, i+2 -- i+1 is "older" than i.) But normal arrays are indexed "left-to-right," with [0] being the "oldest." So an iMAOnArray SMA includes bar i, i-1, and i-2. You need to SetArrayAsSeries to make Empty4 treat the array like bar data, reversing the order that iMAOnArray access it, AND you need to scan i from oldest to newest (iNewBars to 0) so the older ATR data is in the ExtMapBuffer1 array for iMAOnArray to access.

Finally, your "return(0)" was INSIDE your i loop! Whoops!

So I think the attached does what you wanted. You won't see many (any?) yellow bars, because the two values will almost never be exactly equal. If you really want yellow bars for "equal" values, you'll have to do something like "if (MathAbs(x-y) < 0.00001) then ..." instead of using ==.

I don't guarantee this code is done the "right way." I just figured out some of these mysteries myself last week, thanks to sq!

Gary


Code: Select all

    //+------------------------------------------------------------------+
    //|                                                  ATR with MA.mq4 |
    //|                                                       MetaQuotes |
    //|                                                                  |
    //+------------------------------------------------------------------+


    #property indicator_separate_window
    #property indicator_buffers 3
    #property indicator_color1 LimeGreen
    #property indicator_color2 Crimson
    #property indicator_color3 Yellow
    #property indicator_width1 4
    #property indicator_width2 4
    #property indicator_width3 4
    //---- input parameters
    extern int       ATRper=14;
    extern int       MAPeriod=14;
    extern string    c="Method: 0=SMA, 1=EMA, 2=SMMA, 3=LWMA";
    extern int       MAMethod=1;

    //---- buffers
    double TrendUP[];
    double TrendDown[];
    double TrendFlat[];
    //+------------------------------------------------------------------+
    //| Custom indicator initialization function                         |
    //+------------------------------------------------------------------+
    int init()
      {
    //---- indicators
       string type;
       switch(MAMethod)
       {
       case MODE_EMA: type="EMA";break;
       case MODE_SMMA: type="SMMA";break;
       case MODE_LWMA: type="LWMA";break;
       default: type="SMA";MAMethod=0;break; //
       }

       IndicatorBuffers(5);
       IndicatorDigits(Digits);
       SetIndexBuffer(0, TrendUP);
       SetIndexStyle(0, DRAW_HISTOGRAM);
       SetIndexBuffer(1, TrendDown);
       SetIndexStyle(1, DRAW_HISTOGRAM);
       SetIndexBuffer(2, TrendFlat);
       SetIndexStyle(2, DRAW_HISTOGRAM);

    //----
       return(0);
      }
    //+------------------------------------------------------------------+
    //| Custom indicator deinitialization function                       |
    //+------------------------------------------------------------------+
    int deinit()
      {
    //----
       
    //----
       return(0);
      }
    //+------------------------------------------------------------------+
    //| Custom indicator iteration function                              |
    //+------------------------------------------------------------------+
    int start() {
       int i, iNewBars, iCountedBars;
       double ExtMapBuffer1[];
       double MAval;

       // Get unprocessed ticks
       iCountedBars = IndicatorCounted();
       if(iCountedBars < 0) return(-1);
       if(iCountedBars > 0) iCountedBars--;
       iNewBars = Bars - iCountedBars;

       ArrayResize(ExtMapBuffer1, Bars);
       ArraySetAsSeries(ExtMapBuffer1, true);
       int cnt = 0; Print("iNewBars = ",iNewBars);
       for(i = iNewBars; i >= 0; i--) {
         ExtMapBuffer1[i]=iATR(NULL,0,ATRper,i);
         MAval=iMAOnArray(ExtMapBuffer1,0,MAPeriod,0,MAMethod,i);
cnt++; if (cnt < 15) Print("iNewBars = ",iNewBars,", i = ",i,", buffer = ",ExtMapBuffer1[i],", MAval = ",MAval);

       if( ExtMapBuffer1[i] < MAval ) {
             TrendUP[i] = 1;
             TrendDown[i] = 0;
             TrendFlat[i] = 0;
         }
         else if( ExtMapBuffer1[i] > MAval ) {
             TrendUP[i] = 0;
             TrendDown[i] = 1;
             TrendFlat[i] = 0;
         }
         else {
             TrendUP[i] = 0;
             TrendDown[i] = 0;
             TrendFlat[i] = 1;
         }
       }
       return(0);
    }
    //+------------------------------------------------------------------+
Author:  dynel14 [ Mon Apr 30, 2012 12:12 am ]
Post subject:  Re: ATR with MA

Thank you very much :!:

Will try to understand all my mistakes. I have no clue about coding. I have just started to learn it.

Thanks again :!:
All times are UTC Page 1 of 1