Skip to content

Commit

Permalink
revert find_best_results_from_backtesting_log()
Browse files Browse the repository at this point in the history
reverts function from tag v0.0.199
prove-backtesting has not been updating the tickers file correctly since
0.0.200.
  • Loading branch information
Azulinho committed Sep 8, 2023
1 parent 0a84be9 commit 68b4b8f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 205 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ RUN eatmydata /cryptobot/.venv/bin/pip install -r requirements.txt && \

COPY lib/ lib/
COPY utils/__init__.py utils/__init__.py
COPY utils/prove-backtesting.py utils/prove-backtesting.py
COPY utils/prove-backtesting.sh utils/prove-backtesting.sh
COPY utils/pull_klines.py utils/pull_klines.py
COPY utils/config-endpoint-service.py utils/config-endpoint-service.py
COPY utils/config-endpoint-service.sh utils/config-endpoint-service.sh
COPY klines_caching_service.py klines_caching_service.py
COPY price_log_service.py price_log_service.py
COPY app.py .
COPY utils/prove-backtesting.sh utils/prove-backtesting.sh
COPY utils/prove-backtesting.py utils/prove-backtesting.py

36 changes: 14 additions & 22 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
# pylint: disable=missing-function-docstring
# pylint: disable=redefined-outer-name
# pylint: disable=import-outside-toplevel
# pylint: disable=no-self-use
from datetime import datetime
from unittest import mock
import json
from flaky import flaky

import pytest
import app
import lib
import lib.bot
import lib.coin
import pytest
import requests
import json


@pytest.fixture()
Expand Down Expand Up @@ -518,7 +516,7 @@ def test_calculate_volume_size(self, bot, coin):
bot, "get_step_size", return_value=(True, "0.00001000")
) as _:
ok, volume = bot.calculate_volume_size(coin)
assert ok == True
assert ok is True
assert volume == 0.5

def test_get_binance_prices(self, bot):
Expand Down Expand Up @@ -767,12 +765,12 @@ def test_run_stategy_gives_False_if_coin_not_in_tickers(self, bot, coin):
def test_run_stategy_gives_False_if_coin_is_delisted(self, bot, coin):
coin.delisted = True
bot.coins["BTCUSDT"] = coin
bot.run_strategy(coin) is False
assert bot.run_strategy(coin) is False

def test_run_stategy_gives_False_if_coin_is_naughty(self, bot, coin):
coin.naughty = True
bot.coins["BTCUSDT"] = coin
bot.run_strategy(coin) is False
assert bot.run_strategy(coin) is False

def test_run_stategy_calls_sale_if_wallet_not_empty(self, bot, coin):
# if there are coins in WALLET
Expand Down Expand Up @@ -926,7 +924,7 @@ def test_place_sell_order(self, bot, coin):

assert bot.place_sell_order(coin) is False

def test_get_ticker_with_default(self, bot):
def test_get_ticker_with_default(self):
tickers = {
"ETH": {"BUY_AT_PERCENTAGE": "0.05", "SELL_AT_PERCENTAGE": "0.1"},
"BTC": {"BUY_AT_PERCENTAGE": "0.02", "SELL_AT_PERCENTAGE": "0.05"},
Expand All @@ -945,7 +943,7 @@ def test_get_ticker_with_default(self, bot):
== "0.1"
)

