This module use the Metatatrader5 library to connect with the platform, the functions were adapted to make it easier
Project description
- Installation
- Introduction
- MT5
- Constructor Method
- start()
- close()
- account_details(show=0)
- display_symbols(keyword,spread=30)
- open_position(symbol,operation,lot,points=40,comment="Python")
- get_positions(symbol=None)
- close_position(ticket, comment="Close", display=False)
- get_data(symbol, temp, n_periods, plot=0)
- calculate_profit(symbol,points,lot,order)
Installation
- Create a virtual env to avoid issues between versions using
python -m venv name_env - Activate virtual env and run
pip install pip install atlas-algo-trading - Go to the examples section and modify the .env file with your own data to start interacting with MT5.
Note: You need to have a terminal of Metatrader 5 in your computer to work.
Introduction
This module use the Metatatrader5 library to connect with the platform, the functions were adapted to launch operations with own parameters and conditions.To know more information about the functions of Metatrade 5, please refer the next documentation:
https://www.mql5.com/en/docs/integration/python_metatrader5
Next you can read more about each function and how to implement it.
MT5
This class is a wrapper for the MT5 library that contains all related methods to interact with Metatrader 5 such as:
Constructor Method
Create an object to enable the connection with MT5
Parameters
- User (int) --> Account Id
- Password (str) --> Password of the Account
- Server (str) --> Server of the Account
Example:
user = 12345
password = "passwd123"
server = "MetaQuotes-Demo"
conn = MT5(user,password,server)
Note: By default the contructor method call the start method to start the connection to the MT5 server which will open the terminal of MT5.
start()
Create a connection to the Metatrader 5 server.
Example:
conn.start()
close()
Close the connection to Metatrader5 server.
Example:
conn.close()
account_details(show=0)
Return an object of type AccountInfo from Metatrader 5 library that contains all the information related to the account.
Note: If you want to print the info pass 1 as and argument.
Example:
# Display object with attributes
conn.account_details(1)
# Save balance into a variable
balance = conn.account_details().balance
display_symbols(keyword,spread=30)
Parameters
- keyword (list) --> Keywords to match the symbols.
- spread (int) --> Maximum value of spread of the symbols.
The method will return a DataFrame with most relevant data of the symbols that satisfy the criteria..
Example:
# Filter the symbols that contains "EUR" or "USD" and the spread value is less than 30
symbols = conn.display_symbols(["EUR","USD"],30)
open_position(symbol,operation,lot,points=40,comment="Python")
Parameters
- symbol (str) --> Name of the symbol exactly as in the broker appears
- operation (int) --> BUY(1) / SELL(0)
- lot (int) --> Size of the operation to open
- points (int) or (list)
- comment: Comment displayed in the MT5 console.
- a) Number of points to set the SL and TP from the entry price. This will follow 1:1 ratio.
- b) [SL,TP] a list with the specific values of the price where the SL and TP should be set.
This method create and send a request to execute the position with the input parameters.
Note: Use the display_symbols() to retrive the right name of the symbol.
Example:
SELL 0.2 lots in EURUSD with 40 points as SL/TP
order_id = conn.open_position("EURUSD",0,0.2,40,"This trade was executed from my code")
get_positions(symbol=None)
Parameters
- symbol (str) --> Get trades info exclusively of the symbol passed
Returns a pandas dataframe with trades open if exists.
Example:
df = conn.get_positions()
close_position(ticket, comment="Close", display=False)
Parameters
- ticket (int) --> Ticket number of the trade to close
- comment (str) --> Comment to add to the order.
This method create and send the request to close the position with passed args.
Examples:
ticket_id = 12345
conn.close_position(ticket_id,"Trade closed from my code")
get_data(symbol, temp, n_periods, plot=0)
Parameters
- symbo (str) --> Name of the symbol
- temp (str) --> TimeFrame to get data (M1,M3,H1, etc)
- n_periods (int) --> Number of bars to get from current time (Current time - n_periods)
- plot (int) --> Display a chart in japanese format
- 1 - Plot the DataFrame
- 0 - Only returns the DataFrame (default)
Example:
Return a dataFrame with the last 100 min and plot it.
data_from_n_periods = MT5.get_data("EURUSD","M1",100,1)
To check the correct timeframes print the next code:
print(MT5.timeframes)
In this example the name of the stock was manually passed, remember use the aproppiate method to extract the name exactly as the broker to avoid errors.
calculate_profit(symbol,points,lot,order)
Parameters
- symbol:Name of the symbol --> str
- points: Number of points to calculate the profit/loss --> int
- lots: Size of the simulated trade --> float/int
- order: BUY (1) or SELL (0) --> int
This method allow you to calculte the profit or loss without need to open trades.
Examples
Profit from a trade in EURUSD symbol
profit = MT5.calculate_profit("EURUSD",40,0.1,0)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file atlas_algo_trading-1.0.2.tar.gz.
File metadata
- Download URL: atlas_algo_trading-1.0.2.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1421efcb3cec988ab867b690b12ae9b5064ae591946c51e6760a8344f8a4522a
|
|
| MD5 |
48d40c0d971c0358a1a045414f5fd81a
|
|
| BLAKE2b-256 |
e50e3892ca550302b9be034aea6b6f90a8fc36bd3b6726538463562dfee1f5c1
|
File details
Details for the file atlas_algo_trading-1.0.2-py3-none-any.whl.
File metadata
- Download URL: atlas_algo_trading-1.0.2-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7587f6319ec0b6a47c00d3b72be184c59fa31c44a823bcfef125358fd8401487
|
|
| MD5 |
4fb25b102b79b485a5946f6b7b1e7dc8
|
|
| BLAKE2b-256 |
8244632eb4e0787f22a22d749224304fb8a2788580e37d1592cfb38da240bcf9
|