Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
slowpoke111 committed Nov 9, 2024
1 parent ad2ccb0 commit 52143db
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
7 changes: 5 additions & 2 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from .backtest import Backtest, TradeType, Holding, Transaction
from .backtest import Backtest
from .strategy import Strategy
from .types import TradeType, Holding, Transaction, Order
from .utils import (
calculateSMA,
calculateEMA,
Expand All @@ -15,15 +17,16 @@
getCrossSignals
)


__version__ = "1.0.1"
__author__ = "Ben Bell"

__all__ = [
"Backtest",
"Strategy",
"TradeType",
"Holding",
"Transaction",
"Order",
"calculateSMA",
"calculateEMA",
"calculateRSI",
Expand Down
70 changes: 37 additions & 33 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ A Python framework for backtesting trading strategies with support for multiple
- [x] Market Orders (Buy/Sell)
- [x] Limit Orders
- [x] GTC (Good Till Cancelled) Orders
- [x] Short
- [x] Commission Handling
- [x] Flat Fee
- [x] Percentage
- [x] Per Share
- [x] Flat Fee
- [x] Percentage
- [x] Per Share

### Technical Indicators
- [x] Simple Moving Average (SMA)
Expand All @@ -20,12 +21,15 @@ A Python framework for backtesting trading strategies with support for multiple
- [x] Bollinger Bands
- [x] MACD (Moving Average Convergence Divergence)
- [x] Crossover Detection
- [x] Volume Weighted Average Price (VWAP)
- [x] Average True Range (ATR)

### Portfolio Management
- [x] Position Tracking
- [x] Transaction History
- [x] Portfolio Valuation
- [x] Cash Management
- [x] Performance Metrics

## Example

Expand All @@ -38,40 +42,40 @@ from datetime import datetime
import pandas as pd

def maxBuyableShares(price: float, cash: float) -> int:
return int(cash / price)
return int(cash / price)

class SimpleMovingAverageCrossover(Strategy):
def setup(self) -> None:
self.sma20:pd.Series = calculateSMA(self.data['Close'], 20)
self.sma50:pd.Series = calculateSMA(self.data['Close'], 50)

def setup(self) -> None:
self.sma20:pd.Series = calculateSMA(self.data['Close'], 20)
self.sma50:pd.Series = calculateSMA(self.data['Close'], 50)

def next(self, row: pd.Series) -> None:
if len(self.data['Close']) < 51:
return
def next(self, row: pd.Series) -> None:
if len(self.data['Close']) < 51:
return

current_price:float = row['Close']
current_sma20:float = self.sma20[row.name]
current_sma50:float = self.sma50[row.name]
position:int = self.get_position()
current_price:float = row['Close']
current_sma20:float = self.sma20[row.name]
current_sma50:float = self.sma50[row.name]
position:int = self.get_position()

if current_sma20 > current_sma50:
numShares = maxBuyableShares(current_price, self.backtest.cash)
if numShares > 0:
self.backtest.trade(TradeType.BUY, numShares, current_price)

elif current_sma20 < current_sma50 and position > 0:
sharesToSell = max(1, position // 3)
self.backtest.trade(TradeType.SELL, sharesToSell, current_price)

if current_sma20 > current_sma50:
numShares = maxBuyableShares(current_price, self.backtest.cash)
if numShares > 0:
self.backtest.trade(TradeType.BUY, numShares, current_price)
elif current_sma20 < current_sma50 and position > 0:
sharesToSell = max(1, position // 3)
self.backtest.trade(TradeType.SELL, sharesToSell, current_price)
if __name__ == "__main__":
backtest = pbt.Backtest(
"PG",
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}")
backtest = pbt.Backtest(
"PG",
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}")
```

0 comments on commit 52143db

Please sign in to comment.