2017-11-07 16:54:44 +00:00
|
|
|
#!/usr/bin/env python3
|
2018-02-04 09:21:16 +00:00
|
|
|
"""
|
|
|
|
Main Freqtrade bot script.
|
|
|
|
Read the documentation to know what cli arguments you need.
|
|
|
|
"""
|
2017-05-12 17:11:56 +00:00
|
|
|
import logging
|
2017-11-17 16:18:31 +00:00
|
|
|
import sys
|
2018-06-09 02:29:48 +00:00
|
|
|
from argparse import Namespace
|
2018-03-17 23:02:02 +00:00
|
|
|
from typing import List
|
2018-03-17 21:44:47 +00:00
|
|
|
|
2018-06-07 19:35:57 +00:00
|
|
|
from freqtrade import OperationalException
|
2018-02-04 09:21:16 +00:00
|
|
|
from freqtrade.arguments import Arguments
|
2018-07-18 20:52:57 +00:00
|
|
|
from freqtrade.configuration import Configuration, set_loggers
|
2018-02-04 09:21:16 +00:00
|
|
|
from freqtrade.freqtradebot import FreqtradeBot
|
2018-06-09 02:29:48 +00:00
|
|
|
from freqtrade.state import State
|
2018-07-03 18:26:48 +00:00
|
|
|
from freqtrade.rpc import RPCMessageType
|
2018-03-25 19:37:14 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger('freqtrade')
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2017-09-25 12:42:16 +00:00
|
|
|
|
2018-03-17 23:02:02 +00:00
|
|
|
def main(sysargv: List[str]) -> None:
|
2017-05-12 17:11:56 +00:00
|
|
|
"""
|
2018-02-04 09:21:16 +00:00
|
|
|
This function will initiate the bot and start the trading loop.
|
2017-09-08 13:51:00 +00:00
|
|
|
:return: None
|
|
|
|
"""
|
2018-02-04 09:21:16 +00:00
|
|
|
arguments = Arguments(
|
|
|
|
sysargv,
|
|
|
|
'Simple High Frequency Trading Bot for crypto currencies'
|
2017-11-11 18:20:53 +00:00
|
|
|
)
|
2018-02-04 09:21:16 +00:00
|
|
|
args = arguments.get_parsed_arg()
|
2017-12-11 07:56:03 +00:00
|
|
|
|
2018-02-04 09:21:16 +00:00
|
|
|
# A subcommand has been issued.
|
|
|
|
# Means if Backtesting or Hyperopt have been called we exit the bot
|
2018-01-06 10:21:09 +00:00
|
|
|
if hasattr(args, 'func'):
|
|
|
|
args.func(args)
|
2018-03-24 19:55:10 +00:00
|
|
|
return
|
2017-11-08 20:17:51 +00:00
|
|
|
|
2018-03-24 19:54:46 +00:00
|
|
|
freqtrade = None
|
2018-03-24 19:56:27 +00:00
|
|
|
return_code = 1
|
2017-11-20 21:15:19 +00:00
|
|
|
try:
|
2018-02-04 09:21:16 +00:00
|
|
|
# Load and validate configuration
|
2018-03-25 19:37:14 +00:00
|
|
|
config = Configuration(args).get_config()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
# Init the bot
|
2018-03-25 19:37:14 +00:00
|
|
|
freqtrade = FreqtradeBot(config)
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2018-02-04 09:21:16 +00:00
|
|
|
state = None
|
|
|
|
while 1:
|
|
|
|
state = freqtrade.worker(old_state=state)
|
2018-06-09 02:29:48 +00:00
|
|
|
if state == State.RELOAD_CONF:
|
|
|
|
freqtrade = reconfigure(freqtrade, args)
|
2017-11-20 21:15:19 +00:00
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
2018-02-24 16:33:08 +00:00
|
|
|
logger.info('SIGINT received, aborting ...')
|
2018-03-24 19:56:27 +00:00
|
|
|
return_code = 0
|
2018-06-07 19:35:57 +00:00
|
|
|
except OperationalException as e:
|
|
|
|
logger.error(str(e))
|
|
|
|
return_code = 2
|
2017-11-21 19:24:52 +00:00
|
|
|
except BaseException:
|
2018-02-24 16:33:08 +00:00
|
|
|
logger.exception('Fatal exception!')
|
2017-11-20 21:15:19 +00:00
|
|
|
finally:
|
2018-03-24 19:54:46 +00:00
|
|
|
if freqtrade:
|
2018-06-24 22:04:27 +00:00
|
|
|
freqtrade.rpc.send_msg({
|
2018-07-03 18:26:48 +00:00
|
|
|
'type': RPCMessageType.STATUS_NOTIFICATION,
|
2018-06-24 22:04:27 +00:00
|
|
|
'status': 'process died'
|
|
|
|
})
|
2018-06-08 23:19:42 +00:00
|
|
|
freqtrade.cleanup()
|
2018-03-24 19:56:27 +00:00
|
|
|
sys.exit(return_code)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2018-06-09 02:29:48 +00:00
|
|
|
def reconfigure(freqtrade: FreqtradeBot, args: Namespace) -> FreqtradeBot:
|
|
|
|
"""
|
|
|
|
Cleans up current instance, reloads the configuration and returns the new instance
|
|
|
|
"""
|
|
|
|
# Clean up current modules
|
|
|
|
freqtrade.cleanup()
|
|
|
|
|
|
|
|
# Create new instance
|
|
|
|
freqtrade = FreqtradeBot(Configuration(args).get_config())
|
2018-06-24 22:04:27 +00:00
|
|
|
freqtrade.rpc.send_msg({
|
2018-07-03 18:26:48 +00:00
|
|
|
'type': RPCMessageType.STATUS_NOTIFICATION,
|
2018-06-24 22:04:27 +00:00
|
|
|
'status': 'config reloaded'
|
|
|
|
})
|
2018-06-09 02:29:48 +00:00
|
|
|
return freqtrade
|
|
|
|
|
|
|
|
|
2017-09-28 21:47:51 +00:00
|
|
|
if __name__ == '__main__':
|
2018-02-04 09:21:16 +00:00
|
|
|
set_loggers()
|
2018-01-13 12:16:40 +00:00
|
|
|
main(sys.argv[1:])
|