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

 

MetaTrader 5 for Python 5.0.33 released:

pip install --upgrade metatrader5
 
Christian:

copy_rates_from does not provide enough data on the M1 timeframe.


Is this the right course of action?

You have a limit on the number of bars in charts, you need to remove this limit in the Terminal (Tools->Options->Max bars in chart).
In future builds of Terminal we will return error (None) when trying such request.

 
Metaquotes :

MetaTrader 5 for Python 5.0.33 released:

Awesome! All unit tests passed. pymt5adapter has been upgraded to use MetaTrader 5.0.33.

Both packages can be upgraded with the following command.




pip install -U pymt5adapter 

https://pypi.org/project/pymt5adapter/


Note: The calendar module has been permanently removed from pymt5adapter

pymt5adapter
pymt5adapter
  • 2020.05.26
  • pypi.org
A drop in replacement wrapper for the MetaTrader5 package
 
1.BUY order send (): by EURUSD 0.01 lots at 1.0949 

2. order_send failed, retcode = 10016 
   retcode = 10016 
   deal = 0 
   order = 0 
   volume = 0.0 
   price = 0.0 
   bid = 0.0 
   ask = 0.0 
   comment = Invalid stops 
   request_id = 0 
   retcode_external = 0 

   request = TradeRequest (action = 1, magic = 234000, order = 0, symbol = 'EURUSD', volume = 0.01, price = 1.0949, stoplimit = 0.0, sl = 1.0939, tp = 1.0979, 
   deviation = 0, type = 0, type_filling = 0, type_time = 0, expiration = 0, commen 

t = 'EURUSD Buy.', Position = 0, position_by = 0) 
The initialisation of stoplimit = 0.0 simply failed, or rather it did, but the stoplimit became unacceptable.
 
Pithon will be exploring tomorrow with my granddaughter.
 

Python 5.0.33

@Almaz
Does not output matplotlib package figure.
From console or python IDE, the figure runs.

2020.05.28 22:47:51.654 mplib (EURUSD,M5)       Figure(640 x480)
# -*- coding: utf-8 -*-

import MetaTrader5 as mt5
import matplotlib.pyplot as plt

Win = 11

if not mt5.initialize("C:\Program Files\MetaTrader 5\\terminal64.exe"):
    print("mt5.initialize() failed, error code = ", mt5.last_error())
    quit()   
    
rates = mt5.copy_rates_from_pos("EURUSD", mt5.TIMEFRAME_M5, 1, Win)   
close = rates['close']       
    
mt5.shutdown()    


fig = plt.figure()

ax1 = fig.add_subplot(3, 1, 1)
ax2 = fig.add_subplot(3, 1, 2)
ax3 = fig.add_subplot(3, 1, 3)

ax1.plot(close, color=(0.60, 0.60, 0.60), linestyle='dashed', linewidth = 1)
ax2.plot(close, color=(0.60, 0.60, 0.60), linestyle='dashed', linewidth = 1)
ax3.plot(close, color=(0.60, 0.60, 0.60), linestyle='dashed', linewidth = 1)

plt.show()
 
Roman:

Python 5.0.33.

Does not display the matplotlib package figure.
From the console or python IDE, the figure starts.

Try this

pip install -U pymt5adapter pandas mplfinance

code

import mplfinance as mpf
import pandas as pd
import pymt5adapter as mta


def main():
    rates = mta.copy_rates_from_pos("EURUSD", mta.TIMEFRAME.H1, start_pos=0, count=50)
    df = pd.DataFrame(rates).rename(columns={'tick_volume': 'volume'})
    df.columns = (s.title() for s in df.columns)
    df['Time'] = pd.to_datetime(df['Time'], unit='s')
    df = df.set_index('Time')
    print(df)
    mpf.plot(df, type='candle', mav=(7, 15), volume=True)


if __name__ == '__main__':
    with mta.connected():
        main()


 
nicholi shen:

Try this

code

Thank you. mplfinance interesting library.
But I need a subplot.

Can this library display such a figure?

 
Roman:

Python 5.0.33

@Almaz
Does not output the matplotlib package figure.
From console or python IDE, the figure runs.

I assume this is running the script from under the Terminal? Checked, it works.

1. See what errors are written in the Experts tab (at the bottom of the Toolbox)

2. Check in MetaEditor settings that the correct path to the correct Python with matplotlib is in the settings, after changing the settings in MetaEditor you must restart the terminal, because terminal picks up these settings only at startup.

MetaEditor Compiler Options

3. it is better not to specify exact path to terminal in mt5.initialize() if you run python scripts from Terminal itself, they will find correct one by themselves, i.e. the one that started them.

 
Almaz:

I take it it's running the script from the Terminal? Tested it, it works.

1. See what errors are written in the Experts tab (at the bottom of the Toolbox)

2. Check in MetaEditor settings that the correct path to the correct Python with matplotlib is in the settings, after changing the settings in MetaEditor you must restart the terminal, because terminal picks up these settings only at startup.

3. it is better not to specify exact path to terminal in mt5.initialize() if you run python scripts from terminal itself, they will find correct one by themselves, i.e. start it.

Thanks Diamond.
There are no errors. In the Experts tab it was outputted:

2020.05.28 22:47:51.654 mplib (EURUSD,M5)       Figure(640 x480)

The paths are correct.
Removed the exact path in mt5.initialize(), restarted the terminal and the figure started to be output.

Reason: