Find most occuring price over given period

To add comments, please log in or register
ed
394
ed  

I tried to write to some code that would find the most frequently occuring price over the last nMinutes, using OHLC as quotes.  I realize now however, that I am in way over my head.  Please help!

int i;
int PopLength = 100;
double Pop;
double PriceArray[PopLength][2];
double Check;
 
int start()
{
 
//count number of occurances of price
Check == iOpen(NULL,PERIOD_M1,(PopLength+1));
for(i=(PopLength+1;i>=1;i--)
{
   if(iOpen(NULL,PERIOD_M1,i)==Check)
   {
      PriceArray[i][1] = iOpen(NULL,PERIOD_M1,i);
      PriceArray[i][2] = PriceArray[i][2]+1; 
   }
   if(iHigh(NULL,PERIOD_M1,i)==Check)
   {
      PriceArray[i][1] = iHigh(NULL,PERIOD_M1,i);
      PriceArray[i][2] = PriceArray[i][2]+1; 
   }
   if(iLow(NULL,PERIOD_M1,i)==Check)
   {
      PriceArray[i][1] = iLow(NULL,PERIOD_M1,i);
      PriceArray[i][2] = PriceArray[i][2]+1; 
   }
   if(iClose(NULL,PERIOD_M1,i)==Check)
   {
      PriceArray[i][1] = iClose(NULL,PERIOD_M1,i);
      PriceArray[i][2] = PriceArray[i][2]+1; 
   }              
}
 
Pop = PriceArray[100][2];
for(i=(PopLength+1;i>=1;i--)
{
   if(PriceArray[i][2] > Pop)
   {
      Pop = PriceArray[i][1]:
   }
}  
 
//Pop will equal most common price
return(0);
}
Tristen
12
Tristen  

I've thought about this for a while and I think I understand what you want. However, first let's look at the algorithm that would give you the most frequently occurring price(s).

First thought is that there may be more than one price that will occur at a specific frequency. How should we handle this case? I'm not sure.

Anyway, let’s get the general algorithm defined…

  1. First we need to know the highest high and the lowest low for the total bars in question, this will give us the total range of possible prices (in pips.)
  2. Create an array the size of the total range: priceArray[totalRange][2]; // [][0] = price, [][1] = counter.
  3. Initialize the array with all the pip values (using a loop): priceArray[p][0] = pipvalue;
  4. While looping through the historical data: for (i = lookback; i < 0; i--) // This will not include the current bar; use i <= 0 if you want the current bar.
  1. Loop through all the pips for the given bar: for (p= low[i]; p <= high[i]; p = p + pipSize)
  2. Update the array cell for the given pip: priceArray[p][1] += 1;
  • Once you have all the raw data you can do several things with it, for example.
  1. You can find the value with the highest high (as you were doing)
  2. Sort or rank the array based upon percentile, ie. all cells that in the highest top 10%. Which could give you another array. From this array you could create 1 or more summary bars; ie. high/low bars for each continuous range that you find with in this secondary array.

One note about your code, for loops are defined as: for(i = initialValue; i < endingValue; update i). IOW, you have an extra “(“ in your loops. They should read; for(i=PopLength + 1; i >= 1; i--) and not for(i=(PopLength + 1; i >= 1; i--).

Also note that arrays go from 0…Length -1.

Hope this helps.

Kaboo

ed
394
ed  

Thanks Kaboo for spending some time on this.  Your logic is very useful in figuring out what I need to do.  However, I already knew what I wanted to occur, I just don't know how to actually code it.  For example, how do I initialize an array with a variable?  "double PriceArray[PopLength][2]" does not work.  When I compile, I get an error that says "'PopLength' - integer number expected "  The syntax must be wrong here, if it is even possible at all.

ed
394
ed  

OK, I think I have something.  However, the second element in the array (the count of how many occurances) stays at 0.

int i;
int j;
int PopLength = 100;
double Pop;
double PriceArray[500][2];
double CheckHigh;
double CheckLow;
double Range;
double ArrayStart;
double PriceCheck;
 
 
int start()
{
 
//find high and low over Period
CheckHigh = iHigh(NULL,PERIOD_M1,(PopLength+1));
CheckLow = iLow(NULL,PERIOD_M1,(PopLength+1));
for(i=PopLength+1;i>=1;i--)
{
   if(iHigh(NULL,PERIOD_M1,i) > CheckHigh)
   {
      CheckHigh = iHigh(NULL,PERIOD_M1,i);
   }
   if(iLow(NULL,PERIOD_M1,i) < CheckLow)
   {
      CheckLow = iLow(NULL,PERIOD_M1,i);
   }   
}
 
Range = ((CheckHigh - CheckLow)*10000);
 
//load array
ArrayStart = CheckLow;
for(i=1;i<=Range;i++)
{
   PriceArray[i][1] = ArrayStart;
   ArrayStart += (1*Point);
   PriceArray[i][2] = 0;   
}
 
 
//start with first price level in array and count how many times this
//occurs over the period
for(j=1;j<=Range;j++)
{
   PriceCheck = PriceArray[j][1];  
   for(i=PopLength+1;i>=1;i--)
   {
      if(iOpen(NULL,PERIOD_M1,i) == PriceArray[j][1])
      {
         PriceArray[j][2]=PriceArray[j][2]+1;
      }
      if(iHigh(NULL,PERIOD_M1,i) == PriceArray[j][1])
      {
         PriceArray[j][2]=PriceArray[j][2]+1;
      }
      if(iLow(NULL,PERIOD_M1,i) == PriceArray[j][1])
      {
         PriceArray[j][2]=PriceArray[j][2]+1;
      }
      if(iClose(NULL,PERIOD_M1,i) == PriceArray[j][1])
      {
         PriceArray[j][2]=PriceArray[j][2]+1;
      }
   }
}
 
for(i=1;i<=Range;i++)
{
   Print(PriceArray[i][1], ",", PriceArray[i][2]);
}
 
 
return(0);
}
Tristen
12
Tristen  
inkexit wrote >>

OK, I think I have something. However, the second element in the array (the count of how many occurances) stays at 0.

Sorry I have been gone for a while, if you having solved it by now here are a few things that I see.

The array double PriceArray[500][2];

is from 0..499 and 0..1

i.e. PriceArray[0][0] to PriceArray[499][1]

Therefore all your code that looks like

for(i=PopLength+1;i>=1;i--)

and

PriceArray[j][2]=PriceArray[j][2]+1;

isn't refering to anything, it should be changed to

for(i=PopLength;i>=1;i--)

and

PriceArray[j][1]=PriceArray[j][1]+1;

Hope this helps.

To add comments, please log in or register