[go: up one dir, main page]

Menu

[cc83e9]: / tools / commands.py  Maximize  Restore  History

Download this file

137 lines (118 with data), 5.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Drakkar-Software OctoBot
# Copyright (c) Drakkar-Software, All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3.0 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.
import os
import sys
import asyncio
import signal
from threading import Thread
from concurrent.futures import CancelledError
from tentacles_manager.tentacle_creator.tentacle_creator import TentacleCreator
from backtesting.collector.data_collector import DataCollector
from config import TENTACLES_DEFAULT_BRANCH, TENTACLES_FORCE_CONFIRM_PARAM
from config.config import encrypt
from interfaces import get_bot
from tools.logging.logging_util import get_logger
from tentacles_manager.tentacle_manager import TentacleManager
class Commands:
@staticmethod
def data_collector(config, catch=True):
data_collector_inst = None
try:
data_collector_inst = DataCollector(config)
asyncio.run(data_collector_inst.start())
except Exception as e:
data_collector_inst.stop()
if catch:
raise e
@staticmethod
def package_manager(config, commands, catch=False, force=False, default_git_branch=TENTACLES_DEFAULT_BRANCH):
try:
if TENTACLES_FORCE_CONFIRM_PARAM in commands:
force = True
commands.pop(commands.index(TENTACLES_FORCE_CONFIRM_PARAM))
tentacle_manager = TentacleManager(config)
tentacle_manager.parse_commands(commands, force=force, default_git_branch=default_git_branch)
except Exception as e:
if not catch:
raise e
@staticmethod
def tentacle_creator(config, commands, catch=False):
try:
tentacle_creator_inst = TentacleCreator(config)
tentacle_creator_inst.parse_commands(commands)
except Exception as e:
if not catch:
raise e
@staticmethod
def exchange_keys_encrypter(catch=False):
try:
api_key_crypted = encrypt(input("ENTER YOUR API-KEY : ")).decode()
api_secret_crypted = encrypt(input("ENTER YOUR API-SECRET : ")).decode()
print(f"Here are your encrypted exchanges keys : \n "
f"\t- API-KEY : {api_key_crypted}\n"
f"\t- API-SECRET : {api_secret_crypted}\n\n"
f"Your new exchange key configuration is : \n"
f'\t"api-key": "{api_key_crypted}",\n'
f'\t"api-secret": "{api_secret_crypted}"\n')
except Exception as e:
if not catch:
get_logger(Commands.__name__).error(f"Fail to encrypt your exchange keys, please try again ({e}).")
raise e
@staticmethod
def start_strategy_optimizer(config, commands):
from backtesting.strategy_optimizer.strategy_optimizer import StrategyOptimizer
optimizer = StrategyOptimizer(config, commands[0])
if optimizer.is_properly_initialized:
optimizer.find_optimal_configuration()
optimizer.print_report()
@staticmethod
def _signal_handler(_, __):
# run Commands.BOT.stop_threads in thread because can't use the current asyncio loop
stopping_thread = Thread(target=get_bot().stop)
stopping_thread.start()
stopping_thread.join()
os._exit(0)
@staticmethod
async def start_bot(bot, logger, catch=False):
try:
loop = asyncio.get_event_loop()
# handle CTRL+C signal
signal.signal(signal.SIGINT, Commands._signal_handler)
# start
try:
await bot.initialize()
await bot.start()
except CancelledError:
logger.info("Core engine tasks cancelled.")
# join threads in a not loop blocking executor
# TODO remove this when no thread anymore
await loop.run_in_executor(None, bot.task_manager.join_threads)
except Exception as e:
logger.exception(f"OctoBot Exception : {e}")
if not catch:
raise e
Commands.stop_bot(bot)
@staticmethod
def stop_bot(bot):
bot.stop()
os._exit(0)
@staticmethod
def restart_bot():
if sys.argv[0].endswith(".py"):
os.execl(sys.executable, sys.executable, *sys.argv)
else:
# prevent binary to add self as first argument
os.execl(sys.executable, *sys.argv)