Skip to content

Commit

Permalink
Rebuild (#7)
Browse files Browse the repository at this point in the history
This PR includes cleaning up the code and incorporating updates to enhance code quality and maintainability.
  • Loading branch information
Joaopeuko committed Dec 8, 2023
1 parent 7dec32f commit a373466
Show file tree
Hide file tree
Showing 22 changed files with 2,917 additions and 2,156 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Publish distributions

on:
push:
branches:
- '*'
tags:
- v*

jobs:
build-and-publish:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade setuptools
python -m pip install poetry
python -m pip install twine
python -m poetry install
- name: Build and publish to Test PyPI
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.TEST_PYPI }}
run: |
python -m poetry build
twine upload --skip-existing --repository-url https://test.pypi.org/legacy/ dist/*
- name: Publish to PyPI (if it's a new tag)
if: github.ref == 'refs/heads/master'
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: |
python -m poetry build
twine upload dist/*
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Project exclude paths
/tensor-venv/
.env
*.pyc
23 changes: 23 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
language_version: python3



- repo: https://github.com/psf/black
rev: 23.11.0
hooks:
- id: black
args: [--safe, --line-length=120]

- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
args: [--profile=black]
145 changes: 12 additions & 133 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,142 +1,21 @@
# Mql5-Python-Integration

I am working on a **new project** based on this one!
This new project is splitted into modules.
You can find the release of the **first module** on the links below.
## Project Overview

Keep in mind that **some features** that you can find on Mql5-Python-Integration **are not presented yet** on the new one,
but **I will add the features** and many new ones.
**Current Version: v0.6.0**

Welcome to the Mql5-Python-Integration project! This project facilitates the integration between MetaTrader 5 (Mql5) and Python, allowing for efficient algorithmic trading strategies.

My page:
https://joaopeuko.github.io/
## Project Update: Changes in Progress

Pypi of the first module:
https://pypi.org/project/metatrader5EasyT/
🚧 **Work in Progress: v0.6.0**
This project is currently undergoing significant changes and improvements. The latest version is v0.6.0, and various enhancements are being made to provide a more robust and user-friendly experience.

GitHub project
https://github.com/Joaopeuko/metatrader5EasyT
📌 **Previous Version: v0.5.0**
To access the code for the previous version, you can check it out at [v0.5.0](https://github.com/Joaopeuko/Mql5-Python-Integration/releases/tag/v0.5.0).

Project documentation
https://joaopeuko.github.io/metatrader5EasyT/
## Table of Contents

## Patron who makes the difference!

Help me make it possible. Be my [Patreon](https://www.patreon.com/joaopeuko).

## Introduction

I created this library because the development of an Expert Advisor in MQL5 can be complicated,while, in python, the same task flows better.

I believe that the main advantage of using python, instead of MQL5, to develop an expert advisor is the possibility to implement machine learning, deep learning, and reinforcement learning to your code in a faster way. However, I still don´t cover these.

I find that using the “MetaTrader module for integration with Python” is straightforward; yet, there is space for improvement.

Thus, I created this library, aiming to transform the experience of Expert Advisor Creation. Python is a versatile language; it counts with a wide range of useful libraries, which, in turn, allows the implementation of diverse ideas, easily.


## Installation


[pip](https://www.mql5.com/en/docs/integration/python_metatrader5) install MetaTrader5

```python
pip install MetaTrader5
```


## Expert Advisor

There is an expert advisor example for each different technology in the Expert Advisor folder.

### Simple Example

[example.py](https://github.com/Joaopeuko/Mql5-Python-Integration/blob/master/examples_of_expert_advisor/example.py)

It uses just the MetaTrader5 library in the implementation.

### Socket Example

[example_sockets_connection.py](https://github.com/Joaopeuko/Mql5-Python-Integration/blob/master/examples_of_expert_advisor/example_sockets_connection.py)


The “MetaTrader module for integration with Python” enables a wide range of possibilities; still, there is space for improvement. For instance, it lacks a connection to indicators.

That problem can be solved:
- Using Python indicators libraries that already exist through the internet.
- Re-creating all the indicators
- Creating a connection with MetaTrader5, asking for some indicators.

This example uses a socket connection to simplify the usage of indicators and it shows how easy it is to implement. You can find the indicator connection file [here](https://github.com/Joaopeuko/Mql5-Python-Integration/blob/master/include/indicator_connector.py).

To be able to use socket connection you need the client-side and the server-side to allow the communication.

The python code, that is used in this example, is the server-side that waits for the client-side to send the indicator result asked for.

The python code that is used in this example is the server-side that waits for the client-side to send the indicator result asked for. In order to use the client-side, it is possible to code it in MQL-language or to use the ones that I already created:

- [here](https://www.mql5.com/en/market/product/57574) - Free - 5 Indicators
- [here](https://www.mql5.com/en/market/product/58056) - Not Free - [38](https://www.mql5.com/en/docs/indicators) Indicators (5 from free indicators plus 33 new ones). The iCustom is not implemented.

To use the indicator in the expert advisor, you can call it providing the symbol name, all the other variables have pre-configured values. It always uses a time_frame for 1 minute.

[example_sockets_connection.py](https://github.com/Joaopeuko/Mql5-Python-Integration/blob/master/examples_of_expert_advisor/example_sockets_connection.py)

```python
from include.indicator_connector import Indicator

indicator = Indicator()

moving_average = indicator.moving_average(symbol='PETR4') # Brazilian Stock
```

You can check [this](https://github.com/Joaopeuko/Mql5-Python-Integration/blob/master/include/indicator_connector.py) file to find some tips when calling an indicator, for example, on how to change the method or the applied price.

```python
def moving_average(self,
symbol,
time_frame=1,
period=20,
start_position=0, # Change it if you want past values, zero is the most recent.
# method:
# 0 - MODE_SMA
# 1 - MODE_EMA
# 2 - MODE_SMMA
# 3 - MODE_LWMA
method=0,
# applied_price:
# 0 - PRICE_CLOSE
# 1 - PRICE_OPEN
# 2 - PRICE_HIGH
# 3 - PRICE_LOW
# 4 - PRICE_MEDIAN
# 5 - PRICE_TYPICAL
# 6 - PRICE_WEIGHTED
applied_price=0):
```

When using the indicators, please use try and except; sometimes the result can return "None."

[example_sockets_connection.py](https://github.com/Joaopeuko/Mql5-Python-Integration/blob/master/examples_of_expert_advisor/example_sockets_connection.py)

```python
from include.indicator_connector import Indicator

indicator = Indicator()

while True:
try:
moving_average = indicator.moving_average(symbol='PETR4') # Brazilian Stock
except TypeError:
pass
```

The indicator result is a dictionary.
```python
print(moving_average)
{'symbol': 'PETR4', 'time_frame': 1, 'period': 50, 'start_position': 0, 'method': 0, 'applied_price': 0, 'moving_average_result': 23.103}

print(moving_average['moving_average_result'])
23.103

```
- [Mql5-Python-Integration](#mql5-python-integration)
- [Project Update: Changes in Progress](#project-update-changes-in-progress)
- [Table of Contents](#table-of-contents)
45 changes: 45 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from mqpy.src.rates import Rates
from mqpy.src.tick import Tick
from mqpy.src.trade import Trade

# Initialize the trading strategy
trade = Trade(
expert_name="Example",
version=0.1,
symbol="EURUSD",
magic_number=567,
lot=1.0,
stop_loss=25,
emergency_stop_loss=300,
take_profit=25,
emergency_take_profit=300,
start_time="9:15",
finishing_time="17:30",
ending_time="17:50",
fee=0.5,
)

# Main trading loop
time = 0
while True:
# Fetch tick and rates data
tick = Tick(trade.symbol)
rates = Rates(trade.symbol, 1, 0, 1)

# Check for new tick
if tick.time_msc != time:
buy_signal = tick.last > rates.open
sell_signal = tick.last < rates.open

# Execute trading positions based on signals
trade.open_position(buy_signal, sell_signal, "Example Advisor")

time = tick.time_msc

# Check if it's the end of the trading day
if trade.days_end():
trade.close_position("End of the trading day reached.")
break

print("Finishing the program.")
print("Program finished.")
40 changes: 0 additions & 40 deletions examples_of_expert_advisor/example.py

This file was deleted.

18 changes: 0 additions & 18 deletions include/book.py

This file was deleted.

Loading

0 comments on commit a373466

Please sign in to comment.