Skip to content

Commit 784e16a

Browse files
committed
Merge branch 'master' of https://github.com/alpacahq/alpaca-trade-api-python into feature/polygon-daily-oc
2 parents a872008 + 1aed6c9 commit 784e16a

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

alpaca_trade_api/entity.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,28 @@ def __getattr__(self, key):
155155

156156
class Watchlist(Entity):
157157
pass
158+
159+
160+
class PortfolioHistory(Entity):
161+
def __init__(self, raw):
162+
self._raw = raw
163+
164+
@property
165+
def df(self):
166+
if not hasattr(self, '_df'):
167+
df = pd.DataFrame(
168+
self._raw, columns=(
169+
'timestamp', 'profit_loss', 'profit_loss_pct', 'equity'
170+
),
171+
)
172+
df.set_index('timestamp', inplace=True)
173+
if not df.empty:
174+
df.index = pd.to_datetime(
175+
(df.index * 1e6).astype('int64'), utc=True,
176+
).tz_convert(NY)
177+
else:
178+
df.index = pd.to_datetime(
179+
df.index, utc=True
180+
)
181+
self._df = df
182+
return self._df

alpaca_trade_api/polygon/rest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ def historic_agg_v2(self, symbol, multiplier, timespan, _from, to,
135135
raw = self.get(path, params, version='v2')
136136
return Aggsv2(raw)
137137

138+
139+
def daily_open_close(self, symbol, date):
140+
path = '/open-close/{}/{}'.format(symbol, date)
141+
raw = self.get(path)
142+
return raw
143+
144+
138145
def grouped_daily(self, date, unadjusted=False):
139146
path = '/aggs/grouped/locale/US/market/STOCKS/{}'.format(date)
140147
params = {}

alpaca_trade_api/rest.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .entity import (
1313
Account, AccountConfigurations, AccountActivity,
1414
Asset, Order, Position, BarSet, Clock, Calendar,
15-
Watchlist
15+
Watchlist, PortfolioHistory
1616
)
1717
from . import polygon
1818
from . import alpha_vantage
@@ -435,3 +435,22 @@ def delete_watchlist(self, watchlist_id):
435435

436436
def delete_from_watchlist(self, watchlist_id, symbol):
437437
self.delete('/watchlists/{}/{}'.format(watchlist_id, symbol))
438+
439+
def get_portfolio_history(
440+
self, date_start=None, date_end=None, period=None,
441+
timeframe=None, extended_hours=None
442+
):
443+
params = {}
444+
if date_start is not None:
445+
params['date_start'] = date_start
446+
if date_end is not None:
447+
params['date_end'] = date_end
448+
if period is not None:
449+
params['period'] = period
450+
if timeframe is not None:
451+
params['timeframe'] = timeframe
452+
if extended_hours is not None:
453+
params['extended_hours'] = extended_hours
454+
return PortfolioHistory(
455+
self.get('/account/portfolio/history', data=params)
456+
)

0 commit comments

Comments
 (0)