MetaTrader 5 Python User Group - メタトレーダーで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() -big thnx, what we need.

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 のアナログ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]

まあ、ループを使ったヌードルコードとかね。

 
Rashid Umarov:

5.0.27にアップデート

スクリプトのバックアップ

結果

センス!

この部分は本当に快適です。

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

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

history_deals_getが構造体シーケンスに該当しないようです ...へえ

ただ、属性名を正しい順序で与える構成がないのが残念です。history_deals_get_asdict() が実現可能でないか、概念に反する場合 - 少なくともコレクションから_fieldsのアナログ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のウェブサイトのどこかに、プロダクト アップデートのお知らせのようなものがありますか?

いつから、どのような変更が行われたのか知りたい。

リファクタリングを計画するのはちょっと難しいですね。


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

こんにちは。

Rashidさん、ウェブサイトのどこかに「Product Update Announcement」はありませんか?

いつ、どのような変更が行われたかを把握できるように。

リファクタリングを計画するのは少し難しいです。


開発者の方々は、私たちからの継続・改善のためのコメントを待っています。しかし、今のところ、この分野でのユーザーの動きは見られません。

複雑な取引システム(端末(MT4/МТ5)<->TS(異なるIP上)<_>異なるデータベース)の一般的なインフラの問題については、別の枝で議論する必要があると思います。さまざまな構成、問題、困難、利点。

グッドラック