Skip to content

Commit 5e4b64f

Browse files
Merge branch 'dev' into public
# Conflicts: # CHANGELOG.md # martin_binance/__init__.py # martin_binance/executor.py
2 parents ef2066e + 0129600 commit 5e4b64f

16 files changed

+2652
-2578
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 3.0.0rc1 - 2024-03-02
2+
### Fix
3+
# TODO * `cancel_order_call()`: not raised asyncio.TimeoutError on timeout
4+
* `on_balance_update_ex()`: don't update direct initial asset values in Reverse cycle
5+
* Partially filled TP in Hold reverse cycle
6+
7+
### Update
8+
* Project architecture
9+
110
## 2.1.5 - 2024-02-25
211
### Fix
312
* ModuleNotFoundError: No module named 'optimizer' #73

martin_binance/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,29 @@
66
__author__ = "Jerry Fedorenko"
77
__copyright__ = "Copyright © 2021 Jerry Fedorenko aka VM"
88
__license__ = "MIT"
9-
__version__ = "2.1.5"
9+
__version__ = "3.0.0rc1"
1010
__maintainer__ = "Jerry Fedorenko"
1111
__contact__ = "https://github.com/DogsTailFarmer"
1212

13+
# TODO For fee not in pair calculate it for later use
14+
1315
from pathlib import Path
1416
from shutil import copy
1517

18+
from exchanges_wrapper.definitions import Interval
19+
20+
HEARTBEAT = 2 # Sec
21+
ORDER_TIMEOUT = HEARTBEAT * 15 # Sec
22+
KLINES_INIT = [Interval.ONE_MINUTE, Interval.FIFTY_MINUTES, Interval.ONE_HOUR]
23+
#
1624
WORK_PATH = Path(Path.home(), ".MartinBinance")
1725
CONFIG_PATH = Path(WORK_PATH, "config")
1826
CONFIG_FILE = Path(CONFIG_PATH, "ms_cfg.toml")
1927
DB_FILE = Path(WORK_PATH, "funds_rate.db")
2028
LOG_PATH = Path(WORK_PATH, "log")
2129
LAST_STATE_PATH = Path(WORK_PATH, "last_state")
2230
BACKTEST_PATH = Path(WORK_PATH, "back_test")
31+
EQUAL_STR = "================================================================"
2332

2433

2534
def init():

martin_binance/backtest/OoTSP.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
__author__ = "Jerry Fedorenko"
77
__copyright__ = "Copyright © 2021 Jerry Fedorenko aka VM"
88
__license__ = "MIT"
9-
__version__ = "2.1.5"
9+
__version__ = "3.0.0rc1"
1010
__maintainer__ = "Jerry Fedorenko"
1111
__contact__ = "https://github.com/DogsTailFarmer"
1212

13-
13+
import asyncio
1414
from pathlib import Path
1515
import optuna
1616
import inquirer

martin_binance/backtest/optimizer.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,30 @@
66
__author__ = "Jerry Fedorenko"
77
__copyright__ = "Copyright © 2024 Jerry Fedorenko aka VM"
88
__license__ = "MIT"
9-
__version__ = "2.1.0"
9+
__version__ = "3.0.0rc1"
1010
__maintainer__ = "Jerry Fedorenko"
1111
__contact__ = "https://github.com/DogsTailFarmer"
1212

1313

14+
import asyncio
1415
import importlib.util as iu
16+
import stat
17+
import sys
1518
from decimal import Decimal
19+
from pathlib import Path
20+
1621
import optuna
17-
import asyncio
18-
import sys
1922
import ujson as json
20-
from pathlib import Path
21-
import stat
23+
2224

2325
OPTIMIZER = Path(__file__).absolute()
2426
OPTIMIZER.chmod(OPTIMIZER.stat().st_mode | stat.S_IEXEC)
2527
PARAMS_FLOAT = ['KBB']
28+
STRATEGY = None
29+
30+
31+
def notify_exception(*args):
32+
pass
2633

2734

2835
def any2str(_x) -> str:
@@ -35,11 +42,14 @@ def try_trade(mbs, skip_log, **kwargs):
3542
mbs.ex.MODE = 'S'
3643
mbs.ex.SAVE_DS = False
3744
mbs.ex.LOGGING = not skip_log
38-
mbs.trade()
39-
return float(mbs.session_result.get('profit', 0)) + float(mbs.session_result.get('free', 0))
45+
global STRATEGY
46+
STRATEGY = mbs.trade(STRATEGY)
47+
return float(mbs.ex.SESSION_RESULT.get('profit', 0)) + float(mbs.ex.SESSION_RESULT.get('free', 0))
48+
4049

50+
def optimize(study_name, cli, n_trials, storage_name=None, skip_log=True, show_progress_bar=False):
51+
sys.excepthook = notify_exception
4152

42-
def optimize(study_name, strategy, n_trials, storage_name=None, skip_log=True, show_progress_bar=False):
4353
def objective(_trial):
4454
params = {
4555
'GRID_MAX_COUNT': _trial.suggest_int('GRID_MAX_COUNT', 3, 5),
@@ -55,7 +65,7 @@ def objective(_trial):
5565
}
5666
return try_trade(mbs, skip_log, **params)
5767

58-
spec = iu.spec_from_file_location("strategy", strategy)
68+
spec = iu.spec_from_file_location("strategy", cli)
5969
mbs = iu.module_from_spec(spec)
6070
spec.loader.exec_module(mbs)
6171
optuna.logging.set_verbosity(optuna.logging.WARNING)

martin_binance/cli_0_BTCUSDT.py.template

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
__author__ = "Jerry Fedorenko"
88
__copyright__ = "Copyright © 2021 Jerry Fedorenko aka VM"
99
__license__ = "MIT"
10-
__version__ = "2.1.0"
10+
__version__ = "3.0.0"
1111
__maintainer__ = "Jerry Fedorenko"
1212
__contact__ = "https://github.com/DogsTailFarmer"
1313
"""
@@ -26,9 +26,9 @@ import sys
2626
import asyncio
2727
from pathlib import Path
2828
import toml
29-
import martin_binance.executor as ex
29+
3030
from martin_binance import CONFIG_FILE, LOG_PATH, LAST_STATE_PATH
31-
from martin_binance.margin_wrapper import main, ask_exit, LogLevel, session_result # noqa
31+
import martin_binance.params as ex
3232
################################################################
3333
# Exchange setup and parameter settings
3434
################################################################
@@ -90,7 +90,7 @@ ex.MODE = 'T' # 'T' - Trade, 'TC' - Trade and Collect, 'S' - Simulate
9090
ex.SAVE_DS = False # Save session result data (ticker, orders) for compare
9191
ex.SAVE_PERIOD = 24 * 60 * 60 # sec, timetable for save data portion
9292
ex.SELF_OPTIMIZATION = True # Cyclic self-optimization of parameters, together with MODE == 'TC'
93-
ex.N_TRIALS = 500 # Number of optimization cycles for optuna study in self optimization mode
93+
ex.N_TRIALS = 250 # Number of optimization cycles for optuna study in self optimization mode
9494
################################################################
9595
# DO NOT EDIT UNDER THIS LINE ###
9696
################################################################
@@ -110,9 +110,8 @@ for tlg in telegram:
110110
break
111111

112112

113-
def trade():
113+
def trade(strategy=None):
114114
import logging.handlers
115-
loop = asyncio.get_event_loop()
116115
# For autoload last state
117116
ex.LOAD_LAST_STATE = 1 if len(sys.argv) > 1 else 0
118117
#
@@ -126,21 +125,26 @@ def trade():
126125
_logger.addHandler(handler)
127126
_logger.propagate = False
128127
#
128+
if strategy is None:
129+
from martin_binance.executor import Strategy
130+
strategy = Strategy()
131+
loop = asyncio.get_event_loop()
129132
try:
130-
loop.create_task(main(ex.SYMBOL))
133+
loop.create_task(strategy.main(ex.SYMBOL))
131134
loop.run_forever()
132135
except KeyboardInterrupt:
133136
pass
134137
finally:
135138
try:
136-
loop.run_until_complete(ask_exit())
139+
loop.run_until_complete(strategy.ask_exit())
137140
except asyncio.CancelledError:
138141
pass
139142
except Exception as _err:
140143
print(f"Error: {_err}")
141144
loop.run_until_complete(loop.shutdown_asyncgens())
142145
if ex.MODE in ('T', 'TC'):
143146
loop.close()
147+
return strategy
144148

145149

146150
if __name__ == "__main__":

martin_binance/cli_1_BTCUSDT.py.template

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
__author__ = "Jerry Fedorenko"
88
__copyright__ = "Copyright © 2021 Jerry Fedorenko aka VM"
99
__license__ = "MIT"
10-
__version__ = "2.1.0"
10+
__version__ = "3.0.0"
1111
__maintainer__ = "Jerry Fedorenko"
1212
__contact__ = "https://github.com/DogsTailFarmer"
1313
"""
@@ -26,9 +26,9 @@ import sys
2626
import asyncio
2727
from pathlib import Path
2828
import toml
29-
import martin_binance.executor as ex
29+
3030
from martin_binance import CONFIG_FILE, LOG_PATH, LAST_STATE_PATH
31-
from martin_binance.margin_wrapper import main, ask_exit, LogLevel, session_result # noqa
31+
import martin_binance.params as ex
3232
################################################################
3333
# Exchange setup and parameter settings
3434
################################################################
@@ -90,7 +90,7 @@ ex.MODE = 'T' # 'T' - Trade, 'TC' - Trade and Collect, 'S' - Simulate
9090
ex.SAVE_DS = False # Save session result data (ticker, orders) for compare
9191
ex.SAVE_PERIOD = 24 * 60 * 60 # sec, timetable for save data portion
9292
ex.SELF_OPTIMIZATION = True # Cyclic self-optimization of parameters, together with MODE == 'TC'
93-
ex.N_TRIALS = 500 # Number of optimization cycles for optuna study in self optimization mode
93+
ex.N_TRIALS = 250 # Number of optimization cycles for optuna study in self optimization mode
9494
################################################################
9595
# DO NOT EDIT UNDER THIS LINE ###
9696
################################################################
@@ -110,9 +110,8 @@ for tlg in telegram:
110110
break
111111

