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

 
Документация по MQL5: Интеграция / MetaTrader для Python / order_send
Документация по MQL5: Интеграция / MetaTrader для Python / order_send
  • www.mql5.com
[in]  Структура типа MqlTradeRequest, которая описывает требуемое торговое действие. Обязательный неименованный параметр. Пример заполнения запроса и состав перечислений смотрите ниже. Идентификатор эксперта. Позволяет организовать аналитическую обработку торговых ордеров. Каждый эксперт может выставлять свой собственный уникальный...
 
AxxelTrader:

Upgraded to version 2432.
But the error is still there...
Tried it on different brokers even...

r = mt5.Buy(...)
if r is None:
    print(mt5.last_error())
else:
    if r.retcode ...
 

copy_rates_from does not provide enough data on the M1 timeframe.


Is this the right course of action?

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

register_matplotlib_converters()
import MetaTrader5 as mt5


 if not mt5.initialize():
    print("initialize() failed")
    mt5.shutdown()


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

timezone = pytz.timezone("Etc/UTC")
utc_from = datetime ( 2020 , 1 , 10 , tzinfo=timezone)


eurusd_rates_m1 = mt5.copy_rates_from("EURUSD", mt5.TIMEFRAME_M1, utc_from, 1000 )
print('M1 eurusd_rates(', len(eurusd_rates_m1), ')')
eurusd_rates_m5 = mt5.copy_rates_from("EURUSD", mt5.TIMEFRAME_M5, utc_from, 1000 )
print('M5 eurusd_rates(', len(eurusd_rates_m5), ')')
eurusd_rates_m10 = mt5.copy_rates_from("EURUSD", mt5.TIMEFRAME_M10, utc_from, 1000 )
print('M10 eurusd_rates(', len(eurusd_rates_m10), ')')
eurusd_rates_m15 = mt5.copy_rates_from("EURUSD", mt5.TIMEFRAME_M15, utc_from, 1000 )
print('M15 eurusd_rates(', len(eurusd_rates_m15), ')')
eurusd_rates_m30 = mt5.copy_rates_from("EURUSD", mt5.TIMEFRAME_M30, utc_from, 1000 )
print('M30 eurusd_rates(', len(eurusd_rates_m30), ')')


mt5.shutdown()
M1 eurusd_rates( 1 )      ???
M5 eurusd_rates( 1000 )
M10 eurusd_rates( 1000 )
M15 eurusd_rates( 1000 )
M30 eurusd_rates( 1000 )
 
I know this topic will quickly become very long. Metaquotes needs to add a new subforum (Python) for each language.
 
Christian:

copy_rates_range does not provide enough data on the M1 timeframe.


Is this the right course of action?

Pay attention to note on copy rates range

Note

See the CopyRates() function for more information.

The MetaTrader 5 terminal provides bars only within a history available to a user on charts. The number of bars available to users is set in the "Max. bars in chart" parameter.

When creating the 'datetime' object, Python uses the local time zone, while MetaTrader 5 stores tick and bar open time in UTC time zone (without the shift). Therefore, 'datetime' should be created in UTC time for executing functions that use time. Data received from the MetaTrader 5 terminal has UTC time.


Documentation on MQL5: Integration / MetaTrader for Python / copy_rates_range
Documentation on MQL5: Integration / MetaTrader for Python / copy_rates_range
  • www.mql5.com
# create 'datetime' objects in UTC time zone to avoid the implementation of a local time zone offset # get bars from USDJPY M5 within the interval of 2020.01.10 00:00 - 2020.01.11 13:00 in UTC time zone                  time     open     high      low    close  tick_volume  spread  real_volume...
 
Rashid Umarov:

Pay attention at Note on copy rates range

There is a bug in copy rates. I posted this several times but nobody has acknowledged it.

import MetaTrader5 as mt5

mt5.initialize()
maxbars = mt5.terminal_info().maxbars
for count in range(maxbars):
    rates = mt5.copy_rates_from_pos('EURUSD', mt5.TIMEFRAME_M1, 0, count)
    errno, strerror = mt5.last_error()
    if errno != mt5.RES_S_OK:
        print(f"Failed on count={count} with strerror={strerror}")
        break
mt5.shutdown()

##
# Failed on count=7844 with strerror=IPC recv failed
##
 
It's a shame, but my understanding is that there is a problem with the initialisation of structures in MT.
 
By the way, I have to introduce my granddaughter to Python and I don't know him... Bye.
 

I have a script that ran for a few hours. but then I start getting these errors, Invalid_Stops, and then it does not open anymore trades.

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) 

Can anybody please help direct where to start looking for this issue?

Thank you

 
fbrand:

I have a script that ran for a few hours. but then I start getting these errors, Invalid_Stops, and then it does not open anymore trades.

Can anybody please help direct where to start looking for this issue?

Thank you

What was the current price (Bid and Ask)? What was the freeze level (SYMBOL_TRADE_FREEZE_LEVEL)?