Skip to content

Commit

Permalink
Fix #791: [Sw eng] Add sim_ss_test_dict & lake_ss_test_dict to avoid …
Browse files Browse the repository at this point in the history
…DRY violations
  • Loading branch information
trentmc committed Mar 13, 2024
1 parent e8973fa commit 31028cc
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 30 deletions.
19 changes: 19 additions & 0 deletions pdr_backend/ppss/lake_ss.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
from typing import Optional

import numpy as np
from enforce_typing import enforce_types
Expand Down Expand Up @@ -87,3 +88,21 @@ def __str__(self) -> str:
s += f"parquet_dir={self.parquet_dir}\n"
s += "-" * 10 + "\n"
return s


# =========================================================================
# utilities for testing


@enforce_types
def lake_ss_test_dict(parquet_dir: str, feeds: Optional[list] = None):
"""Use this function's return dict 'd' to construct LakeSS(d)"""
feeds = feeds or ["binance BTC/USDT c 5m"]
d = {
"feeds": feeds,
"parquet_dir": parquet_dir,
"st_timestr": "2023-06-18",
"fin_timestr": "2023-06-30",
"timeframe": "5m",
}
return d
21 changes: 21 additions & 0 deletions pdr_backend/ppss/sim_ss.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,24 @@ def tradetype(self) -> str:
@property
def allowed_tradetypes(self) -> List[str]:
return ["livemock", "livereal", "histmock"]


# =========================================================================
# utilities for testing


@enforce_types
def sim_ss_test_dict(log_dir: str) -> dict:
d = {
"do_plot": True,
"log_dir": log_dir,
"test_n": 10,
"exchange_only": {
"timeout": 30000,
"options": {
"createMarketBuyOrderRequiresPrice": False,
"defaultType": "spot",
},
},
}
return d
33 changes: 32 additions & 1 deletion pdr_backend/ppss/test/test_lake_ss.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pdr_backend.cli.arg_feed import ArgFeed
from pdr_backend.cli.arg_feeds import ArgFeeds
from pdr_backend.ppss.lake_ss import LakeSS
from pdr_backend.ppss.lake_ss import LakeSS, lake_ss_test_dict
from pdr_backend.util.time_types import UnixTimeMs

_D = {
Expand Down Expand Up @@ -81,3 +81,34 @@ def test_parquet_dir(tmpdir):
ss = LakeSS(d)
target_parquet_dir = os.path.join(tmpdir, "parquet_data")
assert ss.parquet_dir == target_parquet_dir


@enforce_types
def test_lake_ss_test_dict_1_default_feeds(tmpdir):
parquet_dir = os.path.join(tmpdir, "parquet_data")

d = lake_ss_test_dict(parquet_dir)

assert d["parquet_dir"] == parquet_dir

f = d["feeds"][0]
assert "binance" in f or "kraken" in f
assert "BTC" in f or "ETH" in f
assert "5m" in f or "1h" in f

assert "st_timestr" in d
assert "fin_timestr" in d
assert "timeframe" in d

ss = LakeSS(d)
assert ss.parquet_dir == parquet_dir
assert ss.feeds


@enforce_types
def test_lake_ss_test_dict_2_specify_feeds(tmpdir):
parquet_dir = os.path.join(tmpdir, "parquet_data")
feeds = ["kraken DOT/USDT c 60m", "dydx DOT/USDT c 60m"]
d = lake_ss_test_dict(parquet_dir, feeds)
assert d["parquet_dir"] == parquet_dir
assert d["feeds"] == feeds
12 changes: 5 additions & 7 deletions pdr_backend/ppss/test/test_predictoor_ss.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_predictoor_ss():

@enforce_types
def test_predictoor_ss_test_dict():
# test - reasoonable defaults when nothing passed in
# test - reasonable defaults when nothing passed in
d = predictoor_ss_test_dict()
f = d["predict_feed"]
assert "binance" in f or "kraken" in f
Expand All @@ -64,16 +64,14 @@ def test_predictoor_ss_test_dict():
assert d["aimodel_ss"]["input_feeds"] == ["binance ETH/USDT c 1h"]

# test s_start_payouts attribute set
predictoor_ss = PredictoorSS(d)
ss = PredictoorSS(d)

assert (
predictoor_ss.s_start_payouts == 0
), "Must be unset in the test dict, so should return 0"
assert ss.s_start_payouts == 0, "Must be unset in the test dict, so should return 0"

# let's set it here
d["bot_only"]["s_start_payouts"] = 100
predictoor_ss = PredictoorSS(d)
assert predictoor_ss.s_start_payouts == 100, "Must be set to 100"
ss = PredictoorSS(d)
assert ss.s_start_payouts == 100, "Must be set to 100"


@enforce_types
Expand Down
14 changes: 13 additions & 1 deletion pdr_backend/ppss/test/test_sim_ss.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from enforce_typing import enforce_types

from pdr_backend.ppss.sim_ss import SimSS
from pdr_backend.ppss.sim_ss import SimSS, sim_ss_test_dict

_D = {"do_plot": False, "log_dir": "logs", "test_n": 2}

Expand Down Expand Up @@ -52,3 +52,15 @@ def test_log_dir(tmpdir):
ss = SimSS(d)
target_log_dir = os.path.join(tmpdir, "logs")
assert ss.log_dir == target_log_dir


@enforce_types
def test_sim_ss_test_dict(tmpdir):
log_dir = os.path.join(tmpdir, "logs")
d = sim_ss_test_dict(log_dir)
assert d["do_plot"]
assert d["log_dir"] == log_dir

ss = SimSS(d)
assert ss.do_plot
assert ss.log_dir == log_dir
27 changes: 6 additions & 21 deletions pdr_backend/sim/test/test_sim_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

from enforce_typing import enforce_types

from pdr_backend.ppss.lake_ss import LakeSS
from pdr_backend.ppss.lake_ss import LakeSS, lake_ss_test_dict
from pdr_backend.ppss.ppss import PPSS, fast_test_yaml_str
from pdr_backend.ppss.predictoor_ss import PredictoorSS, predictoor_ss_test_dict
from pdr_backend.ppss.sim_ss import SimSS
from pdr_backend.ppss.sim_ss import SimSS, sim_ss_test_dict
from pdr_backend.sim.sim_engine import SimEngine


Expand All @@ -25,28 +25,13 @@ def test_sim_engine(tmpdir):
ppss.predictoor_ss = PredictoorSS(d)

# lake ss
d = {
"feeds": input_feeds,
"parquet_dir": os.path.join(tmpdir, "parquet_data"),
"st_timestr": "2023-06-18",
"fin_timestr": "2023-06-30",
"timeframe": "5m",
}
parquet_dir = os.path.join(tmpdir, "parquet_data")
d = lake_ss_test_dict(parquet_dir, input_feeds)
ppss.lake_ss = LakeSS(d)

# sim ss
d = {
"do_plot": True,
"log_dir": os.path.join(tmpdir, "logs"),
"test_n": 10,
"exchange_only": {
"timeout": 30000,
"options": {
"createMarketBuyOrderRequiresPrice": False,
"defaultType": "spot",
},
},
}
log_dir = os.path.join(tmpdir, "logs")
d = sim_ss_test_dict(log_dir)
ppss.sim_ss = SimSS(d)

# go
Expand Down

0 comments on commit 31028cc

Please sign in to comment.