Skip to content

Commit

Permalink
add backtest docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Graeme22 committed Sep 30, 2024
1 parent 576d2d2 commit 703f8e4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
35 changes: 35 additions & 0 deletions docs/backtest.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Backtesting
===========

Backtesting is a beta feature recently added to Tastytrade, so please report any issues!

Let's see how to perform a backtest:

.. code-block:: python
from datetime import timedelta
from tastytrade import (
Backtest,
BacktestEntry,
BacktestExit,
BacktestLeg,
BacktestSession,
today_in_new_york
)
from tqdm.asyncio import tqdm # progress bar
backtest_session = BacktestSession(session)
backtest = Backtest(
symbol="SPY",
entry_conditions=BacktestEntry(),
exit_conditions=BacktestExit(at_days_to_expiration=21),
legs=[BacktestLeg(), BacktestLeg(side="put")],
start_date=today_in_new_york() - timedelta(days=365)
)
results = [r async for r in tqdm(backtest_session.run(backtest))]
print(results[-1])
.. note::
As of the time of this writing, the end date of a backtest must be on or before July 31, 2024.

There are lots of configuration options you can find in the documentation for each class.
The ``run`` function is an ``AsyncGenerator``, so you can do something else while a backtest is running, show a progress bar, or maybe even run several at once!
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ A simple, reverse-engineered SDK for Tastytrade built on their (now mostly publi
orders
account-streamer
data-streamer
backtest
watchlists

.. toctree::
Expand Down
6 changes: 6 additions & 0 deletions docs/tastytrade.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Account
:members:
:show-inheritance:

Backtesting
-----------
.. automodule:: tastytrade.backtest
:members:
:show-inheritance:

Instruments
-----------
.. automodule:: tastytrade.instruments
Expand Down
22 changes: 22 additions & 0 deletions tastytrade/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
# ruff: noqa: E402

from .account import Account
from .backtest import (
Backtest,
BacktestEntry,
BacktestExit,
BacktestLeg,
BacktestResponse,
BacktestResults,
BacktestSession,
BacktestSnapshot,
BacktestStatistics,
BacktestTrial,
)
from .dxfeed import EventType
from .instruments import (
Cryptocurrency,
Expand Down Expand Up @@ -61,6 +73,16 @@
"Account",
"AlertStreamer",
"AlertType",
"Backtest",
"BacktestEntry",
"BacktestExit",
"BacktestLeg",
"BacktestResponse",
"BacktestResults",
"BacktestSession",
"BacktestSnapshot",
"BacktestStatistics",
"BacktestTrial",
"ComplexOrderType",
"Cryptocurrency",
"DXLinkStreamer",
Expand Down

0 comments on commit 703f8e4

Please sign in to comment.