Skip to content

Commit

Permalink
add alert_callback to analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
msaltnet committed Mar 10, 2024
1 parent 7dcd9e1 commit 3cd4328
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
14 changes: 13 additions & 1 deletion smtm/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,18 @@ def __init__(self, sma_info=(10, 40, 60)):
self.logger = LogManager.get_logger(__class__.__name__)
self.is_simulation = False
self.sma_info = sma_info
self.alert_callback = None

if os.path.isdir("output") is False:
os.mkdir("output")

def initialize(self, get_asset_info_func):
def initialize(self, get_asset_info_func, alert_callback=None):
"""콜백 함수를 입력받아 초기화한다
get_asset_info_func: 거래 데이터를 요청하는 함수로 func(arg1) arg1은 정보 타입
"""
self.get_asset_info_func = get_asset_info_func
self.alert_callback = alert_callback

def add_drawing_spot(self, date_time, value):
"""그래프에 그려질 점의 위치를 입력받아서 저장한다
Expand Down Expand Up @@ -700,6 +702,12 @@ def __create_plot_data(
new["avr_price"] = last_avr_price
break
plot_data.append(new)

if len(plot_data) > self.GRAPH_MAX_COUNT:
self._make_alert(
f"Graph data is trimmed. {len(plot_data)} -> {self.GRAPH_MAX_COUNT}"
)

return pd.DataFrame(plot_data)[-self.GRAPH_MAX_COUNT :]

def _add_line_plot_info(self, line_graph_list, line_pos, new, info_time):
Expand Down Expand Up @@ -860,6 +868,10 @@ def _load_list_from_file(filename):
target_list = ast.literal_eval(data)
return target_list

def _make_alert(self, msg):
if self.alert_callback is not None:
self.alert_callback(msg)

def dump(self, filename="dump"):
"""주요 데이터를 파일로 저장한다"""
self._write_to_file(filename + ".1", self.request_list)
Expand Down
57 changes: 57 additions & 0 deletions tests/analyzer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,49 @@ def test_make_score_record_create_correct_score_record_when_asset_and_balance_is
self.assertEqual(len(score_record["price_change_ratio"].keys()), 0)
analyzer.make_periodic_record.assert_called_once()

def fill_test_data_for_big_data(self, analyzer):
ISO_DATEFORMAT = "%Y-%m-%dT%H:%M:%S"
start_datetime = "2020-02-23T00:00:00"
dummy_asset_info = {
"balance": 23456,
"asset": {},
"date_time": "2020-02-23T00:00:00",
"quote": {"banana": 1700, "mango": 600, "apple": 500},
}
analyzer.start_asset_info = dummy_asset_info
analyzer.asset_info_list.append(dummy_asset_info)
analyzer.score_list.append(
{
"balance": 5000,
"cumulative_return": -65.248,
"price_change_ratio": {"mango": -50.0, "apple": 50.0},
"asset": [
("mango", 500, 300, 5.23, -40.0),
("apple", 250, 750, 2.11, 200.0),
],
"date_time": "2020-02-23T00:00:00",
"kind": 3,
}
)

for i in range(Analyzer.GRAPH_MAX_COUNT + 1):
target_datetime = datetime.strptime(
start_datetime, ISO_DATEFORMAT
) + timedelta(minutes=i)
new_dt = target_datetime.strftime(ISO_DATEFORMAT)
dummy_info = {
"market": "orange",
"date_time": new_dt,
"opening_price": 5000,
"high_price": 15000,
"low_price": 4500,
"closing_price": 5500,
"acc_price": 1500000000,
"acc_volume": 1500,
"kind": 0,
}
analyzer.info_list.append(dummy_info)

def fill_test_data_for_report(self, analyzer):
dummy_info = {
"market": "orange",
Expand Down Expand Up @@ -669,6 +712,20 @@ def test_get_return_report_return_correct_report(self):

analyzer.update_asset_info.assert_called_once()

@patch("mplfinance.make_addplot")
@patch("mplfinance.plot")
def test_get_return_report_make_alert_with_big_data(self, mock_plot, mock_addplot):
alert_callback = MagicMock()
analyzer = Analyzer()
analyzer.initialize("mango", alert_callback)
analyzer.is_simulation = True
self.fill_test_data_for_big_data(analyzer)
self.assertEqual(len(analyzer.info_list), analyzer.GRAPH_MAX_COUNT + 1)
analyzer.update_asset_info = MagicMock()

analyzer.get_return_report(graph_filename="mango_graph.png")
alert_callback.assert_called_once_with("Graph data is trimmed. 1441 -> 1440")

@patch("mplfinance.plot")
def test_get_return_report_return_correct_report_with_index(self, mock_plot):
"""
Expand Down

0 comments on commit 3cd4328

Please sign in to comment.