Home >Backend Development >Python Tutorial >This code is an example of an automated Expert Advisor (EA) for MetaTrader 5.
Of course! We'll explain the code in detail, focusing on each part and function to ensure you understand how it all works.
import MetaTrader5 as mt5 import time from datetime import datetime import signal import sys
running = True def signal_handler(sig, frame): global running print("\nInterrompendo o programa...") running = False signal.signal(signal.SIGINT, signal_handler)
login = 101108 password = "Jesuse10!" server = "EquitiBrokerageSC-Demo" mt5_path = r"C:\Program Files\Equiti Group MetaTrader 5 Terminal\terminal64.exe"
These variables contain the MetaTrader 5 credentials and path. They are used to connect the script to your MetaTrader account:
def get_symbol_info(symbol): info = mt5.symbol_info(symbol) if info is None: print(f"Falha ao obter informações do símbolo {symbol}") return None return info
This function searches for information about the symbol (asset) you want to trade, such as:
import MetaTrader5 as mt5 import time from datetime import datetime import signal import sys
running = True def signal_handler(sig, frame): global running print("\nInterrompendo o programa...") running = False signal.signal(signal.SIGINT, signal_handler)
This function sends the buy or sell order to MetaTrader. She accepts:
The function creates a request (request) with all the necessary settings to send the order, and then calls mt5.order_send(request) to actually send the order to the MetaTrader 5 platform.
login = 101108 password = "Jesuse10!" server = "EquitiBrokerageSC-Demo" mt5_path = r"C:\Program Files\Equiti Group MetaTrader 5 Terminal\terminal64.exe"
This function searches for the last candle of a given asset. It uses the timeframe to determine the time interval between each candle (e.g. 5 minutes). The function returns the last candle (with data such as opening, closing, high, low price).
def get_symbol_info(symbol): info = mt5.symbol_info(symbol) if info is None: print(f"Falha ao obter informações do símbolo {symbol}") return None return info
This function cancels all pending orders of type BUY_STOP or SELL_STOP for the specified symbol. The function checks for pending orders and, if so, sends a request to cancel them.
import MetaTrader5 as mt5 import time from datetime import datetime import signal import sys
running = True def signal_handler(sig, frame): global running print("\nInterrompendo o programa...") running = False signal.signal(signal.SIGINT, signal_handler)
Here, the main_loop function is called. If an error occurs, it is caught by the except exception, and the connection to MetaTrader is terminated with mt5.shutdown().
This code is an example of an automated Expert Advisor (EA) for MetaTrader 5, which performs buy and sell operations based on the time and candle prices. The program connects to Meta
login = 101108 password = "Jesuse10!" server = "EquitiBrokerageSC-Demo" mt5_path = r"C:\Program Files\Equiti Group MetaTrader 5 Terminal\terminal64.exe"
Here are the changes made:
The above is the detailed content of This code is an example of an automated Expert Advisor (EA) for MetaTrader 5.. For more information, please follow other related articles on the PHP Chinese website!