mirror of
https://github.com/wassname/options_backtester.git
synced 2026-09-09 11:28:08 +08:00
Added simple asset backtester
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
from backtester import Backtest
|
||||
from signal import Signal, get_order
|
||||
@@ -0,0 +1,119 @@
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
import pyprind
|
||||
|
||||
from strategy.strategy import Strategy
|
||||
|
||||
|
||||
class Backtest:
|
||||
"""Processes signals from the Strategy object"""
|
||||
def __init__(self, schema):
|
||||
self.schema = schema
|
||||
self._strategy = None
|
||||
self._data = None
|
||||
|
||||
@property
|
||||
def strategy(self):
|
||||
return self._strategy
|
||||
|
||||
@strategy.setter
|
||||
def strategy(self, strat):
|
||||
assert isinstance(strat, Strategy)
|
||||
self._strategy = strat
|
||||
|
||||
@property
|
||||
def data(self):
|
||||
return self._data
|
||||
|
||||
@data.setter
|
||||
def data(self, data):
|
||||
self._data = data
|
||||
|
||||
def run(self, initial_capital=1_000_000):
|
||||
assert self._data is not None
|
||||
assert self._strategy is not None
|
||||
|
||||
self.current_capital = 0
|
||||
self.current_cash = initial_capital
|
||||
|
||||
self.inventory = pd.DataFrame(columns=['symbol', 'cost', 'qty'])
|
||||
self.balance = pd.DataFrame()
|
||||
|
||||
data_iterator = self._data.iter_dates()
|
||||
monthly_iterator = self._data.iter_months()
|
||||
|
||||
rebalancing_days = []
|
||||
for date, _ in monthly_iterator:
|
||||
rebalancing_days.append(date)
|
||||
|
||||
bar = pyprind.ProgBar(data_iterator.ngroups, bar_char='█')
|
||||
|
||||
self.balance = pd.DataFrame(
|
||||
{
|
||||
'capital': self.current_cash,
|
||||
'cash': self.current_cash
|
||||
},
|
||||
index=[self.data.start_date - pd.Timedelta(1, unit='day')])
|
||||
|
||||
for date, stocks in data_iterator:
|
||||
|
||||
if date in rebalancing_days:
|
||||
self.rebalance_portfolio(stocks)
|
||||
|
||||
self._update_balance(date, stocks)
|
||||
|
||||
bar.update()
|
||||
|
||||
self.balance['% change'] = self.balance['capital'].pct_change()
|
||||
self.balance['accumulated return'] = (
|
||||
1.0 + self.balance['% change']).cumprod()
|
||||
|
||||
return self.balance
|
||||
|
||||
def rebalance_portfolio(self, stocks):
|
||||
money_total = self.current_cash + self.current_capital
|
||||
for asset in self._strategy.assets:
|
||||
stock = stocks[stocks['symbol'] == asset.symbol]
|
||||
stock_price = stock[self.schema['Adj Close']].values[0]
|
||||
qty = (money_total * asset.percentage) // stock_price
|
||||
inventory_entry = self.inventory[self.inventory['symbol'] ==
|
||||
asset.symbol]
|
||||
self.inventory.drop(inventory_entry.index, inplace=True)
|
||||
update = pd.Series([asset.symbol, stock_price, qty])
|
||||
update.index = self.inventory.columns
|
||||
self.inventory = self.inventory.append(update, ignore_index=True)
|
||||
|
||||
# Update current cash
|
||||
invested_capital = sum(self.inventory['cost'] * self.inventory['qty'])
|
||||
self.current_cash = money_total - invested_capital
|
||||
|
||||
def _update_balance(self, date, stocks):
|
||||
"""Updates positions and calculates statistics for the current date.
|
||||
|
||||
Args:
|
||||
date (pd.Timestamp): Current date.
|
||||
stocks (pd.DataFrame): DataFrame of (daily/monthly) stocks.
|
||||
"""
|
||||
|
||||
costs = []
|
||||
|
||||
for asset in self._strategy.assets:
|
||||
asset_entry = stocks[stocks['symbol'] == asset.symbol]
|
||||
inventory_asset_entry = self.inventory[self.inventory['symbol'] ==
|
||||
asset.symbol]
|
||||
cost = asset_entry[self.schema['Adj Close']].values[0]
|
||||
qty = inventory_asset_entry['qty'].values[0]
|
||||
costs.append(cost * qty)
|
||||
|
||||
total_value = sum(costs)
|
||||
self.current_capital = total_value
|
||||
money_total = total_value + self.current_cash
|
||||
|
||||
row = pd.Series(
|
||||
{
|
||||
'total_value': total_value,
|
||||
'cash': self.current_cash,
|
||||
'capital': money_total,
|
||||
},
|
||||
name=date)
|
||||
self.balance = self.balance.append(row)
|
||||
@@ -0,0 +1,2 @@
|
||||
from .schema import *
|
||||
from .historical_stock_data import HistoricalStockData
|
||||
@@ -0,0 +1,80 @@
|
||||
import os
|
||||
from .schema import Schema
|
||||
import pandas as pd
|
||||
|
||||
|
||||
class HistoricalStockData:
|
||||
"""Historical Stock Data container class."""
|
||||
def __init__(self, file, schema=None, **params):
|
||||
if schema:
|
||||
assert isinstance(schema, Schema)
|
||||
else:
|
||||
self.schema = HistoricalStockData.default_schema()
|
||||
|
||||
file_extension = os.path.splitext(file)[1]
|
||||
|
||||
if file_extension == '.h5':
|
||||
self._data = pd.read_hdf(file, **params)
|
||||
elif file_extension == '.csv':
|
||||
params['parse_dates'] = [self.schema.date.mapping]
|
||||
self._data = pd.read_csv(file, **params)
|
||||
|
||||
columns = self._data.columns
|
||||
assert all((col in columns for _key, col in self.schema))
|
||||
|
||||
date_col = self.schema['date']
|
||||
|
||||
self.start_date = self._data[date_col].min()
|
||||
self.end_date = self._data[date_col].max()
|
||||
|
||||
def apply_filter(self, f):
|
||||
"""Apply Filter `f` to the data. Returns a `pd.DataFrame` with the filtered rows."""
|
||||
return self._data.query(f.query)
|
||||
|
||||
def iter_dates(self):
|
||||
"""Returns `pd.DataFrameGroupBy` that groups contracts by date"""
|
||||
return self._data.groupby(self.schema['date'])
|
||||
|
||||
def iter_months(self):
|
||||
"""Returns `pd.DataFrameGroupBy` that groups contracts by month"""
|
||||
date_col = self.schema['date']
|
||||
iterator = self._data.groupby(pd.Grouper(
|
||||
key=date_col, freq="MS")).apply(lambda g: g[g[date_col] == g[
|
||||
date_col].min()]).reset_index(drop=True).groupby(date_col)
|
||||
return iterator
|
||||
|
||||
def __getattr__(self, attr):
|
||||
"""Pass method invocation to `self._data`"""
|
||||
|
||||
method = getattr(self._data, attr)
|
||||
if hasattr(method, '__call__'):
|
||||
|
||||
def df_method(*args, **kwargs):
|
||||
return method(*args, **kwargs)
|
||||
|
||||
return df_method
|
||||
else:
|
||||
return method
|
||||
|
||||
def __getitem__(self, item):
|
||||
if isinstance(item, pd.Series):
|
||||
return self._data[item]
|
||||
else:
|
||||
key = self.schema[item]
|
||||
return self._data[key]
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self._data[key] = value
|
||||
if key not in self.schema:
|
||||
self.schema.update({key: key})
|
||||
|
||||
def __len__(self):
|
||||
return len(self._data)
|
||||
|
||||
def __repr__(self):
|
||||
return self._data.__repr__()
|
||||
|
||||
def default_schema():
|
||||
"""Returns default schema for Historical Options Data"""
|
||||
schema = Schema.canonical()
|
||||
return schema
|
||||
@@ -0,0 +1,163 @@
|
||||
class Schema:
|
||||
"""Data schema class.
|
||||
Used to run validations and provide uniform access to fields in the data set.
|
||||
"""
|
||||
|
||||
columns = [
|
||||
"symbol", "date", "open", "close", "high", "low", "volume", "Adj Close"
|
||||
]
|
||||
|
||||
def canonical():
|
||||
"""Builder method that returns a `Schema` with default mappings"""
|
||||
mappings = {key: key for key in Schema.columns}
|
||||
return Schema(mappings)
|
||||
|
||||
def __init__(self, mappings):
|
||||
assert all((key in mappings for key in Schema.columns))
|
||||
|
||||
self._mappings = mappings
|
||||
|
||||
def update(self, mappings):
|
||||
"""Update schema according to given `mappings`"""
|
||||
self._mappings.update(mappings)
|
||||
return self
|
||||
|
||||
def __contains__(self, key):
|
||||
"""Returns True if key is in schema"""
|
||||
return key in self._mappings.keys()
|
||||
|
||||
def __getattr__(self, key):
|
||||
"""Returns Field object used to build Filters"""
|
||||
return Field(key, self._mappings[key])
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self._mappings[key] = value
|
||||
|
||||
def __getitem__(self, key):
|
||||
"""Returns mapping of given `key`"""
|
||||
return self._mappings[key]
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._mappings.items())
|
||||
|
||||
def __repr__(self):
|
||||
return "Schema({})".format(
|
||||
[Field(k, m) for k, m in self._mappings.items()])
|
||||
|
||||
def __eq__(self, other):
|
||||
return self._mappings == other._mappings
|
||||
|
||||
|
||||
class Field:
|
||||
"""Encapsulates data fields to build filters used by strategies"""
|
||||
|
||||
__slots__ = ("name", "mapping")
|
||||
|
||||
def __init__(self, name, mapping):
|
||||
self.name = name
|
||||
self.mapping = mapping
|
||||
|
||||
def _create_filter(self, op, other):
|
||||
if isinstance(other, Field):
|
||||
query = Field._format_query(self.mapping, op, other.mapping)
|
||||
else:
|
||||
query = Field._format_query(self.mapping, op, other)
|
||||
return Filter(query)
|
||||
|
||||
def _combine_fields(self, op, other, invert=False):
|
||||
if isinstance(other, Field):
|
||||
name = Field._format_query(self.name, op, other.name, invert)
|
||||
mapping = Field._format_query(self.mapping, op, other.mapping,
|
||||
invert)
|
||||
elif isinstance(other, (int, float)):
|
||||
name = Field._format_query(self.name, op, other, invert)
|
||||
mapping = Field._format_query(self.mapping, op, other, invert)
|
||||
else:
|
||||
raise TypeError
|
||||
|
||||
return Field(name, mapping)
|
||||
|
||||
def _format_query(left, op, right, invert=False):
|
||||
if invert:
|
||||
left, right = right, left
|
||||
query = "{left} {op} {right}".format(left=left, op=op, right=right)
|
||||
return query
|
||||
|
||||
def __add__(self, value):
|
||||
return self._combine_fields("+", value)
|
||||
|
||||
def __radd__(self, value):
|
||||
return self._combine_fields("+", value, invert=True)
|
||||
|
||||
def __sub__(self, value):
|
||||
return self._combine_fields("-", value)
|
||||
|
||||
def __rsub__(self, value):
|
||||
return self._combine_fields("-", value, invert=True)
|
||||
|
||||
def __mul__(self, value):
|
||||
return self._combine_fields("*", value)
|
||||
|
||||
def __rmul__(self, value):
|
||||
return self._combine_fields("*", value, invert=True)
|
||||
|
||||
def __truediv__(self, value):
|
||||
return self._combine_fields("/", value)
|
||||
|
||||
def __rtruediv__(self, value):
|
||||
return self._combine_fields("/", value, invert=True)
|
||||
|
||||
def __lt__(self, value):
|
||||
return self._create_filter("<", value)
|
||||
|
||||
def __le__(self, value):
|
||||
return self._create_filter("<=", value)
|
||||
|
||||
def __gt__(self, value):
|
||||
return self._create_filter(">", value)
|
||||
|
||||
def __ge__(self, value):
|
||||
return self._create_filter(">=", value)
|
||||
|
||||
def __eq__(self, value):
|
||||
if isinstance(value, str):
|
||||
value = "'{}'".format(value)
|
||||
return self._create_filter("==", value)
|
||||
|
||||
def __ne__(self, value):
|
||||
return self._create_filter("!=", value)
|
||||
|
||||
def __repr__(self):
|
||||
return "Field(name='{}', mapping='{}')".format(self.name, self.mapping)
|
||||
|
||||
|
||||
class Filter:
|
||||
"""This class determines entry/exit conditions for strategies"""
|
||||
|
||||
__slots__ = ("query")
|
||||
|
||||
def __init__(self, query):
|
||||
self.query = query
|
||||
|
||||
def __and__(self, other):
|
||||
"""Returns logical *and* between `self` and `other`"""
|
||||
assert isinstance(other, Filter)
|
||||
new_query = "({}) & ({})".format(self.query, other.query)
|
||||
return Filter(query=new_query)
|
||||
|
||||
def __or__(self, other):
|
||||
"""Returns logical *or* between `self` and `other`"""
|
||||
assert isinstance(other, Filter)
|
||||
new_query = "(({}) | ({}))".format(self.query, other.query)
|
||||
return Filter(query=new_query)
|
||||
|
||||
def __invert__(self):
|
||||
"""Negates filter"""
|
||||
return Filter("!({})".format(self.query))
|
||||
|
||||
def __call__(self, data):
|
||||
"""Returns dataframe of filtered data"""
|
||||
return data.eval(self.query)
|
||||
|
||||
def __repr__(self):
|
||||
return "Filter(query='{}')".format(self.query)
|
||||
@@ -0,0 +1 @@
|
||||
from .charts import returns_chart
|
||||
@@ -0,0 +1,48 @@
|
||||
"""Generates charts from a portfolio report"""
|
||||
|
||||
import altair as alt
|
||||
|
||||
|
||||
def returns_chart(report):
|
||||
# Time interval selector
|
||||
time_interval = alt.selection(type='interval', encodings=['x'])
|
||||
|
||||
# Area plot
|
||||
areas = alt.Chart().mark_area(opacity=0.7).encode(
|
||||
x='index:T',
|
||||
y=alt.Y('accumulated return:Q', axis=alt.Axis(format='%')))
|
||||
|
||||
# Nearest point selector
|
||||
nearest = alt.selection(type='single',
|
||||
nearest=True,
|
||||
on='mouseover',
|
||||
fields=['index'],
|
||||
empty='none')
|
||||
|
||||
points = areas.mark_point().encode(
|
||||
opacity=alt.condition(nearest, alt.value(1), alt.value(0)))
|
||||
|
||||
# Transparent date selector
|
||||
selectors = alt.Chart().mark_point().encode(
|
||||
x='index:T',
|
||||
opacity=alt.value(0),
|
||||
).add_selection(nearest)
|
||||
|
||||
text = areas.mark_text(
|
||||
align='left', dx=5, dy=-5).encode(text=alt.condition(
|
||||
nearest, 'accumulated return:Q', alt.value(' '), format='.2%'))
|
||||
|
||||
layered = alt.layer(selectors,
|
||||
points,
|
||||
text,
|
||||
areas.encode(
|
||||
alt.X('index:T',
|
||||
axis=alt.Axis(title='date'),
|
||||
scale=alt.Scale(domain=time_interval))),
|
||||
width=700,
|
||||
height=350,
|
||||
title='Wealth over time')
|
||||
|
||||
lower = areas.properties(width=700, height=70).add_selection(time_interval)
|
||||
|
||||
return alt.vconcat(layered, lower, data=report.reset_index())
|
||||
@@ -0,0 +1,2 @@
|
||||
from .strategy import Strategy
|
||||
from .asset import Asset
|
||||
@@ -0,0 +1,16 @@
|
||||
from .direction import Direction
|
||||
from datahandler.schema import Schema
|
||||
|
||||
|
||||
class Asset:
|
||||
"""Strategy Leg data class"""
|
||||
def __init__(self, symbol, percentage, direction=Direction.BUY):
|
||||
assert isinstance(direction, Direction)
|
||||
|
||||
self.symbol = symbol
|
||||
self.percentage = percentage
|
||||
self.direction = direction
|
||||
|
||||
def __repr__(self):
|
||||
return "Asset(symbol={}, percentage={}, direction={})".format(
|
||||
self.symbol, self.percentage, self.direction)
|
||||
@@ -0,0 +1,10 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class Direction(Enum):
|
||||
BUY = 'ask' # Schema field for BUY price
|
||||
SELL = 'bid' # Schema field for SELL price
|
||||
|
||||
def __invert__(self):
|
||||
flip = Direction.SELL if self == Direction.BUY else Direction.BUY
|
||||
return flip
|
||||
@@ -0,0 +1,37 @@
|
||||
import math
|
||||
from functools import reduce
|
||||
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
|
||||
from .direction import Direction
|
||||
from .asset import Asset
|
||||
|
||||
|
||||
class Strategy:
|
||||
def __init__(self, direction=Direction.BUY):
|
||||
assert isinstance(direction, Direction)
|
||||
self.direction = direction
|
||||
self.assets = []
|
||||
|
||||
def add_asset(self, asset):
|
||||
"""Adds asset to the strategy"""
|
||||
assert isinstance(asset, Asset)
|
||||
self.assets.append(asset)
|
||||
return self
|
||||
|
||||
def add_assets(self, assets):
|
||||
"""Adds assets to the strategy"""
|
||||
for asset in assets:
|
||||
self.add_asset(asset)
|
||||
return self
|
||||
|
||||
def remove_asset(self, asset_number):
|
||||
"""Removes asset from the strategy"""
|
||||
self.assets.pop(asset_number)
|
||||
return self
|
||||
|
||||
def clear_assets(self):
|
||||
"""Removes *all* assets from the strategy"""
|
||||
self.assets = []
|
||||
return self
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user