Python codes

 

Am coming from python background and new to MetaTrader but very determined to learn .

I have a few questions :

1. Where is the best location to write python codes for trading: is it python script in the Metatrader terminal or inside python IDE like jupyter?

2. when python codes are run inside a python IDE, how do i see the results/ output in the MetaTrader terminal?

Thanks in advance


John.

 

 
The best Advice to give you is to learn MQL coding Language to save yourself stress and it not so much difficult as all coding languages have similar structure
 

i have leaned python , but i think MQL language is the easiest language i have ever learned, the document is so complete.

so forget python, if you want to make good EAs ,just learn MQL.

 
johnyaddae:

Am coming from python background and new to MetaTrader but very determined to learn .

I have a few questions :

1. Where is the best location to write python codes for trading: is it python script in the Metatrader terminal or inside python IDE like jupyter?

2. when python codes are run inside a python IDE, how do i see the results/ output in the MetaTrader terminal?

Thanks in advance


John.

 

1. An IDE like Pycharm is the best. 

2. You can run your python script from anywhere including a virtualenv. If, however, you use the terminal UI to drop the script on a chart then it will redirect print statements to the Experts tab and log to the experts log file. I prefer to use the automatic logging built into the pymt5adapter package. Here is a hello world... 

import logging
import os
from pathlib import Path

import pymt5adapter as mta

desktop_log_path = Path.home() / 'Desktop/python_mt5.log'
logger = mta.get_logger(path_to_logfile=desktop_log_path, loglevel=logging.DEBUG)
try:
    with mta.connected(raise_on_errors=True, logger=logger):
        rates = mta.copy_rates_from_pos('Invalid Args to raise Exception', 999, 0, 12333)
finally:
    os.startfile(desktop_log_path)

which will result in this log data

2020-08-14 03:50:12,710 INFO    Terminal Initialize Success     {"type": "terminal_connection_state", "state": true}
2020-08-14 03:50:12,710 INFO    Init TerminalInfo       {"type": "init_terminal_info", "terminal_info": {"community_account": false, "community_connection": false, "connected": true, "dlls_allowed": false, "trade_allowed": true, "tradeapi_disabled": false, "email_enabled": false, "ftp_enabled": false, "notifications_enabled": false, "mqid": false, "build": 2560, "maxbars": 100000000, "codepage": 0, "ping_last": 38103, "community_balance": 0.0, "retransmission": 0.0, "company": "MetaQuotes Software Corp.", "name": "MetaTrader 5", "language": "English", "path": "C:\\Users\\User\\Desktop\\MetaTrader\\Terminal1", "data_path": "C:\\Users\\User\\Desktop\\MetaTrader\\Terminal1", "commondata_path": "C:\\Users\\User\\AppData\\Roaming\\MetaQuotes\\Terminal\\Common"}}
2020-08-14 03:50:12,710 INFO    Init AccountInfo        {"type": "init_account_info", "account_info": {"login": 8768768, "trade_mode": 0, "leverage": 100, "limit_orders": 0, "margin_so_mode": 0, "trade_allowed": true, "trade_expert": true, "margin_mode": 0, "currency_digits": 2, "fifo_close": false, "balance": 1002305.13, "credit": 0.0, "profit": 4978.95, "equity": 997326.18, "margin": 3800.0, "margin_free": 993526.18, "margin_level": 26245.425789473687, "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": "nicholishen", "server": "AMPGlobalUSA-Demo", "currency": "USD", "company": "AMP Global Clearing LLC"}}
2020-08-14 03:50:12,710 WARNING Function Debugging: copy_rates_from_pos {"type": "function_debugging", "latency_ms": 0.15, "last_error": [-2, "Terminal: Invalid params"], "call_signature": {"function": "copy_rates_from_pos", "args": ["Non-existant symbol", 999, 0, 12333], "kwargs": {}}}
2020-08-14 03:50:12,710 CRITICAL        UNCAUGHT EXCEPTION: INVALID_PARAMS: Terminal: Invalid params('Non-existant symbol', 999, 0, 12333){}    {"type": "exception", "last_error": [-2, "Terminal: Invalid params"], "exception": {"type": "MT5Error", "message": "INVALID_PARAMS: Terminal: Invalid params('Non-existant symbol', 999, 0, 12333){}"}}
2020-08-14 03:50:12,710 INFO    Terminal Shutdown       {"type": "terminal_connection_state", "state": false}
Reason: