Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions vali_objects/scoring/scoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from time_util.time_util import TimeUtil
from vali_objects.utils.position_filtering import PositionFiltering
from vali_objects.utils.position_penalties import PositionPenalties
from vali_objects.utils.position_utils import PositionUtils
from vali_objects.utils.ledger_utils import LedgerUtils

import bittensor as bt
Expand Down Expand Up @@ -214,14 +215,17 @@ def sharpe(positions: list[Position], ledger: PerfLedgerData) -> float:
# Hyperparameter
min_std_dev = ValiConfig.SHARPE_STDDEV_MINIMUM

# Collect a list of returns based on individual positions
order_based_returns = PositionUtils.order_returns(positions)

# Return at close should already accommodate the risk-free rate as a cost of carry
positional_log_returns = [math.log(
max(position.return_at_close, .00001)) # Prevent math domain error)
for position in positions]
ordered_log_returns = [math.log(
max(order_return, .00001)) # Prevent math domain error)
for order_return in order_based_returns]

# Sharpe ratio is calculated as the mean of the returns divided by the standard deviation of the returns
mean_return = np.mean(positional_log_returns)
std_dev = max(np.std(positional_log_returns), min_std_dev)
mean_return = np.mean(ordered_log_returns)
std_dev = max(np.std(ordered_log_returns), min_std_dev)

if std_dev == 0:
return 0.0
Expand All @@ -238,15 +242,18 @@ def omega(positions: list[Position], ledger: PerfLedgerData) -> float:
if len(positions) == 0:
return 0.0

# Collect a list of returns based on individual positions
order_based_returns = PositionUtils.order_returns(positions)

# Return at close should already accommodate the risk-free rate as a cost of carry
positional_log_returns = [math.log(
max(position.return_at_close, .00001)) # Prevent math domain error
for position in positions]
ordered_log_returns = [math.log(
max(order_return, .00001)) # Prevent math domain error)
for order_return in order_based_returns]

positive_sum = 0
negative_sum = 0

for log_return in positional_log_returns:
for log_return in ordered_log_returns:
if log_return > 0:
positive_sum += log_return
else:
Expand Down
18 changes: 12 additions & 6 deletions vali_objects/utils/position_penalties.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from vali_config import ValiConfig
from vali_objects.position import Position
from vali_objects.utils.functional_utils import FunctionalUtils

from vali_objects.utils.position_utils import PositionUtils

class PositionPenalties:
@staticmethod
Expand Down Expand Up @@ -122,16 +122,22 @@ def returns_ratio(
positions: list[Position] - the list of positions
"""
closed_positions = [position for position in positions if position.is_closed_position]
closed_position_returns = [math.log(
max(position.return_at_close, .00001)) # Prevent math domain error
for position in closed_positions]
closed_return = sum(closed_position_returns)

# Collect a list of returns based on individual positions
order_based_returns = PositionUtils.order_returns(closed_positions)

# Return at close should already accommodate the risk-free rate as a cost of carry
ordered_log_returns = [math.log(
max(order_return, .00001)) # Prevent math domain error)
for order_return in order_based_returns]

closed_return = sum(ordered_log_returns)

# Return early if there will be an issue with the ratio denominator
if closed_return == 0:
return 1

numerator = max(closed_position_returns) if closed_return > 0 else min(closed_position_returns)
numerator = max(ordered_log_returns) if closed_return > 0 else min(ordered_log_returns)
denominator = closed_return

max_return_ratio = np.clip(numerator / denominator, 0, 1)
Expand Down
18 changes: 18 additions & 0 deletions vali_objects/utils/position_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@


class PositionUtils:

@staticmethod
def order_returns(
positions: list[Position]
) -> list[float]:
"""Computes per order returns for a list of positions."""
order_returns = []
for position in positions:
all_orders = position.orders
final_price = all_orders[-1].price

for order in position.orders[:-1]:
percentage_change = order.price / final_price
value_change = order.leverage * percentage_change
order_returns.append(value_change)

return order_returns

@staticmethod
def translate_current_leverage(
positions: list[Position],
Expand Down