stable/freqtrade/main.py

90 lines
2.4 KiB
Python
Raw Normal View History

2017-11-07 16:54:44 +00:00
#!/usr/bin/env python3
"""
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
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
from freqtrade.arguments import Arguments
2018-07-19 19:12:27 +00:00
from freqtrade.configuration import Configuration, set_loggers
from freqtrade.freqtradebot import FreqtradeBot
from freqtrade.state import State
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
2018-03-17 23:02:02 +00:00
def main(sysargv: List[str]) -> None:
2017-05-12 17:11:56 +00:00
"""
This function will initiate the bot and start the trading loop.
:return: None
"""
arguments = Arguments(
sysargv,
'Simple High Frequency Trading Bot for crypto currencies'
)
args = arguments.get_parsed_arg()
# A subcommand has been issued.
# Means if Backtesting or Hyperopt have been called we exit the bot
if hasattr(args, 'func'):
args.func(args)
return
2017-11-08 20:17:51 +00:00
2018-03-24 19:54:46 +00:00
freqtrade = None
return_code = 1
try:
# Load and validate configuration
2018-03-25 19:37:14 +00:00
config = Configuration(args).get_config()
# Init the bot
2018-03-25 19:37:14 +00:00
freqtrade = FreqtradeBot(config)
2018-01-15 08:35:11 +00:00
state = None
while 1:
state = freqtrade.worker(old_state=state)
if state == State.RELOAD_CONF:
freqtrade = reconfigure(freqtrade, args)
except KeyboardInterrupt:
2018-02-24 16:33:08 +00:00
logger.info('SIGINT received, aborting ...')
return_code = 0
2018-06-07 19:35:57 +00:00
except OperationalException as e:
logger.error(str(e))
return_code = 2
except BaseException:
2018-02-24 16:33:08 +00:00
logger.exception('Fatal exception!')
finally:
2018-03-24 19:54:46 +00:00
if freqtrade:
2018-06-24 22:04:27 +00:00
freqtrade.rpc.send_msg({
'type': RPCMessageType.STATUS_NOTIFICATION,
2018-06-24 22:04:27 +00:00
'status': 'process died'
})
freqtrade.cleanup()
sys.exit(return_code)
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({
'type': RPCMessageType.STATUS_NOTIFICATION,
2018-06-24 22:04:27 +00:00
'status': 'config reloaded'
})
return freqtrade
2017-09-28 21:47:51 +00:00
if __name__ == '__main__':
set_loggers()
main(sys.argv[1:])