MetaTrader 5 Python用户组 - 如何在Metatrader中使用Python - 页 54

 

更新至5.0.27

  pip install --upgrade MetaTrader5

备份脚本

import MetaTrader5 as mt5
#  выведем данные о пакете MetaTrader5
print("MetaTrader5 package author: ",mt5.__author__)
print("MetaTrader5 package version: ",mt5.__version__)

#  установим подключение к терминалу MetaTrader 5
if not mt5.initialize():
    print("initialize() failed")
    mt5.shutdown()

#  подключимся к торговому счету с указанием пароля и сервера
authorized=mt5.login(25115284, password="ваш_пароль",server="MetaQuotes-Demo")
if(authorized):
    #  выведем данные о торговом счете
    #print(mt5.account_info())
    account_info=mt5.account_info()._asdict()
    print(account_info)
    print("Вывод каждого свойства отдельно:")
    for property in account_info:
        print("   ",property,"=",account_info[property])
else:
    print("failed to connect to trade account 25115284 with password=gqz0lbdm")

mt5.shutdown()

结果

MetaTrader5 package author:  MetaQuotes Software Corp.
MetaTrader5 package version:  5.0.27
{'login': 25115284, 'trade_mode': 0, 'leverage': 100, 'limit_orders': 200, 'margin_so_mode': 0, 'trade_allowed': True, 'trade_expert': True, 'margin_mode': 2, 'currency_digits': 2, 'fifo_close': False, 'balance': 97639.46, 'credit': 0.0, 'profit': -178.77, 'equity': 97460.69, 'margin': 704.8, 'margin_free': 96755.89, 'margin_level': 13828.134222474464, 'margin_so_call': 50.0, 'margin_so_so': 30.0, 'margin_initial': 0.0, 'margin_maintenance': 0.0, 'assets': 0.0, 'liabilities': 0.0, 'commission_blocked': 0.0, 'name': 'MetaQuotes Dev Demo', 'server': 'MetaQuotes-Demo', 'currency': 'USD', 'company': 'MetaQuotes Software Corp.'}
Вывод каждого свойства отдельно:
    login = 25115284
    trade_mode = 0
    leverage = 100
    limit_orders = 200
    margin_so_mode = 0
    trade_allowed = True
    trade_expert = True
    margin_mode = 2
    currency_digits = 2
    fifo_close = False
    balance = 97639.46
    credit = 0.0
    profit = -178.77
    equity = 97460.69
    margin = 704.8
    margin_free = 96755.89
    margin_level = 13828.134222474464
    margin_so_call = 50.0
    margin_so_so = 30.0
    margin_initial = 0.0
    margin_maintenance = 0.0
    assets = 0.0
    liabilities = 0.0
    commission_blocked = 0.0
    name = MetaQuotes Dev Demo
    server = MetaQuotes-Demo
    currency = USD
    company = MetaQuotes Software Corp.

 
Vladimir Perervenko:

你能告诉我更多关于定时器的信息吗?

我自己没有研究过,只是搜索了一下

 
Almaz:

5.0.27已经有了,所有的结构序列(类似于C API的命名元组)都增加了_asdict()方法

mt5.symbol_info()._asdict() -大的感谢,我们需要的。

history_deals_get似乎不在结构序列中 ...嘿嘿。

    deal = mt5.history_deals_get(position=order.position_id)._asdict()
AttributeError: 'tuple' object has no attribute '_asdict'

只是严重缺少一个能以正确顺序给出属性名称的结构。如果对于history_deals_get_asdict()是不可实现的或与概念相矛盾的--至少是一个类似于_fields fromcollections 的概念namedtuple (python),那么我们就可以拉出正确的属性顺序,不是在循环中手动拉,而是手动拉到目前为止,它的结果是这样的。

       orders_deal = mt5.history_deals_get(from_date, to_date)
        orders_deal_frame = pd.DataFrame(orders_deal)
        print(orders_deal_frame.head())

和输出。

          0          1           2              3   4   5   6   ...   11   12      13   14      15         16  17
