• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 10:54pm
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 10:54pm
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Counting Bars 1 reply

Number of bars between two past crossovers 8 replies

Number of trades before counting consistency 9 replies

need help for counting bars back 2 replies

Delete Order After a Number of Bars have Past. 3 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
Tags: Counting number of past bars since price pattern occurrence
Cancel

Counting number of past bars since price pattern occurrence

  • Post #1
  • Quote
  • First Post: Jan 31, 2015 4:15am Jan 31, 2015 4:15am
  •  Chorlton
  • | Joined Jul 2010 | Status: Member | 211 Posts
Hello All,

With regard to MQL4, I've trying to find out a way to count the number of bars on the chart that have passed since a particular price movement/pattern has occurred.

As a simple example, how would I count the number of bars that has occurred since price last gapped up?

I've been reading through the documentation but can't seem to find an appropriate function to use. In my old amibroker days, I would have used the AFL function, barssince.

Any help would be welcome,

Thanks,

Chorlton
  • Post #2
  • Quote
  • Jan 31, 2015 1:05pm Jan 31, 2015 1:05pm
  •  gravitist
  • | Joined Aug 2014 | Status: Member | 639 Posts
Sounds like you're looking into Markov chains as a potential trading strategy.
 
 
  • Post #3
  • Quote
  • Feb 1, 2015 2:15am Feb 1, 2015 2:15am
  •  Chorlton
  • | Joined Jul 2010 | Status: Member | 211 Posts
Just thought I'd bumped this post up in case anyone may be able to help....

Quoting Chorlton
Disliked
Hello All, With regard to MQL4, I've trying to find out a way to count the number of bars on the chart that have passed since a particular price movement/pattern has occurred. As a simple example, how would I count the number of bars that has occurred since price last gapped up? I've been reading through the documentation but can't seem to find an appropriate function to use. In my old amibroker days, I would have used the AFL function, barssince. Any help would be welcome, Thanks, Chorlton
Ignored
 
 
  • Post #4
  • Quote
  • Feb 1, 2015 1:40pm Feb 1, 2015 1:40pm
  •  honestknave
  • Joined Nov 2014 | Status: Member | 1,300 Posts
It certainly can be done, but the methodology will depend on your application.

The current bar always has an index of 0. The previous bar is 1, the bar before that is 2 etc.
So the bar index will always tell you how long ago it happened... e.g. Bar 43 happened 43 bars ago.
This numbering style required the bar indices to change whenever a new bar is formed: The new bar becomes Bar 0... Bar 0 becomes Bar 1... Bar 1 becomes Bar 2 etc.
See here for more information on the numbering system.

If you want to find out how long ago a pattern or price movement occurred, you will need to either:

  1. Store the Time[] as the patterns appears. Later on, you can translate this back to a bar index using iBarShift(). Or you may decide to simply compare the current time to the stored time. Or,
  2. Search through the old bars looking for past patterns. In these circumstances, you can simply use the bar index (or the time) of the pattern to calculate how long ago it was.

I apologise that it is a vague answer, but it depends so much on how you are writing your code. But hopefully it gives you something to go on.

 
 
  • Post #5
  • Quote
  • Feb 1, 2015 2:12pm Feb 1, 2015 2:12pm
  •  Motiram
  • | Joined Nov 2014 | Status: Member | 610 Posts
Quoting Chorlton
Disliked
Hello All, With regard to MQL4, I've trying to find out a way to count the number of bars on the chart that have passed since a particular price movement/pattern has occurred. As a simple example, how would I count the number of bars that has occurred since price last gapped up? I've been reading through the documentation but can't seem to find an appropriate function to use. In my old amibroker days, I would have used the AFL function, barssince. Any help would be welcome, Thanks, Chorlton
Ignored
Hello Chorlton in the link below is indicator ZigZagWithValue_Bars.mq4 which count the bars and zigzag pips is it what you are looking for

http://www.forexfactory.com/showthre...09#post4384709

Regards,
Moti
 
 
  • Post #6
  • Quote
  • Feb 2, 2015 5:39am Feb 2, 2015 5:39am
  •  Chorlton
  • | Joined Jul 2010 | Status: Member | 211 Posts
Quoting honestknave
Disliked
It certainly can be done, but the methodology will depend on your application. The current bar always has an index of 0. The previous bar is 1, the bar before that is 2 etc. So the bar index will always tell you how long ago it happened... e.g. Bar 43 happened 43 bars ago. This numbering style required the bar indices to change whenever a new bar is formed: The new bar becomes Bar 0... Bar 0 becomes Bar 1... Bar 1 becomes Bar 2 etc. See here for more information on the numbering system. If you want to find...
Ignored
Hi HonestKnave,

