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

 
nicholi shen:

Thank you for the tip. I've read the materials on your link. It's a bit complicated.

 
9805244:

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

https://www.mql5.com/en/forum/306742/page10#comment_15577952

MetaTrader 5 Python User Group - the summary
MetaTrader 5 Python User Group - the summary
  • 2020.03.11
  • www.mql5.com
The Main Study MetaTrader Python online documentation Python Releases for Windows - website MetaTrader5 : Python Package - website...
 
nicholi shen:

https://www.mql5.com/en/forum/306742/page10#comment_15577952

Thanks for the example.

I have a similar script in MKL that closes all profitable positions first.

Can we do the same in your script?

Good luck

PS: You have done so. Saw.

And this script is for netting accounts?
 
Vladimir Perervenko:

Thanks for the example.

In my MKL, a similar script first closes all the profitable positions.

Can you do the same in your script?

Good luck

PS: You have done so. Had seen.

And this script for netting accounts?

I usually prioritize position closing based on risk (position size), but if you want close based on overall profit then then you just have to modify how you sort the symbols.

def open_position_symbols():
    positions = {}
    for p in mt5.positions_get():
        positions.setdefault(p.symbol, []).append(p.profit)
    symbols = sorted(
        positions,
        key=lambda s: sum(positions[s]),
        reverse=True
    )
    return symbols
 
nicholi shen:

I usually prioritise position closing based on risk (position size), but if you want close based on overall profit then you just have to modify how you sort the symbols.

Thank you

 
Can Python scripts be used for backtesting in the terminal?
 
Dmitri Custurov:
Can Python scripts be used for backtesting in the terminal?
No, they only work as scripts on the chart in a separate thread.
 
MetaQuotes:
No, they only work as scripts on the chart in a separate thread.

Can you add a feature so that the terminal passes the chart symbol and time-frame as command-line arguments when evoking a python script by dropping it on the chart? For example, you drop the python script on the EURUSD M15 chart and the command to evoke the script would be

python mt5_script.py EURUSD 15

So that we can know the symbol and timeframe from within the python script

import sys
if len(sys.argv) == 3:
    chart_symbol, chart_tf = sys.argv[1:3]
 
nicholi shen:

Can you add a feature so that the terminal passes the chart symbol and time-frame as command-line arguments when evoking a python script by dropping it on the chart? For example, you drop the python script on the EURUSD M15 chart and the command to evoke the script would be

So that we can know the symbol and timeframe from within the python script

Will be available next beta version tonight:

import sys

chart_symbol='unknown'
chart_tf=1

if len(sys.argv) == 3:
    chart_symbol, chart_tf = sys.argv[1:3];

print("Hello world from", chart_symbol, chart_tf)



>> Hello world from T.NYSE 15
Reason: