help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
stock-time-series
/
tutorials
RSK World
stock-time-series
Stock Market Time Series Dataset - OHLCV + LSTM + Portfolio Optimization
tutorials
  • README.md2.1 KB
  • advanced_ml_tutorial.py6 KB
  • backtesting_tutorial.py8 KB
  • getting_started.py10 KB
advanced_ml_tutorial.py
tutorials/advanced_ml_tutorial.py
Raw Download
Find: Go to:
"""
Stock Market Time Series Dataset - Advanced ML Tutorial

Author: Molla Samser
Organization: RSK World
Designer & Tester: Rima Khatun
Website: https://rskworld.in/
Email: help@rskworld.in
Phone: +91 93305 39277
Address: Nutanhat, Mongolkote, Purba Burdwan, West Bengal, India, 713147

This tutorial covers:
1. Feature Engineering
2. LSTM Model Training
3. Model Evaluation
4. Future Price Prediction
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')

print("=" * 70)
print("Advanced Machine Learning Tutorial")
print("=" * 70)
print("\nAuthor: Molla Samser | RSK World")
print("Website: https://rskworld.in/")
print("=" * 70)

from scripts.ml_models import StockMLModels, FeatureEngineering

# ==============================================================================
# SECTION 1: FEATURE ENGINEERING
# ==============================================================================
print("\n" + "=" * 70)
print("SECTION 1: Advanced Feature Engineering")
print("=" * 70)

print("\nLoading AAPL data...")
df = pd.read_csv('../data/AAPL.csv', parse_dates=['Date'], index_col='Date')
print(f"Original columns: {len(df.columns)}")

print("\nAdding advanced features...")
df_enhanced = FeatureEngineering.add_technical_features(df)
print(f"Enhanced columns: {len(df_enhanced.columns)}")

new_features = list(set(df_enhanced.columns) - set(df.columns))
print(f"\nNew features added ({len(new_features)}):")
for i, feature in enumerate(new_features, 1):
    print(f"  {i}. {feature}")

print("\nSample of enhanced data:")
print(df_enhanced[['Close', 'Returns', 'Volatility_20', 'Momentum_10', 
                   'ROC_10', 'BB_Upper', 'BB_Lower', 'ATR']].tail())

# ==============================================================================
# SECTION 2: LSTM MODEL TRAINING
# ==============================================================================
print("\n" + "=" * 70)
print("SECTION 2: LSTM Model Training")
print("=" * 70)

print("\nInitializing ML model...")
ml_model = StockMLModels('../data/AAPL.csv')

print("\nTraining LSTM model...")
print("Note: This may take a few minutes...")
print("If TensorFlow is not installed, will use Random Forest as fallback")

results = ml_model.train_lstm(epochs=20, batch_size=32, verbose=1)

# ==============================================================================
# SECTION 3: MODEL EVALUATION
# ==============================================================================
print("\n" + "=" * 70)
print("SECTION 3: Model Evaluation")
print("=" * 70)

if results and 'metrics' in results:
    metrics = results['metrics']
    print("\nPerformance Metrics:")
    print(f"{'Metric':<25} {'Training':>15} {'Testing':>15}")
    print("-" * 60)
    print(f"{'RMSE:':<25} ${metrics['train_rmse']:>14.2f} ${metrics['test_rmse']:>14.2f}")
    print(f"{'MAE:':<25} ${metrics['train_mae']:>14.2f} ${metrics['test_mae']:>14.2f}")
    print(f"{'R² Score:':<25} {metrics['train_r2']:>15.4f} {metrics['test_r2']:>15.4f}")
    
    print("\nGenerating prediction plots...")
    ml_model.plot_predictions(results, save_path='ml_predictions.png')
    print("✓ Saved: ml_predictions.png")
    
    if 'history' in results:
        ml_model.plot_training_history(results['history'], save_path='ml_history.png')
        print("✓ Saved: ml_history.png")

# ==============================================================================
# SECTION 4: FUTURE PREDICTION
# ==============================================================================
print("\n" + "=" * 70)
print("SECTION 4: Future Price Prediction")
print("=" * 70)

if ml_model.model is not None:
    print("\nPredicting next 30 days...")
    future_forecast = ml_model.predict_future(days=30, sequence_length=60)
    
    print("\nFuture Forecast:")
    print(future_forecast.head(10))
    
    print(f"\nForecast Summary:")
    print(f"Starting Prediction: ${future_forecast['Predicted_Price'].iloc[0]:.2f}")
    print(f"Ending Prediction: ${future_forecast['Predicted_Price'].iloc[-1]:.2f}")
    print(f"Expected Change: ${future_forecast['Predicted_Price'].iloc[-1] - future_forecast['Predicted_Price'].iloc[0]:.2f}")
    
    # Plot
    plt.figure(figsize=(14, 7))
    historical = ml_model.df['Close'][-60:]
    plt.plot(historical.index, historical, label='Historical', linewidth=2, color='blue')
    plt.plot(future_forecast.index, future_forecast['Predicted_Price'], 
            label='Forecast', linewidth=2, color='red', linestyle='--')
    plt.title('AAPL - 30-Day ML Forecast', fontsize=16, fontweight='bold')
    plt.xlabel('Date', fontsize=12)
    plt.ylabel('Price ($)', fontsize=12)
    plt.legend()
    plt.grid(True, alpha=0.3)
    plt.tight_layout()
    plt.savefig('ml_future_forecast.png', dpi=300, bbox_inches='tight')
    print("✓ Saved: ml_future_forecast.png")
    plt.show()

# ==============================================================================
# SUMMARY
# ==============================================================================
print("\n" + "=" * 70)
print("ADVANCED ML TUTORIAL COMPLETE!")
print("=" * 70)

print("\n📊 What You've Learned:")
print("  ✓ Advanced feature engineering (20+ features)")
print("  ✓ LSTM neural network training")
print("  ✓ Model evaluation metrics")
print("  ✓ Future price prediction")

print("\n📁 Files Generated:")
print("  ✓ ml_predictions.png")
print("  ✓ ml_history.png")
print("  ✓ ml_future_forecast.png")

print("\n🚀 Next Steps:")
print("  1. Experiment with different model architectures")
print("  2. Try different feature combinations")
print("  3. Tune hyperparameters (epochs, batch size, units)")
print("  4. Compare with other stocks")

print("\n" + "=" * 70)
print("Visit https://rskworld.in/ for more datasets and tutorials!")
print("Contact: help@rskworld.in | +91 93305 39277")
print("=" * 70)

166 lines•6 KB
python

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer