Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SorrTask-782 Unit tests for calculate_vwap_twap() #801

Merged
merged 11 commits into from
May 3, 2024
89 changes: 89 additions & 0 deletions im_v2/common/data/transform/test/test_transform_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import im_v2.common.data.extract.extract_utils as imvcdeexut
import im_v2.common.data.transform.resample_daily_bid_ask_data as imvcdtrdbad
import im_v2.common.data.transform.transform_utils as imvcdttrut
import research_amp.transform as ramptran
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved


class TestGetVendorEpochUnit(hunitest.TestCase):
Expand Down Expand Up @@ -776,3 +777,91 @@ def _get_test_data(self) -> None:
scratch_dir = self.get_scratch_space()
aws_profile = "ck"
hs3.copy_data_from_s3_to_local_dir(s3_input_dir, scratch_dir, aws_profile)


# #############################################################################


class TestCalculateVwapTwap(hunitest.TestCase):
def get_test_data(self):
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved
"""
Fetch data for price and volume columns with timestamp index
returns: pandas dataframe with timestamp index, close and volume column with asset_id as full_sumbol
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved
"""
#
index_timestamp = pd.date_range("2024-01-01", periods=2, freq="min")
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved
close = [34501.279, 29952.436]
volume = [229894.0, 99837.4]
asset_id = [18109, 15479]
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved
d = {
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved
"timestamp": index_timestamp,
"close": close,
"volume": volume,
"full_symbol": asset_id,
}
df = pd.DataFrame(data=d).set_index("timestamp")
expected_df_columns = pd.MultiIndex.from_product(
[["close", "twap", "volume", "vwap"], sorted(asset_id)]
).to_list()
return df, expected_df_columns
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved

def test_calculate_vwap_twap_with_5T_resample_kwargs(self) -> None:
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved
"""
Verify dataframe signature for given volume from data WITH 5T resample
rule.
"""
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved
df, expected_df_columns = self.get_test_data()
result_df = ramptran.calculate_vwap_twap(df, "5T")
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved
# Expected Values
expected_length = 2
expected_column_unique_values = None
expected_signature = r"""
# df=
index=[2024-01-01 00:00:00, 2024-01-01 00:05:00]
columns=('close', 15479),('close', 18109),('twap', 15479),('twap', 18109),('volume', 15479),('volume', 18109),('vwap', 15479),('vwap', 18109)
shape=(2, 8)
close twap volume vwap
15479 18109 15479 18109 15479 18109 15479 18109
timestamp
2024-01-01 00:00:00 NaN 34501.279 NaN 34501.279 NaN 229894.0 NaN 34501.279
2024-01-01 00:05:00 29952.436 NaN 29952.436 NaN 99837.4 NaN 29952.436 NaN
"""
# Check signature
self.check_df_output(
result_df,
expected_length,
expected_df_columns,
expected_column_unique_values,
expected_signature,
)

def test_calculate_vwap_twap_with_halfT_resample_kwargs(self) -> None:
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved
"""
Verify dataframe signature for given volume from data WITH .5T resample
rule.
"""
df, expected_df_columns = self.get_test_data()
result_df = ramptran.calculate_vwap_twap(df, "0.5T")
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved
# Expected Values
expected_length = 3
expected_column_unique_values = None
expected_signature = r"""
# df=
index=[2024-01-01 00:00:00, 2024-01-01 00:01:00]
columns=('close', 15479),('close', 18109),('twap', 15479),('twap', 18109),('volume', 15479),('volume', 18109),('vwap', 15479),('vwap', 18109)
shape=(3, 8)
close twap volume vwap
15479 18109 15479 18109 15479 18109 15479 18109
timestamp
2024-01-01 00:00:00 NaN 34501.279 NaN 34501.279 NaN 229894.0 NaN 34501.279
2024-01-01 00:00:30 NaN NaN NaN NaN NaN NaN NaN NaN
2024-01-01 00:01:00 29952.436 NaN 29952.436 NaN 99837.4 NaN 29952.436 NaN
"""
# Check signature
self.check_df_output(
result_df,
expected_length,
expected_df_columns,
expected_column_unique_values,
expected_signature,
)
Loading