Using ticks in arrays for a beginner!

 

Dear Forum,

I've spent the past week reading about MQL4 and trying to turn an excel model I've been working on for about 5 months now into an EA.

The thing is, I was using tick data (from http://ratedata.gaincapital.com/2010/ ) and I've only just realised how difficult it is to work with tick data on MetaTrader. I feel like I've gone from the thrilling heights of finally having a working model to the crushing lows of seeing how complicated MQL4 is for someone with no experience outside of if() functions in excel.

I've read the Array's section in the book and documentation, and also downloaded a guide for dummies on MQL4, but I'm still struggling -- (and dying to finally test my excel EA on a demo account without having to start months of work all over again). From what I've read I need to create an array.

In excel:

1. I created a moving average of the last 500 tick points.

2. I then found the slope of the moving average (I used the 4 most recent values of the MA). And so one of my indicators to buy was the steepness of this slope.

3. A second indicator was if the slope value was negative within the last 500 ticks. That way I'm not going to get onboard when a trend is nearly exhausted and the probability of upwards movement in higher (because it's the start of the wave).

4. The final indicator was comparing the correllation of the last 200 ticks against a line with a gradient of 1 (e.g. {1,2,3,4,5,6,7} using the excel correl function. If it was over a certain % --e.g. 85% -- then it was a good sign to buy (I think it gives a clearer picture than an average as you can find a clear line amongst the noise).


Can anyone suggest any code for creating an array of the most recent 500 ticks?

This is the closest discussion I've found but I had no idea about even where or how to start: https://www.mql5.com/en/forum/109833


Also if anyone has any idea about how to calculate the slope of the MA (do you have to compare it against another array of numbers like in excel?) or knows off the top of their head how to do 3. or 4. above (or even some words of advice for a novice, be it general or specific) it would be immensly appreciated. Thanks so much in advance, even for reading this far, and so may the forex karma gods smile on you.

 

read about arrays in the mql4 book to understand how they are indexed then just make an array to hold 500 values. make a static integer to use as a counter then use that counter to add each new tick to the next array index, like this:

start()

{

double tickarray[500];

static int cnt=499;

tickarray[cnt]=Bid;

cnt--;

}

That will put each new tick into the array until the counter reaches zero.

Then you will need to prevent the counter going below zero and you will need to reindex the array when it is full to make each tick moves up one position in the array. So add some code for that and you end up with something like this:

int start()

{ 

  double tickarray[500];
  static int cnt=499;
  if(cnt<0) cnt=0; 
  
  if(tickarray[0] !=0)
   {
    for (int i=499; i>1; i--)
    {
     tickarray[i]=tickarray[i-1];
    } 
   }
  tickarray[cnt]=Bid;
  cnt--;
  return(0);
} 

 

Thank you so much SDC!

I think I need to do some more reading about how arrays are indexed but I just thought I'd say thank you first.

So correct me if I'm wrong - or anyone else who uses the code above -, the code above creates an array which contains the 500 most recent Bid values and it's updated each time a new tick comes through (provided ofcourse I have no server problems and don't miss any ticks, in which case it just skips to the next tick and is no big problem if i'm doing an average of 500). I can then perform functions like iMAOnArray or iMomentumOnArray.

Thank you so so so so much again!

 

yes thats what it should do ..i didnt test that code i just wrote it but i think it is correct, are you making a tick chart ? If you are using it in an indicator make sure you do all the tickarray code outside of the main indicator loop so it doesnt fill that array while it is trying to index all the history bars.

 

I did something wrong in that code, i meant to move all the ticks up one position in the array, I mistakenly wrote the loop so it would move them down one position, so change the line:

tickarray[i-1]=tickarray[i];

To:

tickarray[i]=tickarray[i-1];

Ill edit my code in the earlier post to reflect that change, sorry about that, I'll test it properly when the price feed starts up again later.

 

Thanks again so much!

I'm planning on using it as the basis for my EA.

I made my model on excel - which essentially is a tick array as the pricing data was tick by tick. I don't know how to make tick charts and it sounds complicated so I thought it would be easier to put everything in an array. I think this means i can apply the EA to any chart time-period as it's not dependent on the number of bars. I also have a filter depending on the most recent ticks so i think this means i couldn't use it if i was using the candlesticks.

From the array I'll calculate a moving average, a slope, and a correlation with a second array. I've still got to learn how to do all those functions so it'll be a busy week.

So this is how it should look?

//Tick Array Code

Init()

Deinit()

Start()
//Filter 1

//Filter 2

//Filter 3

//Ordersend (if all 3 are true)

//Sell conditions


Thanks again for your help with the code, I'd have no idea where the problem was because this is the first coding I've done.

 

well if its an EA it should probably look more like this:

int Init()
  {
   return(0);
  }

int Deinit()
  {
   return(0);
  }

int Start()
  {
  //Tick Array Code

  //Sell conditions

  //Filter 1

  //Filter 2

  //Filter 3
  
  //Ordersend (if all 3 are true)

   return(0);
  }


 
Is not possible to read tick prices like bar prices(OHLC) from some system variable ?
 
jolly:
Is not possible to read tick prices like bar prices(OHLC) from some system variable ?
Yes, you can get the last tick, Bid and Ask
Reason: