Skip to content

Commit ead541e

Browse files
authored
Fix functions for Watchlists API endpoint (#294)
* Add PUT HTTP request method * Fix update_watchlist * Fix add_watchlist * Add docstrings * Add get_watchlist_by_name
1 parent 6ccf8fd commit ead541e

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

alpaca_trade_api/rest.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ def get(self, path, data=None):
157157

158158
def post(self, path, data=None):
159159
return self._request('POST', path, data)
160+
161+
def put(self, path, data=None):
162+
return self._request('PUT', path, data)
160163

161164
def patch(self, path, data=None):
162165
return self._request('PATCH', path, data)
@@ -529,39 +532,61 @@ def get_calendar(self, start: str = None, end: str = None) -> Calendars:
529532
return [Calendar(o) for o in resp]
530533

531534
def get_watchlists(self) -> Watchlists:
535+
"""Get the list of watchlists registered under the account"""
532536
resp = self.get('/watchlists')
533537
return [Watchlist(o) for o in resp]
534538

535539
def get_watchlist(self, watchlist_id: str) -> Watchlist:
540+
"""Get a watchlist identified by the ID"""
536541
resp = self.get('/watchlists/{}'.format((watchlist_id)))
537542
return Watchlist(resp)
538543

539-
def add_watchlist(self, watchlist_name: str) -> Watchlists:
540-
resp = self.post('/watchlists', data=dict(name=watchlist_name))
541-
return [Watchlist(o) for o in resp]
544+
def get_watchlist_by_name(self, watchlist_name: str) -> Watchlist:
545+
"""Get a watchlist identified by its name"""
546+
params = {
547+
'name': watchlist_name,
548+
}
549+
resp = self.get('/watchlists:by_name', data=params)
550+
return Watchlist(resp)
551+
552+
def create_watchlist(self,
553+
watchlist_name: str,
554+
symbols = None) -> Watchlist:
555+
"""Create a new watchlist with an optional initial set of assets"""
556+
params = {
557+
'name': watchlist_name,
558+
}
559+
if symbols is not None:
560+
params['symbols'] = symbols
561+
resp = self.post('/watchlists', data=params)
562+
return Watchlist(resp)
542563

543564
def add_to_watchlist(self, watchlist_id: str, symbol: str) -> Watchlist:
565+
"""Append the asset for a symbol to the end of a watchlist's asset list"""
544566
resp = self.post(
545567
'/watchlists/{}'.format(watchlist_id), data=dict(symbol=symbol)
546568
)
547569
return Watchlist(resp)
548570

549571
def update_watchlist(self,
550-
watchlist_id,
572+
watchlist_id: str,
551573
name: str = None,
552-
symbols=None) -> Watchlist:
574+
symbols = None) -> Watchlist:
575+
"""Update a watchlist's name and/or asset list"""
553576
params = {}
554577
if name is not None:
555578
params['name'] = name
556579
if symbols is not None:
557580
params['symbols'] = symbols
558-
resp = self.patch('/watchlists/{}'.format(watchlist_id), data=params)
581+
resp = self.put('/watchlists/{}'.format(watchlist_id), data=params)
559582
return Watchlist(resp)
560583

561584
def delete_watchlist(self, watchlist_id: str) -> None:
585+
"""Delete a watchlist identified by the ID permanently"""
562586
self.delete('/watchlists/{}'.format(watchlist_id))
563587

564588
def delete_from_watchlist(self, watchlist_id: str, symbol: str) -> None:
589+
"""Remove an asset from the watchlist's asset list"""
565590
self.delete('/watchlists/{}/{}'.format(watchlist_id, symbol))
566591

567592
def get_portfolio_history(self,

0 commit comments

Comments
 (0)