In Python MT5, how to have my code running on every asset change ?

 

Hello,


I would like to display, in the terminal, the price of DJ30 at every change in the market (at every move of one pip say), I don't the frequency of this event, but I guess it's something like every second or less.


So in the Metatrader library of Python, how could I do this ?  I have checked the different functions available and I don't think it's possible actually.


Any idea ?


Thanks

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
 
AYMERIC75:Hello, I would like to display, in the terminal, the price of DJ30 at every change in the market (at every move of one pip say), I don't the frequency of this event, but I guess it's something like every second or less. So in the Metatrader library of Python, how could I do this ?  I have checked the different functions available and I don't think it's possible actually. Any idea ?

You are misunderstand how trading works. Quotes (Ticks) are not updated at a fixed periodic frequency. Price rates change when trade transactions take place and that can happen at any time. There could be for example only a single trade in one hour or hundreds of trades in a single second.

In MQL5, there is an event handler OnTick(), a function which is called whenever there is a new tick (quote price change), but not in Python. The Python API does not have any callback functions, so events are not pushed out from the MetaTrader terminal to the Python program using the API. The Python program has to poll MetaTrader so as to receive new data.

So, if you want to respond to Tick events you will have to do so with a MQL5 program and not with Python.

 
Fernando Carreiro #:

You are misunderstand how trading works. Quotes (Ticks) are not updated at a fixed periodic frequency. Price rates change when trade transactions take place and that can happen at any time. There could be for example only a single trade in one hour or hundreds of trades in a single second.

In MQL5, there is an event handler OnTick(), a function which is called whenever there is a new tick (quote price change), but not in Python. The Python API does not have any callback functions, so events are not pushed out from the MetaTrader terminal to the Python program using the API. The Python program has to poll MetaTrader so as to receive new data.

So, if you want to respond to Tick events you will have to do so with a MQL5 program and not with Python.

Thanks a lot for your answer Fernando. So, I guess I should instead program in MQL5 and call the predictions from my Python written Machine Learning model from MQL5, but is this even possible ?


Or maybe I don't need to go that far, and I can just make a script that call my Python function every second or so. This way I do everything from Python.

 
AYMERIC75 #: Thanks a lot for your answer Fernando. So, I guess I should instead program in MQL5 and call the predictions from my Python written Machine Learning model from MQL5, but is this even possible? Or maybe I don't need to go that far, and I can just make a script that call my Python function every second or so. This way I do everything from Python.

We don't know your strategy specifics, so only you can decide that.