-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdata_stream_example.py
More file actions
188 lines (157 loc) · 5.41 KB
/
Copy pathdata_stream_example.py
File metadata and controls
188 lines (157 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
Example of using the data streaming system.
"""
import asyncio
import logging
from datetime import datetime, timedelta
import pandas as pd
from deepchain.core.data.stream import WebSocketSource, MarketDataStream
from deepchain.core.data.indicators import (
calculate_ma,
calculate_rsi,
calculate_macd,
calculate_bollinger_bands,
calculate_vwap
)
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class DataStreamExample:
"""Example class demonstrating data streaming system usage."""
def __init__(self):
"""Initialize data streaming example."""
# Initialize data stream
self.stream = MarketDataStream(buffer_size=1000)
# Add data source
self.source = WebSocketSource(
url="wss://stream.binance.com:9443/ws",
api_key="YOUR-API-KEY" # Replace with your API key
)
self.stream.add_source("binance", self.source)
# Add technical indicators
self.setup_indicators()
# Add data processors
self.setup_processors()
def setup_indicators(self):
"""Setup technical indicators."""
# Add moving averages
self.stream.add_indicator(
"ma_20",
calculate_ma,
period=20
)
self.stream.add_indicator(
"ma_50",
calculate_ma,
period=50
)
# Add RSI
self.stream.add_indicator(
"rsi",
calculate_rsi,
period=14
)
# Add MACD
self.stream.add_indicator(
"macd",
calculate_macd,
fast_period=12,
slow_period=26,
signal_period=9
)
# Add Bollinger Bands
self.stream.add_indicator(
"bbands",
calculate_bollinger_bands,
period=20,
std_dev=2.0
)
# Add VWAP
self.stream.add_indicator(
"vwap",
calculate_vwap
)
def setup_processors(self):
"""Setup data processors."""
# Add missing value handler
def handle_missing(data: pd.DataFrame) -> pd.DataFrame:
return data.fillna(method='ffill').fillna(method='bfill')
# Add outlier detector
def detect_outliers(data: pd.DataFrame) -> pd.DataFrame:
for col in ['close', 'volume']:
if col in data.columns:
mean = data[col].mean()
std = data[col].std()
data[f'{col}_is_outlier'] = (
(data[col] < mean - 3 * std) |
(data[col] > mean + 3 * std)
)
return data
# Add processors to stream
self.stream.add_processor(handle_missing)
self.stream.add_processor(detect_outliers)
async def start_streaming(self, symbols: list):
"""Start data streaming for given symbols.
Args:
symbols: List of trading pairs (e.g., ['BTCUSDT', 'ETHUSDT'])
"""
logger.info(f"Starting data stream for symbols: {symbols}")
# Start stream
await self.stream.start()
# Subscribe to symbols
await self.source.subscribe(symbols)
try:
while True:
# Keep the stream running
await asyncio.sleep(1)
except KeyboardInterrupt:
logger.info("Stopping data stream...")
await self.stream.stop()
def process_historical_data(self, symbol: str, days: int = 30):
"""Process historical data for backtesting.
Args:
symbol: Trading pair symbol
days: Number of days of historical data
"""
logger.info(f"Processing historical data for {symbol}")
# Get historical data
end_time = datetime.now()
start_time = end_time - timedelta(days=days)
data = asyncio.run(
self.source.get_historical_data(
symbol,
start_time,
end_time,
interval='1h'
)
)
# Calculate indicators
for name, (func, params) in self.stream.indicators.items():
try:
result = func(data, **params)
if isinstance(result, pd.DataFrame):
for col in result.columns:
data[f'{name}_{col}'] = result[col]
else:
data[name] = result
except Exception as e:
logger.error(f"Error calculating {name}: {e}")
# Apply processors
for processor in self.stream.processors:
try:
data = processor(data)
except Exception as e:
logger.error(f"Error in processor: {e}")
return data
async def main():
"""Run data streaming example."""
example = DataStreamExample()
# Process historical data
historical_data = example.process_historical_data('BTCUSDT', days=30)
logger.info("\nHistorical Data Sample:")
logger.info(historical_data.tail())
# Start real-time streaming
symbols = ['BTCUSDT', 'ETHUSDT']
await example.start_streaming(symbols)
if __name__ == '__main__':
asyncio.run(main())