Any example to get current bid and ask data from Metatrader5 (Using Python)?

 

I found many examples that can access data stream from metatrader as follows:

# request 1000 ticks from EURAUD
euraud_ticks = mt5.copy_ticks_from("EURAUD"datetime(2020,1,28,13), 1000mt5.COPY_TICKS_ALL)
# request ticks from AUDUSD within 2019.04.01 13:00 - 2019.04.02 13:00
audusd_ticks = mt5.copy_ticks_range("AUDUSD"datetime(2020,1,27,13), datetime(2020,1,28,13), mt5.COPY_TICKS_ALL)

However they are not for accessing current values but only histories. In datetime() function the examples do not provide seconds but only hours.

I searched for examples obtaining current data but no luck.

I tried by myself as follows (adding current minute and second) but the function throws 10 hours earlier data.

audusd_ticks = mt5.copy_ticks_from("AUDUSD", datetime(2020,1,28,13,30,01), 1, mt5.COPY_TICKS_ALL)


How to access current values of bid and ask etc.. from metatrader 5?

 

 
Gana Bata:

I found many examples that can access data stream from metatrader as follows:

# request 1000 ticks from EURAUD
euraud_ticks = mt5.copy_ticks_from("EURAUD"datetime(2020,1,28,13), 1000mt5.COPY_TICKS_ALL)
# request ticks from AUDUSD within 2019.04.01 13:00 - 2019.04.02 13:00
audusd_ticks = mt5.copy_ticks_range("AUDUSD"datetime(2020,1,27,13), datetime(2020,1,28,13), mt5.COPY_TICKS_ALL)

However they are not for accessing current values but only histories. In datetime() function the examples do not provide seconds but only hours.

I searched for examples obtaining current data but no luck.

I tried by myself as follows (adding current minute and second) but the function throws 10 hours earlier data.

audusd_ticks = mt5.copy_ticks_from("AUDUSD", datetime(2020,1,28,13,30,01), 1, mt5.COPY_TICKS_ALL)


How to access current values of bid and ask etc.. from metatrader 5?

 

Add this in  void OnTick() 

//---We Calculate the Ask Price

   double  Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

   

   //---We Calculate the Bid Price

  double  Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

 
Arvind Shah: Add this in  void OnTick() ...

Unfortunately the OP failed to explain his problem properly and that he is working in Python instead of MQL5, so your solution will not help him much!

 
Fernando Carreiro:

Unfortunately the OP failed to explain his problem properly and that he is working in Python instead of MQL5, so your solution will not help him much!

Yes i am working on python but the link (https://www.mql5.com/en/docs/integration/python_metatrader5 or MQL5 Reference->Integration->MetaTrader for Python) says the source belongs to MQL5.

In addition, my problem is that i am totally new to mql5 and metatrader 5, and i am trying to get current values of pairs (bid and ask) from metatrader 5 using " MetaTrader for Python" source.

However, i cannot find any example for accessing current values from metatrader 5 while there are many examples that access only history data.

Moreover, following code returns 10 hours earlier data even if i set current time.

audusd_ticks = mt5.copy_ticks_from("AUDUSD", datetime(2020,1,28,13,30,01), 1, mt5.COPY_TICKS_ALL)


Please correct me if i failed to explain my problem again.

MQL5 Reference - How to use algorithmic/automated trading language for MetaTrader 5
MQL5 Reference - How to use algorithmic/automated trading language for MetaTrader 5
  • www.mql5.com
MQL5 Reference - How to use algorithmic/automated trading language for MetaTrader 5
 
Arvind Shah:

Add this in  void OnTick() 

//---We Calculate the Ask Price

   double  Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

   

   //---We Calculate the Bid Price

  double  Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

Thanks to your reply. I am using python by MQL5 (source https://www.mql5.com/en/docs/integration/python_metatrader5).

Do you have any suggestion?

Documentation on MQL5: Integration / MetaTrader for Python
Documentation on MQL5: Integration / MetaTrader for Python
  • www.mql5.com
MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Gana Bata: Yes i am working on python ... Please correct me if i failed to explain my problem again.

As far as I know, there are very few users using Python integration with MetaTrader 5. Most of us here, including myself, work exclusively with MQL on MetaTrader.

So you will have to be very patient and wait until someone answers. In the mean time continue with your own research while reading the documentation to see if you can resolve the problem yourself.

 
Fernando Carreiro:

As far as I know, there are very few users using Python integration with MetaTrader 5. Most of us here, including myself, work exclusively with MQL on MetaTrader.

So you will have to be very patient and wait until someone answers. In the mean time continue with your own research while reading the documentation to see if you can resolve the problem yourself.

Hello, I built a library to reduce the boilerplate code and perhaps make your life easier:


The whole library can be found here:

https://github.com/Joaopeuko/Mql5-Python-Integration


If you want just the most near to the last price for bid and ask you can use:


tick.bid

tick.ask


Here is a simple example:

https://github.com/Joaopeuko/Mql5-Python-Integration/blob/master/examples_of_expert_advisor/example.py


In the example, I use tick.last


However, if you want the whole book, you can call the Book function.

Joaopeuko/Mql5-Python-Integration
Joaopeuko/Mql5-Python-Integration
  • github.com
I created this library because the development of an Expert Advisor in MQL5 can be complicated,while, in python, the same task flows better. - Joaopeuko/Mql5-Python-Integration
 
Joao Paulo Euko:

Hello, I built a library to reduce the boilerplate code and perhaps make your life easier:


The whole library can be found here:

https://github.com/Joaopeuko/Mql5-Python-Integration


If you want just the most near to the last price for bid and ask you can use:


tick.bid

tick.ask


Here is a simple example:

https://github.com/Joaopeuko/Mql5-Python-Integration/blob/master/examples_of_expert_advisor/example.py


In the example, I use tick.last


However, if you want the whole book, you can call the Book function.

Dear Mr

Thanks for your reply

From your source code i found my answer. "Mt5.symbol_info_tick("AUDJPY")" this function returns the current information.

BTW can you explain me what is the meaning of the following two lines in example.py?

buy = (tick.last > rates.open)

sell = (tick.last < rates.open)


And your library is a great work. Thank you very much.


Reason: