Skip to content

PineScript-to-Python is a simple tool that converts TradingView Pine Script strategies into executable Python code using libraries like pandas and ta, enabling backtesting and strategy analysis outside TradingView.

Notifications You must be signed in to change notification settings

Maksym-TopDev/Pinescript_to_python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Pinescript_to_python

A comprehensive trading strategy framework that converts Pine Script strategies to Python, with advanced backtesting, optimization, and analysis capabilities.

πŸš€ Quick Start

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd Pinescript_to_python
  2. Install the package:

    pip install -e .
  3. Install additional dependencies (optional):

    pip install -e .[full]  # For all features including dashboards

Run the Demo

To see all features in action with BTC/USDT data:

python main_demo.py

This demo will:

  • βœ… Generate realistic BTC/USDT market data (bull, bear, sideways, high volatility)
  • βœ… Test trading strategies across different market conditions
  • βœ… Demonstrate enhanced backtesting with parameter optimization
  • βœ… Show database storage and professional report generation
  • βœ… Create performance analysis and recommendations

Run Tests

To run the comprehensive test suite including BTC/USDT tests:

python -m pytest tests/test_momentum_strategy.py -v

To run only BTC-specific tests:

python -m pytest tests/test_momentum_strategy.py::TestBTCUSDTData -v
python -m pytest tests/test_momentum_strategy.py::TestMomentumStrategyWithBTC -v

πŸ“Š Framework Architecture

The framework is organized into clean, modular packages:script-to-python Pinescript_to_python is a modular trading library that converts TradingView Pine Script strategies into executable Python code using libraries like pandas and numpy, enabling backtesting and strategy analysis outside TradingView.

οΏ½ Project Structure

The project is now organized into clean, modular packages following SOLID principles:

Pinescript_to_python/
β”œβ”€β”€ models/                 # Data classes and configuration
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ strategy_params.py  # StrategyParams dataclass
β”‚   └── trade_result.py     # TradeResult dataclass
β”œβ”€β”€ indicators/             # Technical analysis indicators
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ base.py            # Abstract base classes and protocols
β”‚   β”œβ”€β”€ moving_averages.py # EMA, SMA, and factory
β”‚   β”œβ”€β”€ momentum.py        # RSI and other momentum indicators
β”‚   β”œβ”€β”€ volatility.py      # ATR and volatility indicators
β”‚   β”œβ”€β”€ trend.py           # ADX and trend indicators
β”‚   └── calculator.py      # Service to calculate all indicators
β”œβ”€β”€ signals/                # Signal generation logic
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── signal_generator.py # Long/short signal generation
β”œβ”€β”€ trading/                # Trade simulation and execution
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── trade_simulator.py  # Trade execution simulator
β”œβ”€β”€ strategy/               # Main strategy orchestration
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── momentum_strategy.py # Main MomentumStrategy class
β”œβ”€β”€ backtesting/            # πŸ†• Enhanced backtesting engine
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ backtesting_engine.py    # Multi-timeframe backtesting
β”‚   β”œβ”€β”€ backtest_config.py       # Configuration management
β”‚   β”œβ”€β”€ timeframe_manager.py     # Custom timeframes (13m, 45m, etc.)
β”‚   └── performance_metrics.py   # Comprehensive metrics
β”œβ”€β”€ optimization/           # πŸ†• Multi-stock optimization system
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ optimization_engine.py   # Parallel optimization
β”‚   β”œβ”€β”€ optimization_config.py   # Parameter grids and config
β”‚   β”œβ”€β”€ stock_data_manager.py    # Data validation and caching
β”‚   └── optimization_results.py  # Results handling
β”œβ”€β”€ analysis/               # πŸ†• Results storage and analysis
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ database_manager.py      # SQLite database operations
β”‚   β”œβ”€β”€ report_generator.py      # Professional reports
β”‚   β”œβ”€β”€ dashboard.py             # HTML dashboards
β”‚   └── scheduler.py             # Automated optimization
β”œβ”€β”€ tests/                  # Comprehensive test suite
β”‚   └── test_momentum_strategy.py # Including BTC/USDT tests
β”œβ”€β”€ main_demo.py           # 🎯 Main demonstration script
β”œβ”€β”€ btc_data_generator.py  # πŸ†• BTC/USDT realistic data generation
β”œβ”€β”€ demo.py                # Basic usage examples
β”œβ”€β”€ requirements.txt       # Dependencies
β”œβ”€β”€ pyproject.toml        # Project configuration
└── README.md             # This file

🎯 Key Features

οΏ½ Enhanced Backtesting Engine

  • Multiple Timeframes: Standard (1m, 5m, 15m, 30m, 1h, 4h, 1d) + Custom (13m, 45m, 2h, 6h, 12h)
  • Advanced Metrics: Sharpe, Sortino, Calmar ratios, VaR, drawdown analysis
  • Commission & Slippage: Realistic trading cost modeling
  • Parameter Optimization: Parallel processing for efficient optimization

πŸ” Multi-Stock Optimization System

  • Batch Processing: Optimize across multiple stocks simultaneously
  • Parameter Grids: Pre-configured sets (quick, comprehensive, risk-focused, momentum-focused)
  • Data Validation: Automatic data quality checks and filtering
  • Memory Efficient: Smart memory management for large datasets

πŸ“ˆ Results & Analysis Platform

  • Database Storage: SQLite with efficient indexing for fast queries
  • Professional Reports: Summary, detailed analysis, rankings, recommendations
  • Interactive Dashboards: HTML dashboards with charts and analytics
  • Automation: Scheduled optimization runs with email notifications

πŸͺ™ Cryptocurrency Support

  • BTC/USDT Data: Realistic cryptocurrency price data generation
  • Market Scenarios: Bull market, bear market, sideways, high volatility scenarios
  • Crypto-Optimized Parameters: Strategy configurations tailored for crypto volatility
  • Comprehensive Testing: Full test suite with crypto-specific test cases

πŸ’» Usage Examples

Basic Strategy Usage

from strategy import create_default_strategy
from btc_data_generator import generate_btc_usdt_data
import pandas as pd

# Generate BTC/USDT test data
btc_data = generate_btc_usdt_data(periods=1000, base_price=30000)

# Create and run strategy
strategy = create_default_strategy()
result = strategy.run_strategy(btc_data)

# Analyze results
trades = strategy.executed_trades
if trades:
    total_pnl = sum(trade.pnl for trade in trades)
    win_rate = len([t for t in trades if t.pnl > 0]) / len(trades)
    print(f"Total PnL: {total_pnl:.2f}")
    print(f"Win Rate: {win_rate:.1%}")
    print(f"Total Trades: {len(trades)}")

Enhanced Backtesting

from backtesting import BacktestingEngine, BacktestConfig
from models import StrategyParams

# Configure backtesting
config = BacktestConfig(
    commission_rate=0.001,  # 0.1% commission
    slippage_bps=5.0,       # 5 basis points slippage
    initial_capital=50000.0
)

# Run backtest
engine = BacktestingEngine(config)
result = engine.single_backtest(btc_data, StrategyParams(), '1h', 'BTCUSDT')

print(f"Profit Factor: {result.performance.profit_factor:.2f}")
print(f"Sharpe Ratio: {result.performance.sharpe_ratio:.2f}")
print(f"Max Drawdown: {result.performance.max_drawdown:.1%}")

Multi-Stock Optimization

from optimization import OptimizationEngine, OptimizationConfig, PARAMETER_GRIDS

# Configure optimization
config = OptimizationConfig(
    stock_list=['BTCUSDT', 'ETHUSDT', 'ADAUSDT'],
    timeframes=['1h', '4h'],
    max_workers=4
)

# Run optimization
engine = OptimizationEngine(config)
results = engine.run_full_optimization(PARAMETER_GRIDS['crypto_focused'])

# View best results
for result in results[:5]:
    print(f"{result.symbol}: PF={result.performance.profit_factor:.2f}")

Results Analysis

from analysis import DatabaseManager, ReportGenerator

# Setup database and save results
db = DatabaseManager('trading_results.db')
for result in optimization_results:
    db.save_backtest_result(result)

# Generate reports
report_gen = ReportGenerator(db)
report_gen.generate_all_reports('output/')

# Get statistics
stats = db.get_performance_statistics()
print(f"Total strategies tested: {stats['total_results']}")
print(f"Profitable strategies: {stats['profitable_strategies']}")

πŸ§ͺ Testing

Run All Tests

# Full test suite
python -m pytest tests/test_momentum_strategy.py -v

# BTC-specific tests only
python -m pytest tests/test_momentum_strategy.py -k "btc" -v

# Integration tests
python -m pytest tests/test_momentum_strategy.py::TestIntegrationBTC -v

Generate Test Coverage

pip install pytest-cov
python -m pytest tests/ --cov=. --cov-report=html

πŸ“‹ Requirements

Core Dependencies

  • pandas>=1.5.0 - Data manipulation and analysis
  • numpy>=1.24.0 - Numerical computing
  • ta>=0.10.0 - Technical analysis library

Optional Dependencies

# For full features (dashboards, visualizations)
pip install -e .[full]

# Individual feature sets
pip install -e .[dev]   # Development tools
pip install -e .[viz]   # Visualization tools
pip install -e .[data]  # Data download capabilities

🎯 Demo Output

When you run python main_demo.py, you'll see:

πŸš€ Trading Strategy Framework - BTC/USDT Demo
============================================================
2025-06-18 13:45:23,456 - INFO - === BTC/USDT DATA GENERATION ===
2025-06-18 13:45:23,465 - INFO - πŸ“Š Bull Market Scenario:
2025-06-18 13:45:23,465 - INFO -   - Periods: 1000
2025-06-18 13:45:23,465 - INFO -   - Price Range: $20123.45 - $45678.90
2025-06-18 13:45:23,465 - INFO -   - Price Change: +127.2%
2025-06-18 13:45:23,465 - INFO -   - Validation: βœ… PASS

=== STRATEGY TESTING ON BTC/USDT ===
πŸš€ Testing CONSERVATIVE strategy:
  πŸ“ˆ bull_market: 23 trades, 65.2% win rate, 12.3% return
  πŸ“ˆ bear_market: 15 trades, 46.7% win rate, -3.2% return
  πŸ“ˆ sideways_market: 8 trades, 62.5% win rate, 2.1% return

πŸ” Running parameter optimization...
βœ… Optimization completed: 32 combinations tested
πŸ† Best result: PF=1.85, WR=68.2%

πŸ“Š Generating analysis reports...
πŸ“„ Summary report: btc_strategy_summary.txt
πŸ“‹ Detailed analysis: btc_strategy_detailed.csv

πŸŽ‰ Demo completed successfully!

Generated files:

  • btc_strategy_summary.txt - Performance summary and recommendations
  • btc_strategy_detailed.csv - Detailed results for further analysis
  • btc_demo_results.db - SQLite database with all results

πŸŽ‰ What's New

Recent Enhancements

  • βœ… BTC/USDT Support: Realistic cryptocurrency data generation with multiple market scenarios
  • βœ… Enhanced Backtesting: Multiple timeframes, custom periods, advanced performance metrics
  • βœ… Multi-Stock Optimization: Parallel processing across multiple assets
  • βœ… Professional Analysis: Database storage, comprehensive reporting, HTML dashboards
  • βœ… Automated Scheduling: Set up automated optimization runs
  • βœ… Comprehensive Testing: 200+ test cases including crypto-specific scenarios

Performance Improvements

  • πŸš€ Parallel Processing: Up to 4x faster optimization with multi-core support
  • πŸ’Ύ Memory Efficiency: Smart memory management for large datasets
  • πŸ“ˆ Database Indexing: Fast queries even with thousands of results
  • ⚑ Optimized Algorithms: Improved technical indicator calculations

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Run tests (python -m pytest tests/ -v)
  4. Commit changes (git commit -m 'Add amazing feature')
  5. Push to branch (git push origin feature/amazing-feature)
  6. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • TradingView for Pine Script inspiration
  • The pandas and numpy communities for excellent data tools
  • The quantitative finance community for strategy insights

Method 1: Use default strategy

strategy = create_default_strategy() results = strategy.run_strategy(df) trades = strategy.executed_trades

Method 2: Create custom strategy

custom_strategy = create_custom_strategy( smooth_type="SMA", enable_shorts=False, sl_percent_long=2.5, use_rsi_filter=True ) results = custom_strategy.run_strategy(df)


### Modular Usage

```python
# Use individual components
from models import StrategyParams
from indicators import TechnicalIndicatorCalculator
from signals import SignalGenerator
from trading import TradeSimulator

# Create configuration
params = StrategyParams(smooth_type="EMA", use_rsi_filter=True)

# Use components individually
indicator_calc = TechnicalIndicatorCalculator()
df_with_indicators = indicator_calc.calculate_all_indicators(df, params)

signal_gen = SignalGenerator()
df_with_signals = signal_gen.generate_all_signals(df_with_indicators, params)

trade_sim = TradeSimulator()
final_results = trade_sim.simulate_trades(df_with_signals, params)

πŸ—οΈ Architecture & Design Patterns

The codebase follows SOLID principles and clean architecture:

Key Components

  • Models: Immutable data classes (StrategyParams, TradeResult)
  • Indicators: Technical analysis calculations (RSI, EMA, SMA, ATR, ADX)
  • Signals: Trading signal generation logic
  • Trading: Trade simulation and execution
  • Strategy: Main orchestration and workflow

Design Patterns Applied

  • Factory Pattern: MovingAverageFactory for creating MA implementations
  • Strategy Pattern: Different moving average types (EMA/SMA)
  • Dependency Injection: ADX calculator receives ATR calculator
  • Service Layer: Clean separation between calculation, signaling, and simulation
  • Immutable Configuration: StrategyParams using frozen dataclasses

SOLID Principles

  • Single Responsibility: Each class has one clear purpose
  • Open/Closed: Strategy can be extended without modification
  • Liskov Substitution: Interfaces can be substituted
  • Interface Segregation: Clean, focused interfaces
  • Dependency Inversion: Depends on abstractions, not concretions

πŸ“Š Features

Current Implementation

  • βœ… Momentum strategy with EMA/SMA support
  • βœ… RSI, ADX, ATR, Bollinger Bands indicators
  • βœ… Long and short position support
  • βœ… Stop loss and take profit management
  • βœ… Modular, testable architecture
  • βœ… Type hints and documentation

Planned Features

  • πŸ”„ 26 additional technical indicators
  • πŸ”„ Multi-timeframe support
  • πŸ”„ Portfolio backtesting
  • πŸ”„ Optimization engine
  • οΏ½ Performance analytics
  • πŸ”„ Web dashboard

πŸ§ͺ Testing

Run the demo to see the modular structure in action:

python demo.py

Run unit tests:

python -m pytest tests/

πŸ“¦ Dependencies

  • pandas: Data manipulation and analysis
  • numpy: Numerical computing
  • typing: Type hints support

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Follow the existing architecture patterns
  4. Add tests for new functionality
  5. Submit a pull request

πŸ“„ License

This project is licensed under the MIT License.

Module 1: Pine Script Conversion

  • Convert a library of 26 indicators, including custom-coded scripts and standard indicators with specific configurations
  • Must match TradingView calculations exactly
  • Full documentation provided for all indicators

Module 2: Backtesting Engine

  • Test combinations of leading + confirmation indicators
  • Support multiple timeframes (5m, 15m, 1h, 1d)
  • Custom timeframe support (e.g., 13min, 45min)
  • Calculate: Return %, Win Rate, Profit Factor, Max Drawdown
  • Commission/slippage settings

Module 3: Optimization System

  • Test all indicator combinations across 50+ stocks
  • Output: Ticker | Timeframe | Strategy Config | PF | WR | DD
  • Efficient parallel processing

Module 4: Results & Analysis

  • Export results to CSV/Database
  • Simple GUI or web dashboard
  • Show best strategy for each stock
  • Optional: Scheduled daily/weekly runs

πŸ—οΈ Architecture & Design Patterns

Code Quality Features

  • βœ… Type hints: All functions include proper type annotations
  • βœ… Docstrings: Comprehensive documentation for all classes/methods
  • βœ… Unit tests: Full test coverage with pytest and mocking
  • βœ… Immutable config: StrategyParams prevents accidental mutations
  • βœ… Error handling: Proper validation and error messages
  • βœ… Clean interfaces: Abstract base classes and protocols
  • βœ… SOLID principles: Each class has single responsibility

Usage Example

from strategy import create_custom_strategy

# Create strategy with custom parameters
strategy = create_custom_strategy(
    smooth_type="EMA",
    enable_shorts=True,
    sl_percent_long=2.5,
    use_rsi_filter=True
)

# Run strategy on OHLCV data
result_df = strategy.run_strategy(df)
trades = strategy.executed_trades

print(f"Total trades: {len(trades)}")
for trade in trades:
    print(f"{trade.position_type}: {trade.pnl:.2%} PnL")

Development Setup

# Install dependencies
pip install -r requirements.txt

# Run tests
pytest tests/ -v

# Code formatting
black strategy/ tests/
ruff check strategy/ tests/
mypy strategy/

About

PineScript-to-Python is a simple tool that converts TradingView Pine Script strategies into executable Python code using libraries like pandas and ta, enabling backtesting and strategy analysis outside TradingView.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages