-
Notifications
You must be signed in to change notification settings - Fork 368
Add hosted MT5 API broker (MetaTrader 5 integration) #1132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
miguelangelo78
wants to merge
4
commits into
Lumiwealth:dev
Choose a base branch
from
miguelangelo78:tickerall-broker
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8f6ba17
Add TickerAll hosted MT5 API broker and data source
miguelangelo78 0b22787
Harden TickerAll broker after full live-strategy verification
miguelangelo78 6e04582
Declare tickerall as an optional dependency (lumibot[tickerall])
miguelangelo78 2bbf5f4
Address PR review feedback
miguelangelo78 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| TickerAll | ||
| ====================================================== | ||
|
|
||
| `TickerAll <https://tickerall.com>`_ is a hosted MetaTrader 5 API. This broker | ||
| lets a Lumibot strategy trade any MetaTrader 5 account (Forex, metals, indices, | ||
| CFDs, crypto) through that hosted API, so it runs on **any operating system with | ||
| no local MetaTrader 5 terminal installed** - unlike the official MetaTrader5 | ||
| Python package, which is Windows-only and requires a running terminal. | ||
|
|
||
| How to Use TickerAll | ||
| -------------------- | ||
|
|
||
| 1. Install Lumibot with the optional TickerAll dependency: | ||
|
|
||
| .. code-block:: shell | ||
|
|
||
| pip install lumibot[tickerall] | ||
|
|
||
| 2. Create an account at `tickerall.com <https://tickerall.com>`_ and connect one | ||
| or more MetaTrader 5 broker accounts in the dashboard. | ||
| 3. Generate an API key. | ||
| 4. Set the environment variables below (or pass a ``config`` dict to the broker). | ||
|
|
||
| The hosted API supports market data (historical bars, last price, quotes), | ||
| account balances and open positions, and order management (market, limit and | ||
| stop orders, with optional stop-loss and take-profit). | ||
|
|
||
| **Environment Variables** | ||
|
|
||
| Set the following in your ``.env`` file or system environment: | ||
|
|
||
| .. code-block:: shell | ||
|
|
||
| TICKERALL_API_KEY=your_tickerall_api_key | ||
| # Optional: only needed when the API key has more than one connected account | ||
| TICKERALL_ACCOUNT_ID=your_connected_account_id | ||
|
|
||
| Instruments are addressed by their MetaTrader 5 symbol (for example | ||
| ``EURUSDm``, ``XAUUSDm`` or ``BTCUSD``). Construct assets with the ``forex`` | ||
| asset type so open positions reconcile against the orders you submit: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from lumibot.entities import Asset | ||
|
|
||
| asset = Asset("EURUSDm", asset_type="forex") | ||
|
|
||
| Example Usage | ||
| ------------- | ||
|
|
||
| **Creating the broker** | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from lumibot.brokers import TickerAll | ||
| from lumibot.traders import Trader | ||
|
|
||
| config = { | ||
| "API_KEY": "your_tickerall_api_key", | ||
| # "ACCOUNT_ID": "your_connected_account_id", # only if the key has several accounts | ||
| } | ||
| broker = TickerAll(config) | ||
|
|
||
| trader = Trader() | ||
| strategy = MyStrategy(broker=broker) | ||
| trader.add_strategy(strategy) | ||
| trader.run_all() | ||
|
|
||
| **Placing a market order with stop-loss and take-profit** | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from lumibot.entities import Asset, Order | ||
|
|
||
| asset = Asset("EURUSDm", asset_type="forex") | ||
| order = self.create_order( | ||
| asset=asset, | ||
| quantity=0.10, # volume in lots | ||
| side=Order.OrderSide.BUY, | ||
| order_type=Order.OrderType.MARKET, | ||
| # stop-loss / take-profit are attached as a bracket order | ||
| secondary_stop_price=1.0800, # stop-loss price | ||
| secondary_limit_price=1.1000, # take-profit price | ||
| ) | ||
| submitted_order = self.submit_order(order) | ||
| if submitted_order: | ||
| self.log_message(f"Placed order: ID={submitted_order.identifier}, Status={submitted_order.status}") | ||
|
|
||
| **Placing a limit order** | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| asset = Asset("XAUUSDm", asset_type="forex") | ||
| order = self.create_order( | ||
| asset=asset, | ||
| quantity=0.10, | ||
| side=Order.OrderSide.BUY, | ||
| order_type=Order.OrderType.LIMIT, | ||
| limit_price=2350.00, | ||
| ) | ||
| self.submit_order(order) | ||
|
|
||
| **Reading bars, last price and account value** | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| asset = Asset("EURUSDm", asset_type="forex") | ||
| bars = self.get_historical_prices(asset, 100, "day") | ||
| last = self.get_last_price(asset) | ||
| cash = self.get_cash() | ||
| self.log_message(f"Cash {cash}, last {last}, {len(bars.df)} bars") | ||
|
|
||
| **Closing a position** | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| asset = Asset("EURUSDm", asset_type="forex") | ||
| self.close_position(asset) | ||
|
|
||
| **Cancelling open (pending) orders** | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| for order in self.get_orders(): | ||
| if order.is_active(): | ||
| self.cancel_order(order) | ||
|
|
||
| .. note:: | ||
| The hosted API offers ``market``, ``limit`` and ``stop`` orders. ``stop_limit`` | ||
| and ``trailing_stop`` order types are not available and are rejected with a | ||
| clear message rather than being silently mishandled. Order volume is | ||
| expressed in lots. | ||
|
|
||
| Documentation | ||
| --------------- | ||
|
|
||
| .. automodule:: lumibot.brokers.tickerall | ||
| :noindex: | ||
| :members: | ||
| :undoc-members: | ||
| :show-inheritance: | ||
|
|
||
| .. automodule:: lumibot.data_sources.tickerall_data | ||
| :noindex: | ||
| :members: | ||
| :undoc-members: | ||
| :show-inheritance: | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.