Array Wizardry and William's %R

 

Well I've helped enough noobs now that I think I'm entitled post my own noob topic. Please let it be clear that I am not asking anyone to write code for me. This is a topic for ideas.

I have written an EA that achieves a profit factor as high as 5 if I let my sloploss slide a bit, or 3 with what feels like a safer stop level.

My signals are based on William's %R, for the most part. What I would like to discuss is this: There are obvious patterns in William's %R when you want to trade the trend and not the reversal. For example, William's %R pegged beneath -80 or above -20. When that is happening you are in a trend. I have done 2 things to code an EA that can identify this behavior. They both include storing historical values of William's %R in an array. One of them looks at the maximum value of that array (not effective), the other one looks at the average of the values in the array (much more effective). At this point I'm using this array to isolate spikes in William's %R which, more times than not, are price reversals. I use the optimizer to determine the optimum length for this array, a.k.a. the number of historical values of William's %R, that I should be tracking in order to determine that I am not in a trend and that I am actually seeing a spike. I also use the optimizer to determine a threshold for this average value so I know when to trade a reversal and when to trade a trend.

I am also using both trailing SL and trailing TP that are multiples of ATR.

I'm trying to incorporate a pair of MAs and capture the rate at which they are moving away from each other or toward each other to help me identify trends vs reversals, but struggling to find a good way to do this.

So... Array Wizardry! This is where I'd like to hear some ideas about how to store the historcal values of indicators, and discern their behavior. Let's see how smart you are!

 
Anomalous:

I'm trying to incorporate a pair of MAs and capture the rate at which they are moving away from each other or toward each other to help me identify trends vs reversals, but struggling to find a good way to do this.


A good place to start would probably be MA[0] - MA[1]

Here is indicator I would like to say I coded it.... I tried to but I ran into problems with the histogram, in fact Zzuegg did most of it, it is to try and identify difference between trending and ranging using MA direction strength, with magenta bars to means not trending you might get some ideas from it

#property link      "https://www.mql5.com/en/users/sdc"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 Lime
#property indicator_color3 Magenta
#property indicator_color4 SteelBlue

extern int       LinePeriods=3;
extern int       Shift=0;
extern int       MacdFast=3;
extern int       MacdSlow=9;
//---- buffers
double do[];
double up[];
double no[];
double macd[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,1);
   SetIndexBuffer(0,do);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,1);
   SetIndexBuffer(1,up);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,1);
   SetIndexBuffer(2,no);
   SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,1);
   SetIndexBuffer(3,macd);
   
   SetIndexDrawBegin(0,MacdSlow);
   SetIndexDrawBegin(1,MacdSlow);
   SetIndexDrawBegin(2,MacdSlow);
   SetIndexDrawBegin(3,MacdSlow);
//----
   IndicatorDigits(Digits+1);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectsDeleteAll();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   double mid,ma_values[2][4];
   double idf;
   int j=0;
   for(int i=Bars-counted_bars-1; i>=0; i--)
    {
     for(j=0; j<2; j++)
      {
       ma_values[j][0]=iMA(NULL,0,LinePeriods,Shift,MODE_LWMA,PRICE_HIGH,i+j);
       ma_values[j][1]=iMA(NULL,0,LinePeriods,Shift,MODE_LWMA,PRICE_LOW,i+j);
       ma_values[j][2]=iMA(NULL,0,LinePeriods,Shift,MODE_LWMA,PRICE_HIGH,i+j);
       ma_values[j][3]=iMA(NULL,0,LinePeriods,Shift,MODE_LWMA,PRICE_LOW,i+j);
      }
//----
     macd[i]=iMA(NULL,0,MacdFast,Shift,MODE_SMA,PRICE_MEDIAN,i)-iMA(NULL,0,MacdSlow,Shift,MODE_SMA,PRICE_MEDIAN,i);
     idf=0;
     for(j=0;j<4;j++)
      {
       if(ma_values[0][j]<ma_values[1][j]) idf--;
       if(ma_values[0][j]>ma_values[1][j]) idf++;         
      }
//----
     if(idf==4)
      {up[i]=macd[i];do[i]=0;no[i]=0;
      }else
      {if(idf<=-4)
        {do[i]=macd[i];up[i]=0;no[i]=0;
        }else
        {no[i]=macd[i];up[i]=0;do[i]=0;
        }
      }
    }
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

A good place to start would probably be MA[0] - MA[1]

I was doing this, but I am using a 55 period LWMA and a 6 period LWMA. I do ( MA[0] - MA[1] ) - ( MA[1] - MA[2] ) to capture the slope of the difference between the 2 MAs. I would do this over a period of 6 bars, and then average the results giving me an average slope. I found that it didn't do much to change the performance of my EA. Like I mention above, this is done along with William's %R which I use to generate my signals. I think the problem I have is that the behavior of William's %R and the slope, as I was calculating it, are too highly correlated to compliment each other.

Thank you for posting this indicator. I'll play with it and see what sort of ideas I get. ;)

 

ok i hope you find it useful you probably want to delete the ObjectsDeletAll() from deinit() that got left in there from a previous test

Reason: