1+ import dateutil
12import requests
23from .entity import (
34 Aggs , Aggsv2 , Aggsv2Set ,
@@ -59,8 +60,8 @@ def historic_trades(self, symbol, date, offset=None, limit=None):
5960 return Trades (raw )
6061
6162 def historic_trades_v2 (
62- self , symbol , date , timestamp = None , timestamp_limit = None ,
63- reverse = None , limit = None
63+ self , symbol , date , timestamp = None , timestamp_limit = None ,
64+ reverse = None , limit = None
6465 ):
6566 path = '/ticks/stocks/trades/{}/{}' .format (symbol , date )
6667 params = {}
@@ -92,8 +93,8 @@ def historic_quotes(self, symbol, date, offset=None, limit=None):
9293 return Quotes (raw )
9394
9495 def historic_quotes_v2 (
95- self , symbol , date , timestamp = None , timestamp_limit = None ,
96- reverse = None , limit = None
96+ self , symbol , date , timestamp = None , timestamp_limit = None ,
97+ reverse = None , limit = None
9798 ):
9899 path = '/ticks/stocks/nbbo/{}/{}' .format (symbol , date )
99100 params = {}
@@ -109,27 +110,46 @@ def historic_quotes_v2(
109110
110111 return QuotesV2 (raw )
111112
112- def historic_agg (self , size , symbol ,
113- _from = None , to = None , limit = None ):
114- path = '/historic/agg/{}/{}' .format (size , symbol )
115- params = {}
116- if _from is not None :
117- params ['from' ] = _from
118- if to is not None :
119- params ['to' ] = to
120- if limit is not None :
121- params ['limit' ] = limit
122- raw = self .get (path , params )
123-
124- return Aggs (raw )
125-
126113 def historic_agg_v2 (self , symbol , multiplier , timespan , _from , to ,
127114 unadjusted = False , limit = None ):
128- path = '/aggs/ticker/{}/range/{}/{}/{}/{}' .format (
129- symbol , multiplier , timespan , _from , to
130- )
131- params = {}
132- params ['unadjusted' ] = unadjusted
115+ """
116+
117+ :param symbol:
118+ :param multiplier: Size of the timespan multiplier (distance between
119+ samples. e.g if it's 1 we get for daily 2015-01-05, 2015-01-06,
120+ 2015-01-07, 2015-01-08.
121+ if it's 3 we get 2015-01-01, 2015-01-04,
122+ 2015-01-07, 2015-01-10)
123+ :param timespan: Size of the time window: minute, hour, day, week,
124+ month, quarter, year
125+ :param _from: some use isoformat some use timestamp. for now we
126+ handle both.
127+ examples of different usages: pylivetrader,
128+ alpaca-backtrader.
129+ :param to:
130+ :param unadjusted:
131+ :param limit: max samples to retrieve (seems like we get "limit - 1" )
132+ :return:
133+ """
134+ path_template = '/aggs/ticker/{symbol}/range/{multiplier}/' \
135+ '{timespan}/{_from}/{to}'
136+ if isinstance (_from , int ):
137+ path = path_template .format (symbol = symbol ,
138+ multiplier = multiplier ,
139+ timespan = timespan ,
140+ _from = _from ,
141+ to = to
142+ )
143+ else :
144+ path = path_template .format (symbol = symbol ,
145+ multiplier = multiplier ,
146+ timespan = timespan ,
147+ _from = dateutil .parser .parse (
148+ _from ).date ().isoformat (),
149+ to = dateutil .parser .parse (
150+ to ).date ().isoformat ()
151+ )
152+ params = {'unadjusted' : unadjusted }
133153 if limit :
134154 params ['limit' ] = limit
135155 raw = self .get (path , params , version = 'v2' )
0 commit comments