Who is this Python service for?
It suits traders whose work has outgrown a single chart. Most requests come from people who:
- trade forex or stocks with a broker that offers an official API,
- want a Python model to place trades on MT5 through the terminal,
- copy fills into a spreadsheet by hand at the end of each day,
- need alerts or a daily report without watching a screen.
Your broker may ship its own Python library, or only a plain REST API that any language can call. Python handles both. If the whole strategy lives on one MT5 chart, an MT5 Expert Advisor is often simpler, and we say which route fits before we quote.
Broker API trading automation: Python tasks we automate
A typical job combines several of these six tasks.
| Task | What the script does |
|---|---|
| Rule-based orders | Reads prices, applies your rules and sends each order with a stop and a target |
| Scheduling | Runs at fixed times, such as every five minutes or once at the market open, started by Windows Task Scheduler or a Linux service |
| Logging | Writes every request, order and error to a dated log file, so you can trace what happened and when |
| Alerts | Messages your Telegram bot or your email when an order fills, a rule triggers or a job fails |
| Data export | Saves trade history, positions and prices to CSV or Excel files for your own records |
| Dashboard | Shows open orders, recent fills, job status and errors on a simple local page in your browser |
How do broker REST and WebSocket APIs work?
Most forex and stock brokers that offer an API split it into two channels. The REST API answers requests: place an order, cancel it, read your positions. A WebSocket stream stays open and pushes live prices and order updates to the script as they happen.
Each broker sets its own limits on top. Many cap the number of requests per minute, and access tokens often expire after a set time. Our scripts count requests against the limit, renew tokens before they lapse and retry with short waits after a network error.
Retries carry one classic risk: a duplicate order. Where the API supports it, every order carries a client order ID that the script chooses. Before a retry, the script looks that ID up, and if the broker already holds the order, the script does not send it again.
A kill switch sits above all of this. It stops new orders once the script hits a daily limit or too many errors.
How do you automate US stock trades with Python?
Many Gulf traders reach US shares through a broker API, and that route needs three extra checks.
The first is order state. An HTTP 200 reply only means the broker received the request. After that, the order moves through states such as accepted, partially filled, filled, rejected or cancelled. Our scripts log a trade as done only once a final state arrives.
Next comes your market data plan. Some broker plans stream real-time prices from a single exchange and send the full market feed delayed unless you pay for it. A rule that reads delayed prices acts on old numbers, so we check your data feed before writing any rule.
The third check is the clock. The NYSE core session runs from 9:30 am to 4:00 pm New York time. In UAE time that is 6:30 pm to 1:00 am while the US is on winter time, and 5:30 pm to midnight during US summer time. Scheduled jobs run on the exchange clock in New York time, whatever time zone your PC uses.
What can the MetaTrader5 Python package do?
It gives MT5 traders a second route. The official MetaTrader5 package from MetaQuotes lets Python talk to a running MT5 terminal through your normal MT5 login. In it, initialize opens the connection to the terminal, copy_rates_from reads price bars, positions_get lists open positions and order_send passes a trade request from the terminal to the trade server. Each function has its own page in the MetaTrader5 Python package reference.
Two details from that reference shape our code. First, order_send returns a result with a return code (retcode), which the official example compares with TRADE_RETCODE_DONE. Our scripts write that code to the log with every order, along with the error from last_error() when a request fails. Second, each request can carry a magic number, an ID that tags the orders one script sends, so your reports can tell them apart from manual trades.
This route suits Python models that need MT5 prices, and traders who want reports from MT5 history. However, the terminal must stay open, and PyPI lists the package only as Windows builds. A Windows VPS is the usual home for these scripts.
Where do your API keys live?
Your keys stay on your own PC or VPS, in a local config file that you fill in yourself. They are the most sensitive part of any broker API setup, so we build and test with demo or paper keys that you create and never ask for your live ones.
- The script loads keys from that local file or from Windows environment variables, never from the code itself.
- Turn off any key permission the script does not need, such as withdrawals.
- Logs stay clean, because keys and tokens never appear in log files or alerts.
Read your broker’s API terms and your local regulations as well. Some brokers ask you to apply for API access, while others limit automated trading on certain account types.
How a broker API trading automation project works
- Rules. You send your rules and a link to your broker’s API docs. We read the docs and confirm what the API allows before we quote.
- Code. We write commented Python, plus a config file for every setting you may change.
- Demo test. The script runs against the broker’s demo or paper environment. Where it makes sense, we also backtest your rules on past data first. A backtest and a demo run test different things, so we report them apart.
- Delivery. You get the files, a setup guide and a walkthrough on your PC or VPS.
What you receive
Every project ends with files you can run and maintain yourself:
- a packaged Windows app that runs without a Python install,
- the Python source code with comments, if you chose that option,
- a requirements.txt file and a config template,
- one setup guide for your PC or VPS, plus a walkthrough call,
- 7 days of fixes after delivery.
Safe delivery on a demo or paper account
The trial build runs against your broker’s demo or paper environment, never a live account, and it carries an expiry date. After the final payment, you get the full build and, if you chose it, the Python source.
Trading UAE shares on DFM or ADX? Our page on UAE stock market API automation covers what a local broker connection can and cannot do.
Pricing for broker API trading automation (Python scripts)
A single scheduled script with logs and alerts starts from $100, which is about AED 367 or SAR 375 at the fixed dirham and riyal pegs of 3.6725 and 3.75 to the US dollar. A full rule-based trading script with a dashboard costs more, because it needs more testing. You get one fixed USD quote once we have read the API docs. Coding starts after a 50% deposit in USDT. The deposit is refunded only if you cancel before coding starts, and there is no refund once coding has begun. Our expert advisor development cost page compares typical ranges across services.
Prefer to keep TradingView as your signal source? Our TradingView to MT4/MT5 copier turns alerts into MetaTrader 4 (MT4) or MT5 trades. To mirror trades across your own accounts, see the MT4/MT5 trade copier. Trading Saudi shares instead? Our Saudi stock market automation page explains what you can automate on Tadawul today.
Sources
- MetaQuotes, MetaTrader5 package for Python with its order_send page: functions, request fields, return codes (checked 17 Nov 2026).
- PyPI, MetaTrader5 release files, Windows win_amd64 builds only (checked 17 Nov 2026).
- Telegram, Bot API reference and the BotFather tutorial: bot tokens, sendMessage, and why the user writes first (checked 17 Nov 2026).
- Python Software Foundation, logging module documentation (checked 17 Nov 2026).
- NYSE, market hours and calendars, core trading session (checked 17 Nov 2026).



