marketforge 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. marketforge/__init__.py +38 -0
  2. marketforge/__main__.py +40 -0
  3. marketforge/aggregation/__init__.py +22 -0
  4. marketforge/aggregation/timeframes.py +362 -0
  5. marketforge/cli/__init__.py +22 -0
  6. marketforge/cli/parser.py +350 -0
  7. marketforge/cli/runner.py +339 -0
  8. marketforge/config/__init__.py +35 -0
  9. marketforge/config/defaults.py +230 -0
  10. marketforge/config/settings.py +284 -0
  11. marketforge/configs/__init__.py +43 -0
  12. marketforge/configs/base.py +198 -0
  13. marketforge/configs/crypto_config.py +349 -0
  14. marketforge/configs/forex_config.py +482 -0
  15. marketforge/configs/loader.py +283 -0
  16. marketforge/configs/stocks_config.py +474 -0
  17. marketforge/core/__init__.py +30 -0
  18. marketforge/core/correlation.py +305 -0
  19. marketforge/core/garch.py +358 -0
  20. marketforge/core/regimes.py +396 -0
  21. marketforge/core/returns.py +392 -0
  22. marketforge/generators/__init__.py +28 -0
  23. marketforge/generators/anomalies.py +490 -0
  24. marketforge/generators/ohlcv.py +440 -0
  25. marketforge/generators/volume.py +368 -0
  26. marketforge/markets/__init__.py +30 -0
  27. marketforge/markets/base.py +235 -0
  28. marketforge/markets/crypto.py +168 -0
  29. marketforge/markets/forex.py +241 -0
  30. marketforge/markets/stocks.py +289 -0
  31. marketforge/output/__init__.py +22 -0
  32. marketforge/output/csv_writer.py +353 -0
  33. marketforge/processing/__init__.py +32 -0
  34. marketforge/processing/batch.py +639 -0
  35. marketforge/utils/__init__.py +32 -0
  36. marketforge/utils/random.py +290 -0
  37. marketforge/utils/time.py +237 -0
  38. marketforge-1.0.0.dist-info/METADATA +621 -0
  39. marketforge-1.0.0.dist-info/RECORD +44 -0
  40. marketforge-1.0.0.dist-info/WHEEL +5 -0
  41. marketforge-1.0.0.dist-info/entry_points.txt +2 -0
  42. marketforge-1.0.0.dist-info/licenses/LICENSE +651 -0
  43. marketforge-1.0.0.dist-info/licenses/NOTICE +16 -0
  44. marketforge-1.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,38 @@
1
+ # MarketForge
2
+ # Copyright (C) 2026 REICHHART Damien
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU Affero General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU Affero General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Affero General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ """
18
+ MarketForge
19
+
20
+ A professional-grade synthetic OHLCV data generator for backtesting,
21
+ stress testing, and development purposes. Supports multi-asset generation
22
+ with correlations, realistic market behavior, and multiple timeframes.
23
+ """
24
+
25
+ __version__ = "1.0.0"
26
+ __author__ = "REICHHART Damien"
27
+
28
+ from marketforge.config.settings import GeneratorConfig
29
+ from marketforge.core.returns import ReturnGenerator
30
+ from marketforge.generators.ohlcv import OHLCVBuilder
31
+
32
+ __all__ = [
33
+ "GeneratorConfig",
34
+ "ReturnGenerator",
35
+ "OHLCVBuilder",
36
+ "__version__",
37
+ ]
38
+
@@ -0,0 +1,40 @@
1
+ # MarketForge
2
+ # Copyright (C) 2026 REICHHART Damien
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU Affero General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU Affero General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Affero General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ """
18
+ Entry point for the MarketForge CLI.
19
+
20
+ Run with: python -m marketforge [options]
21
+
22
+ Example:
23
+ python -m marketforge \
24
+ --output-dir ./data \
25
+ --market crypto \
26
+ --from 1704067200 \
27
+ --to 1704153600 \
28
+ --assets BTC,ETH,SOL \
29
+ --start-prices 50000,3000,120 \
30
+ --volatility 0.02 \
31
+ --drift 0.0001 \
32
+ --seed 42 \
33
+ --correlations 0.8,0.6,0.7
34
+ """
35
+
36
+ from marketforge.cli.parser import main
37
+
38
+ if __name__ == "__main__":
39
+ main()
40
+
@@ -0,0 +1,22 @@
1
+ # MarketForge
2
+ # Copyright (C) 2026 REICHHART Damien
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU Affero General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU Affero General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Affero General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ """Timeframe aggregation module."""
18
+
19
+ from marketforge.aggregation.timeframes import TimeframeAggregator, Timeframe
20
+
21
+ __all__ = ["TimeframeAggregator", "Timeframe"]
22
+
@@ -0,0 +1,362 @@
1
+ # MarketForge
2
+ # Copyright (C) 2026 REICHHART Damien
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU Affero General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU Affero General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Affero General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ """
18
+ Timeframe aggregation for OHLCV data.
19
+
20
+ Aggregates minute-level (m1) OHLCV data to higher timeframes
21
+ using standard OHLCV aggregation rules.
22
+ """
23
+
24
+ from __future__ import annotations
25
+
26
+ from dataclasses import dataclass
27
+ from enum import Enum
28
+ from typing import Optional
29
+ import numpy as np
30
+ import pandas as pd
31
+
32
+ from marketforge.generators.ohlcv import OHLCVData, MultiAssetOHLCVData
33
+
34
+
35
+ class Timeframe(Enum):
36
+ """
37
+ Supported timeframes with their minute equivalents.
38
+
39
+ Naming convention:
40
+ - m = minutes (m1, m5, m15, m30)
41
+ - H = hours (H1, H4)
42
+ - D = days (D1)
43
+ - W = weeks (W1)
44
+ - M = months (M1)
45
+ """
46
+ m1 = 1
47
+ m5 = 5
48
+ m15 = 15
49
+ m30 = 30
50
+ H1 = 60
51
+ H4 = 240
52
+ D1 = 1440 # 24 * 60
53
+ W1 = 10080 # 7 * 24 * 60
54
+ M1 = 43200 # 30 * 24 * 60 (approximate)
55
+
56
+ @property
57
+ def minutes(self) -> int:
58
+ """Return timeframe in minutes."""
59
+ return self.value
60
+
61
+ @property
62
+ def seconds(self) -> int:
63
+ """Return timeframe in seconds."""
64
+ return self.value * 60
65
+
66
+ @classmethod
67
+ def from_string(cls, s: str) -> "Timeframe":
68
+ """
69
+ Parse timeframe from string.
70
+
71
+ Args:
72
+ s: Timeframe string (e.g., "m1", "H4", "D1").
73
+
74
+ Returns:
75
+ Corresponding Timeframe enum.
76
+
77
+ Raises:
78
+ ValueError: If string doesn't match any timeframe.
79
+ """
80
+ try:
81
+ return cls[s]
82
+ except KeyError:
83
+ raise ValueError(f"Unknown timeframe: {s}. Valid: {[t.name for t in cls]}")
84
+
85
+ @classmethod
86
+ def all_timeframes(cls) -> list["Timeframe"]:
87
+ """Return list of all timeframes in ascending order."""
88
+ return sorted(cls, key=lambda t: t.value)
89
+
90
+
91
+ @dataclass
92
+ class AggregatedData:
93
+ """
94
+ Container for aggregated OHLCV data across multiple timeframes.
95
+
96
+ Attributes:
97
+ symbol: Asset symbol.
98
+ timeframes: Dictionary mapping timeframe to OHLCVData.
99
+ """
100
+ symbol: str
101
+ timeframes: dict[Timeframe, OHLCVData]
102
+
103
+ def __getitem__(self, tf: Timeframe | str) -> OHLCVData:
104
+ """Get data for a specific timeframe."""
105
+ if isinstance(tf, str):
106
+ tf = Timeframe.from_string(tf)
107
+ return self.timeframes[tf]
108
+
109
+ @property
110
+ def available_timeframes(self) -> list[Timeframe]:
111
+ """Return list of available timeframes."""
112
+ return list(self.timeframes.keys())
113
+
114
+
115
+ class TimeframeAggregator:
116
+ """
117
+ Aggregator for converting minute data to higher timeframes.
118
+
119
+ Implements standard OHLCV aggregation:
120
+ - Open: First candle's open
121
+ - High: Maximum of all highs
122
+ - Low: Minimum of all lows
123
+ - Close: Last candle's close
124
+ - Volume: Sum of all volumes
125
+
126
+ Supports all standard timeframes from m1 to M1 (monthly).
127
+
128
+ Attributes:
129
+ source_timeframe: Base timeframe of input data.
130
+ target_timeframes: List of timeframes to generate.
131
+
132
+ Example:
133
+ >>> aggregator = TimeframeAggregator()
134
+ >>> aggregated = aggregator.aggregate(m1_data)
135
+ >>> h1_data = aggregated[Timeframe.H1]
136
+ """
137
+
138
+ # Default timeframes to generate
139
+ DEFAULT_TIMEFRAMES = [
140
+ Timeframe.m1,
141
+ Timeframe.m5,
142
+ Timeframe.m15,
143
+ Timeframe.m30,
144
+ Timeframe.H1,
145
+ Timeframe.H4,
146
+ Timeframe.D1,
147
+ Timeframe.W1,
148
+ Timeframe.M1,
149
+ ]
150
+
151
+ def __init__(
152
+ self,
153
+ source_timeframe: Timeframe = Timeframe.m1,
154
+ target_timeframes: Optional[list[Timeframe]] = None
155
+ ) -> None:
156
+ """
157
+ Initialize the timeframe aggregator.
158
+
159
+ Args:
160
+ source_timeframe: Timeframe of input data (default m1).
161
+ target_timeframes: Timeframes to generate. Default is all.
162
+ """
163
+ self._source_tf = source_timeframe
164
+ self._target_tfs = target_timeframes or self.DEFAULT_TIMEFRAMES
165
+
166
+ # Filter out timeframes smaller than source
167
+ self._target_tfs = [
168
+ tf for tf in self._target_tfs
169
+ if tf.minutes >= source_timeframe.minutes
170
+ ]
171
+
172
+ @property
173
+ def source_timeframe(self) -> Timeframe:
174
+ """Return source timeframe."""
175
+ return self._source_tf
176
+
177
+ @property
178
+ def target_timeframes(self) -> list[Timeframe]:
179
+ """Return list of target timeframes."""
180
+ return self._target_tfs
181
+
182
+ def aggregate(self, data: OHLCVData) -> AggregatedData:
183
+ """
184
+ Aggregate OHLCV data to all target timeframes.
185
+
186
+ Args:
187
+ data: Source OHLCV data at source timeframe.
188
+
189
+ Returns:
190
+ AggregatedData containing all timeframes.
191
+ """
192
+ timeframes = {}
193
+
194
+ for tf in self._target_tfs:
195
+ if tf == self._source_tf:
196
+ # No aggregation needed
197
+ timeframes[tf] = data
198
+ else:
199
+ timeframes[tf] = self._aggregate_to_timeframe(data, tf)
200
+
201
+ return AggregatedData(symbol=data.symbol, timeframes=timeframes)
202
+
203
+ def aggregate_multi_asset(
204
+ self,
205
+ data: MultiAssetOHLCVData
206
+ ) -> dict[str, AggregatedData]:
207
+ """
208
+ Aggregate multi-asset OHLCV data to all timeframes.
209
+
210
+ Args:
211
+ data: Multi-asset OHLCV data.
212
+
213
+ Returns:
214
+ Dictionary mapping symbol to AggregatedData.
215
+ """
216
+ result = {}
217
+ for symbol, ohlcv in data.assets.items():
218
+ result[symbol] = self.aggregate(ohlcv)
219
+ return result
220
+
221
+ def _aggregate_to_timeframe(
222
+ self,
223
+ data: OHLCVData,
224
+ target_tf: Timeframe
225
+ ) -> OHLCVData:
226
+ """
227
+ Aggregate data to a specific target timeframe.
228
+
229
+ Args:
230
+ data: Source OHLCV data.
231
+ target_tf: Target timeframe.
232
+
233
+ Returns:
234
+ Aggregated OHLCVData.
235
+ """
236
+ # Calculate aggregation ratio
237
+ ratio = target_tf.minutes // self._source_tf.minutes
238
+
239
+ if ratio <= 1:
240
+ return data
241
+
242
+ n_source = len(data)
243
+ n_target = n_source // ratio
244
+
245
+ if n_target == 0:
246
+ raise ValueError(
247
+ f"Not enough data to aggregate from {self._source_tf.name} "
248
+ f"to {target_tf.name}. Have {n_source} candles, need at least {ratio}."
249
+ )
250
+
251
+ # Truncate to complete periods
252
+ n_used = n_target * ratio
253
+
254
+ # Aggregate using numpy operations for efficiency
255
+ timestamps = data.timestamps[:n_used:ratio].copy()
256
+
257
+ # Reshape for aggregation
258
+ open_reshaped = data.open[:n_used].reshape(n_target, ratio)
259
+ high_reshaped = data.high[:n_used].reshape(n_target, ratio)
260
+ low_reshaped = data.low[:n_used].reshape(n_target, ratio)
261
+ close_reshaped = data.close[:n_used].reshape(n_target, ratio)
262
+ volume_reshaped = data.volume[:n_used].reshape(n_target, ratio)
263
+
264
+ # Apply aggregation rules
265
+ open_prices = open_reshaped[:, 0] # First open
266
+ high_prices = high_reshaped.max(axis=1) # Max high
267
+ low_prices = low_reshaped.min(axis=1) # Min low
268
+ close_prices = close_reshaped[:, -1] # Last close
269
+ volumes = volume_reshaped.sum(axis=1) # Sum volume
270
+
271
+ return OHLCVData(
272
+ symbol=data.symbol,
273
+ timestamps=timestamps,
274
+ open=open_prices,
275
+ high=high_prices,
276
+ low=low_prices,
277
+ close=close_prices,
278
+ volume=volumes,
279
+ )
280
+
281
+ def aggregate_single_timeframe(
282
+ self,
283
+ data: OHLCVData,
284
+ target_tf: Timeframe
285
+ ) -> OHLCVData:
286
+ """
287
+ Aggregate to a single target timeframe.
288
+
289
+ Convenience method for aggregating to just one timeframe.
290
+
291
+ Args:
292
+ data: Source OHLCV data.
293
+ target_tf: Target timeframe.
294
+
295
+ Returns:
296
+ Aggregated OHLCVData.
297
+ """
298
+ return self._aggregate_to_timeframe(data, target_tf)
299
+
300
+
301
+ def aggregate_ohlcv_pandas(
302
+ df: pd.DataFrame,
303
+ target_minutes: int,
304
+ timestamp_col: str = "timestamp"
305
+ ) -> pd.DataFrame:
306
+ """
307
+ Aggregate OHLCV DataFrame using pandas.
308
+
309
+ Alternative implementation using pandas for flexibility.
310
+
311
+ Args:
312
+ df: DataFrame with OHLCV columns.
313
+ target_minutes: Target timeframe in minutes.
314
+ timestamp_col: Name of timestamp column.
315
+
316
+ Returns:
317
+ Aggregated DataFrame.
318
+ """
319
+ # Convert timestamp to datetime for resampling
320
+ df = df.copy()
321
+ df["datetime"] = pd.to_datetime(df[timestamp_col], unit="s", utc=True)
322
+ df = df.set_index("datetime")
323
+
324
+ # Resample rule
325
+ rule = f"{target_minutes}min"
326
+
327
+ # Aggregate
328
+ agg_dict = {
329
+ "open": "first",
330
+ "high": "max",
331
+ "low": "min",
332
+ "close": "last",
333
+ "volume": "sum",
334
+ }
335
+
336
+ resampled = df.resample(rule).agg(agg_dict).dropna()
337
+
338
+ # Convert back to timestamp
339
+ resampled[timestamp_col] = resampled.index.astype(np.int64) // 10**9
340
+ resampled = resampled.reset_index(drop=True)
341
+
342
+ return resampled[[timestamp_col, "open", "high", "low", "close", "volume"]]
343
+
344
+
345
+ def parse_timeframe_list(timeframe_str: str) -> list[Timeframe]:
346
+ """
347
+ Parse comma-separated timeframe string.
348
+
349
+ Args:
350
+ timeframe_str: Comma-separated timeframes (e.g., "m1,m5,H1,D1").
351
+
352
+ Returns:
353
+ List of Timeframe enums.
354
+ """
355
+ parts = [p.strip() for p in timeframe_str.split(",")]
356
+ return [Timeframe.from_string(p) for p in parts if p]
357
+
358
+
359
+ def get_standard_timeframes() -> list[Timeframe]:
360
+ """Return list of standard timeframes."""
361
+ return TimeframeAggregator.DEFAULT_TIMEFRAMES.copy()
362
+
@@ -0,0 +1,22 @@
1
+ # MarketForge
2
+ # Copyright (C) 2026 REICHHART Damien
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU Affero General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU Affero General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Affero General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ """CLI module for MarketForge."""
18
+
19
+ from marketforge.cli.parser import create_cli
20
+ from marketforge.cli.runner import run_generation
21
+
22
+ __all__ = ["create_cli", "run_generation"]