Pulling Prices Directly

 

Is it possible to get a price for a symbol directly? In other words, is the price pushed to the user somehow through MetaTrader or do I have to constantly pull it from the network?


I am trying to take the price of each symbol and convert it to bits, but I am having problems just getting the price in a workable format. Thoughts?

 
boomworks:

Is it possible to get a price for a symbol directly? In other words, is the price pushed to the user somehow through MetaTrader or do I have to constantly pull it from the network?


I am trying to take the price of each symbol and convert it to bits, but I am having problems just getting the price in a workable format. Thoughts?

The prices are constantly being pushed to the user as long as the Terminal is connected. There is no need 'to pull it from the network', the Terminal does that for u.

The price comes as a double with a predefined number of digits that could be read via MarketInfo() function... What can be simpler than that?

 

The only thing simpler than that is that the prices are ALSO supplied (for the current chart symbol) as a pair of variables (Ask and Bid) that are pre-normalised and don't even require a function call.

:-)


CB

 
cloudbreaker:

The only thing simpler than that is that the prices are ALSO supplied (for the current chart symbol) as a pair of variables (Ask and Bid) that are pre-normalised and don't even require a function call.

To flesh things out for boomworks's benefit, there are two ways of getting the current ask and bid. One is to use the Ask and Bid variables. The other is to use MarketInfo(Symbol(), MODE_ASK) and MarketInfo(Symbol(), MODE_BID).


There is a difference between these two. The Ask and Bid variables contain the prices at the time of entry to the start() function of the EA/indicator/script. If the start() function is long-running, e.g. because of trading operations, then the prices can go stale - unless you explicitly call RefreshRates() to update them. Whereas MarketInfo() always returns the latest price, i.e. the equivalent of calling RefreshRates() before accessing Ask and Bid. Personally, I tend to use MarketInfo() rather than the Ask and Bid variables.


You can examine these differences using a script such as the following:


int start()
{
   // Pause for 10 seconds during start()
   Sleep(10000);
   
   // Get the Bid without a RefreshRates(). This price is usually out of date.
   double UnrefreshedBid = Bid;
   
   // Get from MarketInfo() - without doing a refresh 
   double ViaMarketInfo = MarketInfo(Symbol(), MODE_BID);
   
   // Do a refresh, and then re-read the Bid variable
   RefreshRates();
   double RefreshedBid = Bid;

   // Display the results. ViaMarketInfo will equal RefreshedBid (unless there is a tick in the very small amount of time 
   // between those two calls). Whereas UnrefreshedBid will be a stale value.
   MessageBox("Unrefreshed Bid: " + UnrefreshedBid + "\r\nRefreshed Bid: " + RefreshedBid + "\r\nVia MarketInfo(): " + ViaMarketInfo);
}
 

I just want the double so that I can export it to another program in real time.


Broker --> MetaTrader --> Me--> outside program


What from what I gather, calling int Start() will do that for each chart. However, I assume that the message that is sent from the broker to MetaTrader is then converted into a display, I just want the easiest way to get that message without the display. I don't want the charts, I just want the number. If all I have to do is call Start(), awesome. But where does that message from the broker go? How do I pick it up and use it?


Sorry if I am missing something very obvious, but the documentation does not make it clear how to pick simple messages up. Is there another set of documentation I could read?

 

You will need to write an EA which does the following at each arriving tick. Since the start() function is triggered by arriving ticks, that's where you'll place your logic:

1. Obtain the price (and move to a double-type variable of your choice if you need to)

- For the ask and bid prices of the current chart this is simply Ask or Bid and they come pre-normalized for the correct number of digits in the current chart. dMyAsk = Ask;

- For the prices of another chart, use the MarketInfo() function. eg. dMyAsk = NormailzeDouble(MarketInfo("USDCHF",MODE_ASK),MarketInfo("USDCHF",MODE_DIGITS));

2. Write this value

- either to a file - check the forum - I posted a couple of file read/write functions yesterday

- or to an external program using a DLL call - again check the forum for details on how


CB

 

Precisely what I was asking. Thank you so much! Did I miss somewhere that said "the start() function is triggered by arriving ticks" because that was the crux of my problem.


Also, would it be faster to start EAs for every currency pair or just call from one logic block?


Thank you again!

 
jjc:

To flesh things out for boomworks's benefit, there are two ways of getting the current ask and bid. One is to use the Ask and Bid variables. The other is to use MarketInfo(Symbol(), MODE_ASK) and MarketInfo(Symbol(), MODE_BID).


There is a difference between these two. The Ask and Bid variables contain the prices at the time of entry to the start() function of the EA/indicator/script. If the start() function is long-running, e.g. because of trading operations, then the prices can go stale - unless you explicitly call RefreshRates() to update them. Whereas MarketInfo() always returns the latest price, i.e. the equivalent of calling RefreshRates() before accessing Ask and Bid. Personally, I tend to use MarketInfo() rather than the Ask and Bid variables.


