permute_returns#
Returns n randomized copies of a price series as a (T, n) array. mode sets
what is shuffled:
"log"(default): the log returns. The path is rebuilt from the first valid value, so every copy keeps the first and last values and the distribution of returns, but not their order. Values must be positive."diff": the differences, rebuilt the same way. Use it for series that can be zero or negative, such as a spread."level": the values themselves. Use it for series that are stationary in level, such as a funding rate, where a rebuilt path would be meaningless.
The NaN warmup prefix stays in place.
With volume, the function returns (prices, volume): the volume of the row
each step (or value) came from. volume_mode="keep" (default) moves it as it is;
"notional" rescales it so that volume times price stays that of its source
row: where a copy trades at twice the price, the volume halves. That keeps the
traded amount, which is what most volume-based signals respond to.
The function is batch-only and runs in C++; the same seed gives the same
copies on every platform. strategy_test uses it with
method="returns".
Examples#
import numpy as np
from screamer import permute_returns
price = np.array([100.0, 101.0, 100.5, 102.0, 103.0, 102.5])
print(np.round(permute_returns(price, 3, seed=0), 2))
[[100. 100. 100. ]
[100.98 100.98 101. ]
[100.48 100.48 102.51]
[101.98 99.99 103.51]
[101.49 100.99 103. ]
[102.5 102.5 102.5 ]]
Signature#
permute_returns(x, n, mode="log", seed=0, volume=None, volume_mode="keep")
Used in the Guide#
See also#
permute_bars: Bar-permuted copies of an OHLC series with no serial structure; in "log" and "diff" mode they keep the distributions of gaps and intrabar shapes, the first bar and the last closestrategy_test: Test a whole strategy, model included, against the same strategy run on permuted market data or shifted featurescircular_shift: Randomly rotated copies of a series, for randomization tests