Skip to main content
hyperdrive: an algorithmic trading library

Build Pipeline Dev Pipeline New Release Downloads PyPI

hyperdrive is an algorithmic trading library that powers quant research firm   Algotrade.io.

Unlike other backtesting libraries, hyperdrive specializes in data collection and quantitative research.

In the examples below, we explore how to:

  1. store market data
  2. create trading strategies
  3. test strategies against historical data (backtesting)
  4. execute orders.

Getting Started

Prerequisites

You will need Python 3.8+

Installation

To install the necessary packages, run

pythom -m pip install hyperdrive -U

Examples

Most secrets must be passed as environment variables. Future updates will allow secrets to be passed directly into class object (see example on order execution).

1. Storing data

Pre-requisites:

  • a Polygon API key
  • an AWS account and an S3 bucket

Environment Variables:

  • POLYGON
  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_DEFAULT_REGION
  • S3_BUCKET
from hyperdrive import DataSource
from DataSource import Polygon, MarketData

# Polygon API token loaded as an environment variable (os.environ['POLYGON'])

symbol = 'TSLA'
timeframe = '7d'

md = MarketData()
poly = Polygon()

poly.save_ohlc(symbol=symbol, timeframe=timeframe)
df = md.get_ohlc(symbol=symbol, timeframe=timeframe)

print(df)

Output:

           Time     Open       High      Low    Close       Vol
2863 2021-11-10  1010.41  1078.1000   987.31  1067.95  42802722
2864 2021-11-11  1102.77  1104.9700  1054.68  1063.51  22396568
2865 2021-11-12  1047.50  1054.5000  1019.20  1033.42  25573148
2866 2021-11-15  1017.63  1031.9800   978.60  1013.39  34775649
2867 2021-11-16  1003.31  1057.1999  1002.18  1054.73  26542359

2. Creating a model

Much of this code is still closed-source, but you can take a look at the Historian class in the History module for some ideas.

3. Backtesting a strategy

We use vectorbt to backtest strategies.

from hyperdrive import History, DataSource, Constants as C
from History import Historian
from DataSource import MarketData

hist = Historian()
md = MarketData()

symbol = 'TSLA'
timeframe = '1y'

df = md.get_ohlc(symbol=symbol, timeframe=timeframe)

holding = hist.from_holding(df[C.CLOSE])
signals = hist.get_optimal_signals(df[C.CLOSE])
my_strat = hist.from_signals(df[C.CLOSE], signals)

metrics = [
    'Total Return [%]', 'Benchmark Return [%]',
    'Max Drawdown [%]', 'Max Drawdown Duration',
    'Total Trades', 'Win Rate [%]', 'Avg Winning Trade [%]',
    'Avg Losing Trade [%]', 'Profit Factor',
    'Expectancy', 'Sharpe Ratio', 'Calmar Ratio',
    'Omega Ratio', 'Sortino Ratio'
]

holding_stats = holding.stats()[metrics]
my_strat_stats = my_strat.stats()[metrics]

print(f'Buy and Hold Strat\n{"-"*42}')
print(holding_stats)

print(f'My Strategy\n{"-"*42}')
print(my_strat_stats)

# holding.plot()
my_strat.plot()

Output:

Buy and Hold Strat
------------------------------------------
Total Return [%]                138.837436
Benchmark Return [%]            138.837436
Max Drawdown [%]                 36.246589
Max Drawdown Duration    186 days 00:00:00
Total Trades                             1
Win Rate [%]                           NaN
Avg Winning Trade [%]                  NaN
Avg Losing Trade [%]                   NaN
Profit Factor                          NaN
Expectancy                             NaN
Sharpe Ratio                      2.206485
Calmar Ratio                      6.977133
Omega Ratio                       1.381816
Sortino Ratio                     3.623509
Name: Close, dtype: object

My Strategy
------------------------------------------
Total Return [%]                364.275727
Benchmark Return [%]            138.837436
Max Drawdown [%]                  35.49422
Max Drawdown Duration    122 days 00:00:00
Total Trades                             6
Win Rate [%]                          80.0
Avg Winning Trade [%]            52.235227
Avg Losing Trade [%]             -3.933059
Profit Factor                     45.00258
Expectancy                      692.157004
Sharpe Ratio                      4.078172
Calmar Ratio                     23.220732
Omega Ratio                       2.098986
Sortino Ratio                     7.727806
Name: Close, dtype: object

4. Executing an order

Pre-requisites:

  • a Binance.US API key

Environment Variables:

  • BINANCE
from pprint import pprint
from hyperdrive import Exchange
from Exchange import Binance

# Binance API token loaded as an environment variable (os.environ['BINANCE'])

bn = Binance()

# use 45% of your USD account balance to buy BTC
order = bn.order('BTC', 'USD', 'BUY', 0.45)

pprint(order)

Output:

{'clientOrderId': '3cfyrJOSXqq6Zl1RJdeRRC',
 'cummulativeQuoteQty': 46.8315,
 'executedQty': 0.000757,
 'fills': [{'commission': '0.0500',
            'commissionAsset': 'USD',
            'price': '61864.6400',
            'qty': '0.00075700',
            'tradeId': 25803914}],
 'orderId': 714855908,
 'orderListId': -1,
 'origQty': 0.000757,
 'price': 0.0,
 'side': 'SELL',
 'status': 'FILLED',
 'symbol': 'BTCUSD',
 'timeInForce': 'GTC',
 'transactTime': 1637030680121,
 'type': 'MARKET'}

Use

Use the scripts provided in the scripts/ directory as a reference since they are actually used in production daily.

Available data collection functions:

  • Symbols (from Robinhood)
  • OHLC (from Polygon)
  • Intraday (from Polygon)
  • Dividends (from Polygon)
  • Splits (from Polygon)
  • Unemployment (from the Bureau of Labor Statistics)


Metadata

Release files for hyperdrive 1.15.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hyperdrive 1.15.3
File Size Uploaded
hyperdrive-1.15.3.tar.gz 44.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hyperdrive 1.15.3
File Interpreter ABI Platform
hyperdrive-1.15.3-py3-none-any.whl Python 3 none any Details

Total release size: 78.9 kB

Release files / hyperdrive-1.15.3.tar.gz

Download URL hyperdrive-1.15.3.tar.gz
Size 44.9 kB
Tags Source
SHA-256 checksum
How to use checksums
90e8b6153660da9c13c4e196db1bcee93504edbcf5bf4b500bb7a3cf4df10938
BLAKE2b-256 checksum
How to use checksums
56c47ba4ddd2a8cd08e505f001e1404edac5f63dc7ccb74ac8f3d37cf8fef1ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.13

Release files / hyperdrive-1.15.3-py3-none-any.whl

Download URL hyperdrive-1.15.3-py3-none-any.whl
Size 34.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
346e74ddd95711f33c3d06a102f9518afae1421a8cc89150db33d96d938d4ec0
BLAKE2b-256 checksum
How to use checksums
25f55a663ea9f712b76983d0d45022a916b63ddde924a496bbad74a606b25775
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.13

Release history Release notifications | RSS feed

This release

1.15.3 This release

2 release files

1.15.2

2 release files

1.15.1

2 release files

1.13.2

2 release files

1.13.1

2 release files

1.13.0

2 release files

1.10.7

2 release files

1.10.4

2 release files

1.10.3

2 release files

1.10.0

2 release files

1.9.6

2 release files

1.9.5

2 release files

1.9.4

2 release files

1.9.3

2 release files

1.9.2

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.1

2 release files

1.7.0

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.57.0

2 release files

0.56.0

2 release files

0.51.3

2 release files

0.51.2

2 release files

0.51.1

2 release files

0.50.2

2 release files

0.50.1

2 release files

0.50.0

2 release files

0.47.2

2 release files

0.47.1

2 release files

0.47.0

2 release files

0.42.0

2 release files

0.41.0

2 release files

0.40.0

2 release files

0.39.0

2 release files

0.38.0

2 release files

0.37.0

2 release files

0.36.0

2 release files

0.35.0

2 release files

0.34.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page