Skip to content

Commit

Permalink
1.3.1b5
Browse files Browse the repository at this point in the history
  • Loading branch information
DogsTailFarmer committed Jun 21, 2023
1 parent 83be491 commit 010092c
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 86 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Docker Image CI

on:
workflow_dispatch:
release:
types: [published]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: PrepareReg Names
run: |
echo IMAGE_REPOSITORY=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV
echo IMAGE_TAG=$(echo ${{ github.ref }} | tr '[:upper:]' '[:lower:]' | awk '{split($0,a,"/"); print a[3]}') >> $GITHUB_ENV
- name: Login to GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
run: |
docker build . --tag ghcr.io/$IMAGE_REPOSITORY:$IMAGE_TAG
docker push ghcr.io/$IMAGE_REPOSITORY:$IMAGE_TAG
docker build . --tag ghcr.io/$IMAGE_REPOSITORY:latest
docker push ghcr.io/$IMAGE_REPOSITORY:latest
35 changes: 35 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries

name: Upload Python Package

on:
workflow_dispatch:
release:
types: [published]

permissions:
contents: read

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v1.3.1 2023-06-xx
### Update
* Up requirements for exchanges-wrapper to 1.3.1
* Refactoring the deployment process

## v1.3.0-4 2023-06-20
### Fix
* ```get_free_assets(mode='free', backtest=True)``` return incorrect value for backtest Reverse cycle
Expand Down
48 changes: 25 additions & 23 deletions martin_binance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@
__author__ = "Jerry Fedorenko"
__copyright__ = "Copyright © 2021 Jerry Fedorenko aka VM"
__license__ = "MIT"
__version__ = "1.3.0-4"
__version__ = "1.3.1b5"
__maintainer__ = "Jerry Fedorenko"
__contact__ = "https://github.com/DogsTailFarmer"

from pathlib import Path
from shutil import copy
#
import platform

print(f"Python {platform.python_version()}")
#
STANDALONE = True
if 'margin' in str(Path().resolve()):
print('margin detected')
Expand All @@ -33,21 +29,27 @@
LOG_PATH = None
LAST_STATE_PATH = None
BACKTEST_PATH = None
if CONFIG_FILE.exists():
print(f"Client config found at {CONFIG_FILE}")
else:
print("Can't find config file! Creating it...")
CONFIG_PATH.mkdir(parents=True, exist_ok=True)
if STANDALONE:
LOG_PATH.mkdir(parents=True, exist_ok=True)
LAST_STATE_PATH.mkdir(parents=True, exist_ok=True)
copy(Path(Path(__file__).parent.absolute(), "ms_cfg.toml.template"), CONFIG_FILE)
copy(Path(Path(__file__).parent.absolute(), "funds_rate.db.template"), DB_FILE)
copy(Path(Path(__file__).parent.absolute(), "cli_0_BTCUSDT.py.template"), Path(WORK_PATH, "cli_0_BTCUSDT.py"))
copy(Path(Path(__file__).parent.absolute(), "cli_1_BTCUSDT.py.template"), Path(WORK_PATH, "cli_1_BTCUSDT.py"))
copy(Path(Path(__file__).parent.absolute(), "cli_2_TESTBTCTESTUSDT.py.template"),
Path(WORK_PATH, "cli_2_TESTBTCTESTUSDT.py"))
print(f"Before the first run, set the parameters in {CONFIG_FILE}")
if STANDALONE:
raise SystemExit(1)
raise UserWarning()

def init():
if CONFIG_FILE.exists():
print(f"Client config found at {CONFIG_FILE}")
else:
print("Can't find client config file! Creating it...")
CONFIG_PATH.mkdir(parents=True, exist_ok=True)
if STANDALONE:
LOG_PATH.mkdir(parents=True, exist_ok=True)
LAST_STATE_PATH.mkdir(parents=True, exist_ok=True)
copy(Path(Path(__file__).parent.absolute(), "ms_cfg.toml.template"), CONFIG_FILE)
copy(Path(Path(__file__).parent.absolute(), "funds_rate.db.template"), DB_FILE)
copy(Path(Path(__file__).parent.absolute(), "cli_0_BTCUSDT.py.template"), Path(WORK_PATH, "cli_0_BTCUSDT.py"))
copy(Path(Path(__file__).parent.absolute(), "cli_1_BTCUSDT.py.template"), Path(WORK_PATH, "cli_1_BTCUSDT.py"))
copy(Path(Path(__file__).parent.absolute(), "cli_2_TESTBTCTESTUSDT.py.template"),
Path(WORK_PATH, "cli_2_TESTBTCTESTUSDT.py"))
print(f"Before the first run, set the parameters in {CONFIG_FILE}")
if STANDALONE:
raise SystemExit(1)
raise UserWarning()


