-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtv-mt4.py
71 lines (57 loc) · 2.04 KB
/
tv-mt4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import email, imaplib
import zmq
import random
import datetime, time
volume = '0.1'
user = ''
pwd = ''
c = zmq.Context()
print("Connecting to the mt4 server...")
s = c.socket(zmq.REQ)
s.connect("tcp://127.0.0.1:5557")
r = c.socket(zmq.PULL)
r.connect("tcp://127.0.0.1:5558")
def generate_nonce(length=8):
"""Generate pseudorandom number."""
return ''.join([str(random.randint(0, 9)) for i in range(length)])
def trade(signal,volume,pair):
try:
trade = 'TRADE|OPEN|' + signal + '|' + pair + '|0|0|0|IcarusBot Trade|' + generate_nonce() + '|' + volume
s.send_string(trade, encoding='utf-8')
print("Waiting for metatrader to respond...")
m = s.recv()
print("Reply from server ", m)
except Exception as e:
print(e)
print("Listening to email server...")
def readmail(volume):
#print("Sleeping 3 Seconds")
time.sleep(1.5)
m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login(user, pwd)
m.select('"[Gmail]/All Mail"')
day = time.strftime("%d")
resp, items = m.search(None,
"NOT SEEN FROM tradingview SENTON " + day + "-JUN-2018")
items = items[0].split()
for emailid in items:
resp, data = m.fetch(emailid,
"(RFC822)")
email_body = data[0][1]
mail = email.message_from_bytes(email_body)
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
try:
pair = mail['Subject'].split()[2]
if mail['Subject'].split()[3] == "Buy":
m.store(emailid, '+FLAGS', '\Seen')
print(st + ' \x1b[6;30;42m' + 'Buy' + '\x1b[0m' + ' Triggered on ' + pair)
trade('0', volume, pair)
if mail['Subject'].split()[3] == "Sell":
m.store(emailid, '+FLAGS', '\Seen')
print(st + ' \x1b[6;30;41m' + 'Sell' + '\x1b[0m' + ' Triggered on ' + pair)
trade("1", volume, pair)
except Exception as e:
print(e)
while True:
readmail(volume)