add IDLE timeout and restart

This commit is contained in:
Death916 2018-07-16 01:59:40 -07:00
parent 44bdaa1f0c
commit ccb6794ab5

View file

@ -1,6 +1,6 @@
#! python3
import pyzmail
import glogin
import trade
import history as hist
@ -16,20 +16,8 @@ def gconnect():
return imap
last_alert = 0
# remnant of old version TODO: save this somewhere
"""def kill(proc_pid):
process = psutil.Process(proc_pid)
for procs in process.children(recursive=True):
procs.kill()
process.kill() """
def start_buy():
print('trade is a buy')
@ -47,21 +35,23 @@ def start_sell():
hist.tradehist('sell test')
def getsignal():
def get_signal():
# noinspection PyPep8
try:
global uid
if last_alert == 'buy' and last_uid != uid:
print('sell signal found')
return "sell"
signal = 'sell'
return signal
elif last_alert == 'sell' or last_alert == 0:
print('buy signal found')
#coin = message.get_subject()
return"buy"
# coin = message.get_subject()
signal = 'buy'
return signal
except:
print('failed')
print('get_signal() failed')
last_uid = 0
@ -69,30 +59,38 @@ last_uid = 0
def main():
s = gconnect()
idle = s
s.idle()
start_time = time.time()
while True:
responses = idle.idle_check(180)
responses = s.idle_check(30)
print("Server sent:", responses if responses else "nothing")
list_uid = ([i[0] for i in responses])
if list_uid != []:
global last_uid
if list_uid:
global uid
print(list_uid[0])
uid = list_uid[0]
signal = getsignal()
else:
signal = get_signal()
global last_uid
if last_uid != uid:
if signal == "buy" and last_alert != 'buy':
start_buy()
print('last alert was', last_alert)
last_uid = uid
elif signal == "sell" and last_alert == "buy":
start_sell()
print('last alert was ', last_alert)
last_uid = uid
## for debugging only ## TODO: Remove when done
elif last_uid == 0:
uid = 0
print(time.ctime())
global last_uid
if last_uid != uid:
signal = get_signal()
if signal == "buy" and last_alert != 'buy':
start_buy()
@ -102,19 +100,28 @@ def main():
start_sell()
print('last alert was ', last_alert)
last_uid = uid
## ##
else:
print('same uid')
if trade.open_orders() != None:
print(time.ctime())
if trade.open_orders():
print(trade.open_orders())
if time.time() - start_time > 1740:
s.idle_done()
print('restarting connection')
s.logout()
s = gconnect()
s.idle()
start_time = time.time()
if __name__ == '__main__':
main()