Skip to content

Commit

Permalink
fixed Alpine docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
omdv committed Sep 15, 2017
1 parent 5458d55 commit 8248448
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 41 deletions.
31 changes: 19 additions & 12 deletions backend/backend.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pandas as pd
from collections import OrderedDict
from pickle import dump, load
from backend.portfolio_model import PortfolioModels
from backend.robinhood_data import RobinhoodData
Expand Down Expand Up @@ -84,7 +85,13 @@ def update_market_data(self, **keyword_parameters):
"""
md = MarketData(self.datafile)

if ('fresh_start' in keyword_parameters):
# check if symbols match
s1 = list(self._df_ord.symbol.unique())
s1.sort()
s2 = list(self._market.minor_axis)
s2.sort()

if ('fresh_start' in keyword_parameters) or (s1 != s2):
min_date = self.user['rb_dates'][0]
max_date = pd.Timestamp("today")
self._market = md.download_save_market_data(
Expand Down Expand Up @@ -132,17 +139,17 @@ def _get_latest_portfolio_snapshot(self):
# use only the last row
df = self._panel.iloc[:, -1, :-1]

columns_to_names = {
'cum_size': ['Shares', '{:,.0f}'],
'current_weight': ['Portfolio weight', '{:.2f}'],
'cum_cost_basis': ['Current cost basis', '{:,.2f}'],
'cum_value_close': ['Current value', '{:,.2f}'],
'cum_realized_gain': ['Realized gain', '{:,.2f}'],
'cum_dividends': ['Dividends', '{:,.2f}'],
'cum_unrealized_gain': ['Unrealized gain', '{:,.2f}'],
'cum_total_return': ['Total return', '{:,.2f}'],
'current_return_rate': ['Total return rate', '{:,.2f}%']
}
columns_to_names = OrderedDict([
('cum_size', ['Shares', '{:,.0f}']),
('current_weight', ['Portfolio weight', '{:.2f}']),
('cum_cost_basis', ['Current cost basis', '{:,.2f}']),
('cum_value_close', ['Current value', '{:,.2f}']),
('cum_realized_gain', ['Realized gain', '{:,.2f}']),
('cum_dividends', ['Dividends', '{:,.2f}']),
('cum_unrealized_gain', ['Unrealized gain', '{:,.2f}']),
('cum_total_return', ['Total return', '{:,.2f}']),
('current_return_rate', ['Total return rate', '{:,.2f}%'])
])

# convert ratios to percent
df['current_weight'] = df['current_weight'] * 100
Expand Down
3 changes: 3 additions & 0 deletions backend/portfolio_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ def actual_portfolio_stats(self):
- Series with portfolio stats
"""
pf = self.panelframe

# can choose either a total return or capital gain only
return_to_use = 'cum_total_return'

cum_return_D1 = pf[return_to_use].sum(1).shift(1)
cum_return_D2 = pf[return_to_use].sum(1)
cost_basis = pf['cum_cost_basis'].sum(1)
Expand Down
51 changes: 38 additions & 13 deletions docker/Alpine
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
FROM jfloff/alpine-python:3.4-slim
RUN /entrypoint.sh \
-b git \
-p "numpy>=1.11.0" \
-p "pandas>=0.17.1,<=0.19.2" \
-p "pandas_datareader==0.5.0" \
-p requests \
-p seaborn \
-p matplotlib \
-p flask \
-p git+https://github.com/quantopian/empyrical \
-p git+https://github.com/czielinski/portfolioopt
CMD python app.py
FROM frolvlad/alpine-python3

RUN apk add --no-cache libstdc++ lapack-dev freetype-dev libpng wget zlib-dev build-base && \
apk add --no-cache \
--virtual=.build-dependencies \
g++ gfortran musl-dev \
python3-dev git && \
ln -s locale.h /usr/include/xlocale.h

# Build HDF5
RUN cd ; wget https://support.hdfgroup.org/ftp/HDF5/current/src/hdf5-1.10.1.tar.gz && \
tar -xf hdf5-1.10.1.tar.gz && cd hdf5-1.10.1 && \
./configure --prefix=/usr/local/ && make && make install

RUN apk update && apk add ca-certificates && update-ca-certificates && cd / && \
wget http://faculty.cse.tamu.edu/davis/SuiteSparse/SuiteSparse-4.5.3.tar.gz && \
tar -xf SuiteSparse-4.5.3.tar.gz

ENV CVXOPT_SUITESPARSE_SRC_DIR /SuiteSparse

RUN git clone https://github.com/omdv/robinhood-portfolio && \
pip install -r robinhood-portfolio/requirements.txt && \
find /usr/lib/python3.*/ -name 'tests' -exec rm -r '{}' + && \
rm /usr/include/xlocale.h && \
rm -r /root/.cache && \
apk del .build-dependencies

RUN mkdir -p /root/.config/matplotlib && \
echo backend:Agg > /root/.config/matplotlib/matplotlibrc

# cleanup
#RUN cd ; rm -rf hdf5-vol .subversion
#RUN apt-get -yq remove subversion
#RUN apt-get -yq autoremove
#RUN apt-get clean
#RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

