-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynthetic_data_generator.py
More file actions
55 lines (47 loc) · 1.7 KB
/
synthetic_data_generator.py
File metadata and controls
55 lines (47 loc) · 1.7 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
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
def generate_synthetic_data(timeframe: str = "1m", points: int = 1000) -> pd.DataFrame:
"""
Generates synthetic OHLCV data for backtesting.
:param timeframe: Timeframe for synthetic data (e.g., '1m', '5m', '1h', '1d').
:param points: Number of data points to generate.
:return: A pandas DataFrame containing OHLCV data.
"""
# Map timeframe to minutes
timeframe_map = {
"1m": 1,
"5m": 5,
"10m": 10,
"15m": 15,
"30m": 30,
"1h": 60,
"2h": 120,
"4h": 240,
"8h": 480,
"1d": 1440,
"1w": 10080,
"1M": 43200 # Approximation for 30 days
}
if timeframe not in timeframe_map:
raise ValueError(f"Unsupported timeframe: {timeframe}")
interval_minutes = timeframe_map[timeframe]
start_time = datetime.now() - timedelta(minutes=interval_minutes * points)
# Generate synthetic data
timestamps = [start_time + timedelta(minutes=interval_minutes * i) for i in range(points)]
prices = np.cumsum(np.random.normal(0, 1, points)) + 100 # Random walk around 100
high = prices + np.random.uniform(0.5, 2.0, points)
low = prices - np.random.uniform(0.5, 2.0, points)
open_ = prices + np.random.uniform(-1, 1, points)
close = prices + np.random.uniform(-1, 1, points)
volume = np.random.randint(1, 1000, points)
# Create DataFrame
synthetic_data = pd.DataFrame({
"timestamp": [int(ts.timestamp() * 1000) for ts in timestamps],
"open": open_,
"high": high,
"low": low,
"close": close,
"volume": volume
})
return synthetic_data