-
Notifications
You must be signed in to change notification settings - Fork 0
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
48 feature eval computation #55
Open
kyeoul
wants to merge
22
commits into
main
Choose a base branch
from
48-feature-eval-computation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ede06a3
placeholders
kyeoul a326575
merge
kyeoul c8bef3c
feature evaluation stubbing
kyeoul e3756da
Merge branch 'main' into 48-feature-eval-computation
kyeoul b6c6b97
vague implementations, some things are still weird
kyeoul aaa2a89
hm
kyeoul ad5d6c1
Merge branch 'main' into 48-feature-eval-computation
kyeoul ecdff32
something
kyeoul ab17c71
Merge branch 'main' into 48-feature-eval-computation
kyeoul 5c98818
more fleshed out implementatiosn
kyeoul 9737da2
some tweaks
kyeoul b14ad15
merge
kyeoul 9661015
final implementation, moving to unit tests
kyeoul fc3dcaf
something
kyeoul d76fb97
deleting useless files
kyeoul fc49fb9
Merge branch 'main' into 48-feature-eval-computation
echavemann f677bb1
Merge branch 'main' into 48-feature-eval-computation
kyeoul 69260d1
Merge branch '48-feature-eval-computation' of https://github.com/nort…
kyeoul a33e628
resolving comments
kyeoul 92c27c0
underscore prefix
kyeoul ad439e2
Merge branch 'main' into 48-feature-eval-computation
kyeoul 2be86a1
more solid feature eval
kyeoul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from typing import override | ||
|
||
from pysrc.adapters.kraken.asset_mappings import asset_to_kraken | ||
from pysrc.adapters.messages import SnapshotMessage, TradeMessage | ||
from pysrc.signal.base_feature_generator import BaseFeatureGenerator | ||
from pysrc.util.types import Asset, Market | ||
|
||
|
||
class ExampleKrakenFeatureGenerator(BaseFeatureGenerator): | ||
order_features = ["open", "high", "low", "close"] | ||
assets = [Asset.BTC, Asset.ETH, Asset.ADA, Asset.SOL, Asset.DOGE] | ||
|
||
def __init__(self) -> None: | ||
pass | ||
|
||
def compute_ohlc(self, trades: list[TradeMessage]) -> list[float]: | ||
if not trades: | ||
return [0.0, 0.0, 0.0, 0.0] | ||
prices = [trade.price for trade in trades] | ||
open_price = prices[0] | ||
high_price = max(prices) | ||
low_price = min(prices) | ||
close_price = prices[-1] | ||
return [open_price, high_price, low_price, close_price] | ||
|
||
@override | ||
def on_tick( | ||
self, | ||
snapshots: dict[str, SnapshotMessage], | ||
trades: dict[str, list[TradeMessage]], | ||
) -> dict[Asset, dict[str, list[float]]]: | ||
output = {} | ||
for asset in self.assets: | ||
asset_key = asset_to_kraken(asset, Market.KRAKEN_SPOT) | ||
asset_trades = trades.get(asset_key, []) | ||
features = self.compute_ohlc(asset_trades) | ||
output[asset] = {"features": features} | ||
return output |
Binary file added
BIN
+99 Bytes
src/pysrc/test/unit/util/resources/snapshots/XXBTZUSD/06_25_2024.bin
Binary file not shown.
Binary file added
BIN
+92 Bytes
src/pysrc/test/unit/util/resources/snapshots/XXBTZUSD/11_22_2024.bin
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import os | ||
from datetime import datetime | ||
|
||
import pytest | ||
from pyzstd import CParameter, compress | ||
|
||
from pysrc.adapters.messages import SnapshotMessage, TradeMessage | ||
from pysrc.signal.base_feature_generator import BaseFeatureGenerator | ||
from pysrc.signal.example_kraken_feature_generator import ExampleKrakenFeatureGenerator | ||
from pysrc.test.helpers import get_resources_path | ||
from pysrc.util.feature_eval import Evaluator | ||
from pysrc.util.types import Asset, Market, OrderSide | ||
|
||
resource_path = get_resources_path(__file__) | ||
|
||
|
||
@pytest.fixture | ||
def client() -> Evaluator: | ||
test_features = ["open", "high", "low", "close"] | ||
asset = Asset.BTC | ||
market = Market.KRAKEN_SPOT | ||
start = datetime(year=2024, month=6, day=25) | ||
end = datetime(year=2024, month=6, day=26) | ||
return Evaluator( | ||
features=test_features, | ||
asset=asset, | ||
market=market, | ||
start=start, | ||
end=end, | ||
resource_path=resource_path, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def feature_gen() -> BaseFeatureGenerator: | ||
return ExampleKrakenFeatureGenerator() | ||
|
||
|
||
def test_feature_calculation( | ||
client: Evaluator, feature_gen: BaseFeatureGenerator | ||
) -> None: | ||
snapshots = [ | ||
SnapshotMessage( | ||
time=1719273600, | ||
feedcode="XXBTZUSD", | ||
market=Market.KRAKEN_SPOT, | ||
bids=[], | ||
asks=[[1.0, 2.0]], | ||
), | ||
SnapshotMessage( | ||
time=1719273600, | ||
feedcode="XXBTZUSD", | ||
market=Market.KRAKEN_SPOT, | ||
bids=[[3.0, 4.0], [68717.5, -3000.0]], | ||
asks=[[1.0, 2.0], [68717.5, -3000.0]], | ||
), | ||
SnapshotMessage( | ||
time=1719273601, | ||
feedcode="XXBTZUSD", | ||
market=Market.KRAKEN_SPOT, | ||
bids=[[3.0, 4.0], [68717.5, -3000.0], [7.0, 8.0]], | ||
asks=[[1.0, 2.0], [68717.5, -3000.0]], | ||
), | ||
SnapshotMessage( | ||
time=1719273602, | ||
feedcode="XXBTZUSD", | ||
market=Market.KRAKEN_SPOT, | ||
bids=[[3.0, 4.0], [68717.5, -3000.0], [7.0, 8.0]], | ||
asks=[[1.0, 2.0], [68717.5, -3000.0]], | ||
), | ||
] | ||
|
||
snapshots_bytes = b"" | ||
for s in snapshots: | ||
snapshots_bytes += s.to_bytes() | ||
|
||
test_dir_snapshots = resource_path / "snapshots" / "XXBTZUSD" | ||
os.makedirs(test_dir_snapshots, exist_ok=True) | ||
test_path = test_dir_snapshots / "06_25_2024.bin" | ||
|
||
with open(test_path, "wb") as f: | ||
f.write( | ||
compress(snapshots_bytes, level_or_option={CParameter.compressionLevel: 10}) | ||
) | ||
|
||
trades = [ | ||
TradeMessage( | ||
1719273600, "XXBTZUSD", 1, 10.0, 1.0, OrderSide.BID, Market.KRAKEN_SPOT | ||
), | ||
TradeMessage( | ||
1719273600, "XXBTZUSD", 1, 10.02, 0.5, OrderSide.BID, Market.KRAKEN_SPOT | ||
), | ||
TradeMessage( | ||
1719273601, "XXBTZUSD", 1, 9.9, 1.5, OrderSide.BID, Market.KRAKEN_SPOT | ||
), | ||
TradeMessage( | ||
1719273602, "XXBTZUSD", 1, 9.0, 1.5, OrderSide.BID, Market.KRAKEN_SPOT | ||
), | ||
] | ||
|
||
trades_bytes = b"" | ||
for t in trades: | ||
trades_bytes += t.to_bytes() | ||
|
||
test_dir_snapshots = resource_path / "trades" / "XXBTZUSD" | ||
os.makedirs(test_dir_snapshots, exist_ok=True) | ||
test_path = test_dir_snapshots / "06_25_2024.bin" | ||
|
||
with open(test_path, "wb") as f: | ||
f.write( | ||
compress(trades_bytes, level_or_option={CParameter.compressionLevel: 10}) | ||
) | ||
|
||
result = client.calculate_features(feature_gen) | ||
assert result["open"][0] == pytest.approx(10.0, rel=1e-7) | ||
assert result["open"][1] == pytest.approx(9.9, rel=1e-7) | ||
assert result["open"][2] == pytest.approx(9.0, rel=1e-7) | ||
|
||
assert result["high"][0] == pytest.approx(10.02, rel=1e-7) | ||
assert result["high"][1] == pytest.approx(9.9, rel=1e-7) | ||
assert result["high"][2] == pytest.approx(9.0, rel=1e-7) | ||
|
||
assert result["low"][0] == pytest.approx(10.0, rel=1e-7) | ||
assert result["low"][1] == pytest.approx(9.9, rel=1e-7) | ||
assert result["low"][2] == pytest.approx(9.0, rel=1e-7) | ||
|
||
assert result["close"][0] == pytest.approx(10.02, rel=1e-7) | ||
assert result["close"][1] == pytest.approx(9.9, rel=1e-7) | ||
assert result["close"][2] == pytest.approx(9.0, rel=1e-7) | ||
|
||
|
||
def test_feature_evaluation(client: Evaluator) -> None: | ||
features = { | ||
"open": [1.0, 2.0, 3.0], | ||
"high": [2.0, 4.0, 6.0], | ||
"low": [-1.0, -2.0, -3.0], | ||
"close": [1.0, 2.0, 3.0], | ||
} | ||
|
||
target = [4.0, 8.0, 12.0] | ||
result = client.evaluate_features(features, target) | ||
|
||
assert result is not None | ||
|
||
expected_result = [ | ||
[1, 1, -1, 1, 1], | ||
[1, 1, -1, 1, 1], | ||
[-1, -1, 1, -1, -1], | ||
[1, 1, -1, 1, 1], | ||
[1, 1, -1, 1, 1], | ||
] | ||
|
||
assert (result == expected_result).any() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from datetime import datetime | ||
from pathlib import Path | ||
|
||
import numpy as np | ||
|
||
from pysrc.adapters.kraken.asset_mappings import asset_to_kraken | ||
from pysrc.data_loaders.tick_snapshots_data_loader import TickSnapshotsDataLoader | ||
from pysrc.data_loaders.tick_trades_data_loader import TickTradesDataLoader | ||
from pysrc.signal.base_feature_generator import BaseFeatureGenerator | ||
from pysrc.util.types import Asset, Market | ||
|
||
|
||
class Evaluator: | ||
def __init__( | ||
self, | ||
features: list[str], | ||
asset: Asset, | ||
market: Market, | ||
start: datetime, | ||
end: datetime, | ||
resource_path: Path, | ||
): | ||
self._features = features | ||
self._asset = asset | ||
self._start = start | ||
self._end = end | ||
self._market = market | ||
self._resource_path = resource_path | ||
|
||
def calculate_features( | ||
self, | ||
generator_client: BaseFeatureGenerator, | ||
) -> dict[str, list[float]]: | ||
trades_client = TickTradesDataLoader( | ||
self._resource_path, self._asset, self._market, self._start, self._end | ||
) | ||
snapshots_client = TickSnapshotsDataLoader( | ||
self._resource_path, self._asset, self._market, self._start, self._end | ||
) | ||
|
||
feature_dict: dict[str, list[float]] = {} | ||
for feature in self._features: | ||
feature_dict[feature] = [] | ||
|
||
asset_str = asset_to_kraken(self._asset, self._market) | ||
|
||
while (trade := trades_client.next()) is not None and ( | ||
snapshot := snapshots_client.next() | ||
) is not None: | ||
calc_features = generator_client.on_tick( | ||
{asset_str: snapshot}, {asset_str: trade} | ||
) | ||
for i in range(len(self._features)): | ||
feature = self._features[i] | ||
feature_dict[feature].append(calc_features[self._asset]["features"][i]) | ||
return feature_dict | ||
|
||
def evaluate_features( | ||
self, calc_features: dict[str, list[float]], target: list[float] | ||
) -> np.ndarray: | ||
input_matrix = [] | ||
for feature in self._features: | ||
input_matrix.append(calc_features[feature]) | ||
return np.corrcoef(input_matrix, target) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why r we using dateutil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
im stupid, removed and iterating with regular datetime