Search for a pattern within the last 20 bars

 
Hello,

I was hoping for some guidance on coding that I can't seem to figure out. Simply put, I see the problem of being able to do addition but not being able translate that into multiplication.

Here's the problem:
I need to look for a specific pattern on the Force indicator over 3 different periods in one timeframe. Now the problem is that this pattern can occur anytime in the past 20 bars for anyone period.

I have done simple crosses before by looking at the previous 2 bars to make a decision, but not anything where the condition can be met anytime within the past 20 bars. I think I've seen an indicator that did something like this and might have used an array function, but don't remember exactly.

So the indicator must remember that it has recognized the pattern and watch out for the same pattern in the subsequent periods. So the counter would start once the first pattern is recognized and then would reset after 30 bars have passed and the other periods don't create the necessary pattern.

I've attached a screen shot showing an example which has the Force Indicator withthe periods, 2, 13, and 26. I'm looking for upside down "v" formations over specific levels for a short trade. In this case the critical "v" formation on the 2 period is 350, 200 on the 13 period and between 50 and 100 on the 26 period.
I've searched the forums but can't seem to find anything that addresses this problem.

Thanks for anyone's help out there!
Mike
 
You need to do a time stamp when you first find your formation. Then count the bars after the signal occurs. Try something like this:

int TimeStamp,NewBar,Barcounter;

int init()
{
int NewBar=Time[0];
int Barcouner=0;
return(0);
}

int start()
{
if (NewBar != Time[0])
{
if(Force(26)>50 && Force(26)<100) TimeStamp=Time[0];
if(Barcounter >=20) Barcounter=0;
else {search the other periods for formations i so on....... }
NewBar = Time[0];
Barcounter=Barcounter+1;
}
return(0);
}
 

What is the definition of your "pattern" in code?

 

"in code" my pattern would look like the following (I think):

First the variables...

int 2PForceBegin = iForce(NULL, 0, 2,MODE_SMA,PRICE_CLOSE,x-3);

int 2PForceMid = iForce(NULL,0,2,MODE_SMA,PRICE_CLOSE,x-2);

int 2PForceEnd = iForce(NULL,0,2,MODE_SMA,PRICE_CLOSE,x-1);

...similar for 13 period and 26 period variable definitions...

So the main definition of whether the conditions would be met for anyone combination in one point in time would go like this...

if((2PForceBegin <=350 && 2PForceMid >350 && 2PForceEnd < 350) && (13PForceBegin <=200 && 13PForceMid >200&& 13PForceEnd < 200) && (26PForceBegin <=100 && 26PForceBegin >=50 && 26PForceMid >100 && 26PForceEnd <=100 && 26PForceEnd >=50 ) )

So the problem is that I've got 8,000 permutations (20 bars of history to look through with 3 combinations at any time) and rather than write 8,000 combination of lines that will take up a lot of processing power is there an easier way of doing it?


I hope this makes sense now.

 

Chew on this for a while:


