Skip to content

Commit cf0f7e7

Browse files
committed
refactor: split market data into modular components
1 parent b00e758 commit cf0f7e7

8 files changed

Lines changed: 56 additions & 55 deletions

File tree

tests/data/test_market_data.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import pytest
2-
from traid.data.market_data import MarketData
1+
from traid.data.handlers.market_data import MarketData
32

43

54
def test_market_data_initialization():

traid/data/clients/__init__.py

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import Dict
2+
import requests
3+
4+
class KrakenClient:
5+
BASE_URL = "https://api.kraken.com/0/public"
6+
7+
def get_ohlcv(self, symbol: str, timeframe: str) -> Dict:
8+
endpoint = f"{self.BASE_URL}/OHLC"
9+
params = {
10+
"pair": symbol,
11+
"interval": timeframe
12+
}
13+
response = requests.get(endpoint, params=params)
14+
return response.json()

traid/data/handlers/__init__.py

Whitespace-only changes.

traid/data/handlers/market_data.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List, Dict
2+
from ..clients.kraken_client import KrakenClient
3+
from ..models.ohlcv import OHLCV
4+
5+
class MarketData:
6+
def __init__(self, symbol: str, timeframe: str) -> None:
7+
self.symbol = symbol
8+
self.timeframe = timeframe
9+
self._client = KrakenClient()
10+
11+
def get_ohlcv(self) -> List[Dict]:
12+
"""Fetch OHLCV data.
13+
Returns:
14+
List[Dict]: List of OHLCV candles
15+
"""
16+
response = self._client.get_ohlcv(self.symbol, self.timeframe)
17+
return self._parse_ohlcv_response(response)
18+
19+
def _parse_ohlcv_response(self, response: Dict) -> List[Dict]:
20+
return []

traid/data/market_data.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

traid/data/models/__init__.py

Whitespace-only changes.

traid/data/models/ohlcv.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from dataclasses import dataclass
2+
from typing import Dict
3+
4+
@dataclass
5+
class OHLCV:
6+
timestamp: int
7+
open: float
8+
high: float
9+
low: float
10+
close: float
11+
volume: float
12+
13+
def to_dict(self) -> Dict:
14+
return {
15+
'timestamp': self.timestamp,
16+
'open': self.open,
17+
'high': self.high,
18+
'low': self.low,
19+
'close': self.close,
20+
'volume': self.volume
21+
}

0 commit comments

Comments
 (0)