Skip to content

Commit 85de9f4

Browse files
committed
ENH: Add lib.random_ohlc_data(frac=)
1 parent 431644f commit 85de9f4

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

backtesting/lib.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,16 @@ def wrap_func(resampled, *args, **kwargs):
287287
return array
288288

289289

290-
def random_ohlc_data(example_data: pd.DataFrame, random_state: int = None) -> pd.DataFrame:
290+
def random_ohlc_data(example_data: pd.DataFrame, *,
291+
frac=1., random_state: int = None) -> pd.DataFrame:
291292
"""
292293
OHLC data generator. The generated OHLC data has basic
293294
[descriptive statistics](https://en.wikipedia.org/wiki/Descriptive_statistics)
294295
similar to the provided `example_data`.
295296
297+
`frac` is a fraction of data to sample (with replacement). Values greater
298+
than 1 result in oversampling.
299+
296300
Such random data can be effectively used for stress testing trading
297301
strategy robustness, Monte Carlo simulations, significance testing, etc.
298302
@@ -304,7 +308,7 @@ def random_ohlc_data(example_data: pd.DataFrame, random_state: int = None) -> pd
304308
...
305309
"""
306310
def shuffle(x):
307-
return x.sample(frac=1, random_state=random_state)
311+
return x.sample(frac=frac, replace=frac > 1, random_state=random_state)
308312

309313
if len(example_data.columns & {'Open', 'High', 'Low', 'Close'}) != 4:
310314
raise ValueError("`data` must be a pandas.DataFrame with columns "

backtesting/test/_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ def test_plot_heatmaps(self):
729729
time.sleep(5)
730730

731731
def test_random_ohlc_data(self):
732-
generator = random_ohlc_data(GOOG)
732+
generator = random_ohlc_data(GOOG, frac=1)
733733
new_data = next(generator)
734734
self.assertEqual(list(new_data.index), list(GOOG.index))
735735
self.assertEqual(new_data.shape, GOOG.shape)

0 commit comments

Comments
 (0)