MetaTrader 5 Python User Group - how to use Python in Metatrader - page 42

 
Дмитрий Прокопьев:

Have the constants also changed?

On output:

yes the constants have the MT5_ prefix removed, here is an example of use:

from datetime import datetime
import MetaTrader5 as mt5

mt5.initialize()
mt5.wait()

print(mt5.terminal_info())
print(mt5.version())

ticks1 = mt5.copy_ticks_from("EURAUD", datetime(2020,1,28,13), 10000, mt5.COPY_TICKS_ALL)
ticks2 = mt5.copy_ticks_range("AUDUSD", datetime(2020,1,27,13), datetime(2020,1,28,13,1), mt5.COPY_TICKS_ALL)

rates1 = mt5.copy_rates_from("EURUSD", mt5.TIMEFRAME_M1, datetime(2020,1,28,13), 1000)
rates2 = mt5.copy_rates_from_pos("EURGBP", mt5.TIMEFRAME_M1, 0, 1000)
rates3 = mt5.copy_rates_range("EURCAD", mt5.TIMEFRAME_M1, datetime(2020,1,27,13), datetime(2020,1,28,13))

mt5.shutdown()

print('ticks1(', len(ticks1), ')')
print('ticks2(', len(ticks2), ')')
print('rates1(', len(rates1), ')')
print('rates2(', len(rates2), ')')
print('rates3(', len(rates3), ')')

import matplotlib.pyplot as plt

plt.plot(rates2['time'], rates2['low'], 'g-')
plt.plot(rates2['time'], rates2['high'], 'r-')

plt.show()

more complex example - Expert Advisor with trading on two moving averages without using history:

import MetaTrader5 as mt5
import time

mt5.initialize()
mt5.wait()

info = mt5.terminal_info()
if info.trade_allowed == False:
    print("Auto-trading disabled in Terminal, enable it")
    quit()

symbol = "EURUSD" #  currency
lot = 0.01;       #  buy lot
interval = 5      #  price requesting interval in sec
long_len = 11     # long moving average length
short_len = 7     # short moving average length
long_ma = []      # long ma list
short_ma = []     # short ma list
ticket = 0        #  ticket for sell

mt5.symbol_select(symbol)

#  wait some time
time.sleep(1)

print("\nPreparing...")
for i in range(long_len):
    p = mt5.symbol_info_tick(symbol)
    print(p.bid,'/',p.ask)
    avg = (p.ask + p.bid) / 2

    long_ma.append(avg)
    if i >= long_len - short_len:
        short_ma.append(avg)
    time.sleep(interval)

print("\nWorking...")
while True:
    p = mt5.symbol_info_tick(symbol)
    print(p.bid,'/',p.ask)
    avg = (p.ask + p.bid) / 2
    
    # short values
    prev_short = sum(short_ma) / short_len
    short_ma.pop(0)
    short_ma.append(avg)
    short = sum(short_ma) / short_len
    
    # long values
    prev_long = sum(long_ma) / long_len
    long_ma.pop(0)
    long_ma.append(avg)
    long = sum(long_ma) / long_len

    #  buy signal
    if prev_short < prev_long and short > long:
        print("BUY: ([-1]:",prev_short,"/",prev_long,", [0]:",short,"/",long,")")
        r = mt5.Buy(symbol, lot)
        if r.retcode != mt5.TRADE_RETCODE_DONE:
            print("Buy failed: ", r)
        else:
            ticket = r.order
    elif prev_short > prev_long and short < long and ticket>0:
        print("CLOSE: ([-1]:",prev_short,"/",prev_long,", [0]:",short,"/",long,")")
        r = mt5.Sell(symbol, lot, ticket=ticket)
        if r.retcode != mt5.TRADE_RETCODE_DONE:
            print("Sell failed: ", r)
        else:
            ticket = 0
           
    time.sleep(interval)

mt5.shutdown()
 
Almaz:

Yes, the constants have the MT5_ prefix removed, here is an example of use:

A more complex example is an EA with trading on two moving averages without using history:

Thank you.

The evolution of the lib is good. Question to developers - where is the roadmap for the pinova lib?

What innovations can we still expect, what not to expect?

 
I would like an update on the documentation, yeah
 

In the test server (MetaQuotes-Beta server, address 78.140.180.203:443) a beta version of terminal 2323 with new features is available. Public beta will be released tomorrow.

We have also released new MetaTrader 5.0.20 version for Python (pip install --upgrade metatrader5) with new syntax for account switching:

import MetaTrader5 as mt5

mt5.initialize(timeout=10000)
mt5.initialize(login=25035662, password="oxeb7lpb", server="MetaQuotes-Demo")
mt5.login(25035662)
mt5.login(25035662, timeout=10000)
mt5.login(25035662, "oxeb7lpb")
mt5.login(25035662, password="oxeb7lpb")
mt5.login(25035662, password="oxeb7lpb", server="MetaQuotes-Demo")

It is now possible to specify authorisation directly in the initialisation, in both full and short form.

The wait function is abolished and now the full initialization cycle with waiting is performed directly in initialize, where you can specify the waiting time in milliseconds.

The Python library is now guaranteed to find the latest active copy of the terminal even if it has been installed in portable mode (/portable key).


Python programs are already shown directly in the navigator:


This Friday's release will allow to run them as usual MQL5 scripts and they will be bound to charts.


Later we will add access to all (including custom) indicators in read mode to the Python library. This will make the work in python more productive. But this is not a priority yet, we will do it much later.

 
Renat Fatkhullin:

Also released a new version of MetaTrader 5.0.20 for Python (pip install --upgrade metatrader5) with new syntax for switching accounts

There are more features than MQL5.

 

In parallel, we are expanding the editor's capabilities and in the next release (not coming this Friday) it will be fully possible to use Clang/LLVM and Microsoft Visual Studio to compile C++ programs:


Perhaps we'll include C# too.


Extensive work to fully use SQLite databases: SQLite: native work with SQL databases in MQL5

This allows you to easily operate and exchange large amounts of data between different systems and within the terminal.

 
fxsaber:

More features than MQL5.

Are we getting there?

 
Renat Fatkhullin:

In parallel, we are expanding the editor's capabilities and in the next release (not coming this Friday)
will be fully capable of using Clang/LLVM and Microsoft Visual Studio to compile C++ programs:

A linker will be added for linking .lib files in
projects?

 
Roman:

For linking .lib files in projects
will a linker be added?

Multiple target variants can be specified within projects:



DLL/EXE files have custom defines, libraries and additional parameters for the compiler:

The linker is automatically used from Clang or Visual Studio. It does not need to be explicitly specified.

 
fxsaber:

There are more features than MQL5.

It is as a result that the MetaTrader 5 platform has more features.

By itself, any language is useless without an information and trading platform (a platform is not a terminal, but an entire complex of servers with an infrastructure and ecosystem) that feeds it with data and provides trade execution.

Our goal is to present a seamless platform with a broad set of tools for algotrading, covering different consumer segments.

Python is a clear winner in ML and a very effective language for doing research. Even though you can't get it into a tester, it's also good in direct integration mode.


By the way, unlike other Python libraries (often written head-on and poorly optimized), we have strongly optimized the payoff of massive charted data. This means that you can get deep history efficiently in memory and quickly.