Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…-python into feature/polygon-daily-oc
  • Loading branch information
ttt733 committed Mar 2, 2020
2 parents a872008 + 1aed6c9 commit 784e16a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
25 changes: 25 additions & 0 deletions alpaca_trade_api/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,28 @@ def __getattr__(self, key):

class Watchlist(Entity):
pass


class PortfolioHistory(Entity):
def __init__(self, raw):
self._raw = raw

@property
def df(self):
if not hasattr(self, '_df'):
df = pd.DataFrame(
self._raw, columns=(
'timestamp', 'profit_loss', 'profit_loss_pct', 'equity'
),
)
df.set_index('timestamp', inplace=True)
if not df.empty:
df.index = pd.to_datetime(
(df.index * 1e6).astype('int64'), utc=True,
).tz_convert(NY)
else:
df.index = pd.to_datetime(
df.index, utc=True
)
self._df = df
return self._df
7 changes: 7 additions & 0 deletions alpaca_trade_api/polygon/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ def historic_agg_v2(self, symbol, multiplier, timespan, _from, to,
raw = self.get(path, params, version='v2')
return Aggsv2(raw)


def daily_open_close(self, symbol, date):
path = '/open-close/{}/{}'.format(symbol, date)
raw = self.get(path)
return raw


def grouped_daily(self, date, unadjusted=False):
path = '/aggs/grouped/locale/US/market/STOCKS/{}'.format(date)
params = {}
Expand Down
21 changes: 20 additions & 1 deletion alpaca_trade_api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .entity import (
Account, AccountConfigurations, AccountActivity,
Asset, Order, Position, BarSet, Clock, Calendar,
Watchlist
Watchlist, PortfolioHistory
)
from . import polygon
from . import alpha_vantage
Expand Down Expand Up @@ -435,3 +435,22 @@ def delete_watchlist(self, watchlist_id):

def delete_from_watchlist(self, watchlist_id, symbol):
self.delete('/watchlists/{}/{}'.format(watchlist_id, symbol))

def get_portfolio_history(
self, date_start=None, date_end=None, period=None,
timeframe=None, extended_hours=None
):
params = {}
if date_start is not None:
params['date_start'] = date_start
if date_end is not None:
params['date_end'] = date_end
if period is not None:
params['period'] = period
if timeframe is not None:
params['timeframe'] = timeframe
if extended_hours is not None:
params['extended_hours'] = extended_hours
return PortfolioHistory(
self.get('/account/portfolio/history', data=params)
)

0 comments on commit 784e16a

Please sign in to comment.