Skip to content

Commit

Permalink
add simple alert strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
msaltnet committed Apr 28, 2024
1 parent 5263cdb commit 231a588
Show file tree
Hide file tree
Showing 87 changed files with 66,055 additions and 15 deletions.
154 changes: 154 additions & 0 deletions integration_tests/strategy_sas/mass_config_sml.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{
"title": "SAS_REF_CHECK",
"description": "mass simulation test for reference period",
"budget": 5000000,
"strategy": "SAS",
"interval": 1e-06,
"currency": "BTC",
"period_list": [
{
"start": "2022-07-01T00:00:00",
"end": "2022-07-01T19:59:00"
},
{
"start": "2022-07-10T04:00:00",
"end": "2022-07-10T23:59:00"
},
{
"start": "2022-07-11T20:00:00",
"end": "2022-07-12T15:59:00"
},
{
"start": "2022-07-12T16:00:00",
"end": "2022-07-13T11:59:00"
},
{
"start": "2022-07-15T04:00:00",
"end": "2022-07-15T23:59:00"
},
{
"start": "2022-07-23T12:00:00",
"end": "2022-07-24T07:59:00"
},
{
"start": "2022-07-24T13:00:00",
"end": "2022-07-25T08:59:00"
},
{
"start": "2022-07-28T12:00:00",
"end": "2022-07-29T07:59:00"
},
{
"start": "2022-07-31T00:00:00",
"end": "2022-07-31T19:59:00"
},
{
"start": "2022-08-11T23:00:00",
"end": "2022-08-12T18:59:00"
},
{
"start": "2022-08-14T04:00:00",
"end": "2022-08-14T23:59:00"
},
{
"start": "2022-08-15T00:00:00",
"end": "2022-08-15T19:59:00"
},
{
"start": "2022-08-26T16:00:00",
"end": "2022-08-27T11:59:00"
},
{
"start": "2022-08-30T00:00:00",
"end": "2022-08-30T19:59:00"
},
{
"start": "2022-10-04T08:00:00",
"end": "2022-10-05T03:59:00"
},
{
"start": "2022-10-10T04:00:00",
"end": "2022-10-10T23:59:00"
},
{
"start": "2022-10-26T17:00:00",
"end": "2022-10-27T12:59:00"
},
{
"start": "2022-11-01T00:00:00",
"end": "2022-11-01T19:59:00"
},
{
"start": "2022-11-14T04:00:00",
"end": "2022-11-14T23:59:00"
},
{
"start": "2022-11-18T08:00:00",
"end": "2022-11-19T03:59:00"
},
{
"start": "2023-01-12T16:00:00",
"end": "2023-01-13T11:59:00"
},
{
"start": "2023-01-13T10:00:00",
"end": "2023-01-14T05:59:00"
},
{
"start": "2023-01-17T16:00:00",
"end": "2023-01-18T11:59:00"
},
{
"start": "2023-02-10T00:00:00",
"end": "2023-02-10T19:59:00"
},
{
"start": "2023-02-27T12:00:00",
"end": "2023-02-28T07:59:00"
},
{
"start": "2023-04-22T13:00:00",
"end": "2023-04-23T08:59:00"
},
{
"start": "2023-04-24T08:00:00",
"end": "2023-04-25T03:59:00"
},
{
"start": "2023-04-25T22:00:00",
"end": "2023-04-26T17:59:00"
},
{
"start": "2023-04-28T12:00:00",
"end": "2023-04-29T07:59:00"
},
{
"start": "2023-05-07T16:00:00",
"end": "2023-05-08T11:59:00"
},
{
"start": "2023-05-11T20:00:00",
"end": "2023-05-12T15:59:00"
},
{
"start": "2023-05-12T16:00:00",
"end": "2023-05-13T11:59:00"
},
{
"start": "2023-05-16T00:00:00",
"end": "2023-05-16T19:59:00"
},
{
"start": "2023-05-27T16:00:00",
"end": "2023-05-28T11:59:00"
},
{
"start": "2023-05-28T12:00:00",
"end": "2023-05-29T07:59:00"
},
{
"start": "2023-05-30T04:00:00",
"end": "2023-05-30T23:59:00"
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-0.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-1.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-10.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,817 changes: 1,817 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-11.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-12.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,818 changes: 1,818 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-13.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,822 changes: 1,822 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-14.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-15.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-16.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-17.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-18.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,817 changes: 1,817 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-19.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-2.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,817 changes: 1,817 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-20.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,818 changes: 1,818 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-21.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,817 changes: 1,817 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-22.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-23.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-24.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,817 changes: 1,817 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-25.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-26.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,818 changes: 1,818 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-27.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-28.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-29.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-3.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-30.txt

Large diffs are not rendered by default.

1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-31.txt

Large diffs are not rendered by default.

1,817 changes: 1,817 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-32.txt

Large diffs are not rendered by default.

1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-33.txt

Large diffs are not rendered by default.

1,815 changes: 1,815 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-34.txt

Large diffs are not rendered by default.

1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-35.txt

Large diffs are not rendered by default.

1,817 changes: 1,817 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-4.txt

Large diffs are not rendered by default.

1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-5.txt

Large diffs are not rendered by default.

1,817 changes: 1,817 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-6.txt

Large diffs are not rendered by default.

1,824 changes: 1,824 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-7.txt

Large diffs are not rendered by default.

1,813 changes: 1,813 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-8.txt

Large diffs are not rendered by default.

1,817 changes: 1,817 additions & 0 deletions integration_tests/strategy_sas/result/MASS-SML_REF_CHECK-9.txt

Large diffs are not rendered by default.

89 changes: 89 additions & 0 deletions integration_tests/strategy_sas/result/SML_REF_CHECK.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
Title: SML_REF_CHECK
Description: mass simulation test for reference period
Strategy: SML-R7-1126, Budget: 5000000, Currency: BTC
2022-07-01T00:00:00 ~ 2023-05-30T23:59:00 (36)
수익률 평균: 0.18
수익률 편차: 0.849
수익률 최대: 3.423, 20
수익률 최소: -0.774, 6
수익률 TOP 10 ===============================================
3.423, 20
2.394, 7
1.871, 27
1.318, 34
0.858, 25
0.591, 13
0.302, 32
0.0, 23
0.0, 24
0.0, 26
수익률 WORST 10 ===============================================
-0.774, 6
-0.742, 22
-0.701, 21
-0.586, 14
-0.431, 11
-0.422, 19
-0.379, 4
-0.247, 9
0.0, 35
0.0, 16
순간 최대 수익률 BEST 10 =====================================
4.713, 20
3.681, 7
2.813, 27
1.467, 34
1.398, 25
1.224, 13
0.755, 22
0.637, 32
0.222, 4
0.025, 11
순간 최저 수익률 WORST 10 =====================================
-0.774, 6
-0.742, 22
-0.701, 21
-0.586, 14
-0.431, 11
-0.422, 19
-0.379, 4
-0.247, 9
-0.193, 34
-0.182, 7
순번, 인덱스, 구간 수익률, 최대 수익률, 최저 수익률 ===
1, 20, 3.423, 4.713, -0.041
2, 7, 2.394, 3.681, -0.182
3, 27, 1.871, 2.813, 0.0
4, 34, 1.318, 1.467, -0.193
5, 25, 0.858, 1.398, -0.05
6, 13, 0.591, 1.224, -0.018
7, 32, 0.302, 0.637, -0.069
8, 23, 0.0, 0.0, 0.0
9, 24, 0.0, 0.0, 0.0
10, 26, 0.0, 0.0, 0.0
11, 0, 0.0, 0.0, 0.0
12, 28, 0.0, 0.0, 0.0
13, 1, 0.0, 0.0, 0.0
14, 30, 0.0, 0.0, 0.0
15, 31, 0.0, 0.0, 0.0
16, 33, 0.0, 0.0, 0.0
17, 29, 0.0, 0.0, 0.0
18, 18, 0.0, 0.0, 0.0
19, 17, 0.0, 0.0, 0.0
20, 12, 0.0, 0.0, 0.0
21, 2, 0.0, 0.0, 0.0
22, 3, 0.0, 0.0, 0.0
23, 5, 0.0, 0.0, 0.0
24, 8, 0.0, 0.0, 0.0
25, 10, 0.0, 0.0, 0.0
26, 15, 0.0, 0.0, 0.0
27, 16, 0.0, 0.0, 0.0
28, 35, 0.0, 0.0, 0.0
29, 9, -0.247, 0.0, -0.247
30, 4, -0.379, 0.222, -0.379
31, 19, -0.422, 0.0, -0.422
32, 11, -0.431, 0.025, -0.431
33, 14, -0.586, 0.0, -0.586
34, 21, -0.701, 0.0, -0.701
35, 22, -0.742, 0.755, -0.742
36, 6, -0.774, 0.0, -0.774
99 changes: 99 additions & 0 deletions integration_tests/strategy_sas/sas_ITG_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# python -m unittest discover .\integration_tests\strategy_sas *test.py -v
import json
import unittest
from smtm import Simulator
from unittest.mock import *

KEY_PERIOD = [
"220701.000000-220701.195900",
"220710.040000-220710.235900",
"220711.200000-220712.155900",
"220712.160000-220713.115900",
"220715.040000-220715.235900",
"220723.120000-220724.075900",
"220724.130000-220725.085900",
"220728.120000-220729.075900",
"220731.000000-220731.195900",
"220811.230000-220812.185900",
"220814.040000-220814.235900",
"220815.000000-220815.195900",
"220826.160000-220827.115900",
"220830.000000-220830.195900",
"221004.080000-221005.035900",
"221010.040000-221010.235900",
"221026.170000-221027.125900",
"221101.000000-221101.195900",
"221114.040000-221114.235900",
"221118.080000-221119.035900",
"230112.160000-230113.115900",
"230113.100000-230114.055900",
"230117.160000-230118.115900",
"230210.000000-230210.195900",
"230227.120000-230228.075900",
"230422.130000-230423.085900",
"230424.080000-230425.035900",
"230425.220000-230426.175900",
"230428.120000-230429.075900",
"230507.160000-230508.115900",
"230511.200000-230512.155900",
"230512.160000-230513.115900",
"230516.000000-230516.195900",
"230527.160000-230528.115900",
"230528.120000-230529.075900",
"230530.040000-230530.235900",
]


class SasIntegrationTests(unittest.TestCase):
def test_ITG_run_single_simulation(self):
result_list = []
for period in KEY_PERIOD:
request_list = []
simulator = Simulator(
budget=1000000,
interval=0.0001,
strategy="SAS",
from_dash_to=period,
currency="BTC",
)
simulator.run_single()

for request in simulator.operator.analyzer.request_list:
request_list.append(
{
"type": request["type"],
"price": request["price"],
"amount": request["amount"],
"date_time": request["date_time"],
}
)
result_list.append(request_list)

# If you want to update the expected result, uncomment the following code.
# with open("./sml_test_result.json", "w") as f:
# json.dump(result_list, f)

with open("./integration_tests/strategy_sml/sml_test_result.json", "r") as f:
expected = json.load(f)

for result_idx, request_list in enumerate(result_list):
for request_idx, requst in enumerate(request_list):
target = expected[result_idx][request_idx]
try:
self.assertEqual(
requst["type"], target["type"], f"{result_idx}-{request_idx}"
)
self.assertEqual(
requst["price"], target["price"], f"{result_idx}-{request_idx}"
)
self.assertEqual(
requst["amount"], target["amount"], f"{result_idx}-{request_idx}"
)
self.assertEqual(
requst["date_time"],
target["date_time"],
f"{result_idx}-{request_idx}",
)
except AssertionError as e:
print(f"[FAIL] result_idx:{result_idx} - request_idx:{request_idx}")
raise e
1 change: 1 addition & 0 deletions integration_tests/strategy_sas/sml_test_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
1 change: 1 addition & 0 deletions smtm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .strategy.strategy_sma_ml import StrategySmaMl
from .strategy.strategy_rsi import StrategyRsi
from .strategy.strategy_factory import StrategyFactory
from .strategy.strategy_sas import StrategySas
from .trader.simulation_trader import SimulationTrader
from .trader.virtual_market import VirtualMarket
from .trader.demo_trader import DemoTrader
Expand Down
2 changes: 1 addition & 1 deletion smtm/controller/telegram_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,5 +561,5 @@ def _terminate(self, signum=None, frame=None):
def alert_callback(self, msg):
"""예외 상황 처리"""
self._send_text_message(
f"트레이딩 중 문제가 발생하여 트레이딩이 중단되었습니다! {msg}", self.main_keyboard
f"Alert: {msg}", self.main_keyboard
)
15 changes: 11 additions & 4 deletions smtm/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,28 @@ def initialize(self, data_provider, strategy, trader, analyzer, budget=500):
if self.state is not None:
return

def add_spot_callback(date_time, value):
def _add_spot_callback(date_time, value):
analyzer.add_drawing_spot(date_time, value)

def add_line_callback(date_time, value):
def _add_line_callback(date_time, value):
analyzer.add_value_for_line_graph(date_time, value)

def _alert_callback(msg):
if self.alert_callback is not None:
self.alert_callback(msg)
else:
self.logger.warning(f"alert callback is called: {msg}")

self.data_provider = data_provider
self.strategy = strategy
self.trader = trader
self.analyzer = analyzer
self.state = "ready"
self.strategy.initialize(
budget,
add_spot_callback=add_spot_callback,
add_line_callback=add_line_callback,
add_spot_callback=_add_spot_callback,
add_line_callback=_add_line_callback,
alert_callback=_alert_callback,
)
self.analyzer.initialize(trader.get_account_info)
self.tag = datetime.now().strftime("%Y%m%d-%H%M%S")
Expand Down
Loading

0 comments on commit 231a588

Please sign in to comment.