Skip to content

Commit

Permalink
added new function to handle cm-specific timestamp formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
hayden-yuma committed Dec 9, 2024
1 parent 3d21396 commit 52e3901
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
10 changes: 5 additions & 5 deletions precog/miners/base_miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from precog.protocol import Challenge
from precog.utils.cm_data import CMData
from precog.utils.timestamp import datetime_to_iso8601, iso8601_to_datetime
from precog.utils.timestamp import datetime_to_CM_timestamp, iso8601_to_datetime


def get_point_estimate(timestamp: str) -> float:
Expand All @@ -23,8 +23,8 @@ def get_point_estimate(timestamp: str) -> float:

# Set the time range to be as small as possible for query speed
# Set the start time as 2 seconds prior to the provided time
start_time: str = datetime_to_iso8601(iso8601_to_datetime(timestamp) - timedelta(seconds=2))
end_time: str = timestamp
start_time: str = datetime_to_CM_timestamp(iso8601_to_datetime(timestamp) - timedelta(days=1))
end_time: str = datetime_to_CM_timestamp(iso8601_to_datetime(timestamp)) # built-ins handle CM API's formatting

# Query CM API for a pandas dataframe with only one record
price_data: pd.DataFrame = cm.get_CM_ReferenceRate(assets="BTC", start=start_time, end=end_time)
Expand Down Expand Up @@ -56,8 +56,8 @@ def get_prediction_interval(timestamp: str, point_estimate: float) -> Tuple[floa
cm = CMData()

# Set the time range to be 24 hours
start_time: str = datetime_to_iso8601(iso8601_to_datetime(timestamp) - timedelta(days=1))
end_time: str = timestamp
start_time: str = datetime_to_CM_timestamp(iso8601_to_datetime(timestamp) - timedelta(days=1))
end_time: str = datetime_to_CM_timestamp(iso8601_to_datetime(timestamp)) # built-ins handle CM API's formatting

# Query CM API for sample standard deviation of the 1s residuals
historical_price_data: pd.DataFrame = cm.get_CM_ReferenceRate(
Expand Down
13 changes: 10 additions & 3 deletions precog/utils/timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ def datetime_to_iso8601(timestamp: datetime) -> str:
"""
Convert datetime to iso 8601 string
"""
return timestamp.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
return timestamp.isoformat()


def iso8601_to_datetime(timestamp: str) -> datetime:
"""
Convert iso 8601 string to datetime
"""
return datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%fZ")
return datetime.fromisoformat(timestamp)


def posix_to_datetime(timestamp: float) -> datetime:
Expand All @@ -83,6 +83,13 @@ def posix_to_datetime(timestamp: float) -> datetime:
return datetime.fromtimestamp(timestamp, tz=get_timezone())


def datetime_to_CM_timestamp(timestamp: datetime) -> str:
"""
Convert iso 8601 string to coinmetrics timestamp
"""
return timestamp.strftime("%Y-%m-%dT%H:%M:%S.%fZ")


###############################
# FUNCTIONS #
###############################
Expand Down Expand Up @@ -155,7 +162,7 @@ def align_timepoints(filtered_pred_dict, cm_dict):
Args:
filtered_pred_dict (dict): {datetime: float} dictionary of predictions.
cm_data (dict): {datetime: float} dictionary of coinmetrics prices.
cm_data (dict): {datetime: float} dictionary of prices.
Returns:
Expand Down
6 changes: 3 additions & 3 deletions precog/validators/reward.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from precog.protocol import Challenge
from precog.utils.cm_data import CMData
from precog.utils.general import pd_to_dict, rank
from precog.utils.timestamp import align_timepoints, datetime_to_iso8601, iso8601_to_datetime, mature_dictionary
from precog.utils.timestamp import align_timepoints, datetime_to_CM_timestamp, iso8601_to_datetime, mature_dictionary


################################################################################
Expand All @@ -24,8 +24,8 @@ def calc_rewards(
decayed_weights = decay**weights
timestamp = responses[0].timestamp
cm = CMData()
start_time: str = datetime_to_iso8601(iso8601_to_datetime(timestamp) - timedelta(hours=1))
end_time: str = datetime_to_iso8601(iso8601_to_datetime(timestamp)) # built-ins handle CM API's formatting
start_time: str = datetime_to_CM_timestamp(iso8601_to_datetime(timestamp) - timedelta(hours=1))
end_time: str = datetime_to_CM_timestamp(iso8601_to_datetime(timestamp)) # built-ins handle CM API's formatting
# Query CM API for sample standard deviation of the 1s residuals
historical_price_data: DataFrame = cm.get_CM_ReferenceRate(
assets="BTC", start=start_time, end=end_time, frequency="1s"
Expand Down

0 comments on commit 52e3901

Please sign in to comment.