MetaTrader 5 Python User Group - the summary - page 22

 

Practical application of neural networks in trading. Python (Part I) - the article

Practical application of neural networks in trading. Python (Part I)

In the previous article entitled "Practical application of neural networks in trading. It's time to practice", we considered the practical application of a neural network module implemented using Matlab neural networks. However, that article did not cover questions related to the preparation of input data and network training related operations. In this article, we will consider these questions using examples and will implement further code using neural networks of libraries working with Python. This time, the trading system implementation principles will be different. This variant was briefly described in paragraph 5 of the basic article "Practical application of neural networks in trading". For this implementation, we will use the TensorFlow machine learning library developed by Google. We will also use the Keras library for describing neural networks.
Practical application of neural networks in trading. Python (Part I)
Practical application of neural networks in trading. Python (Part I)
  • www.mql5.com
In this article, we will analyze the step-by-step implementation of a trading system based on the programming of deep neural networks in Python. This will be performed using the TensorFlow machine learning library developed by Google. We will also use the Keras library for describing neural networks.
 

New version of MetaTrader5-Python-5.0.34 available for MetaTrader 5 build 2765

pip install --upgrade metatrader5

Functions added market_book_add, market_book_release, market_book_get

import MetaTrader5 as mt5
import time

mt5.initialize()

print(mt5.version())
print(mt5.last_error())

mt5.market_book_add('EURUSD')

for i in range(10):
    time.sleep(5)
    print()
    items = mt5.market_book_get('EURUSD')
    print(items)
    # alternative
    #if items:
    #    for it in items:
    #        print(it._asdict())

mt5.market_book_release('EURUSD')

mt5.shutdown()
 

Hi,


I python programers and I now find python module metatrader5


I trying simple test, bud cannot get value from symbol:

I trying in jupyter:

from datetime import datetime 
import matplotlib.pyplot as plt 
import pandas as pd 
from pandas.plotting import register_matplotlib_converters 
register_matplotlib_converters() 

import MetaTrader5 as mt5 
mt5.initialize( 
   login=66xxx,             
   password="gggggg"       
   )

print("MetaTrader5 package author: ",mt5.__author__) 
print("MetaTrader5 package version: ",mt5.__version__) 

out:
MetaTrader5 package author:  MetaQuotes Software Corp.
MetaTrader5 package version:  5.0.34

# get the number of financial instruments 
symbols=mt5.symbols_total() 
if symbols>0: 
    print("Total symbols =",symbols) 
else: 
    print("symbols not found") 

out:
Total symbols = 38

but problem is:

# attempt to enable the display of the EURJPY symbol in MarketWatch 
selected=mt5.symbol_select("EURJPY",True) 
if not selected: 
    print("Failed to select EURJPY = ",mt5.last_error()) 

out:

Failed to select EURJPY =  (-1, 'Terminal: Call failed')

where I have bug?


Thanks

EDIT:

I trying also from script with the same - cannot select the symbol :(

 

Hi all,


I'm facing an issue with the OrderSendResult structure returned after an order_send() request using the Python API. One of the fields returned is the "comment", which should contain broker's comment to operation. However, this field is returning the same comment that was passed to the request, and not the broker return, as it should.


To reproduce the bug do the following:

1. Create a request that will generate a comment as a result. Fill the request "comment" with a dummy commentary.

2. Send the request using the order_send() command from the Python API.

3. On the OrderSendResult object returned, check the "comment" field.


The "comment" field will have the same comment passed, when it should have the comment returned by the broker, which can be checked in the Journal tab of the MT5 Terminal.


Can someone confirm this? Is there any workaround to get the broker's comment?


 
nvicki112:

ERROR: Could not find a version that satisfies the requirement MetaTrader5 (from versions: none)

ERROR: No matching distribution found for MetaTrader5


I couldn't install MetaTrader5 using pip install. The error was as below.

Anyone can help me? Thanks a lot.

Uninstall your Python from PC.

Download the latest version of Python and double clic to start installation.
Select the Add to path option like the link below indicate.
Run the pip install by Windows Prompt.
Enjoy

https://datatofish.com/add-python-to-windows-path/

How to add Python to Windows PATH
How to add Python to Windows PATH
  • datatofish.com
There are few ways in which you can add Python to Windows PATH. In this guide, I’ll share with you two methods that you can use to add Python to Windows path: But why would you want to add Python to Windows path in the first place? Well, if you try to install a Python package using PIP for example, you may get the following error in the Windows...
 
MetaQuotes:

New version of MetaTrader5-Python-5.0.34 available for MetaTrader 5 build 2765

Functions added market_book_add, market_book_release, market_book_get

Hi, the function maket_book_add doesn't seems to trigger internal MT5 OnBookEvent. Is that correct?

I'm trying to communicate between MT5 and Python via sockets when a new BookEvent arrives (as Python doesn't have access to Events), but it's not working. The event was never triggered inside of MetaTrader.

