Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbonilla-tao committed Jan 9, 2025
1 parent 849b4c6 commit a99eef1
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 62 deletions.
4 changes: 3 additions & 1 deletion tests/shared_objects/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from vali_objects.enums.order_type_enum import OrderType
from vali_objects.vali_dataclasses.perf_ledger import PerfCheckpoint, PerfLedger
from vali_objects.vali_config import ValiConfig
from vali_objects.vali_dataclasses.perf_ledger import TP_ID_PORTFOLIO


def get_time_in_range(percent, start, end):
Expand Down Expand Up @@ -125,7 +126,8 @@ def generate_ledger(
)
)

return ledger_generator(checkpoints=checkpoint_list)
base_ledger = ledger_generator(checkpoints=checkpoint_list)
return {TP_ID_PORTFOLIO: base_ledger, TradePair.BTCUSD.trade_pair_id: base_ledger}


def ledger_generator(
Expand Down
6 changes: 3 additions & 3 deletions tests/vali_tests/test_challengeperiod_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from vali_objects.enums.order_type_enum import OrderType
from vali_objects.vali_dataclasses.order import Order
from vali_objects.vali_dataclasses.perf_ledger import PerfLedgerData
from vali_objects.vali_dataclasses.perf_ledger import PerfLedgerData, TP_ID_PORTFOLIO
from tests.shared_objects.mock_classes import (
MockMetagraph, MockChallengePeriodManager, MockPositionManager, MockPerfLedgerManager, MockCacheController
)
Expand Down Expand Up @@ -319,15 +319,15 @@ def test_refresh_elimination_disk(self):
# Check one of the failing miners, to see if they are screened
failing_miner = self.FAILING_MINER_NAMES[0]
failing_screen, _ = self.challengeperiod_manager.screen_failing_criteria(
ledger_element=self.LEDGERS[failing_miner]
ledger_element=self.LEDGERS[failing_miner][TP_ID_PORTFOLIO]
)

self.assertEqual(failing_screen, True)

# Now inspect all the hotkeys
challenge_success, challenge_eliminations = self.challengeperiod_manager.inspect(
positions=self.POSITIONS,
ledger=self.LEDGERS,
ledger={hk: v[TP_ID_PORTFOLIO] for hk, v in self.LEDGERS.items()},
success_hotkeys=self.SUCCESS_MINER_NAMES,
inspection_hotkeys=self.challengeperiod_manager.challengeperiod_testing,
current_time=self.OUTSIDE_OF_CHALLENGE
Expand Down
30 changes: 15 additions & 15 deletions tests/vali_tests/test_challengeperiod_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from vali_objects.scoring.scoring import Scoring
from vali_objects.vali_dataclasses.order import Order
from vali_objects.enums.order_type_enum import OrderType

from vali_objects.vali_dataclasses.perf_ledger import TP_ID_PORTFOLIO

class TestChallengePeriodUnit(TestBase):

Expand Down Expand Up @@ -136,15 +136,15 @@ def test_screen_drawdown(self):
base_positions[i].return_at_close = 1.1

# Drawdown is high - 50% drawdown on the first period
base_ledger.cps[0].mdd = 0.5
base_ledger[TP_ID_PORTFOLIO].cps[0].mdd = 0.5

# Drawdown criteria
max_drawdown = LedgerUtils.recent_drawdown(base_ledger.cps, restricted=False)
max_drawdown = LedgerUtils.recent_drawdown(base_ledger[TP_ID_PORTFOLIO].cps, restricted=False)
max_drawdown_percentage = LedgerUtils.drawdown_percentage(max_drawdown)
self.assertGreater(max_drawdown_percentage, ValiConfig.DRAWDOWN_MAXVALUE_PERCENTAGE)

# Check that the miner is successfully screened as failing
screening_logic, _ = self.challengeperiod_manager.screen_failing_criteria(ledger_element=base_ledger)
screening_logic, _ = self.challengeperiod_manager.screen_failing_criteria(ledger_element=base_ledger[TP_ID_PORTFOLIO])
self.assertTrue(screening_logic)


Expand All @@ -163,7 +163,7 @@ def test_failing_remaining_time(self):
# Check that the miner is screened as failing
passing, failing = self.challengeperiod_manager.inspect(
positions=inspection_positions,
ledger=inspection_ledger,
ledger={hk: v[TP_ID_PORTFOLIO] for hk, v in inspection_ledger.items()},
success_hotkeys=[],
inspection_hotkeys={"miner": current_time},
current_time=current_time,
Expand All @@ -190,7 +190,7 @@ def test_failing_no_remaining_time(self):
# Check that the miner is screened as failing
passing, failing = self.challengeperiod_manager.inspect(
positions=inspection_positions,
ledger=inspection_ledger,
ledger={hk: v[TP_ID_PORTFOLIO] for hk, v in inspection_ledger.items()},
success_hotkeys=[],
inspection_hotkeys=inspection_hotkeys,
current_time=current_time,
Expand Down Expand Up @@ -218,7 +218,7 @@ def test_passing_remaining_time(self):
# Check that the miner is screened as failing
passing, failing = self.challengeperiod_manager.inspect(
positions=inspection_positions,
ledger=inspection_ledger,
ledger={hk: v[TP_ID_PORTFOLIO] for hk, v in inspection_ledger.items()},
success_hotkeys=[],
inspection_hotkeys=inspection_hotkeys,
current_time=current_time,
Expand All @@ -245,7 +245,7 @@ def test_passing_no_remaining_time(self):
# Check that the miner is screened as failing
passing, failing = self.challengeperiod_manager.inspect(
positions=inspection_positions,
ledger=inspection_ledger,
ledger={hk: v[TP_ID_PORTFOLIO] for hk, v in inspection_ledger.items()},
success_hotkeys=[],
inspection_hotkeys=inspection_hotkeys,
current_time=current_time,
Expand All @@ -271,7 +271,7 @@ def test_lingering_no_positions(self):
# Check that the miner is screened as testing still
passing, failing = self.challengeperiod_manager.inspect(
positions=inspection_positions,
ledger=inspection_ledger,
ledger={hk: v[TP_ID_PORTFOLIO] for hk, v in inspection_ledger.items()},
success_hotkeys=[],
inspection_hotkeys=inspection_hotkeys,
current_time=current_time
Expand All @@ -289,7 +289,7 @@ def test_recently_re_registered_miner(self):
base_ledger = deepcopy(self.DEFAULT_LEDGER)

base_position = deepcopy(self.DEFAULT_POSITION)
base_position.orders[0].processed_ms = base_ledger.start_time_ms + 1
base_position.orders[0].processed_ms = base_ledger[TP_ID_PORTFOLIO].start_time_ms + 1
base_positions = [base_position]

inspection_positions = {"miner": base_positions}
Expand All @@ -300,7 +300,7 @@ def test_recently_re_registered_miner(self):
# Check that the miner is screened as testing still
passing, failing = self.challengeperiod_manager.inspect(
positions=inspection_positions,
ledger=inspection_ledger,
ledger={hk: v[TP_ID_PORTFOLIO] for hk, v in inspection_ledger.items()},
success_hotkeys=self.SUCCESS_MINER_NAMES,
inspection_hotkeys=inspection_hotkeys,
current_time=current_time,
Expand All @@ -325,7 +325,7 @@ def test_lingering_with_positions(self):
# Check that the miner is screened as testing still
passing, failing = self.challengeperiod_manager.inspect(
positions=inspection_positions,
ledger=inspection_ledger,
ledger={hk: v[TP_ID_PORTFOLIO] for hk, v in inspection_ledger.items()},
success_hotkeys=self.SUCCESS_MINER_NAMES,
inspection_hotkeys=inspection_hotkeys,
current_time=current_time,
Expand All @@ -350,7 +350,7 @@ def test_just_above_threshold(self):
# Check that the miner is screened as failing
passing, failing = self.challengeperiod_manager.inspect(
positions=inspection_positions,
ledger=inspection_ledger,
ledger={hk: v[TP_ID_PORTFOLIO] for hk, v in inspection_ledger.items()},
success_hotkeys=[],
inspection_hotkeys={"miner": current_time},
current_time=current_time,
Expand All @@ -376,7 +376,7 @@ def test_just_below_threshold(self):
# Check that the miner is screened as failing
passing, failing = self.challengeperiod_manager.inspect(
positions=inspection_positions,
ledger=inspection_ledger,
ledger={hk: v[TP_ID_PORTFOLIO] for hk, v in inspection_ledger.items()},
success_hotkeys=[],
inspection_hotkeys={"miner": current_time},
current_time=current_time,
Expand Down Expand Up @@ -415,7 +415,7 @@ def test_at_threshold(self):
# Check that the miner is screened as failing
passing, failing = self.challengeperiod_manager.inspect(
positions=inspection_positions,
ledger=inspection_ledger,
ledger={hk: v[TP_ID_PORTFOLIO] for hk, v in inspection_ledger.items()},
success_hotkeys=[],
inspection_hotkeys={"miner": current_time},
current_time=current_time,
Expand Down
11 changes: 6 additions & 5 deletions tests/vali_tests/test_concentration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from vali_objects.vali_config import TradePair

from vali_objects.utils.ledger_utils import LedgerUtils

from vali_objects.vali_dataclasses.perf_ledger import TP_ID_PORTFOLIO
from vali_objects.utils.functional_utils import FunctionalUtils
from tests.shared_objects.test_utilities import generate_ledger, ledger_generator, position_generator

Expand Down Expand Up @@ -62,17 +62,18 @@ def test_daily_log_returns_concentration(self):

# Daily returns with a single element
sample_ledger = generate_ledger(gain=0.1, loss=0.0, nterms=1)
log_returns = LedgerUtils.daily_return_log(sample_ledger.cps)
log_returns = LedgerUtils.daily_return_log(sample_ledger[TP_ID_PORTFOLIO].cps)
concentration_penalty = FunctionalUtils.concentration(log_returns)
self.assertEqual(concentration_penalty, 1)

# Daily returns with multiple elements
sample_ledger = generate_ledger(gain=0.1, loss=0.0, nterms=50)
sample_concentrated = copy.deepcopy(sample_ledger)
sample_concentrated.cps[-1].gain = 0.5
for tp_id, dat in sample_concentrated.items():
sample_concentrated[tp_id].cps[-1].gain = 0.5

log_returns = LedgerUtils.daily_return_log(sample_ledger.cps)
log_returns_concentrated = LedgerUtils.daily_return_log(sample_concentrated.cps)
log_returns = LedgerUtils.daily_return_log(sample_ledger[TP_ID_PORTFOLIO].cps)
log_returns_concentrated = LedgerUtils.daily_return_log(sample_concentrated[TP_ID_PORTFOLIO].cps)

concentration_penalty = FunctionalUtils.concentration(log_returns)
concentration_penalty_concentrated = FunctionalUtils.concentration(log_returns_concentrated)
Expand Down
37 changes: 19 additions & 18 deletions tests/vali_tests/test_ledger_penalty.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import copy
from copy import deepcopy
from tests.vali_tests.base_objects.test_base import TestBase
from vali_objects.vali_dataclasses.perf_ledger import TP_ID_PORTFOLIO
from vali_objects.utils.ledger_utils import LedgerUtils

from vali_objects.vali_config import ValiConfig
Expand Down Expand Up @@ -45,10 +46,10 @@ def test_daily_consistency(self):
)
l4 = copy.deepcopy(l3)

l1_cps = l1.cps
l2_cps = l2.cps
l3_cps = l3.cps
l4_cps = l4.cps
l1_cps = l1[TP_ID_PORTFOLIO].cps
l2_cps = l2[TP_ID_PORTFOLIO].cps
l3_cps = l3[TP_ID_PORTFOLIO].cps
l4_cps = l4[TP_ID_PORTFOLIO].cps

# Inconsistent growth period - one interval
l2_cps[len(l2_cps) // 2].gain = 1.0
Expand All @@ -65,7 +66,7 @@ def test_daily_consistency(self):

# Many days, but all in one week
l5 = deepcopy(self.DEFAULT_LEDGER)
l5_cps = l5.cps
l5_cps = l5[TP_ID_PORTFOLIO].cps

# Set all gains to 1.0 for these days
n_days_in_biweekly = biweekly_window // daily_window
Expand Down Expand Up @@ -113,10 +114,10 @@ def test_biweekly_consistency(self):
)
l4 = copy.deepcopy(l3)

l1_cps = l1.cps
l2_cps = l2.cps
l3_cps = l3.cps
l4_cps = l4.cps
l1_cps = l1[TP_ID_PORTFOLIO].cps
l2_cps = l2[TP_ID_PORTFOLIO].cps
l3_cps = l3[TP_ID_PORTFOLIO].cps
l4_cps = l4[TP_ID_PORTFOLIO].cps

# Inconsistent growth period - one interval
l2_cps[len(l2_cps) // 2].gain = 1.0
Expand All @@ -132,7 +133,7 @@ def test_biweekly_consistency(self):

# Many days, but all in one week
l5 = generate_ledger(0.1)
l5_cps = l5.cps
l5_cps = l5[TP_ID_PORTFOLIO].cps

# Set all gains to 1.0 for these days
for i in range(0, biweekly_window, daily_window):
Expand Down Expand Up @@ -180,7 +181,7 @@ def test_drawdown_abnormality(self):
# Drawdown abnormality should work for only a few checkpoints
self.assertLess(LedgerUtils.drawdown_abnormality(checkpoints), 1.0)
l1 = generate_ledger(0.1, mdd=0.98)
l1_cps = l1.cps
l1_cps = l1[TP_ID_PORTFOLIO].cps

# All recent drawdowns equal to 0 should return low abnormality
for i in range(1, ValiConfig.RETURN_SHORT_LOOKBACK_LEDGER_WINDOWS + 1):
Expand All @@ -190,27 +191,27 @@ def test_drawdown_abnormality(self):

# All equal drawdown
l2 = generate_ledger(0.1, mdd=0.98)
self.assertAlmostEqual(LedgerUtils.drawdown_abnormality(l2.cps), 1.0, places=2) # Essentially no penalty
self.assertAlmostEqual(LedgerUtils.drawdown_abnormality(l2[TP_ID_PORTFOLIO].cps), 1.0, places=2) # Essentially no penalty

# Large abnormality near maximum drawdown threshold should have a score of 0 (i.e., high penalty)
l3 = generate_ledger(0.1, mdd=0.99)
l3_cps = l3.cps
l3_cps = l3[TP_ID_PORTFOLIO].cps
l3_cps[-1].mdd = 0.91
self.assertAlmostEqual(LedgerUtils.drawdown_abnormality(l3.cps), 0.0, places=6)
self.assertAlmostEqual(LedgerUtils.drawdown_abnormality(l3[TP_ID_PORTFOLIO].cps), 0.0, places=6)

def test_max_drawdown_threshold(self):
l1 = generate_ledger(0.1, mdd=0.99) # 1% drawdown
l1_cps = l1.cps
l1_cps = l1[TP_ID_PORTFOLIO].cps
l2 = copy.deepcopy(l1)
l2_cps = l2.cps
l2_cps = l2[TP_ID_PORTFOLIO].cps
l2_cps[-1].mdd = 0.8 # 20% drawdown only on the most recent checkpoint

l3 = copy.deepcopy(l1)
l3_cps = l3.cps
l3_cps = l3[TP_ID_PORTFOLIO].cps
l3_cps[0].mdd = 0.8 # 20% drawdown only on the first checkpoint

l4 = generate_ledger(0.1, mdd=0.8) # 20% drawdown
l4_cps = l4.cps
l4_cps = l4[TP_ID_PORTFOLIO].cps

self.assertEqual(LedgerUtils.max_drawdown_threshold_penalty(l1_cps), 1.0)
self.assertEqual(LedgerUtils.max_drawdown_threshold_penalty(l2_cps), 0.0)
Expand Down
Loading

0 comments on commit a99eef1

Please sign in to comment.