forked from qasem-talaee/trading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
132 lines (116 loc) · 4.21 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import requests
import threading
import time
import os
from libs import coin
n = 2
money = 10
global main_obj
class ListenerCoin(threading.Thread):
def __init__(self, obj, main_obj):
threading.Thread.__init__(self)
print('Listener start ' + obj.coin)
self.flag = True
self.obj = obj
self.main_obj = main_obj
def run(self):
while self.flag:
if self.obj.flag_sell:
print('sell ' + self.obj.coin)
self.main_obj.runlist.remove(self.obj)
self.main_obj.j -= 1
self.main_obj.delete_logger(self.obj.coin.lower())
self.obj.kill_flag = True
self.flag = False
break
class Main(threading.Thread):
def set_n(self, n):
self.n = int(n)
def set_money(self, money):
self.money = float(money)
def get_market_list(self):
flag = 0
while flag != 1:
try:
result = requests.get(
'https://api.coinex.com/v1/market/list',
headers={
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36'
}
)
except:
print('Try again!')
else:
flag = 1
result = result.json()['data']
for i in result:
if i.find('USDT') != -1:
self.list.append(i.replace('USDT', '').lower())
def check_log_file(self):
if not os.path.isdir('./logs'):
os.mkdir('./logs')
if not os.path.isfile('./logs/log_MAIN.txt'):
open('./logs/log_MAIN.txt', 'a').close()
def read_logger(self):
if os.stat('./logs/log_MAIN.txt').st_size != 0:
each_coin = self.money / self.n
with open('./logs/log_MAIN.txt', 'r') as logFile:
for line in logFile:
if line.replace('\n', '') != '':
print('continue ' + line.replace('\n', ''))
r = coin.Coin(line.replace('\n', ''), each_coin, 0, 0, 1.5)
r.start()
self.runlist.append(r)
l = ListenerCoin(self.runlist[self.j], main_obj)
l.start()
self.j = self.j + 1
def logger(self, i):
with open('logs/log_MAIN.txt', 'a') as logFile:
logFile.write('\n' + i)
def delete_logger(self, i):
a_file = open("logs/log_MAIN.txt", "r")
lines = a_file.readlines()
a_file.close()
new_file = open("logs/log_MAIN.txt", "w")
for line in lines:
if line.strip("\n") != i:
new_file.write(line)
new_file.close()
def __init__(self, n, money):
threading.Thread.__init__(self)
self.list = []
self.runlist = []
self.run_flag = True
self.get_market_list()
self.set_n(n)
self.set_money(money)
self.j = 0
self.check_log_file()
self.read_logger()
def run(self):
each_coin = self.money / self.n
while True:
if self.run_flag:
for i in self.list:
if len(self.runlist) != self.n:
print('start ' + i)
r = coin.Coin(i, each_coin, 0, 0, 1.5)
r.start()
time.sleep(20)
print(self.runlist)
if r.flag_test:
print('buy ' + i)
self.runlist.append(r)
l = ListenerCoin(self.runlist[self.j], main_obj)
l.start()
self.j = self.j + 1
self.logger(i)
continue
else:
print('del ' + i)
r.kill_flag = True
continue
main_obj = Main(n, money)
main_obj.start()