0  519632807          0  1583873757  1583873757976   2   0   0  ...  0.0  0.0  1000.0  0.0
1  519653875  541985093  1583875090  1583875090615   1   0   0  ...  0.0  0.0     0.0  0.0  EURUSD  541984991
2  519654046  541985264  1583875102  1583875102086   0   0   0  ...  0.0  0.0     0.0  0.0  USDCHF  541984999
3  519654270  541985482  1583875116  1583875116676   0   0   0  ...  0.0  0.0     0.0  0.0  USDJPY  541985006
4  519654761  541985971  1583875151  1583875151725   1   0   0  ...  0.0  0.0     0.0  0.0  EURUSD  541985807

[5 rows x 18 columns]

好吧,或者说是带循环的noodlecode。

 
Rashid Umarov:

更新至5.0.27

备份脚本

结果

Sens!

在这部分真的很舒服。

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

mt5.symbol_info()._asdict() -big thnx, that's it.

history_deals_get似乎并不属于结构序列 ...嘿嘿。

只是严重缺少一个能以正确顺序给出属性名称的结构。如果对于history_deals_get_asdict()是不可实现的或与概念相矛盾的--至少是一个类似于_fields fromcollections 的概念namedtuple (python),那么我们就可以拉出正确的属性顺序,不是在循环中手动拉,而是手动拉到目前为止,它的结果是这样的。

和输出。

或者用循环的面条代码。

history_deals_get 总是返回一个正常的Python元组,里面有一个名为TradeDeal的集合。为了使其发挥作用,你需要访问一些索引。

r = mt5.history_deals_get(position=544536443)
print(r[0]._asdict())
 
Дмитрий Прокопьев:

mt5.symbol_info()._asdict() -big thnx, that's it.

history_deals_get似乎并不属于结构序列 ...嘿嘿。


这样试试吧。

import MetaTrader5 as mt5
#  выведем данные о пакете MetaTrader5
print("MetaTrader5 package author: ",mt5.__author__)
print("MetaTrader5 package version: ",mt5.__version__)

#  установим подключение к терминалу MetaTrader 5
if not mt5.initialize():
    print("initialize() failed")
    quit()

#  проверим наличие открытых позиций
positions=mt5.positions_total()
if(positions>0):
    print("Total positions=",positions)
    #  выведем все открытые позиции
    positions=mt5.positions_get()
    count=0
    for position in positions:
        count+=1
        print(count,":",position)
        position_info=position._asdict()
        for property in position_info:
            print("   ",property,"=",position_info[property])
        if count>=5:
            break
else:
    print("Positions not found")

#  завершим подключение к терминалу MetaTrader 5
mt5.shutdown()

结果

MetaTrader5 package author:  MetaQuotes Software Corp.
MetaTrader5 package version:  5.0.27
Total positions= 80
1 : TradePosition(ticket=543426097, time=1584009003, time_msc=1584009003243, time_update=1584009003, time_update_msc=1584009003243, type=1, magic=0, identifier=543426097, reason=3, volume=0.01, price_open=1.12686, sl=1.14372, tp=1.10328, price_current=1.10665, swap=-0.01, profit=20.21, symbol='EURUSD', comment='', external_id='')
    ticket = 543426097
    time = 1584009003
    time_msc = 1584009003243
    time_update = 1584009003
    time_update_msc = 1584009003243
    type = 1
    magic = 0
    identifier = 543426097
    reason = 3
    volume = 0.01
    price_open = 1.12686
    sl = 1.14372
    tp = 1.10328
    price_current = 1.10665
    swap = -0.01
    profit = 20.21
    symbol = EURUSD
    comment = 
    external_id = 
2 : TradePosition(ticket=543438758, time=1584009602, time_msc=1584009602982, time_update=1584009602, time_update_msc=1584009602982, type=1, magic=0, identifier=543438758, reason=3, volume=0.01, price_open=1.1261700000000001, sl=1.14566, tp=1.09924, price_current=1.10665, swap=-0.01, profit=19.52, symbol='EURUSD', comment='', external_id='')
    ticket = 543438758
    time = 1584009602
    time_msc = 1584009602982
    time_update = 1584009602
    time_update_msc = 1584009602982
    type = 1
    magic = 0
    identifier = 543438758
    reason = 3
    volume = 0.01
    price_open = 1.1261700000000001
    sl = 1.14566
    tp = 1.09924
    price_current = 1.10665
    swap = -0.01
    profit = 19.52
    symbol = EURUSD
    comment = 
    external_id = 