if __name__ == '__main__':
init()
123 changes: 64 additions & 59 deletions martin_binance/backtest/OoTSP.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
__author__ = "Jerry Fedorenko"
__copyright__ = "Copyright © 2021 Jerry Fedorenko aka VM"
__license__ = "MIT"
__version__ = "1.3.0-2"
__version__ = "1.3.1b5"
__maintainer__ = "Jerry Fedorenko"
__contact__ = "https://github.com/DogsTailFarmer"

Expand All @@ -18,53 +18,21 @@
from inquirer.themes import GreenPassion
from martin_binance import BACKTEST_PATH

questions = [
inquirer.List(
"path",
message="Select from saved: exchange_PAIR with the strategy you want to optimize",
choices=[f.name for f in BACKTEST_PATH.iterdir() if f.is_dir() and f.name.count('_') == 1],
),
inquirer.List(
"mode",
message="New study session or optimization history plot from saved one",
choices=["New", "Plot from saved"],
),
inquirer.Text(
"n_trials",
message="Enter number of cycles, from 50 to 500",
ignore=lambda x: x["mode"] == "Plot from saved",
default='150',
validate=lambda _, c: 50 <= int(c) < 500,
),
]

answers = inquirer.prompt(questions, theme=GreenPassion())
PARAMS_FLOAT = ['PRICE_SHIFT', 'KBB']

study_name = answers.get('path') # Unique identifier of the study
storage_name = f"sqlite:///{Path(BACKTEST_PATH, study_name, f'{study_name}.db')}"
def try_trade(mbs, **kwargs):
for key, value in kwargs.items():
print(key, value)
setattr(mbs.ex, key, value if isinstance(value, int) or key in PARAMS_FLOAT else Decimal(f"{value}"))
mbs.ex.MODE = 'S'
mbs.ex.SAVE_DS = False
mbs.trade()
result = float(mbs.session_result.get('profit', 0)) + float(mbs.session_result.get('free', 0))
return result

if answers.get('mode') == 'New':
Path(BACKTEST_PATH, study_name, f'{study_name}.db').unlink(missing_ok=True)
try:
strategy = next(Path(BACKTEST_PATH, answers.get('path')).glob("cli_*.py"))
except StopIteration:
raise UserWarning(f"Can't find cli_*.py in {Path(BACKTEST_PATH, answers.get('path'))}")
spec = importlib.util.spec_from_file_location("strategy", strategy)
mbs = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mbs)

PARAMS_FLOAT = ['PRICE_SHIFT', 'KBB']

def try_trade(**kwargs):
for key, value in kwargs.items():
print(key, value)
setattr(mbs.ex, key, value if isinstance(value, int) or key in PARAMS_FLOAT else Decimal(f"{value}"))
mbs.ex.MODE = 'S'
mbs.ex.SAVE_DS = False
mbs.trade()
result = float(mbs.session_result.get('profit', 0)) + float(mbs.session_result.get('free', 0))
return result

def main():
def objective(trial):
params = {
'GRID_MAX_COUNT': trial.suggest_int('GRID_MAX_COUNT', 3, 5),
Expand All @@ -78,22 +46,59 @@ def objective(trial):
'KBB': trial.suggest_float('KBB', 1, 5, step=0.5),
'LINEAR_GRID_K': trial.suggest_int('LINEAR_GRID_K', 0, 100, step=20),
}
return try_trade(**params)
return try_trade(mbs, **params)