CMD cd robinhood-portfolio && python3 app.py
16 changes: 8 additions & 8 deletions docker/Dockerfile → docker/Ubuntu
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
FROM blitznote/debootstrap-amd64:16.04
FROM phusion/baseimage:latest
MAINTAINER Oleg Medvedev <[email protected]>
ARG VERSION=unknown

RUN apt-get update && \
apt-get install -y \
python3 python3-numpy python3-nose python3-pandas \
pep8 python3-pip python3-matplotlib git openssl && \
pep8 python3-pip python3-matplotlib git && \
pip3 install --upgrade setuptools

RUN git clone https://github.com/omdv/robinhood-portfolio && \
pip3 install --upgrade --force-reinstall -r robinhood-portfolio/requirements.txt

RUN openssl s_client -showcerts -connect stooq.com:443 \
</dev/null 2>/dev/null|openssl x509 -outform PEM > stooq.pem && \
mkdir -p /root/.config/matplotlib && \
RUN mkdir -p /root/.config/matplotlib && \
echo backend:Agg > /root/.config/matplotlib/matplotlibrc

RUN VERSION=${VERSION} git clone https://github.com/omdv/robinhood-portfolio && \
pip3 install --upgrade --force-reinstall -r robinhood-portfolio/requirements.txt

CMD cd robinhood-portfolio && python3 app.py
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ numpy>=1.11.0
pandas==0.20.3
numexpr>=2.4.6
pandas_datareader==0.5.0
tables
requests
seaborn
matplotlib
flask
git+https://github.com/quantopian/empyrical
git+https://github.com/quantopian/empyrical
git+https://github.com/czielinski/portfolioopt
4 changes: 4 additions & 0 deletions static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ h2 {
margin-bottom: 5px;
}

.intro {
display: none;
}

.footer {
bottom: 0;
width: 100%;
Expand Down
11 changes: 11 additions & 0 deletions templates/layouts/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@
}
}

function toggleIntro() {
var list = document.querySelectorAll('.intro');
for (x in list) {
if (list[x].style.display === 'block') {
list[x].style.display = 'none';
} else {
list[x].style.display = 'block';
}
}
}

var modal = document.getElementById('myModal');
var btn = document.getElementById('openLogin');
var span = document.getElementsByClassName('close-modal')[0];
Expand Down
15 changes: 8 additions & 7 deletions templates/pages/portfolio.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
{% block content %}
<a href="https://github.com/omdv/robinhood-portfolio" class="github-corner" aria-label="View source on Github"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>

<span><a href="javascript:hideAllText()">Toggle all text</a></span>
<!-- <span><a href="javascript:hideAllText()">Toggle all text</a></span> -->
<div class="section">
<h2>Introduction</h2>
<p>This tool uses the <a href="https://github.com/Jamonek/Robinhood">unofficial Robinhood API</a> to download the history of orders and dividends of your Robinhood account. Since there is no official Robinhood API using oauth you need to enter your account information and temporarily disable the two-factor authentication to download the data. The script runs locally and does not save your account information - feel free to verify/fork the source code. It is your responsibility to make sure that your account is safe.</p>
<p>It also needs the market data for securities you traded, including the history of the market index such as <a href="https://en.wikipedia.org/wiki/S%26P_500_Index">S&P 500</a> which is used as the benchmark. This data is downloaded from open sources, so please use it responsibly and do not spam requests as you may get banned, or worse, prevent others from using it as well.</p>
<p>If you choose to refresh the Robinhood data the market data will be downloaded automatically for the corresponding securities and dates. Alternatively you can refresh only market data if you didn't have any new Robinhood transactions recently.</p>
<h2>Setup</h2>
<span><a href="javascript:toggleIntro()">Show Introduction</a></span>
<p class="intro">This tool uses the <a href="https://github.com/Jamonek/Robinhood">unofficial Robinhood API</a> to download the history of orders and dividends of your Robinhood account. Since there is no official Robinhood API using oauth you need to enter your account information and temporarily disable the two-factor authentication to download the data. The script runs locally and does not save your account information - feel free to validate/fork the source code. It is your responsibility to make sure that your account is safe.</p>
<p class="intro">It also needs the market data for securities you traded, including the history of the market index such as <a href="https://en.wikipedia.org/wiki/S%26P_500_Index">S&P 500</a> which is used as the benchmark. This data is downloaded from open sources, so please use it responsibly and do not spam requests as you may get banned, or worse, prevent others from using it as well.</p>
<p class="intro">If you choose to refresh the Robinhood data the market data will be downloaded automatically for the corresponding securities and dates. Alternatively you can refresh only market data if you didn't have any new Robinhood transactions recently.</p>
<table style="margin:1em auto;">
<tr>
<td style="text-align: right">Robinhood data&nbsp;</td>
<td style="text-align: right">Robinhood data: &nbsp;</td>
<td>{{ rb_dates[0] | safe }} - {{ rb_dates[1] | safe }}</td>
<td>
{% if rb_dates[1] != today %}
Expand All @@ -21,7 +22,7 @@ <h2>Introduction</h2>
</td>
</tr>
<tr>
<td style="text-align: right">Market data&nbsp;</td>
<td style="text-align: right">Market data: &nbsp;</td>
<td>{{ mkt_dates[0] | safe }} - {{ mkt_dates[1] | safe }}</td>
<td>
<form class="form-inline" action="/" method="POST">
Expand Down

0 comments on commit 8248448

Please sign in to comment.