112112

113-
def trade():
113+
def trade(strategy=None):
114114
import logging.handlers
115-
loop = asyncio.get_event_loop()
116115
# For autoload last state
117116
ex.LOAD_LAST_STATE = 1 if len(sys.argv) > 1 else 0
118117
#
@@ -126,21 +125,26 @@ def trade():
126125
_logger.addHandler(handler)
127126
_logger.propagate = False
128127
#
128+
if strategy is None:
129+
from martin_binance.executor import Strategy
130+
strategy = Strategy()
131+
loop = asyncio.get_event_loop()
129132
try:
130-
loop.create_task(main(ex.SYMBOL))
133+
loop.create_task(strategy.main(ex.SYMBOL))
131134
loop.run_forever()
132135
except KeyboardInterrupt:
133136
pass
134137
finally:
135138
try:
136-
loop.run_until_complete(ask_exit())
139+
loop.run_until_complete(strategy.ask_exit())
137140
except asyncio.CancelledError:
138141
pass
139142
except Exception as _err:
140143
print(f"Error: {_err}")
141144
loop.run_until_complete(loop.shutdown_asyncgens())
142145
if ex.MODE in ('T', 'TC'):
143146
loop.close()
147+
return strategy
144148

145149

146150
if __name__ == "__main__":

martin_binance/cli_2_TESTBTCTESTUSDT.py.template

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
__author__ = "Jerry Fedorenko"
88
__copyright__ = "Copyright © 2021 Jerry Fedorenko aka VM"
99
__license__ = "MIT"
10-
__version__ = "2.1.0"
10+
__version__ = "3.0.0"
1111
__maintainer__ = "Jerry Fedorenko"
1212
__contact__ = "https://github.com/DogsTailFarmer"
1313
"""
@@ -26,9 +26,9 @@ import sys
2626
import asyncio
2727
from pathlib import Path
2828
import toml
29-
import martin_binance.executor as ex
29+
3030
from martin_binance import CONFIG_FILE, LOG_PATH, LAST_STATE_PATH
31-
from martin_binance.margin_wrapper import main, ask_exit, LogLevel, session_result # noqa
31+
import martin_binance.params as ex
3232
################################################################
3333
# Exchange setup and parameter settings
3434
################################################################
@@ -90,7 +90,7 @@ ex.MODE = 'T' # 'T' - Trade, 'TC' - Trade and Collect, 'S' - Simulate
9090
ex.SAVE_DS = False # Save session result data (ticker, orders) for compare
9191
ex.SAVE_PERIOD = 24 * 60 * 60 # sec, timetable for save data portion
9292
ex.SELF_OPTIMIZATION = True # Cyclic self-optimization of parameters, together with MODE == 'TC'
93-
ex.N_TRIALS = 500 # Number of optimization cycles for optuna study in self optimization mode
93+
ex.N_TRIALS = 250 # Number of optimization cycles for optuna study in self optimization mode
9494
################################################################
9595
# DO NOT EDIT UNDER THIS LINE ###
9696
################################################################
@@ -110,9 +110,8 @@ for tlg in telegram:
110110
break
111111

112112

113-
def trade():
113+
def trade(strategy=None):
114114
import logging.handlers
115-
loop = asyncio.get_event_loop()
116115
# For autoload last state
117116
ex.LOAD_LAST_STATE = 1 if len(sys.argv) > 1 else 0
118117
#
@@ -126,21 +125,26 @@ def trade():
126125
_logger.addHandler(handler)
127126
_logger.propagate = False
128127
#
128+
if strategy is None:
129+
from martin_binance.executor import Strategy
130+
strategy = Strategy()
131+
loop = asyncio.get_event_loop()
129132
try:
130-
loop.create_task(main(ex.SYMBOL))
133+
loop.create_task(strategy.main(ex.SYMBOL))
131134
loop.run_forever()
132135
except KeyboardInterrupt:
133136
pass
134137
finally:
135138
try:
136-
loop.run_until_complete(ask_exit())
139+
loop.run_until_complete(strategy.ask_exit())
137140
except asyncio.CancelledError:
138141
pass
139142
except Exception as _err:
140143
print(f"Error: {_err}")
141144
loop.run_until_complete(loop.shutdown_asyncgens())
142145
if ex.MODE in ('T', 'TC'):
143146
loop.close()
147+
return strategy
144148

145149

146150
if __name__ == "__main__":

0 commit comments

Comments
 (0)