From 06516d1d46f9039212c0ec06536a0f7051eca63a Mon Sep 17 00:00:00 2001 From: Christian_x7 Date: Sat, 24 Oct 2020 05:04:59 +0200 Subject: [PATCH] add a simple python example --- Examples/Python/Python_to_MT5/.gitignore | 1 + Examples/Python/Python_to_MT5/main.py | 61 +++++++++++++++++++ .../Python/Python_to_MT5/requirements.txt | 1 + 3 files changed, 63 insertions(+) create mode 100644 Examples/Python/Python_to_MT5/.gitignore create mode 100644 Examples/Python/Python_to_MT5/main.py create mode 100644 Examples/Python/Python_to_MT5/requirements.txt diff --git a/Examples/Python/Python_to_MT5/.gitignore b/Examples/Python/Python_to_MT5/.gitignore new file mode 100644 index 00000000..85e7c1df --- /dev/null +++ b/Examples/Python/Python_to_MT5/.gitignore @@ -0,0 +1 @@ +/.idea/ diff --git a/Examples/Python/Python_to_MT5/main.py b/Examples/Python/Python_to_MT5/main.py new file mode 100644 index 00000000..58722f76 --- /dev/null +++ b/Examples/Python/Python_to_MT5/main.py @@ -0,0 +1,61 @@ +import sys +import clr +import time + + +sys.path.append(r"C:\Program Files\MtApi5") +asm = clr.AddReference("MtApi5") +import MtApi5 as mt5 + + +def testCopyRates(): + symbol = str('EURUSD') + startPos = 1 + count = 100 + rates = [] + tf = mt5.ENUM_TIMEFRAMES.PERIOD_M5 + out_dummy = [] + try: + result_count, rates_out = client.CopyRates(symbol, tf, startPos, count, out_dummy) + except Exception as e: + print(e) + print('Error in CopyRates = %s\n ', e) + exit() + + print("CopyRates: result_count = ", result_count) + + + + +if __name__ == '__main__': + + client = mt5.MtApi5Client() + ip = str('192.168.178.15') + port = 8300 + sleeps = 10 + try: + + while sleeps > 0: + + print("Connecting...") + client.BeginConnect(ip, port) + time.sleep(1) + + sleeps = sleeps - 1 + + if client.ConnectionState == 1: + print("Connected") + testCopyRates() + break + if client.ConnectionState == 0: + print("Connecting...") + continue + if client.ConnectionState == 4: + print("Connection failed") + break + + + except Exception as e: + print(e) + + client.BeginDisconnect() diff --git a/Examples/Python/Python_to_MT5/requirements.txt b/Examples/Python/Python_to_MT5/requirements.txt new file mode 100644 index 00000000..31600ee4 --- /dev/null +++ b/Examples/Python/Python_to_MT5/requirements.txt @@ -0,0 +1 @@ +pythonnet \ No newline at end of file