Remove deprecated modules and files related to the backtesting framework, including backtest.py, cli.py, config.py, data.py, intrabar.py, logging_utils.py, market_costs.py, metrics.py, trade.py, and supertrend indicators. Introduce a new structure for the backtesting engine with improved organization and functionality, including a CLI handler, data manager, and reporting capabilities. Update dependencies in pyproject.toml to support the new architecture.
This commit is contained in:
97
strategies/examples.py
Normal file
97
strategies/examples.py
Normal file
@@ -0,0 +1,97 @@
|
||||
"""
|
||||
Example trading strategies for backtesting.
|
||||
|
||||
These are simple strategies demonstrating the framework usage.
|
||||
"""
|
||||
import pandas as pd
|
||||
import vectorbt as vbt
|
||||
|
||||
from engine.market import MarketType
|
||||
from strategies.base import BaseStrategy
|
||||
|
||||
|
||||
class RsiStrategy(BaseStrategy):
|
||||
"""
|
||||
RSI mean-reversion strategy.
|
||||
|
||||
Long entry when RSI crosses below oversold level.
|
||||
Long exit when RSI crosses above overbought level.
|
||||
"""
|
||||
default_market_type = MarketType.SPOT
|
||||
default_leverage = 1
|
||||
|
||||
def run(
|
||||
self,
|
||||
close: pd.Series,
|
||||
period: int = 14,
|
||||
rsi_lower: int = 30,
|
||||
rsi_upper: int = 70,
|
||||
**kwargs
|
||||
) -> tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, pd.DataFrame]:
|
||||
"""
|
||||
Generate RSI-based trading signals.
|
||||
|
||||
Args:
|
||||
close: Price series
|
||||
period: RSI calculation period
|
||||
rsi_lower: Oversold threshold (buy signal)
|
||||
rsi_upper: Overbought threshold (sell signal)
|
||||
|
||||
Returns:
|
||||
4-tuple of (long_entries, long_exits, short_entries, short_exits)
|
||||
"""
|
||||
# Calculate RSI
|
||||
rsi = vbt.RSI.run(close, window=period)
|
||||
|
||||
# Long signals: buy oversold, sell overbought
|
||||
long_entries = rsi.rsi_crossed_below(rsi_lower)
|
||||
long_exits = rsi.rsi_crossed_above(rsi_upper)
|
||||
|
||||
# No short signals for this strategy (spot-focused)
|
||||
short_entries = BaseStrategy.create_empty_signals(long_entries)
|
||||
short_exits = BaseStrategy.create_empty_signals(long_entries)
|
||||
|
||||
return long_entries, long_exits, short_entries, short_exits
|
||||
|
||||
|
||||
class MaCrossStrategy(BaseStrategy):
|
||||
"""
|
||||
Moving Average crossover strategy.
|
||||
|
||||
Long entry when fast MA crosses above slow MA.
|
||||
Long exit when fast MA crosses below slow MA.
|
||||
"""
|
||||
default_market_type = MarketType.SPOT
|
||||
default_leverage = 1
|
||||
|
||||
def run(
|
||||
self,
|
||||
close: pd.Series,
|
||||
fast_window: int = 10,
|
||||
slow_window: int = 20,
|
||||
**kwargs
|
||||
) -> tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, pd.DataFrame]:
|
||||
"""
|
||||
Generate MA crossover trading signals.
|
||||
|
||||
Args:
|
||||
close: Price series
|
||||
fast_window: Fast MA period
|
||||
slow_window: Slow MA period
|
||||
|
||||
Returns:
|
||||
4-tuple of (long_entries, long_exits, short_entries, short_exits)
|
||||
"""
|
||||
# Calculate Moving Averages
|
||||
fast_ma = vbt.MA.run(close, window=fast_window)
|
||||
slow_ma = vbt.MA.run(close, window=slow_window)
|
||||
|
||||
# Long signals
|
||||
long_entries = fast_ma.ma_crossed_above(slow_ma)
|
||||
long_exits = fast_ma.ma_crossed_below(slow_ma)
|
||||
|
||||
# No short signals for this strategy
|
||||
short_entries = BaseStrategy.create_empty_signals(long_entries)
|
||||
short_exits = BaseStrategy.create_empty_signals(long_entries)
|
||||
|
||||
return long_entries, long_exits, short_entries, short_exits
|
||||
Reference in New Issue
Block a user