int start(){
   int BarsToProcess = 20;
   bool PForce2, PForce13, PForce26;
   double PForce_2 [BarsToProcess+3];
   double PForce_13[BarsToProcess+3];
   double PForce_26[BarsToProcess+3];
 
   // get some values
   for(int i = BarsToProcess+2; i >= 0; i--){
      PForce_2[i]  = iForce(NULL, 0, 2, MODE_SMA,PRICE_CLOSE,i);
      PForce_13[i] = iForce(NULL, 0, 13,MODE_SMA,PRICE_CLOSE,i);
      PForce_26[i] = iForce(NULL, 0, 26,MODE_SMA,PRICE_CLOSE,i);
   }
   // Check conditions
   for( i = BarsToProcess; i >= 0; i--){

      if(  PForce_2[i+2]  < 350 && PForce_2 [i+1] > 350 && PForce_2 [i] < 350 ) PForce2  = true;

      if(  PForce_13[i+2] < 200 && PForce_13[i+1] > 200 && PForce_13[i] < 200 ) PForce13 = true;

      if( (PForce_26[i+2] <= 100   &&  PForce_26[i+2] >= 50)   &&
          (PForce_26[i+1] >  100 )                             && 
          (PForce_26[i]   <= 100   &&  PForce_26[i]   >= 50 ))                  PForce26 = true;

   }
 
   // if all conditions met
   if( Pforce2 == true && PForce13 == true && PForce26 == true){
      ... do your thing
   }
 
nice
 

Phy - thank you for your help!

I put in the code and think I understand the logic behind the method. The problem however is in compiling the code - I keep getting a pile of errors and am not quite sure why there is a problem. I've looked at the syntax in other indicators as a guide and I'm still stumped.

Can you take a peek at the code help me understand why I'm getting these errors?

Thanks!


//+------------------------------------------------------------------+
//|                                           Multiple Force Cross   |
//+------------------------------------------------------------------+
 
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 LimeGreen
#property indicator_color2 Red
 
double CrossUp[];
double CrossDown[];
 
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0, DRAW_ARROW, EMPTY,3);
   SetIndexArrow(0, 233);
   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1, DRAW_ARROW, EMPTY,3);
   SetIndexArrow(1, 234);
   SetIndexBuffer(1, CrossDown);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
  
   int BarsToProcess = 20;
   bool PForce2Buy = false, PForce13Buy = false, PForce26Buy = false;
   bool PForce2Sell = false, PForce13Sell = false, PForce26Sell = false;
   double PForce_2[BarsToProcess+3];
   double PForce_13[BarsToProcess+3];
   double PForce_26[BarsToProcess+3];
 
   // get some values
   for(int i = BarsToProcess+2; i >= 0; i--){
      double PForce_2[i]  = iForce(NULL, 0, 2, MODE_SMA,PRICE_CLOSE,i);
      double PForce_13[i] = iForce(NULL, 0, 13,MODE_SMA,PRICE_CLOSE,i);
      double PForce_26[i] = iForce(NULL, 0, 26,MODE_SMA,PRICE_CLOSE,i);
      
   }
 
   // Check conditions
   for( i = BarsToProcess; i >= 0; i--){
   
      
      if(  PForce_2[i+2]  < 350 && PForce_2[i+1] > 350 && PForce_2[i] < 350 ) PForce2Sell  = true;
 
      if(  PForce_13[i+2] < 200 && PForce_13[i+1] > 200 && PForce_13[i] < 200 ) PForce13Sell = true;
 
      if( (PForce_26[i+2] <= 100   &&  PForce_26[i+2] >= 50)   && (PForce_26[i+1] >  100 ) && (PForce_26[i]   <= 100   &&  PForce_26[i]   >= 50 )) PForce26Sell = true;
 
      if(  PForce_2[i+2]  > -350 && PForce_2[i+1] < -350 && PForce_2[i] > -350 ) PForce2Buy  = true;
 
      if(  PForce_13[i+2] > -200 && PForce_13[i+1] < -200 && PForce_13[i] > -200 ) PForce13Buy = true;
 
      if( (PForce_26[i+2] >= -100   &&  PForce_26[i+2] <= -50)   && (PForce_26[i+1] <  -100 ) && (PForce_26[i]   >= -100   &&  PForce_26[i]   <= -50 )) PForce26Buy = true;
   }
 
   // if all conditions met
   if( PForce2Buy == true && PForce13Buy == true && PForce26Buy == true){
       CrossUp[i] = Low[i] - 50*Point;
   }    
   
   else if( PForce2Sell == true && PForce13Sell == true && PForce26Sell == true){
       CrossDown[i] = High[i] + 50*Point;
      
   }
 
 
   return(0);
}
 

Two errors corrected

