Python can't get active orders

 

account_info is work

orders_get  not work

output  No open orders found


import MetaTrader5 as mt5
import requests
from datetime import datetime



# start the platform with initialize()
if not mt5.initialize():
    print("initialize() failed, error code =",mt5.last_error())
    quit()

# login to Trade Account with login()
# make sure that trade server is enabled in MT5 client terminal

login = 000000000
password = 'xxxxxxxxx'
server = 'Exness-MT5Trial7'

authorized = mt5.login(login, password, server)


if authorized:
    print("connected to account #{}".format(login))
    # get account info
    account_info = mt5.account_info()


    # display data on active orders on GBPUSD
     # Get account info
    account_info = mt5.account_info()

    if account_info:
        print("Account Info:")
        print("Account Number:", account_info.login)
        print("Account Balance:", account_info.balance)
        print("Account Equity:", account_info.equity)
        print()

        # Display data on active orders
        all_orders = mt5.orders_get()

        if all_orders:
            print("All Orders:")
            for order in all_orders:
                print(f"Order ID: {order.order}")
                print(f"Symbol: {order.symbol}")
                print(f"Order Type: {order.action}")
                print(f"Lot Size: {order.volume}")
                print(f"Open Price: {order.price}")
                print(f"Stop Loss: {order.sl}")
                print(f"Take Profit: {order.tp}")
                print()
        else:
            print("No open orders found")
Documentation on MQL5: Python Integration / account_info
Documentation on MQL5: Python Integration / account_info
  • www.mql5.com
account_info - Python Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Facing Same problem for code below :
import MetaTrader5 as mt5
import pandas as pd
pd.set_option('display.max_columns', 500) # number of columns to be displayed
pd.set_option('display.width', 1500)      # max table width to display
# display data on the MetaTrader 5 package
print("MetaTrader5 package author: ",mt5.__author__)
print("MetaTrader5 package version: ",mt5.__version__)
print()
# establish connection to the MetaTrader 5 terminal
if not mt5.initialize():
    print("initialize() failed, error code =",mt5.last_error())
    quit()
 
# display data on active orders on EURUSD
orders=mt5.orders_get()
print(orders)

if orders is None:
    print("No orders on EURUSD, error code={}".format(mt5.last_error()))
else:
    print("Total orders on EURUSD:",len(orders))
    # display all active orders
    for order in orders:
        print(order)
print()
 

# Retrieve orders
EUR_orders = mt5.orders_get(group="*EUR*")

if EUR_orders is None:
    print("No orders on EURUSD, error code={}".format(mt5.last_error()))
else:
    print("Total orders on EURUSD:", len(EUR_orders))
    # Check if there are any orders before creating a DataFrame
    if len(EUR_orders) > 0:
        df = pd.DataFrame([order._asdict() for order in EUR_orders])
        print(df)
    else:
        print("No orders found on EURUSD.")

# Shut down connection to the MetaTrader 5 terminal
mt5.shutdown()
Error
 
joe1992 #:
Facing Same problem for code below :

Fixed it with the below code:

import MetaTrader5 as mt5
import pandas as pd
pd.set_option('display.max_columns', 500) # number of columns to be displayed
pd.set_option('display.width', 1500)      # max table width to display
# display data on the MetaTrader 5 package
print("MetaTrader5 package author: ",mt5.__author__)
print("MetaTrader5 package version: ",mt5.__version__)
print()
# establish connection to the MetaTrader 5 terminal
if not mt5.initialize():
    print("initialize() failed, error code =",mt5.last_error())
    quit()
 
# get open positions on EURUSD
positions=mt5.positions_get(symbol="EURUSD")
if positions==None:
    print("No positions on EURUSD, error code={}".format(mt5.last_error()))
elif len(positions)>0:
    print("Total positions on EURUSD =",len(positions))
    # display all open positions
    for position in positions:
        print(position)
 
# get the list of positions on symbols whose names contain "*EUR*"
usd_positions=mt5.positions_get(group="*EUR*")
if usd_positions==None:
    print("No positions with group=\"*EUR*\", error code={}".format(mt5.last_error()))
elif len(usd_positions)>0:
    print("positions_get(group=\"*EUR*\")={}".format(len(usd_positions)))
    # display these positions as a table using pandas.DataFrame
    df=pd.DataFrame(list(usd_positions),columns=usd_positions[0]._asdict().keys())
    df['time'] = pd.to_datetime(df['time'], unit='s')
    df.drop(['time_update', 'time_msc', 'time_update_msc', 'external_id'], axis=1, inplace=True)
    print(df)
 
# shut down connection to the MetaTrader 5 terminal
mt5.shutdown()
d
 
joe1992 #:

Fixed it with the below code:

d
Yes, I also fixed it this way. thanks