Skip to main content

An algorithmic trading framework for PyData.

Project description

What is margot?

Margot is a library of components that may be used together or separately. The first major component; margot.data is now available for public preview. It should be considered an early-beta. It works, but may still have sharp edges.

What is margot data?

Margot data makes it super easy to create neat and tidy Pandas dataframes for time-series analysis.

Margot data aims to manage data collection, caching, cleaning, feature generation, management and persistence using a clean, declarative API. If you've ever used Django you'll find this approach similar to the Django ORM.

Columns

The heart of any time-series dataframe is the original data. Margot can retrieve time-series data from external sources (currently AlphaVantage). To add a time- series from an original source, such as "closing_price" or "volume", we declare a Column:

e.g. Let's get closing_price and volume from AlphaVantage:

adjusted_close = av.Column(time_series='adjusted_close')

daily_volume = av.Column(time_series='volume')

Features

Columns are useful, but we usually want to derive new time-series from them, such as "log_returns" or "SMA20". Margot does this for you; we've called these derived time-series, Features.

simple_returns = finance.SimpleReturns(column='adjusted_close')
log_returns = finance.LogReturns(column='adjusted_close')
sma20 = finance.SimpleMovingAverage(column='adjusted_close', window=20)

Features can be piled on top of one another. For example, to create a time-series of realised volatility based on log_returns with a lookback of 30 trading days, simply add the following feature:

realised_vol = finance.RealisedVolatility(column='log_returns', window=30)

Margot includes many common financial Features, and we'll be adding more soon. It's also very easy to add your own.

Symbols

Often, you want to make a dataframe combining a number of columns and features. Margot makes this very easy by providing the Symbol class e.g.

class MyEquity(Symbol):

    adjusted_close = av.Column(time_series='adjusted_close')
    log_returns = finance.LogReturns(column='adjusted_close')
    realised_vol = finance.RealisedVolatility(column='log_returns', 
                                              window=30)
    upper_band = finance.UpperBollingerBand(column='adjusted_close', 
                                            window=20, 
                                            width=2.0)
    sma20 = finance.SimpleMovingAverage(column='adjusted_close', 
                                        window=20)
    lower_band = finance.LowerBollingerBand(column='adjusted_close', 
                                            window=20, 
                                            width=2.0)

spy = MyEquity(symbol='SPY')

MargotDataFrames

You usually you want to look at more than one symbol. That's where MargotDataFrames come in. MargotDataFrames combine multiple Symbols with dataframe-wide Features and Ratios. For example:

class MyEnsemble(MargotDataFrame):
    spy = MyEquity(symbol='SPY', trading_calendar='NYSE')
    iwm = MyEquity(symbol='IWM', trading_calendar='NYSE')
    spy_iwm_ratio = Ratio(numerator=spy.adjusted_close, 
                          denominator=iwm.adjusted_close,
                          label='spy_iwm_ratio')

my_df = MyEnsemble().to_pandas() 

The above code creates a Pandas DataFrame of two equities, and an additional feature that calculates a time-series of the ratio of their respective adjusted close prices.

Margot's other parts

not yet released.

Margot aims to provide a simple framework for writing and backtesting trading signal generation algorithms using margot.data.

Results from margot's trading algorithms can be analysed with pyfolio.

Getting Started

pip install margot

Next you need to make sure you have a couple of important environment variables set:

export ALPHAVANTAGE_API_KEY=YOUR_API_KEY
export DATA_CACHE=PATH_TO_FOLDER_TO_STORE_HDF5_FILES

Once you've done that, try running the code in the notebook.

Status

This is still an early stage software project, and should not be used for live trading.

Documentation

in progress - for examples see the notebook.

Contributing

Feel free to make a pull request or chat about your idea first using issues.

Dependencies are kept to a minimum. Generally if there's a way to do something in the standard library (or numpy / Pandas), let's do it that way rather than add another library.

License

Margot is licensed for use under Apache 2.0. For details see the License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

margot-0.6.2.tar.gz (35.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

margot-0.6.2-py2.py3-none-any.whl (22.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file margot-0.6.2.tar.gz.

File metadata

  • Download URL: margot-0.6.2.tar.gz
  • Upload date:
  • Size: 35.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for margot-0.6.2.tar.gz
Algorithm Hash digest
SHA256 0669de467ff5050855d8be8fa627be8d8b6fa377b507352174e5a45b2e39a868
MD5 073c0656bc8a58798dcbfb053e5c4f06
BLAKE2b-256 41d25c7db3aa33835dc7010c4a6efce01b2a7c91b56646016e50c4ffb9c0a5fc

See more details on using hashes here.

File details

Details for the file margot-0.6.2-py2.py3-none-any.whl.

File metadata

  • Download URL: margot-0.6.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 22.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for margot-0.6.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 896f1d316ffbe76e87434d0f6e061e0b2ddea927b6eab7b7cc888a0ef69f2d61
MD5 b560692609491c633f53c29070ad447c
BLAKE2b-256 d506d6aaa9ecfb2599e6484223e9da2021c16bb95500722688caac2e65ac4cf1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page