Skip to main content

A cryptocurrency market data utility package

Project description

# Lattice

> A cryptocurrency portfolio analytics Python package

![PyPI]( [![Maintainability](](

## Installation

Install with pip:

$ pip install lattice

## Usage

**Backtesting trading strategies**

from datetime import datetime
from lattice.backtest import Portfolio

# Create a portfolio on October 1st 2017 with $100k
created_at = datetime(year=2017, month=10, day=1)
portfolio = Portfolio(
assets={'USD': 100000},

# Make some trades
trade_date = datetime(2017, 10, 1)
portfolio.trade_asset(39000, 'USD', 'BTC', trade_date)
portfolio.trade_asset(39000, 'USD', 'ETH', trade_date)
portfolio.trade_asset(22000, 'USD', 'LTC', trade_date)

# Get the current value
date = datetime(2017, 10, 24)
portfolio.get_value(date) # at a given date

# View the current portfolio
# => {'USD': 8515.5, 'BTC': 8.8335220838, 'ETH': 130.434782609, 'LTC': 423.07692307}

# View the portfolio's history
# => [{'amount': 100000, 'asset': 'USD', 'datetime': '2017-10-01 00:00:00'}, {'amount': -39000, 'asset': 'USD', 'datetime': '2017-10-01 00:00:00'}, {'amount': 8.8335220838, 'asset': 'BTC', 'datetime': '2017-10-01 00:00:00'}, {'amount': -39000, 'asset': 'USD', 'datetime': '2017-10-01 00:00:00'}, {'amount': 130.434782609, 'asset': 'ETH', 'datetime': '2017-10-01'00:00:00 }, {'amount': -22000, 'asset': 'USD', 'datetime': '2017-10-01 00:00:00'}, {'amount': 423.07692307, 'asset': 'LTC', 'datetime': '2017-10-01'00:00:00 }, {'amount': -1.5, 'asset': 'BTC', 'datetime': '2017-10-24 18:57:30.665241' }, {'amount': 8515.5, 'asset': 'USD', 'datetime': '2017-10-24 18:57:30.665241' }]

# View the historical value data points
portfolio.get_historical_value(datetime(2017, 10, 1))

# Get portfolio value of all 10 portfolios for a portfolio created at the start of October
from lattice.backtest import Portfolio
from lattice.optimize import Allocator

def polyledger_portfolio_values(since):
allocations = Allocator(coins=['BTC', 'LTC', 'ETH']).allocate()
for index, allocation in allocations.iterrows():
p = Portfolio({'USD': 10000}, since)
for coin in allocation.keys():
amount = (allocation[coin]/100) * 10000
p.trade_asset(amount, 'USD', coin, since)

# Compare to Bitwise
def bitwise_portfolio_value(since):
p = Portfolio({'USD': 10000}, since)
bitwise_alloc = {
'BTC': 0.6815, 'ETH': 0.1445, 'BCH': 0.0568, 'XRP': 0.0481, 'LTC': 0.0194,
'DASH': 0.0147, 'ZEC': 0.0141, 'XMR': 0.0077, 'ETC': 0.0069, 'NEO': 0.0062
for coin, fraction in bitwise_alloc.items():
amount = fraction * 10000
p.trade_asset(amount, 'USD', coin, since)

since = datetime(year=2017, month=10, day=1)

**Optimizing portfolio allocations**

from lattice.optimize import Allocator

coins = ['BTC', 'ETH', 'LTC', 'XRP']
allocator = Allocator(coins=coins)
allocations = allocator.allocate()
risk_index = 5 # Risk indices are from 0 to 5

**Saving data to a CSV**

from datetime import date
from import Manager

start = date(year=2017, month=1, day=1)
end = date(year=2017, month=6, day=1)
coins = ['BTC', 'LTC', 'ETH']
filepath = '/Users/ari/Desktop/prices.csv'

manager = Manager(coins)
df = manager.get_historic_data(start, end)

## Development

### Getting Started

**Virtual Environment**

This project uses [virtualenv]( to isolate the development environment from the rest of the local filesystem. You must create the virtual environment, activate it, and deactivate it when you finish working.

* Create the virtual environment with `python3 -m venv venv`.
* Activate the virtual environment from within the project directory with `$ source venv/bin/activate`.
* Now you can install the project dependencies with `(venv) $ pip3 install -r requirements.txt`.
* When you are done working in the virtual environment, you can deactivate it: `(env) $ deactivate`. See the [python guide]( for more information.


Lattice comes with a Makefile which enables some useful commands. From the project root, run `make help` for a list of commands.


To run the full test suite, use `make test`. To get a code coverage report, ensure [coverage]( is installed and run

$ coverage run --source lattice test

To get the report,

$ coverage report -m

### Packaging and Distributing

#### GitHub Release

Before tagging a release, ensure that `make test` and `make lint` pass without errors.

$ git tag -a MAJOR.MINOR.PATCH -m "Description of release goes here"
$ git push --tags

Although not necessary, you should also update the version in ``.

#### PyPi

Ensure `wheel` and `twine` are installed. Then inside the directory,

1. `make clean-build`
1. `python sdist`
1. `python bdist_wheel`
1. `twine upload dist/*`

See the [Python documentation]( for more info.

### Generating Documentation

Lattice uses [Sphinx]( to generate documentation output to `./docs`.

Command to build documentation:

$ cd docs && make html

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

lattice-1.0.0.tar.gz (14.6 kB view hashes)

Uploaded source

Built Distribution

lattice-1.0.0-py2.py3-none-any.whl (62.1 kB view hashes)

Uploaded py2 py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page