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

 
Vladimir Perervenko :

Failed to install

Win10, Py3.6.10 and WinPy3.7.7.

Try it again. I had to refactor it to work with Python < 3.8

 
nicholi shen:

Try it again. I had to refactor it to work with Python < 3.8

Ok

Successfully built pymt5adapter
Installing collected packages: pymt5adapter
Successfully installed pymt5adapter-0.2.1
----- Установка "pymt5adapter" выполнена. -

What's new in the release?

Good luck

 
Vladimir Perervenko:

Ok

What's new in the release?

Good luck

calendar_eventshttps://github.com/nicholishen/pymt5adapter/blob/master/pymt5adapter/calendar.py

from datetime import datetime
from datetime import timedelta

import pymt5adapter as mt5
from pymt5adapter.calendar import calendar_events
from pymt5adapter.calendar import Currency
from pymt5adapter.calendar import Importance


def forex_symbol(s):
    modes = [mt5.SYMBOL_CALC_MODE_FOREX, mt5.SYMBOL_CALC_MODE_FOREX_NO_LEVERAGE]
    return s.trade_calc_mode in modes


def main():
    symbol = mt5.symbols_get(function=forex_symbol)[0]
    one_week = timedelta(weeks=1)
    now = datetime.now()
    default_one_week_ahead_all_events = calendar_events()
    filtered_by_callback = calendar_events(function=lambda e: 'fed' in e['event_name'].lower())
    filtered_by_flags = calendar_events(importance=Importance.MEDIUM | Importance.HIGH,
                                        currencies=Currency.USD | Currency.JPY)
    filtered_by_strings = calendar_events(importance='medium high',
                                          currencies='usdjpy')
    filtered_by_iterables = calendar_events(importance=('medium', ' high'),
                                            currencies=('usd', 'jpy'))
    filtered_by_specific_times = calendar_events(time_from=now, time_to=now + one_week)
    filtered_by_timedeltas = calendar_events(time_from=(-one_week), time_to=one_week)
    filtered_by_timedelta_lookback = calendar_events(-one_week)
    calendar_events_in_russian = calendar_events(language='ru')

    employment_events_next_month = calendar_events(
        currencies=Currency.USD,
        importance=Importance.HIGH
        function=lambda e: 'employment' in (name:=e['event_name'].lower()) or 'payroll' in name
    )

    next_event = employment_events_next_month[0]

    for k, v in next_event.items():
        print(k, v)


if __name__ == '__main__':
    with mt5.connected():
        main()
nicholishen/pymt5adapter
nicholishen/pymt5adapter
  • nicholishen
  • github.com
A drop-in pythonic adapter for the MetaTrader5 package to enhance usability. - nicholishen/pymt5adapter
 

Good afternoon! How do I work with TRADE_ACTION_CLOSE_BY ??? or who and how closes all open positions?

 
9805244:

Good afternoon! How to work with TRADE_ACTION_CLOSE_BY ??? or who and how closes all open positions?

Using Python?

 

This line is what VS2019(WinPy3.7.7) is ruling (underlined):

"unexpected token 'function' "

unexpected token ':"

unexpected token '='

invalid syntax

employment_events_next_month = calendar_events(
        currencies=Currency.USD,
        importance=Importance.HIGH
        
function=lambda e: 'employment' in (name
:=e['event_name'].lower()) 
or 'payroll' in name
    )
 
Vladimir Perervenko:

This line is what VS2019(WinPy3.7.7) is ruling (underlined):

"unexpected token 'function' "

unexpected token ':"

unexpected token '='

invalid syntax

That's the new assignment expression for python >=3.8, and they're awesome. I had to remove all those from the library to make it work with your version, but I think you can refactor that example script to make it work. You should consider upgrading your python interpreter to the latest version. :)

https://www.python.org/dev/peps/pep-0572/

PEP 572 -- Assignment Expressions
PEP 572 -- Assignment Expressions
  • www.python.org
PEP: Title: Author: Status: Type: Created: Python-Version: Post-History: Resolution:
 
nicholi shen:

That's the new assignment expression for python >=3.8, and they're awesome. I had to remove all those from the library to make it work with your version, but I think you can refactor that example script to make it work. You should consider upgrading your python interpreter to the latest version. :)

https://www.python.org/dev/peps/pep-0572/

Can't. Too many packages are tied into 3.7.7 and 3.6.10.

Just suggest what or how to replace it, if possible. If not, no problem.

I'm not an expert in Python. My language is R/.

Good luck.

 
Vladimir Perervenko:

I can not. Too many packages are tied to 3.7.7 and 3.6.10.

Just tell me what or how to replace, if possible. If not, no problem.

I'm not a Python expert. My language is R /

Good luck.

def employment_event(e):
    event_name = e['event_name'].lower()
    return 'payroll' in event_name or 'employment' in event_name


employment_events_next_month = calendar_events(
    currencies=Currency.USD,
    importance=Importance.HIGH,
    function=employment_event
)
 
Vladimir Perervenko:

Using Python?

yes