/interfaces/web/__init__.py

https://github.com/Drakkar-Software/OctoBot
Python | 140 lines | 84 code | 37 blank | 19 comment | 10 complexity | c6f55db72ba462e3be709fba58f2d096 MD5 | raw file
  1. # Drakkar-Software OctoBot
  2. # Copyright (c) Drakkar-Software, All rights reserved.
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 3.0 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public
  15. # License along with this library.
  16. import copy
  17. import logging
  18. import time
  19. import flask
  20. import numpy
  21. from config import PriceIndexes, LOG_DATABASE, LOG_NEW_ERRORS_COUNT
  22. from interfaces.web.api import api
  23. from tools.logging import logs_database, reset_errors_count
  24. server_instance = flask.Flask(__name__)
  25. from interfaces.web.advanced_controllers import advanced
  26. server_instance.register_blueprint(advanced)
  27. server_instance.register_blueprint(api)
  28. # disable Flask logging
  29. log = logging.getLogger('werkzeug')
  30. log.setLevel(logging.WARNING)
  31. notifications = []
  32. matrix_history = []
  33. symbol_data_history = {}
  34. portfolio_value_history = {
  35. "real_value": [],
  36. "simulated_value": [],
  37. "timestamp": []
  38. }
  39. TIME_AXIS_TITLE = "Time"
  40. def add_to_matrix_history(matrix):
  41. matrix_history.append({
  42. "matrix": copy.deepcopy(matrix.get_matrix()),
  43. "timestamp": time.time()
  44. })
  45. def add_to_portfolio_value_history(real_value, simulated_value):
  46. portfolio_value_history["real_value"].append(real_value)
  47. portfolio_value_history["simulated_value"].append(simulated_value)
  48. portfolio_value_history["timestamp"].append(time.time())
  49. def add_to_symbol_data_history(symbol, data, time_frame, force_data_reset=False):
  50. if symbol not in symbol_data_history:
  51. symbol_data_history[symbol] = {}
  52. if force_data_reset or time_frame not in symbol_data_history[symbol]:
  53. symbol_data_history[symbol][time_frame] = data
  54. else:
  55. # merge new data into current data
  56. # find index from where data is new
  57. new_data_index = 0
  58. candle_times = data[PriceIndexes.IND_PRICE_TIME.value]
  59. current_candle_list = symbol_data_history[symbol][time_frame]
  60. for i in range(1, len(candle_times)):
  61. if candle_times[-i] > current_candle_list[PriceIndexes.IND_PRICE_TIME.value][-1]:
  62. new_data_index = i
  63. else:
  64. # update last candle if necessary, then break loop
  65. if current_candle_list[PriceIndexes.IND_PRICE_TIME.value][-1] == candle_times[-i]:
  66. current_candle_list[PriceIndexes.IND_PRICE_CLOSE.value][-1] = \
  67. data[PriceIndexes.IND_PRICE_CLOSE.value][-i]
  68. current_candle_list[PriceIndexes.IND_PRICE_HIGH.value][-1] = \
  69. data[PriceIndexes.IND_PRICE_HIGH.value][-i]
  70. current_candle_list[PriceIndexes.IND_PRICE_LOW.value][-1] = \
  71. data[PriceIndexes.IND_PRICE_LOW.value][-i]
  72. current_candle_list[PriceIndexes.IND_PRICE_VOL.value][-1] = \
  73. data[PriceIndexes.IND_PRICE_VOL.value][-i]
  74. break
  75. if new_data_index > 0:
  76. data_list = [None] * len(PriceIndexes)
  77. for i, _ in enumerate(data):
  78. data_list[i] = data[i][-new_data_index:]
  79. new_data = numpy.array(data_list)
  80. symbol_data_history[symbol][time_frame] = numpy.concatenate((symbol_data_history[symbol][time_frame],
  81. new_data), axis=1)
  82. async def add_notification(level, title, message):
  83. notifications.append({
  84. "Level": level.value,
  85. "Title": title,
  86. "Message": message
  87. })
  88. def flush_notifications():
  89. notifications.clear()
  90. def get_matrix_history():
  91. return matrix_history
  92. def get_portfolio_value_history():
  93. return portfolio_value_history
  94. def get_symbol_data_history(symbol, time_frame):
  95. return symbol_data_history[symbol][time_frame]
  96. def get_notifications():
  97. return notifications
  98. def get_logs():
  99. return logs_database[LOG_DATABASE]
  100. def get_errors_count():
  101. return logs_database[LOG_NEW_ERRORS_COUNT]
  102. def flush_errors_count():
  103. reset_errors_count()