当然!我们将详细解释代码,重点关注每个部分和功能,以确保您了解其全部工作原理。
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"
这些变量包含 MetaTrader 5 凭证和路径,它们用于将脚本连接到您的 MetaTrader 帐户:
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
此功能搜索您想要交易的品种(资产)的信息,例如:
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)
此函数将买入或卖出订单发送到 MetaTrader。她接受:
该函数创建一个请求(request),其中包含发送订单的所有必要设置,然后调用 mt5.order_send(request) 将订单实际发送到 MetaTrader 5 平台。
login = 101108 password = "Jesuse10!" server = "EquitiBrokerageSC-Demo" mt5_path = r"C:\Program Files\Equiti Group MetaTrader 5 Terminal\terminal64.exe"
此函数搜索给定资产的最后一根蜡烛。它使用时间范围来确定每根蜡烛之间的时间间隔(例如5分钟)。该函数返回最后一根蜡烛(包含开盘价、收盘价、最高价、最低价等数据)。
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
此函数取消指定交易品种的所有 BUY_STOP 或 SELL_STOP 类型的挂单。该函数检查挂单,如果有,则发送取消请求。
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)
这里调用了main_loop函数。如果发生错误,则会被 except 异常捕获,并通过 mt5.shutdown() 终止与 MetaTrader 的连接。
此代码是 MetaTrader 5 的自动化 EA 交易 示例,它根据时间和蜡烛价格执行买入和卖出操作。该程序连接到 Meta
login = 101108 password = "Jesuse10!" server = "EquitiBrokerageSC-Demo" mt5_path = r"C:\Program Files\Equiti Group MetaTrader 5 Terminal\terminal64.exe"
以下是所做的更改:
以上是此代码是 MetaTrader 5 自动化 Expert Advisor (EA) 的示例。的详细内容。更多信息请关注PHP中文网其他相关文章!