int start() {
  
   int BarsToProcess = 20;
   bool PForce2Buy = false, PForce13Buy = false, PForce26Buy = false;
   bool PForce2Sell = false, PForce13Sell = false, PForce26Sell = false;
   double PForce_2[];
   double PForce_13[];
   double PForce_26[];
   
   ArrayResize(PForce_2, BarsToProcess+3);
   ArrayResize(PForce_13, BarsToProcess+3);
   ArrayResize(PForce_26, BarsToProcess+3);
 
   // get some values
   for(int i = BarsToProcess+2; i >= 0; i--){
      PForce_2[i]  = iForce(NULL, 0, 2, MODE_SMA,PRICE_CLOSE,i);
      PForce_13[i] = iForce(NULL, 0, 13,MODE_SMA,PRICE_CLOSE,i);
      PForce_26[i] = iForce(NULL, 0, 26,MODE_SMA,PRICE_CLOSE,i);
      
   }
 
   // Check conditions
 

Okay - it's great that it now compiles. "Slight" problem in that it doesn't actually put any arrows on the chart - yikes! Any idea why that may be the case? I changed BarsToProcess to equal 100 in the event that it needed a longer history to review, but that didn't work either. I also added a few lines to clear the value of the arrow prior to the loop that looks for the conditions...


Any idea why it's not working?


//+------------------------------------------------------------------+
//|                                           Multiple Force Cross   |
//+------------------------------------------------------------------+
 
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 LimeGreen
#property indicator_color2 Red
 
double CrossUp[];
double CrossDown[];
 
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0, DRAW_ARROW, EMPTY,3);
   SetIndexArrow(0, 233);
   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1, DRAW_ARROW, EMPTY,3);
   SetIndexArrow(1, 234);
   SetIndexBuffer(1, CrossDown);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
  
   int BarsToProcess = 20;
   bool PForce2Buy = false, PForce13Buy = false, PForce26Buy = false;
   bool PForce2Sell = false, PForce13Sell = false, PForce26Sell = false;
   double PForce_2[];
   double PForce_13[];
   double PForce_26[];
   
   ArrayResize(PForce_2, BarsToProcess+3);
   ArrayResize(PForce_13, BarsToProcess+3);
   ArrayResize(PForce_26, BarsToProcess+3);
 
   // get some values
   for(int i = BarsToProcess+2; i >= 0; i--){
   PForce_2[i]  = iForce(NULL, 0, 2, MODE_SMA,PRICE_CLOSE,i);
   PForce_13[i] = iForce(NULL, 0, 13,MODE_SMA,PRICE_CLOSE,i);
   PForce_26[i] = iForce(NULL, 0, 26,MODE_SMA,PRICE_CLOSE,i);
 
      
   }
 
   // Check conditions
   for( i = BarsToProcess; i >= 0; i--){
   
      CrossDown[i]=EMPTY_VALUE;
      CrossUp[i]=EMPTY_VALUE;
      
      if(  PForce_2[i+2]  < 350 && PForce_2[i+1] > 350 && PForce_2[i] < 350 ) PForce2Sell  = true;
 
      if(  PForce_13[i+2] < 200 && PForce_13[i+1] > 200 && PForce_13[i] < 200 ) PForce13Sell = true;
 
      if( (PForce_26[i+2] <= 100   &&  PForce_26[i+2] >= 50)   && (PForce_26[i+1] >  100 ) && (PForce_26[i]   <= 100   &&  PForce_26[i]   >= 50 )) PForce26Sell = true;
 
      if(  PForce_2[i+2]  > -350 && PForce_2[i+1] < -350 && PForce_2[i] > -350 ) PForce2Buy  = true;
 
      if(  PForce_13[i+2] > -200 && PForce_13[i+1] < -200 && PForce_13[i] > -200 ) PForce13Buy = true;
 
      if( (PForce_26[i+2] >= -100   &&  PForce_26[i+2] <= -50)   && (PForce_26[i+1] <  -100 ) && (PForce_26[i]   >= -100   &&  PForce_26[i]   <= -50 )) PForce26Buy = true;
   }
 
   // if all conditions met
   if( PForce2Buy == true && PForce13Buy == true && PForce26Buy == true){
       CrossUp[i] = Low[i] - 25*Point;
   }    
   
   else if( PForce2Sell == true && PForce13Sell == true && PForce26Sell == true){
       CrossDown[i] = High[i] + 25*Point;
      
   }
 
 
   return(0);
}
 

Well... debug time...

// if all conditions met
if ( PForce2Buy == true && PForce13Buy == true && PForce26Buy == true ){
Print("Found a CROSS UP");
CrossUp [ i ] = Low [ i ] - 25 * Point ;
}

else if ( PForce2Sell == true && PForce13Sell == true && PForce26Sell == true ){
Print("Found a CROSS DOWN");
CrossDown [ i ] = High [ i ] + 25 * Point ;

}

if no print, the conditions are not being met.

But more to the point...

What is the value of "i" when you assign your arrows?

 

Well, my thought was an arrow would be placed on the close of the candle where all conditions were satisfied and I think I'm asking the indicator to place the arrow 25 pips below the low of that candle for a buy and 25 pips above the high for a sell.

The thing is I've tried cranking up the BarsToProcess to 1,000 and know that all conditions have been met but still, no arrows...

Hmmm...

Reason: