Python与MT5:在MT5上实现自动化订单执行

余汉波看波涛 2024-02-22 08:06:20

这段代码是一个 Python 函数,用于从 SQLite 数据库读取外汇订单数据,并在 MetaTrader 5(MT5)平台上执行这些订单。这个函数对于自动化交易策略非常有用,特别是在需要从数据库中批量处理多个交易指令时。

# 读取数据库forex_order的委托订单,进行批量委托,参数有两个,一个是数据库路径,一个是表名def execute_order_from_db(db_path, table_name): try: # 连接到SQLite数据库 conn = sqlite3.connect(db_path) # 读取数据库中的订单数据 df = pd.read_sql(f"SELECT * FROM {table_name}", conn) except Exception as e: print(f"数据库读取错误: {e}") return finally: # 确保在任何情况下都关闭数据库连接 conn.close() # 检查数据框是否为空 if df.empty: print("No orders found in the database.") return # 遍历DataFrame中的每一行 for index, order_info in df.iterrows(): try: symbol = order_info['品种名称'] order_type = int(order_info['订单类型']) requested_price = float(order_info['价格']) # 获取当前市场价格 symbol_info = mt5.symbol_info(symbol) if symbol_info is None: print(f"Failed to get symbol info for {symbol}") continue current_price = symbol_info.bid if order_type in [0, 2] else symbol_info.ask # 调整订单类型和行动 if order_type == 2 and requested_price > current_price: action = 1 # 市价委托 order_type = 0 # 市价买单 elif order_type == 3 and requested_price < current_price: action = 1 # 市价委托 order_type = 1 # 市价卖单 else: action = 5 # 限价委托 # 检查是否已有相同的委托单 existing_orders = mt5.orders_get(symbol=symbol, magic=int(order_info['EA_id']), type=order_type) if existing_orders: for order in existing_orders: # 撤销已有的委托单 request_cancel = { "action": mt5.TRADE_ACTION_REMOVE, "order": order.ticket } mt5.order_send(request_cancel) # 设置交易请求字典 request = { "action": action, "magic": int(order_info['EA_id']), "order": int(order_info['订单号']), "symbol": symbol, "volume": float(order_info['交易量']), "price": requested_price if action == 5 else current_price, "stoplimit": float(order_info['Limit挂单']), "sl": float(order_info['止损']), "tp": float(order_info['止盈']), "deviation": int(order_info['价格偏差']), "type": order_type, "type_filling": int(order_info['成交类型']), "type_time": int(order_info['订单有效期']), # 设置订单的有效期 "expiration": int(order_info['订单到期']), # 订单到期 "comment": order_info['订单注释'], "position": int(order_info['持仓单号']), # 持仓单号 "position_by": int(order_info['反向持仓单号']) # 反向持仓单号 } print("交易请求:", request) # 发送交易请求 result = mt5.order_send(request) # 检查执行结果,并输出相关信息 if result is not None and result.retcode == mt5.TRADE_RETCODE_DONE: print("Order executed successfully:", result) insert_into_db(db_path, result) else: print("Order execution failed:", result) except Exception as e: print(f"处理订单时出错: {e}")

代码功能和解释连接到 SQLite 数据库: sqlite3.connect(db_path): 这行代码通过指定的数据库路径连接到 SQLite 数据库。读取订单数据: 使用 pd.read_sql 从指定的表中读取全部订单数据,这里的表名通过 table_name 参数给出。异常处理: 如果在读取数据库过程中发生错误,函数将打印错误信息并终止执行。关闭数据库连接: 无论是否成功读取数据,都确保关闭数据库连接。遍历并执行订单: 如果从数据库中成功读取订单数据,函数将遍历每一行数据(即每一个订单)。 对于每个订单,函数将从 MT5 平台获取相关品种的当前市场价格。 根据订单类型和市场价格,函数调整交易请求的具体参数。发送交易请求到 MT5: 使用 mt5.order_send 发送交易请求到 MT5 平台。 检查交易执行的结果,并输出相关信息。 如果执行成功,将结果记录回数据库(需要一个名为 insert_into_db 的额外函数)。代码的作用

这个函数使金融量化交易者和编程师能够自动化执行交易策略。它从数据库中提取订单信息,自动在 MT5 上执行这些订单。这对于那些需要处理大量订单、或希望减少手动交易操作错误的交易者特别有用。这种自动化方法可以提高交易效率,减少延迟,并允许更快地响应市场变化。

0 阅读:0

余汉波看波涛

简介:感谢大家的关注