Skip to content

Commit

Permalink
Merge pull request #120 from Azulinho/next_release
Browse files Browse the repository at this point in the history
Next release
  • Loading branch information
Azulinho authored Oct 23, 2022
2 parents 8cd1959 + a467db1 commit 0fee4c0
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 68 deletions.
45 changes: 11 additions & 34 deletions .hookz.yaml
Original file line number Diff line number Diff line change
@@ -1,39 +1,16 @@
# run with:
# mkdir .git/hooks
# hookz initialize --verbose-output
# hookz reset --verbose-output
version: 2.4.2
#
hooks:
- type: pre-commit
actions:
- name: black
script: |
#!/bin/bash
set -e
black --check app.py
black --check load_klines_for_coin.py
black --check strategies/
black --check lib/
black --check tests/
- name: pylint
script: |
#!/bin/bash
set -e
pylint app.py
pylint load_klines_for_coin.py
ls strategies/*.py |grep -v Local | xargs pylint
pylint lib/*.py
- name: mypy
script: |
#!/bin/bash
set -e
mypy app.py
mypy load_klines_for_coin.py
ls strategies/*.py |grep -v Local | xargs mypy
mypy lib/*.py
- name: pytest
script: |
#!/bin/bash
set -e
pytest tests/
- name: "make pre-commit-checks"
exec: "make"
args: ["checks"]
- type: pre-rebase
actions:
- name: "make pre-commit-checks"
exec: "make"
args: ["checks"]
25 changes: 24 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.PHONY: default
default: help ;
PATH := $(PWD)/.venv/bin:$(PATH)
SHELL := /usr/bin/env bash

WHOAMI := $$(whoami)
Expand Down Expand Up @@ -91,9 +92,31 @@ prove-backtesting: checks dcompose_id
config-endpoint-service: checks dcompose_id
$(PREFIX_VARS) docker compose --profile config-endpoint-service -p $(DCOMPOSE_ID) run --rm \
--service-ports \
-e CONFIG=$(CONFIG) -e BACKTRACK=$(BACKTRACK) -e SORTBY=$(SORTBY) -e RUN_AT=$(RUN_AT) -e PAIRING=$(PAIRING) -e MIN=$(MIN) -e TUNED_CONFIG=$(TUNED_CONFIG) \
-e CONFIG=$(CONFIG) -e BACKTRACK=$(BACKTRACK) -e SORTBY=$(SORTBY) -e PAIRING=$(PAIRING) -e MIN=$(MIN) -e TUNED_CONFIG=$(TUNED_CONFIG) \
config-endpoint-service

pre-commit-checks:
# needs virtualenv
# black
black --check app.py
black --check klines_caching_service.py
black --check strategies/
black --check lib/
black --check tests/
# pylint
pylint app.py
pylint klines_caching_service.py
ls strategies/*.py |grep -v Local | xargs pylint
pylint lib/*.py
# mypy
mypy app.py
mypy klines_caching_service.py
ls strategies/*.py |grep -v Local | xargs mypy
mypy lib/*.py
# pytests
pytest tests/


help:
@echo "USAGE:"
@echo "make logmode CONFIG=< config.yaml >"
Expand Down
76 changes: 47 additions & 29 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@
from lz4.frame import open as lz4open
from tenacity import retry, wait_exponential

from lib.helpers import (add_100, c_date_from, c_from_timestamp,
cached_binance_client, floor_value, mean, percent)
from lib.helpers import (
add_100,
c_date_from,
c_from_timestamp,
cached_binance_client,
floor_value,
mean,
percent,
)


def control_center() -> None:
Expand Down Expand Up @@ -1630,14 +1637,14 @@ def run(self) -> None:
self.clear_all_coins_stats()

while True:
if self.pull_config_address:
self.refresh_config_from_config_endpoint_service()
self.process_coins()
# saves all coin and wallet data to disk
self.save_coins()
self.process_control_flags()
if self.quit:
return
if self.pull_config_address:
self.refresh_config_from_config_endpoint_service()

self.wait()

Expand Down Expand Up @@ -1893,38 +1900,49 @@ def calculates_exposure(self):

def refresh_config_from_config_endpoint_service(self):
"""updates the bot config (ticker list) from the config endpoint"""
old_tickers_in_use = {}
try:
r = requests.get(self.pull_config_address).json()
if r["md5"] != self.pull_config_md5:
for symbol in self.wallet:
# if SELL_ALL_ON_PULL_CONFIG_CHANGE is set, we will
# simply sell all tokens and start from an empty wallet
if self.sell_all_on_pull_config_change:
self.sell_coin(self.coins[symbol])
else:
old_tickers_in_use[symbol] = self.tickers[symbol]
if r["md5"] == self.pull_config_md5:
return

old_tickers_in_use = {}
for symbol in self.wallet:
# if SELL_ALL_ON_PULL_CONFIG_CHANGE is set, we will
# simply sell all tokens and start from an empty wallet
if self.sell_all_on_pull_config_change:
self.sell_coin(self.coins[symbol])
else:
old_tickers_in_use[symbol] = self.tickers[symbol]

old_tickers_in_use.update(r["TICKERS"])
self.tickers = old_tickers_in_use
logging.info(
"updating tickers: had: "
+ f"{self.pull_config_md5} now: {r['md5']}"
)
logging.info("new tickers:")
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(self.tickers)
self.pull_config_md5 = r["md5"]
# clean old coins data, or we will get errors later on
symbols = list(self.coins.keys())
for symbol in symbols:
if symbol not in self.tickers.keys():
del self.coins[symbol]
# we now need to update the config file, so that when we restart
# the bot will have access to all the ticker info on any coins
# it might be holding
with open(self.config_file, encoding="utf-8") as f:
_cfg = yaml.safe_load(f.read())
_cfg["TICKERS"] = self.tickers
with open(self.config_file, "wt", encoding="utf-8") as f:
yaml.dump(_cfg, f, default_flow_style=False)

old_tickers_in_use.update(r["TICKERS"])
self.tickers = old_tickers_in_use
logging.info(
"updating tickers: had: "
+ f"{self.pull_config_md5} now: {r['md5']}"
)
logging.info("new tickers:")
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(self.tickers)
self.pull_config_md5 = r["md5"]
# clean old coins data, or we will get errors later on
symbols = self.coins.keys()
for symbol in symbols:
if symbol not in self.tickers.keys():
del self.coins[symbol]
except Exception as error_msg: # pylint: disable=broad-except
logging.warning(
f"Failed to pull config from {self.pull_config_address}"
)
logging.warning(error_msg)
logging.error(error_msg)

def process_control_flags(self):
"""process control/flags"""
Expand Down
3 changes: 2 additions & 1 deletion utils/automated-backtesting.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
ulimit -n 65535
source /cryptobot/.venv/bin/activate
python -u utils/automated-backtesting.py -l ${LOGFILE} -c ${CONFIG} -m ${MIN} -f "${FILTER}" -s "${SORTBY}"
python -u utils/automated-backtesting.py \
-l ${LOGFILE} -c ${CONFIG} -m ${MIN} -f "${FILTER}" -s "${SORTBY}"
2 changes: 0 additions & 2 deletions utils/config-endpoint-service.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ def api_endpoint():
"-b", "--backtrack", help="number of days to backtrack"
)
parser.add_argument("-s", "--sortby", help="wins|profit")
parser.add_argument("-r", "--run-at", help="01:10")
parser.add_argument(
"-t",
"--tuned-config",
Expand All @@ -135,7 +134,6 @@ def api_endpoint():
config = args.config
backtrack = args.backtrack
sortby = args.sortby
run_at = args.run_at
tuned_config = args.tuned_config
pairing = args.pairing
min_profit = args.min
Expand Down
2 changes: 1 addition & 1 deletion utils/config-endpoint-service.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
ulimit -n 65535
source /cryptobot/.venv/bin/activate
python -u utils/config-endpoint-service.py -c ${CONFIG} -b ${BACKTRACK} -s ${SORTBY} -r ${RUN_AT} -t ${TUNED_CONFIG} -p ${PAIRING} -m ${MIN}
python -u utils/config-endpoint-service.py -c ${CONFIG} -b ${BACKTRACK} -s ${SORTBY} -t ${TUNED_CONFIG} -p ${PAIRING} -m ${MIN}

0 comments on commit 0fee4c0

Please sign in to comment.