def test_get_ticker_with_default_not_in_ticket(self, bot):
def test_get_ticker_with_default_not_in_ticket(self):
tickers = {}
assert (
lib.bot.get_ticker_with_default(
Expand Down Expand Up @@ -1169,7 +1167,7 @@ def test_place_buy_order_limit(
timeInForce="FOK",
price=50000,
)
assert result == True
assert result is True

def test_place_buy_order_market(
self,
Expand Down Expand Up @@ -1204,7 +1202,7 @@ def test_place_buy_order_market(
type="MARKET",
quantity=1.0,
)
assert result == True
assert result is True


class TestCoinStatus:
Expand Down Expand Up @@ -1676,15 +1674,7 @@ def test_coin_bought_when_price_above_averages_threshold(self, bot, coin):
assert result is True


class TestBacktesting:
def backtesting(self):
pass

def backtest_logfile(self):
pass


class TestBotState:
class TestBotState: # pylint: disable=too-few-public-methods
def test_save_coins(self, bot, coin):
mock_open = mock.mock_open()

Expand Down Expand Up @@ -1715,7 +1705,7 @@ def test_coin_is_not_delisted(self, bot, coin):
assert bot.check_for_delisted_coin("BTCUSDT") is False


class TestProcessLine:
class TestProcessLine: # pylint: disable=too-few-public-methods
def test_process_line(self, bot, coin):
symbol = "BTCUSDT"
date = datetime(2021, 1, 1, 0, 0, 0).timestamp()
Expand All @@ -1724,8 +1714,10 @@ def test_process_line(self, bot, coin):
bot.coins = {}
bot.coins["BTCUSDT"] = coin

lib.bot.udatetime.now = mock.MagicMock(
return_value=datetime(2021, 1, 1, 0, 0, 0)
)
bot.process_line(symbol, date, market_price)
lib.bot.udatetime.now.return_value = datetime(2021, 1, 1, 0, 0, 0)

assert symbol in bot.coins
assert coin.symbol == symbol
Expand Down
101 changes: 1 addition & 100 deletions tests/test_prove_backtesting.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
""" test_prove_backtesting """

import os
import unittest
from typing import Tuple, Dict, Any
from typing import Dict
from unittest import mock
from datetime import datetime

Expand Down Expand Up @@ -61,72 +60,6 @@ def __init__(self):
class TestProveBacktesting(unittest.TestCase):
"""Test ProveBacktesting"""

def test_parse_backtesting_line(self):
"""test parse backtesting line"""

pb.get_index_json = mocked_get_index_json_call
obj = pb.ProveBacktesting(CONFIG)

# Define test inputs
line = "|".join(
[
"profit:99.000",
"investment:199.0",
"days:1",
"w1,l0,s0,h0",
"cfg:coin.fake.yaml",
", ".join(
[
'{"CLEAR_COIN_STATS_AT_BOOT": true',
'"CLEAR_COIN_STATS_AT_SALE": true',
'"DEBUG": false',
'"ENABLE_NEW_LISTING_CHECKS": true',
'"ENABLE_NEW_LISTING_CHECKS_AGE_IN_DAYS": 31',
'"INITIAL_INVESTMENT": 100.0',
'"KLINES_CACHING_SERVICE_URL": "http://klines:8999"',
'"MAX_COINS": 1',
'"PAIRING": "USDT"',
'"PAUSE_FOR": 1.0',
'"PRICE_LOGS": ["20211207.log.gz"]',
'"PRICE_LOG_SERVICE_URL": "http://price-log-service:8998"',
'"RE_INVEST_PERCENTAGE": 100.0',
'"SELL_AS_SOON_IT_DROPS": true',
'"STOP_BOT_ON_LOSS": false',
'"STOP_BOT_ON_STALE": false',
'"STRATEGY": "BuyDropSellRecoveryStrategy"',
'"TICKERS": {"fake": {}}',
'"TRADING_FEE": "1e-06"',
'"MODE": "backtesting"}',
]
),
]
)

coins: Dict = {}

# Call the method being tested
result: Any = obj.parse_backtesting_line(line, coins)

# Define the expected output
expected: Tuple = (
True,
{
"fake": {
"profit": 99.0,
"wls": "w1,l0,s0,h0",
"w": 1,
"l": 0,
"s": 0,
"h": 0,
"cfgname": "cfg:coin.fake.yaml",
"coincfg": {},
}
},
)

# Assert the result
self.assertEqual(result, expected)

def test_generate_start_dates(self):
"""Test Generate Start Dates"""

Expand Down Expand Up @@ -299,35 +232,3 @@ def test_gather_best_results_from_run(self, mock_open):
self.assertEqual(result["total_stales"], 3)
self.assertEqual(result["total_holds"], 4)
self.assertEqual(result["total_profit"], 100.0)

def test_find_best_results_from_backtesting_log(self):
"""test find best results from backtesting log"""
pb.get_index_json = mocked_get_index_json_call
instance = pb.ProveBacktesting(CONFIG)

# TODO: mock open and os.remove
# Create a mock backtesting.log file
with open("log/backtesting.log", "w", encoding="utf-8") as f:
f.write("line 1\n")
f.write("line 2\n")
f.write("line 3\n")

def mock_parse_backtesting_line(_, __) -> Tuple[bool, Dict[str, Any]]:
"""mock parse backtesting line"""
return True, {
"coin1": {"coincfg": "config1"},
"coin2": {"coincfg": "config2"},
}

with mock.patch.object(
instance,
"parse_backtesting_line",
side_effect=mock_parse_backtesting_line,
):
# Call the method under test
result = instance.find_best_results_from_backtesting_log("coincfg")

os.remove("log/backtesting.log")

expected_result = {"coin1": "config1", "coin2": "config2"}
self.assertEqual(result, expected_result)
Loading

0 comments on commit 68b4b8f

Please sign in to comment.