Thank you for the detailed reply.

As I'm slowly learning MQL4 would you be able to expand on (with maybe some actual code) how to use bar index to calculate how long ago a pattern occurred?

If, for example, my pattern was basically a gap up in price
GapUpPattern = iLow(NULL,0,1) > iHigh(NULL,0,2)

How would I use the above with the bar index function to find out how many bars ago this happened?
 
 
  • Post #7
  • Quote
  • Feb 2, 2015 5:40am Feb 2, 2015 5:40am
  •  Chorlton
  • | Joined Jul 2010 | Status: Member | 211 Posts
Quoting Motiram
Disliked
{quote} Hello Chorlton in the link below is indicator ZigZagWithValue_Bars.mq4 which count the bars and zigzag pips is it what you are looking for http://www.forexfactory.com/showthre...09#post4384709 Regards, Moti
Ignored
Hi Motiram,

This isn't what I'm after at the moment but it may come in useful so thank you....
 
 
  • Post #8
  • Quote
  • Feb 2, 2015 7:50am Feb 2, 2015 7:50am
  •  cyber1
  • Joined Jan 2011 | Status: Member | 1,299 Posts
Quoting Chorlton
Disliked
Hello All, With regard to MQL4, I've trying to find out a way to count the number of bars on the chart that have passed since a particular price movement/pattern has occurred. As a simple example, how would I count the number of bars that has occurred since price last gapped up? I've been reading through the documentation but can't seem to find an appropriate function to use. In my old amibroker days, I would have used the AFL function, barssince. Any help would be welcome, Thanks, Chorlton
Ignored
Chorlton-
You will need to set up a nested "for loop". At each iteration of the main loop you will need to check for that condition.
 
 
  • Post #9
  • Quote
  • Edited 7:36pm Feb 2, 2015 12:08pm | Edited 7:36pm
  •  honestknave
  • Joined Nov 2014 | Status: Member | 1,300 Posts
Quoting Chorlton
Disliked
{quote} Hi HonestKnave, Thank you for the detailed reply. As I'm slowly learning MQL4 would you be able to expand on (with maybe some actual code) how to use bar index to calculate how long ago a pattern occurred? If, for example, my pattern was basically a gap up in price GapUpPattern = iLow(NULL,0,1) > iHigh(NULL,0,2) How would I use the above with the bar index function to find out how many bars ago this happened?
Ignored
Here is some rough code which should give you an idea of the concept.

The code runs once per bar.

If the code finds a gap up pattern, the time is stored.

Additionally, the comment is updated each bar to reflect how long ago the last pattern happened.

Hope that helps
Inserted Code
#property strict
#property indicator_chart_window
 
datetime LastGapUp=0;
 
int OnInit() { return(INIT_SUCCEEDED); }
 
void OnDeinit(const int reason) { Comment(""); }
 
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   int Limit=rates_total-1-MathMax(prev_calculated,2);
   for(int i=Limit; i>=0; i--)
     {
      if(low[i+1]>high[i+2]) { LastGapUp=time[i+1]; }
      int Bar=iBarShift(NULL,0,LastGapUp);
      string When=TimeToStr(LastGapUp);
      Comment(StringFormat("Last gap up was %i bars ago (at %s)",Bar,When));
     }
   return(rates_total);
  }
 
 
  • Post #10
  • Quote
  • Feb 10, 2015 5:09am Feb 10, 2015 5:09am
  •  Chorlton
  • | Joined Jul 2010 | Status: Member | 211 Posts
Quoting cyber1
Disliked
{quote} Chorlton- You will need to set up a nested "for loop". At each iteration of the main loop you will need to check for that condition.
Ignored
Thanks cyber...
 
 
  • Post #11
  • Quote
  • Last Post: Feb 10, 2015 5:10am Feb 10, 2015 5:10am
  •  Chorlton
  • | Joined Jul 2010 | Status: Member | 211 Posts
Quoting honestknave
Disliked
{quote} Here is some rough code which should give you an idea of the concept. The code runs once per bar. If the code finds a gap up pattern, the time is stored. Additionally, the comment is updated each bar to reflect how long ago the last pattern happened. Hope that helps #property strict #property indicator_chart_window datetime LastGapUp=0; int OnInit() { return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { Comment(""); } int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const...
Ignored
Thank you so much Honestknave,

This gives me a good idea of how to structure it....
 
 
  • Platform Tech
  • /
  • Counting number of past bars since price pattern occurrence
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023