freqtrade_origin/utils.py

102 lines
3.9 KiB
Python
Raw Normal View History

2017-05-12 17:11:56 +00:00
import json
import logging
2017-05-14 12:14:16 +00:00
from wrapt import synchronized
2017-05-22 19:58:03 +00:00
from bittrex.bittrex import Bittrex
2017-05-14 12:14:16 +00:00
2017-05-12 17:11:56 +00:00
logger = logging.getLogger(__name__)
2017-05-14 12:14:16 +00:00
_cur_conf = None
2017-05-12 17:11:56 +00:00
2017-05-14 12:14:16 +00:00
@synchronized
def get_conf(filename='config.json'):
2017-05-12 17:11:56 +00:00
"""
Loads the config into memory and returns the instance of it
:return: dict
"""
2017-05-14 12:14:16 +00:00
global _cur_conf
if not _cur_conf:
2017-08-27 13:40:27 +00:00
with open(filename) as file:
_cur_conf = json.load(file)
2017-05-14 12:14:16 +00:00
validate_conf(_cur_conf)
return _cur_conf
2017-05-12 17:11:56 +00:00
def validate_conf(conf):
"""
Validates if the minimal possible config is provided
:param conf: config as dict
:return: None, raises ValueError if something is wrong
"""
if not isinstance(conf.get('max_open_trades'), int):
raise ValueError('max_open_trades must be a int')
2017-05-18 16:53:22 +00:00
if not isinstance(conf.get('stake_currency'), str):
raise ValueError('stake_currency must be a str')
2017-05-12 17:11:56 +00:00
if not isinstance(conf.get('stake_amount'), float):
raise ValueError('stake_amount must be a float')
if not isinstance(conf.get('dry_run'), bool):
raise ValueError('dry_run must be a boolean')
2017-05-20 19:30:42 +00:00
if not isinstance(conf.get('minimal_roi'), dict):
raise ValueError('minimal_roi must be a dict')
2017-05-12 17:11:56 +00:00
2017-08-27 13:40:27 +00:00
for index, (minutes, threshold) in enumerate(conf.get('minimal_roi').items()):
2017-05-12 17:11:56 +00:00
if not isinstance(minutes, str):
2017-08-27 13:40:27 +00:00
raise ValueError('minimal_roi[{}].key must be a string'.format(index))
2017-05-12 17:11:56 +00:00
if not isinstance(threshold, float):
2017-08-27 13:40:27 +00:00
raise ValueError('minimal_roi[{}].value must be a float'.format(index))
2017-05-12 17:11:56 +00:00
if conf.get('telegram'):
telegram = conf.get('telegram')
if not isinstance(telegram.get('token'), str):
raise ValueError('telegram.token must be a string')
if not isinstance(telegram.get('chat_id'), str):
raise ValueError('telegram.chat_id must be a string')
if conf.get('poloniex'):
poloniex = conf.get('poloniex')
if not isinstance(poloniex.get('key'), str):
raise ValueError('poloniex.key must be a string')
if not isinstance(poloniex.get('secret'), str):
raise ValueError('poloniex.secret must be a string')
if not isinstance(poloniex.get('pair_whitelist'), list):
raise ValueError('poloniex.pair_whitelist must be a list')
if poloniex.get('enabled', False):
2017-05-22 19:58:03 +00:00
raise ValueError('poloniex is currently not implemented')
#if not poloniex.get('pair_whitelist'):
# raise ValueError('poloniex.pair_whitelist must contain some pairs')
2017-05-12 17:11:56 +00:00
if conf.get('bittrex'):
bittrex = conf.get('bittrex')
if not isinstance(bittrex.get('key'), str):
raise ValueError('bittrex.key must be a string')
if not isinstance(bittrex.get('secret'), str):
raise ValueError('bittrex.secret must be a string')
if not isinstance(bittrex.get('pair_whitelist'), list):
raise ValueError('bittrex.pair_whitelist must be a list')
if bittrex.get('enabled', False):
if not bittrex.get('pair_whitelist'):
raise ValueError('bittrex.pair_whitelist must contain some pairs')
2017-05-22 19:58:03 +00:00
validate_bittrex_pairs(bittrex.get('pair_whitelist'))
2017-05-12 17:11:56 +00:00
if conf.get('poloniex', {}).get('enabled', False) \
and conf.get('bittrex', {}).get('enabled', False):
raise ValueError('Cannot use poloniex and bittrex at the same time')
logger.info('Config is valid ...')
2017-05-22 19:58:03 +00:00
def validate_bittrex_pairs(pairs):
2017-05-22 20:15:33 +00:00
"""
Validates if all given pairs exist on bittrex
:param pairs: list of str
:return: None
"""
data = Bittrex(None, None).get_markets()
if not data['success']:
raise RuntimeError('BITTREX: {}'.format(data['message']))
2017-08-27 13:40:27 +00:00
available_markets = [market['MarketName'].replace('-', '_')for market in data['result']]
for pair in pairs:
if pair not in available_markets:
raise ValueError('Invalid pair: {}'.format(pair))