You can examine these differences using a script such as the following:



Don't mean to hijack the thread, but I'm working on much the same think that boomworks seems to be working on - getting quotes out of MT to use on a separate black box running a learning algorithm, and I only plan to use MQL to get the quotes out. Crucial to the learning behavior of this algorithm is the timing of the arrival of the quotes. Obviously me calling something like RefreshRates() means I'm determining the timing, basically doing a pull.


What I need to use is a callback function that gets triggered when a new quote comes in from the network, so a listener has to be constantly open in MT, that calls my callback library function (that I can compile to a DLL), which in turn pushes the quote to my algorithm. This preserves the timing and order of market quotes. Oh, and I need to do this for all available currency pairs concurrently.


Before y'all get sick of my rambling, is using MT for this overkill? Should I be looking for a direct (non MetaTrader) API? It seems the industry has pretty much standardized on MT, though. The only broker that offers close to to what I need is Oanda, and they want 600 bucks/mo.

Any technical or strategic guidance is appreciated!

 
breakingturing:

Don't mean to hijack the thread, but I'm working on much the same think that boomworks seems to be working on - getting quotes out of MT to use on a separate black box running a learning algorithm, and I only plan to use MQL to get the quotes out. Crucial to the learning behavior of this algorithm is the timing of the arrival of the quotes. Obviously me calling something like RefreshRates() means I'm determining the timing, basically doing a pull.


What I need to use is a callback function that gets triggered when a new quote comes in from the network, so a listener has to be constantly open in MT, that calls my callback library function (that I can compile to a DLL), which in turn pushes the quote to my algorithm. This preserves the timing and order of market quotes. Oh, and I need to do this for all available currency pairs concurrently.


Before y'all get sick of my rambling, is using MT for this overkill? Should I be looking for a direct (non MetaTrader) API? It seems the industry has pretty much standardized on MT, though. The only broker that offers close to to what I need is Oanda, and they want 600 bucks/mo.

Any technical or strategic guidance is appreciated!

start() function gets triggered on each incoming tick, but more precisely - it's triggered on each incoming tick of the symbol displayed on the chart (the one the expert is attached to). So you can put your code there for passing that tick to the dll, but that will only get you the ticks for that particular symbol. To get all symbols you have 2 options:


1. Open many charts with different symbols and attach an expert to each -> might be impractical?

2. Loop every 100msecs or so and check if there are any new incoming ticks on all symbols, then send them to your dll. But that will make you "determine the timing" as you put it. You can attempt to loop every 1ms to enhance the resolution but as far as I know (and I am NOT sure so u might want to ask around) the minimum possible time in MT4 is 100ms (but hardware-wise I think 16ms should be the minimum anyway).


Do you need it the ticks in real time? Cause if you don't - u can get tick data from dukascopy with time/date stamp that has milliseconds as well... I know there are PHP scripts available to mass-download from dukascopy.

 
gordon:

start() function gets triggered on each incoming tick, but more precisely - it's triggered on each incoming tick of the symbol displayed on the chart (the one the expert is attached to). So you can put your code there for passing that tick to the dll, but that will only get you the ticks for that particular symbol. To get all symbols you have 2 options:


1. Open many charts with different symbols and attach an expert to each -> might be impractical?

2. Loop every 100msecs or so and check if there are any new incoming ticks on all symbols, then send them to your dll. But that will make you "determine the timing" as you put it. You can attempt to loop every 1ms to enhance the resolution but as far as I know (and I am NOT sure so u might want to ask around) the minimum possible time in MT4 is 100ms (but hardware-wise I think 16ms should be the minimum anyway).


Do you need it the ticks in real time? Cause if you don't - u can get tick data from dukascopy with time/date stamp that has milliseconds as well... I know there are PHP scripts available to mass-download from dukascopy.

Thanks for your time, gordon.


Option (1) seems fraught with peril, if not from crashing or overwhelming the machine, at least from the fact that it's "manual" - imagine trying an audit by hand to make sure you've opened *all* pairs... ugh, probably unmanageable. I guess I could do some dynamic gap filling and call an alert if a window goes missing for some reason, it'd be layering an ugly hack on top another one.


Option (2) suffers from exactly the timing flaw you called out, assuming a granularity of 1ms is not possible. Otherwise it should suffice.


Yep, need the ticks in real time, since the whole point is to be able to react and place a trade when the black box thinks it found a pattern.


I'm going to keep thinking about it.


Cheers,

bt

 
I don't think it's such a big deal to have a chart open for each pair. Your DLL will be a single collection point, so you could flag up a warning if a certain pair hasn't spoken in a reasonable period. CB
Reason: