Comments (1)
I probably found a solution, but not fully tested:
class MultipleTimeSeriesCV:
"""Generates tuples of train_idx, test_idx pairs
Assumes the MultiIndex contains levels 'symbol' and 'date'
purges overlapping outcomes"""
def __init__(self,
n_splits=3,
train_period_length=126,
test_period_length=21,
lookahead=None,
date_idx='date',
shuffle=False):
self.n_splits = n_splits
self.lookahead = lookahead
self.test_length = test_period_length
self.train_length = train_period_length
self.shuffle = shuffle
self.date_idx = date_idx
def split(self, X, y=None, groups=None):
unique_dates = X.index.get_level_values(self.date_idx).unique()
days = sorted(unique_dates) # Ascending order
split_idx = []
for i in range(self.n_splits):
# Calculate split indices based on ascending order of days
train_start_idx = i * self.test_length
train_end_idx = train_start_idx + self.train_length
test_start_idx = train_end_idx + (self.lookahead or 0)
test_end_idx = test_start_idx + self.test_length
# Ensure we do not exceed the length of days
if test_end_idx >= len(days):
break
split_idx.append((train_start_idx, train_end_idx, test_start_idx, test_end_idx))
dates = X.reset_index()[[self.date_idx]]
for train_start, train_end, test_start, test_end in split_idx:
# Adjust the condition to select the right slice based on sorted ascending days
train_idx = dates[(dates[self.date_idx] >= days[train_start]) &
(dates[self.date_idx] < days[train_end])].index
test_idx = dates[(dates[self.date_idx] >= days[test_start]) &
(dates[self.date_idx] < days[test_end])].index
if self.shuffle:
train_idx = np.random.permutation(train_idx)
yield train_idx.to_numpy(), test_idx.to_numpy()
def get_n_splits(self, X, y, groups=None):
return self.n_splits
from machine-learning-for-trading.
Related Issues (20)
- %load_ext zipline HOT 1
- problem in Long-Short Strategy, Part 1: Preparing Alpha Factors and Features HOT 1
- rsi HOT 1
- Daily historical return deciles HOT 1
- back testing is not working HOT 1
- Chapter 7 - Evaluating signals using alphalens HOT 1
- Chapter 8 02_backtesting_with_zipline HOT 1
- 01_parse_itch_order_flow_messages HOT 3
- Chapter 8 02_backtesting_with_zipline custom_loader not working HOT 1
- --
- installation get stuck / mamba env update -n ml4t -f installation/ml4t-base.yml
- No object named P in the file HOT 3
- Twitter data gone? Broken link in "data/twitter_sentiment.ipynb"
- Unable to replicate backtest performance | Chapter 8 02_backtesting_with_zipline HOT 1
- TypeError: download() got multiple values for argument 'start' when using pandas_datareader.data
- KeyError: 'No object named quandl/wiki/prices in the file' HOT 1
- Install instructions for windows and linux suck - no offence
- Chapter 22_deep_reinforcement_learning Google Colab Python 3.10 HOT 4
- Chapter 22_deep_reinforcement_learning Google Colab Python 3.10 03_lunar_lander
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from machine-learning-for-trading.