Skip to content

Commit

Permalink
Merge pull request #4 from bsdz/asset_field_extensions
Browse files Browse the repository at this point in the history
Asset field extensions + strategy runner refactor
  • Loading branch information
bsdz authored Mar 29, 2024
2 parents f528476 + e3de8ea commit 4bb1193
Show file tree
Hide file tree
Showing 27 changed files with 12,186 additions and 11,936 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 88
extend-ignore = E203, E704, E741, F401
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ celerybeat.pid
# Environments
.env
.venv
.venv.*
env/
venv/
ENV/
Expand Down
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ Below is an example usage (the performance of the example strategy won't be good
```python
import pandas as pd

from yabte.backtest import Strategy, StrategyRunner, Order, Book
from yabte.utilities.plot.plotly.strategy_runner import plot_strategy_runner
from yabte.utilities.strategy_helpers import crossover
from yabte.backtest import Book, SimpleOrder, Strategy, StrategyRunner
from yabte.tests._helpers import generate_nasdaq_dataset
from yabte.utilities.plot.plotly.strategy_runner import plot_strategy_runner_result
from yabte.utilities.strategy_helpers import crossover


class SMAXO(Strategy):
Expand Down Expand Up @@ -64,9 +64,9 @@ class SMAXO(Strategy):
data = df.loc[ix_2d, ("CloseSMAShort", "CloseSMALong")].dropna()
if len(data) == 2:
if crossover(data.CloseSMAShort, data.CloseSMALong):
self.orders.append(Order(asset_name=symbol, size=-100))
self.orders.append(SimpleOrder(asset_name=symbol, size=-100))
elif crossover(data.CloseSMALong, data.CloseSMAShort):
self.orders.append(Order(asset_name=symbol, size=100))
self.orders.append(SimpleOrder(asset_name=symbol, size=100))


# load some data
Expand All @@ -79,18 +79,17 @@ book = Book(name="Main", cash="100000")
sr = StrategyRunner(
data=df_combined,
assets=assets,
strat_classes=[SMAXO],
strategies=[SMAXO()],
books=[book],
)
sr.run()
srr = sr.run()

# see the trades or book history
th = sr.transaction_history
bch = sr.book_history.loc[:, (slice(None), "cash")]
th = srr.transaction_history
bch = srr.book_history.loc[:, (slice(None), "cash")]

# plot the trades against book value
plot_strategy_runner(sr);

plot_strategy_runner_result(srr, sr)
```

![Output from code](https://raw.githubusercontent.com/bsdz/yabte/main/readme_image.png)
Expand Down
12 changes: 6 additions & 6 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ A typical strategy looks as follows:

.. code-block:: python
from yabte.backtest import Strategy, StrategyRunner, Order, Book, Asset
from yabte.backtest import Strategy, StrategyRunner, SimpleOrder, Book, OHLCAsset
from yabte.utilities.strategy_helpers import crossover
# All strategies inherit from the Strategy base class.
Expand Down Expand Up @@ -80,19 +80,19 @@ A typical strategy looks as follows:
# executed the next day. There are different types
# of order, e.g. positional and basket and some orders
# support hooks for running arbitrary checks for limits etc.
self.orders.append(Order(asset_name=symbol, size=-100))
self.orders.append(SimpleOrder(asset_name=symbol, size=-100))
elif crossover(data.CloseSMALong, data.CloseSMAShort):
self.orders.append(Order(asset_name=symbol, size=100))
self.orders.append(SimpleOrder(asset_name=symbol, size=100))
To run our strategy, we use a StrategyRunner:

.. code-block:: python
# The Asset class is any pricable object with accompanying High, Low, Close,
# Open & Volume field data. One can subclass Asset to support different field
# The OHLCAsset class is any pricable object with accompanying High, Low, Close,
# Open & Volume field data. One can subclass OHLCAsset to support different field
# types, e.g. Volatility.
assets = [Asset(name="GOOG", denom="USD"), Asset(name="MSFT", denom="USD")]
assets = [OHLCAsset(name="GOOG", denom="USD"), OHLCAsset(name="MSFT", denom="USD")]
sr = StrategyRunner(
# Data is provided as a pandas dataframe with two level
Expand Down
4 changes: 2 additions & 2 deletions docs/yabte.backtest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Strategy
:members:
:member-order: bysource

Order
SimpleOrder
-----
.. autoclass:: yabte.backtest.order.OrderStatus
:members:
Expand Down Expand Up @@ -39,7 +39,7 @@ Book
:members:
:member-order: bysource

Asset
OHLCAsset
-----

.. automodule:: yabte.backtest.asset
Expand Down
1,343 changes: 696 additions & 647 deletions notebooks/Delta_Hedging.ipynb

Large diffs are not rendered by default.

83 changes: 48 additions & 35 deletions notebooks/Portfolio_Optimization.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit 4bb1193

Please sign in to comment.