3 : TradePosition(ticket=543438858, time=1584009610, time_msc=1584009610242, time_update=1584009610, time_update_msc=1584009610242, type=1, magic=0, identifier=543438858, reason=3, volume=0.02, price_open=1.12622, sl=1.14531, tp=1.09973, price_current=1.10665, swap=-0.02, profit=39.14, symbol='EURUSD', comment='', external_id='')
    ticket = 543438858
    time = 1584009610
    time_msc = 1584009610242
    time_update = 1584009610
    time_update_msc = 1584009610242
    type = 1
    magic = 0
    identifier = 543438858
    reason = 3
    volume = 0.02
    price_open = 1.12622
    sl = 1.14531
    tp = 1.09973
    price_current = 1.10665
    swap = -0.02
    profit = 39.14
    symbol = EURUSD
    comment = 
    external_id = 
4 : TradePosition(ticket=543521116, time=1584014410, time_msc=1584014410921, time_update=1584014410, time_update_msc=1584014410921, type=1, magic=0, identifier=543521116, reason=3, volume=0.02, price_open=1.1245, sl=1.14334, tp=1.09808, price_current=1.10665, swap=-0.02, profit=35.7, symbol='EURUSD', comment='', external_id='')
    ticket = 543521116
    time = 1584014410
    time_msc = 1584014410921
    time_update = 1584014410
    time_update_msc = 1584014410921
    type = 1
    magic = 0
    identifier = 543521116
    reason = 3
    volume = 0.02
    price_open = 1.1245
    sl = 1.14334
    tp = 1.09808
    price_current = 1.10665
    swap = -0.02
    profit = 35.7
    symbol = EURUSD
    comment = 
    external_id = 
5 : TradePosition(ticket=543686473, time=1584024008, time_msc=1584024008400, time_update=1584024008, time_update_msc=1584024008400, type=1, magic=0, identifier=543686473, reason=3, volume=0.01, price_open=1.12238, sl=1.13967, tp=1.09793, price_current=1.10665, swap=-0.01, profit=15.73, symbol='EURUSD', comment='', external_id='')
    ticket = 543686473
    time = 1584024008
    time_msc = 1584024008400
    time_update = 1584024008
    time_update_msc = 1584024008400
    type = 1
    magic = 0
    identifier = 543686473
    reason = 3
    volume = 0.01
    price_open = 1.12238
    sl = 1.13967
    tp = 1.09793
    price_current = 1.10665
    swap = -0.01
    profit = 15.73
    symbol = EURUSD
    comment = 
    external_id = 
 
Almaz:

history_deals_get总是返回一个正常的Python元组,里面有一个名为TradeDeal的集合。为了使其工作,我们需要访问一些索引。

啊哈,非常感谢你,我已经通过类比找到了这一点。感觉。

如果在未来的版本中,不仅可以用history_deals_get(和类似物)选择命名的元组,而且说list[_asdict()]将只是

奇妙。;)谢谢。

 
Rashid Umarov:

这样试试吧。

结果

谢谢你!它正在发挥作用。

哦,感谢你对:)改进解放军的建议的回应。

 
Rashid Umarov:

更新至5.0.27

备份脚本

结果

下午。

Rashid,MetaTrader5网站上是否有某种产品 更新公告?

我想知道什么时候以及实施了哪些变化。

对重构进行规划是有点困难的。


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

下午好。

拉希德,在网站的某个地方是否有产品 更新公告?

这样,你就可以追踪到什么时候做了什么改变。

对重构进行规划有点困难。


我正在关注Pypi,开发人员正在等待我们的意见,以便继续/改进。但到目前为止,我们没有看到这个领域的任何用户活动。

我认为,我们需要在一个单独的分支中讨论复杂交易系统的一般基础设施问题(终端(MT4/МТ5)<->TS(在不同的IP上)<_>不同的数据库。不同的配置、问题、困难、优势。

祝好运