Python receives book data normally with mt5.market_book_get.

Steps to reproduce:

1. Run an EA just printing a new BookEvent (code attached below)

2. Run a Python script that connects to MetaTrader and do a market_book_add on any symbol (code attached below)

3. Check on Metatrader experts log if something is printed.


EA code:

//+------------------------------------------------------------------+
//|                                                  EventPython.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

// Just add different symbol from Python to check if the book event is working inside of MT5
string mainSymbol = "CIEL3";

int OnInit()
{   
   MarketBookAdd(mainSymbol);
   return(INIT_SUCCEEDED);
}

void OnBookEvent(const string &symbol)
{
        Print("Book event - ", symbol);
        // Just print once for testing and then remove from book
        MarketBookRelease(mainSymbol);
}

Python code:

import MetaTrader5 as mt5
import time

# Your MT5 exec path
path = "C:\\Program Files\\MetaTrader 5 Terminal\\terminal64.exe"
# Symbol you want to be checked
symbol = "PETR4"

# Connecting to MT5
if not mt5.initialize(path):
    print("initialize() failed")
    mt5.shutdown()

# Add symbol to market watch
selected=mt5.symbol_select(symbol,True)
if not selected:
    print("Failed to select", symbol, ", error code =",mt5.last_error())
else:
    # Add symbol to book
    bookAdd = mt5.market_book_add(symbol)
    if not bookAdd:
        print("Failed to add", symbol, "to book, error code =",mt5.last_error())
    else:
        # get book array
        for i in range(10):
            bookItems = mt5.market_book_get(symbol)
            if bookItems is not None:
                print(bookItems)
                break
    
    # Sleep a while. Go to MT5 and check if EA is printing the BookEvent
    time.sleep(60)

    # Remove symbol from book
    mt5.market_book_release(symbol)

mt5.shutdown()

Thanks in advance,

Daniel

 
Daniel Faustino:

Hi, the function maket_book_add doesn't seems to trigger internal MT5 OnBookEvent. Is that correct?

I'm trying to communicate between MT5 and Python via sockets when a new BookEvent arrives (as Python doesn't have access to Events), but it's not working. The event was never triggered inside of MetaTrader.

Python receives book data normally with mt5.market_book_get.

Steps to reproduce:

1. Run an EA just printing a new BookEvent (code attached below)

2. Run a Python script that connects to MetaTrader and do a market_book_add on any symbol (code attached below)

3. Check on Metatrader experts log if something is printed.


EA code:

Python code:

Thanks in advance,

Daniel


What you have output from this code?

# Add symbol to market watch
selected=mt5.symbol_select(symbol,True)
if not selected:
    print("Failed to select", symbol, ", error code =",mt5.last_error())
 
NomeX5cor:


What you have output from this code?

selected is always true to me. 

It goes through until the end of code, it prints the book.

But the OnBookEvent was never triggered inside MT5.

 

Practical application of neural networks in trading. Python (Part I) - the article

In the previous article entitled "Practical application of neural networks in trading. It's time to practice", we considered the practical application of a neural network module implemented using Matlab neural networks. However, that article did not cover questions related to the preparation of input data and network training related operations. In this article, we will consider these questions using examples and will implement further code using neural networks of libraries working with Python. This time, the trading system implementation principles will be different. This variant was briefly described in paragraph 5 of the basic article "Practical application of neural networks in trading". For this implementation, we will use the TensorFlow machine learning library developed by Google. We will also use the Keras library for describing neural networks.

Practical application of neural networks in trading (Part 2). Computer vision

An essential problem in preparing data to training neural networks designed for trading, is related the preparation of the necessary input data. For example, consider the case when we use a dozen indicators. These indicators may represent a set of several informative charts. If we calculate these indicators to a certain depth, then as a result we will get up to a hundred entries, and in some cases even more. Can we make neural network training easier by using computer vision? To solve this problem, let us use convolutional neural networks, which are often utilized to solve classification and recognition problems.
Practical application of neural networks in trading (Part 2). Computer vision
Practical application of neural networks in trading (Part 2). Computer vision
  • www.mql5.com
The use of computer vision allows training neural networks on the visual representation of the price chart and indicators. This method enables wider operations with the whole complex of technical indicators, since there is no need to feed them digitally into the neural network.
Reason: