Search for a formation over many 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 30 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 x 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.

Thanks for anyone's help out there!

Mike

Files:
 

Hello, anyone....Bueller?

 

Well I was able to get a response from someone in the mql4 forum but the indicator doesn't paint arrows on the chart - and they haven't been available to provide anymore help.

Can someone take a look at the - I think this type of event tracking indicator can be very useful for others and that it could be adapted to your favorite.

Newdigital - not sure if this thread should stay here or be moved to the 'indicator' posts.

Thanks,

Mike

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

//| Multiple Force Cross |

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

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 LimeGreen

#property indicator_color2 Red

double CrossUp[];

double CrossDown[];

extern int BarsToProcess = 20;

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

//| 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 = 1000;

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--){

//for (int i = BarsToProcess+2; i >= 0; i++) {

PForce_2 = iForce(NULL, 0, 2, MODE_SMA,PRICE_CLOSE,i);

PForce_13 = iForce(NULL, 0, 13,MODE_SMA,PRICE_CLOSE,i);

PForce_26 = iForce(NULL, 0, 26,MODE_SMA,PRICE_CLOSE,i);

// Check conditions

for( i = BarsToProcess; i >= 0; i--){

//for (i = BarsToProcess; i >= 0; i++) {

CrossDown=EMPTY_VALUE;

CrossUp=EMPTY_VALUE;

if( PForce_2 350 && PForce_2 < 350 ) PForce2Sell = true;

if( PForce_13 200 && PForce_13 < 200 ) PForce13Sell = true;

if( (PForce_26 = 50) && (PForce_26 > 100 ) && (PForce_26 = 50 )) PForce26Sell = true;

if( PForce_2 > -350 && PForce_2 -350 ) PForce2Buy = true;

if( PForce_13 > -200 && PForce_13 -200 ) PForce13Buy = true;

if( (PForce_26 >= -100 && PForce_26 <= -50) && (PForce_26 = -100 && PForce_26 <= -50 )) PForce26Buy = true;

// if all conditions met

if( PForce2Buy == true && PForce13Buy == true && PForce26Buy == true) CrossUp = Low - 25*Point;

if( PForce2Sell == true && PForce13Sell == true && PForce26Sell == true) CrossDown = High + 25*Point;

}

}

return(0);

}
Files:
 

thanks for sharing it.

 
vladv:
thanks for sharing it.

No problem, but the code still needs work to make it viable...

 
 
mikep:
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 x 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.

It's not really that hard, just use the same code that you use for the 2 bar verification and just loop back 30 days and verify the condition for every of those X days.

this sounds quite an interesting indicator so I might take a look at coding it later, the most important things are the exact values on how high the peak should be on each indicator.

 
 
mikkom:
It's not really that hard, just use the same code that you use for the 2 bar verification and just loop back 30 days and verify the condition for every of those X days. this sounds quite an interesting indicator so I might take a look at coding it later, the most important things are the exact values on how high the peak should be on each indicator.

Hi mikkom,

You're right, I could create 30 pair of 'or' statements per indicator period, but I was hoping the coding could be done cleaner so that I could begin running tests looking for optimized levels and the amount of time to review.

That's why I alluded to this being a problem similar to expressing it in additional form vs. multiplication.

 
mikep:
Hi mikkom,

You're right, I could create 30 pair of 'or' statements per indicator period, but I was hoping the coding could be done cleaner so that I could begin running tests looking for optimized levels and the amount of time to review.

That's why I alluded to this being a problem similar to expressing it in additional form vs. multiplication.

I didn't mean 30 ors at all, more like (pseudocode follows..)

/** 1st indicator / timeframe **/

found = false;

for(int i=0; i<30; i++) {

// Check for current time +i

if(pattern found) {

found=true;

break;

}

}

/** 2nd indicator **/

found2 = false;

for(int i=0; i<30; i++) {

// Check for current time +i

if(pattern found at current time +i) {

found2=true;

break;

}

}

/** 3rd indicator **/

found3 = false;

for(int i=0; i<30; i++) {

// Check for current time +i

if(pattern found) {

found3=true;

break;

}

}

bool allPatternsFound = found && found2 && found3;

Something like that.. (by using arrays as cache it will be faster but the idea is the same)

(ps. just running annoyingly long optimization to find some more good probabilities for my little EA, 4 more hours to go... booring)

Reason: