A backtesting framework for stock price prediction strategies
Project description
Stock Trading Backtest Framework
A Python framework for backtesting trading strategies with support for multiple order types and technical indicators.
Features
Trading Operations
- Market Orders (Buy/Sell)
- Limit Orders
- GTC (Good Till Cancelled) Orders
- Commission Handling
- Flat Fee
- Percentage
- Per Share
Technical Indicators
- Simple Moving Average (SMA)
- Exponential Moving Average (EMA)
- Relative Strength Index (RSI)
- Bollinger Bands
- MACD (Moving Average Convergence Divergence)
- Crossover Detection
Portfolio Management
- Position Tracking
- Transaction History
- Portfolio Valuation
- Cash Management
Example
import pyBacktest as pbt
from pyBacktest.strategy import Strategy
from pyBacktest.utils import calculateSMA
from datetime import datetime
import pandas as pd
def maxBuyableShares(price: float, cash: float) -> int:
return int(cash / price)
class SimpleMovingAverageCrossover(Strategy):
def setup(self) -> None:
self.sma20 = calculateSMA(self.data['Close'], 20)
def next(self, row: pd.Series) -> None:
if len(self.data) < 20:
return
current_price = row['Close']
current_sma = self.sma20[row.name]
position = self.get_position()
if position == 0 and current_price > current_sma:
self.backtest.trade(pbt.TradeType.MARKET_BUY, maxBuyableShares(current_price, self.backtest.cash))
elif position > 0 and current_price < current_sma:
self.backtest.trade(pbt.TradeType.MARKET_SELL, self.get_position())
if __name__ == "__main__":
backtest = pbt.Backtest(
"AAPL",
1000.0,
strategy=SimpleMovingAverageCrossover(),
startDate=datetime(2023, 1, 1),
endDate=datetime(2024, 1, 1)
)
results = backtest.run()
print(f"Final Portfolio Value: ${results['final_value']:,.2f}")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pybacktest-0.0.8.tar.gz
(11.8 kB
view details)
Built Distribution
File details
Details for the file pybacktest-0.0.8.tar.gz
.
File metadata
- Download URL: pybacktest-0.0.8.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41b950533ada46a2beade71b870fc80445c3f132b6e2c2a9a2b9e5d14b767b92 |
|
MD5 | 918bb3bc655d4161d18562883f684ae8 |
|
BLAKE2b-256 | 83e6942b4b693d108a5cc937d0bc28c94ed3aadc3283980e1fb2f69f959cb73b |
File details
Details for the file pyBacktest-0.0.8-py3-none-any.whl
.
File metadata
- Download URL: pyBacktest-0.0.8-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50424f2f0869e75611fd8c410ee8563a248f13837dfbf5319382178ce898a64c |
|
MD5 | 438d28bdf086f75c62e22e795e2f3cbd |
|
BLAKE2b-256 | 0cdc2241fea666fd2b1d19fd234233144d7e083775ba34747c0e173f8f55f717 |