Skip to main content

Factor Investing Library

Project description

PyPI - Python Version PyPI PyPI - Status GitHub

Factor Investing LIBrary is a lightweight algorithmic trading Python library built for easy testing of predictive factors and portfolio rebalance via Oanda. Inspired by and compatible with Quantopian Open Source.

Changelog »

Installation

Install with pip:

$ pip install filib

Check requirements.txt for dependencies.

Usage

Write and execute a script based on following template:

from filib.oanda import Oanda  # Currently only Oanda is available
from filib.helpers import *    # Optional functions, useful for factor generation


class MyFactors(Oanda):

    def momentum(self):  # Hypothesis: there is persistence in an asset's performance
        factor = self.returns
        split = [-1, -.003, .003, 1]  # List of thresholds or int to split equally
        return factor, split  # Always return atleast factor and follow this order

    def relative_strenght_index(self):
        import pandas as pd
        import pandas_ta as ta  # Sample Technical Analysis Library
        factor = pd.DataFrame({
            instrument: self.price_data[instrument].ta.rsi(length=14)
            for instrument, _ in self.price_data
        }) * -1  # Short low factor values and long high factor values
        split = [-100, -70, -30, 0]
        return factor, split

    def big_mac_index(self):
        import quandl  # Sample Financial, Economic and Alternative Data Library
        iso_codes = get_iso_codes(self.price_data)
        codes = [f'ECONOMIST/BIGMAC_{COUNTRY}.5' for COUNTRY in iso_codes]
        factor = quandl.get(codes).dropna(how='all', axis=1)
        factor.columns = [iso_codes[c.split('_')[1].split()[0]] for c in factor]
        factor.index = factor.index.tz_localize('UTC')  # Convert time zone to UTC
        return factor  # If not specified split = 3 by default


if __name__ == "__main__":

    strategy = MyFactors(  # Initialize strategy
        instruments = ['EUR_USD', 'GBP_USD', 'USD_JPY', 'AUD_USD', 'NZD_USD',
                       'USD_CAD', 'USD_CHF', 'USD_NOK', 'USD_SEK'],  # Define universe
        granularity = 'D',    # Time range between rebalancing and between each candle
        count = 500,          # Number of historical candles to return for analysis
        symbol = 'USD',       # Optional, specify symbol to arrange your price data
        periods = (1, 2, 3),  # Optional, specify periods to analyze factor decay
        split = 3,            # Number of quantiles to split your combined factor data
        accountID = '',       # Your Oanda's account ID for creating orders
        target = .8,          # Set portfolio value target
        long_short = True,    # Trade only top and bottom quantile of combined factor
    )

    strategy.performance()  # Check performance of your factors combined together
    strategy.performance('momentum')  # Check performance for individual factors
    [strategy.performance(factor) for factor in strategy.factors]

    strategy.rebalance()  # Generate orders needed to rebalance your portfolio
    strategy.rebalance(live=True)  # Actually place orders
    # PLEASE USE AT YOUR OWN RISK - THIS CAN TRADE REAL MONEY - NO WARRANTY IS GIVEN

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

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

filib-0.1.0.tar.gz (32.5 kB view hashes)

Uploaded Source

Built Distribution

filib-0.1.0-py3-none-any.whl (16.1 kB view hashes)

Uploaded Python 3

Supported by

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