study = optuna.create_study(study_name=study_name, storage=storage_name, direction="maximize")
study.optimize(objective, n_trials=int(answers.get('n_trials', '0')))
questions = [
inquirer.List(
"path",
message="Select from saved: exchange_PAIR with the strategy you want to optimize",
choices=[f.name for f in BACKTEST_PATH.iterdir() if f.is_dir() and f.name.count('_') == 1],
),
inquirer.List(
"mode",
message="New study session or optimization history plot from saved one",
choices=["New", "Plot from saved"],
),
inquirer.Text(
"n_trials",
message="Enter number of cycles, from 50 to 500",
ignore=lambda x: x["mode"] == "Plot from saved",
default='150',
validate=lambda _, c: 2 <= int(c) < 500,
),
]

print(f"Optimal parameters: {study.best_params} for get {study.best_value}")
answers = inquirer.prompt(questions, theme=GreenPassion())

study_name = answers.get('path') # Unique identifier of the study
storage_name = f"sqlite:///{Path(BACKTEST_PATH, study_name, f'{study_name}.db')}"

if answers.get('mode') == 'New':
Path(BACKTEST_PATH, study_name, f'{study_name}.db').unlink(missing_ok=True)
try:
strategy = next(Path(BACKTEST_PATH, answers.get('path')).glob("cli_*.py"))
except StopIteration:
raise UserWarning(f"Can't find cli_*.py in {Path(BACKTEST_PATH, answers.get('path'))}")
spec = importlib.util.spec_from_file_location("strategy", strategy)
mbs = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mbs)
study = optuna.create_study(study_name=study_name, storage=storage_name, direction="maximize")
study.optimize(objective, n_trials=int(answers.get('n_trials', '0')))
print(f"Optimal parameters: {study.best_params} for get {study.best_value}")
importance_params = optuna.importance.get_param_importances(study)
print("Evaluate parameter importance based on completed trials in the given study:")
for p in importance_params.items():
print(p)
print(f"Study instance saved to {storage_name} for later use")
else:
study = optuna.load_study(study_name=study_name, storage=storage_name)
#
try:
fig = optuna.visualization.plot_optimization_history(study)
fig.show()
except ImportError:
print("Can't find GUI, you can copy study instance to another environment for analyze it")

importance_params = optuna.importance.get_param_importances(study)
print("Evaluate parameter importance based on completed trials in the given study:")
for p in importance_params.items():
print(p)
else:
study = optuna.load_study(study_name=study_name, storage=storage_name)

try:
fig = optuna.visualization.plot_optimization_history(study)
fig.show()
except ImportError:
print(f"Study instance saved to {storage_name} for later use")
if __name__ == '__main__':
main()
4 changes: 2 additions & 2 deletions martin_binance/service/relaunch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
__author__ = "Jerry Fedorenko"
__copyright__ = "Copyright © 2021 Jerry Fedorenko aka VM"
__license__ = "MIT"
__version__ = "1.3.0-4"
__version__ = "1.3.1"
__maintainer__ = "Jerry Fedorenko"
__contact__ = 'https://github.com/DogsTailFarmer'
##################################################################
Expand All @@ -26,7 +26,7 @@
window_name = window.get('window_name')
pane = window.attached_pane
if window_name == 'srv':
pane.send_keys('exch_srv.py', enter=True)
pane.send_keys('exchanges-wrapper-srv', enter=True)
else:
last_state = Path(LAST_STATE_PATH, f"{window_name.replace('-', '_').replace('/', '')}.json")
pair = Path(WORK_PATH, f"cli_{window_name.replace('-', '_').replace('/', '')}.py")
Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dynamic = ["version", "description"]
requires-python = ">=3.8"

dependencies = [
"exchanges-wrapper==1.3.0.post2",
"exchanges-wrapper==1.3.1",
"margin-strategy-sdk==0.0.11",
"aiohttp==3.8.4",
"grpcio==1.48.1",
Expand All @@ -43,3 +43,7 @@ name = "martin_binance"

[project.urls]
Source = "https://github.com/DogsTailFarmer/martin-binance"

[project.scripts]
martin-binance-init = "martin_binance.__init__:init"
martin-binance-backtest = "martin_binance.backtest.OoTSP:main"
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exchanges-wrapper==1.3.0.post2
exchanges-wrapper==1.3.1
margin-strategy-sdk==0.0.11
aiohttp==3.8.4
grpcio==1.48.1
Expand Down

0 comments on commit 010092c

Please sign in to comment.