This commit is contained in:
gcarq 2017-09-01 20:22:15 +02:00
commit f56fa25505
3 changed files with 32 additions and 31 deletions

43
main.py
View File

@ -27,27 +27,15 @@ CONFIG = get_conf()
api_wrapper = get_exchange_api(CONFIG) api_wrapper = get_exchange_api(CONFIG)
@synchronized
def get_instance(recreate=False):
"""
Get the current instance of this thread. This is a singleton.
:param recreate: Must be True if you want to start the instance
:return: TradeThread instance
"""
global _instance, _should_stop
if recreate and not _instance.is_alive():
logger.debug('Creating TradeThread instance')
_should_stop = False
_instance = TradeThread()
return _instance
def stop_instance():
global _should_stop
_should_stop = True
class TradeThread(threading.Thread): class TradeThread(threading.Thread):
def __init__(self):
super().__init__()
self._should_stop = False
def stop(self):
""" stops the trader thread """
self._should_stop = True
def run(self): def run(self):
""" """
Threaded main function Threaded main function
@ -56,7 +44,7 @@ class TradeThread(threading.Thread):
try: try:
TelegramHandler.send_msg('*Status:* `trader started`') TelegramHandler.send_msg('*Status:* `trader started`')
logger.info('Trader started') logger.info('Trader started')
while not _should_stop: while not self._should_stop:
try: try:
self._process() self._process()
except (ConnectionError, JSONDecodeError, ValueError) as error: except (ConnectionError, JSONDecodeError, ValueError) as error:
@ -113,7 +101,20 @@ class TradeThread(threading.Thread):
# Initial stopped TradeThread instance # Initial stopped TradeThread instance
_instance = TradeThread() _instance = TradeThread()
@synchronized
def get_instance(recreate=False):
"""
Get the current instance of this thread. This is a singleton.
:param recreate: Must be True if you want to start the instance
:return: TradeThread instance
"""
global _instance
if recreate and not _instance.is_alive():
logger.debug('Creating TradeThread instance')
_should_stop = False _should_stop = False
_instance = TradeThread()
return _instance
def close_trade_if_fulfilled(trade): def close_trade_if_fulfilled(trade):

View File

@ -1,14 +1,14 @@
-e git+https://github.com/s4w3d0ff/python-poloniex.git#egg=Poloniex -e git+https://github.com/s4w3d0ff/python-poloniex.git#egg=Poloniex
-e git+https://github.com/ericsomdahl/python-bittrex.git#egg=python-bittrex -e git+https://github.com/ericsomdahl/python-bittrex.git#egg=python-bittrex
SQLAlchemy==1.1.9 SQLAlchemy==1.1.13
python-telegram-bot==7.0.1 python-telegram-bot==7.0.1
arrow==0.10.0 arrow==0.10.0
requests==2.18.4 requests==2.18.4
urllib3==1.22 urllib3==1.22
wrapt==1.10.10 wrapt==1.10.11
pandas==0.20.1 pandas==0.20.3
matplotlib==2.0.0 matplotlib==2.0.2
PYQT5==5.8 PYQT5==5.9
scikit-learn==0.18.1 scikit-learn==0.19.0
scipy==0.18.1 scipy==0.19.1
stockstats==0.2.0 stockstats==0.2.0

View File

@ -174,10 +174,10 @@ class TelegramHandler(object):
:param update: message update :param update: message update
:return: None :return: None
""" """
from main import get_instance, stop_instance from main import get_instance
if get_instance().is_alive(): if get_instance().is_alive():
TelegramHandler.send_msg('`Stopping trader ...`', bot=bot) TelegramHandler.send_msg('`Stopping trader ...`', bot=bot)
stop_instance() get_instance().stop()
else: else:
TelegramHandler.send_msg('*Status:* `already stopped`', bot=bot) TelegramHandler.send_msg('*Status:* `already stopped`', bot=bot)