Download create_timeframes.py from SurgeSoftwareSolutions/intraday-btc-model-11yr: direct link, hf CLI and curl.
- Browser
- Download file 5.61 kB
-
https://huggingface.co/SurgeSoftwareSolutions/intraday-btc-model-11yr/resolve/main/create_timeframes.py
- Command line
-
hf download hf://SurgeSoftwareSolutions/intraday-btc-model-11yr/create_timeframes.py
-
curl -L -o create_timeframes.py https://huggingface.co/SurgeSoftwareSolutions/intraday-btc-model-11yr/resolve/main/create_timeframes.py
5.61 kB
| """ | |
| Convert 1-minute data to higher timeframes (5m, 15m, 1h, 4h, daily). | |
| This allows testing the same strategy on different timeframes to find | |
| the optimal trading interval. | |
| """ | |
| import pandas as pd | |
| import os | |
| import sys | |
| sys.path.append(os.path.dirname(os.path.abspath(__file__))) | |
| import config | |
| def resample_to_timeframe(df, timeframe): | |
| """ | |
| Resample 1-minute OHLCV data to a higher timeframe. | |
| Parameters | |
| ---------- | |
| df : pd.DataFrame | |
| 1-minute OHLCV data with DatetimeIndex | |
| timeframe : str | |
| Pandas resample frequency: '5min', '15min', '1H', '4H', '1D' | |
| Returns | |
| ------- | |
| pd.DataFrame | |
| Resampled OHLCV data | |
| """ | |
| # Ensure index is datetime | |
| if not isinstance(df.index, pd.DatetimeIndex): | |
| df = df.set_index('timestamp') | |
| # Resample OHLCV data | |
| resampled = df.resample(timeframe).agg({ | |
| 'open': 'first', | |
| 'high': 'max', | |
| 'low': 'min', | |
| 'close': 'last', | |
| 'volume': 'sum' | |
| }).dropna() | |
| return resampled | |
| def create_all_timeframes(source_file): | |
| """ | |
| Create datasets for all timeframes from 1-minute source data. | |
| Parameters | |
| ---------- | |
| source_file : str | |
| Path to 1-minute CSV file | |
| Returns | |
| ------- | |
| dict | |
| Dictionary of {timeframe: output_file_path} | |
| """ | |
| print("\n" + "="*70) | |
| print("CREATING MULTI-TIMEFRAME DATASETS") | |
| print("="*70) | |
| # Load source data | |
| print(f"\n๐ฅ Loading source data: {source_file}") | |
| df = pd.read_csv(source_file) | |
| df['timestamp'] = pd.to_datetime(df['timestamp']) | |
| df = df.set_index('timestamp') | |
| print(f" Loaded {len(df):,} 1-minute candles") | |
| print(f" Period: {df.index[0]} to {df.index[-1]}") | |
| # Define timeframes to create | |
| timeframes = { | |
| '5min': '5-minute', | |
| '15min': '15-minute', | |
| '30min': '30-minute', | |
| '1H': '1-hour', | |
| '4H': '4-hour', | |
| '1D': 'daily' | |
| } | |
| output_files = {} | |
| for tf_code, tf_name in timeframes.items(): | |
| print(f"\n๐ Creating {tf_name} dataset...") | |
| # Resample | |
| resampled = resample_to_timeframe(df, tf_code) | |
| # Save | |
| output_file = os.path.join( | |
| config.DATA_DIR, | |
| f"btc_{tf_code.lower()}_binance.csv" | |
| ) | |
| # Reset index to have timestamp as column | |
| resampled = resampled.reset_index() | |
| resampled.to_csv(output_file, index=False) | |
| output_files[tf_name] = output_file | |
| print(f" โ Created {len(resampled):,} {tf_name} candles") | |
| print(f" โ Saved to: {output_file}") | |
| return output_files | |
| def estimate_trades_per_timeframe(): | |
| """Display estimated trades per day for each timeframe.""" | |
| print("\n" + "="*70) | |
| print("ESTIMATED TRADING CHARACTERISTICS BY TIMEFRAME") | |
| print("="*70) | |
| print("\nBased on current parameters (MA 15/60, ~2 trades/day on 1-min):\n") | |
| timeframes = [ | |
| ("1-minute", 1, "~2 trades/day", "High frequency, many signals"), | |
| ("5-minute", 5, "~8-12 trades/week", "Active intraday"), | |
| ("15-minute", 15, "~2-4 trades/week", "Swing trading"), | |
| ("30-minute", 30, "~1-2 trades/week", "Position trading"), | |
| ("1-hour", 60, "~5-10 trades/month", "Low frequency"), | |
| ("4-hour", 240, "~1-3 trades/month", "Very selective"), | |
| ("Daily", 1440, "~2-6 trades/year", "Long-term holds") | |
| ] | |
| print(f"{'Timeframe':<15} {'Candle (min)':<15} {'Frequency':<20} {'Style'}") | |
| print("-" * 70) | |
| for tf, mins, freq, style in timeframes: | |
| print(f"{tf:<15} {mins:<15} {freq:<20} {style}") | |
| print("\n๐ก Hypothesis:") | |
| print(" - Longer timeframes = Less noise, fewer false signals") | |
| print(" - Better win rate but fewer opportunities") | |
| print(" - May need to adjust MA periods (15/60 โ 10/30 or 20/80)") | |
| def main(): | |
| """Main execution.""" | |
| print("\n๐ MULTI-TIMEFRAME DATA CREATOR") | |
| # Use the 2-year Binance data as source | |
| source_file = os.path.join(config.DATA_DIR, "btc_1m_binance_730d.csv") | |
| if not os.path.exists(source_file): | |
| print(f"\nโ Source file not found: {source_file}") | |
| print(f" Please ensure you have 1-minute data available.") | |
| return | |
| # Create all timeframes | |
| output_files = create_all_timeframes(source_file) | |
| # Show characteristics | |
| estimate_trades_per_timeframe() | |
| print("\n" + "="*70) | |
| print("โ MULTI-TIMEFRAME DATASETS CREATED") | |
| print("="*70) | |
| print("\n๐ Created files:") | |
| for tf_name, filepath in output_files.items(): | |
| print(f" - {tf_name}: {filepath}") | |
| print("\n๐ก Next steps:") | |
| print(" 1. Test current parameters on each timeframe:") | |
| print(" - Update config.py to use each timeframe file") | |
| print(" - Run: python run_backtest.py") | |
| print(" - Compare results") | |
| print("\n 2. Optimize parameters per timeframe:") | |
| print(" - Run: python optimize_parameters.py (for each TF)") | |
| print(" - Find best settings for each interval") | |
| print("\n 3. Compare performance:") | |
| print(" - Which timeframe gives best Sharpe ratio?") | |
| print(" - Which has best win rate?") | |
| print(" - Which has lowest drawdown?") | |
| print("="*70 + "\n") | |
| if __name__ == "__main__": | |
| main() | |