2121# momentum. Returns a dataframe mapping stock symbols to ratings and prices.
2222# Note: If algo_time is None, the API's default behavior of the current time
2323# as `end` will be used. We use this for live trading.
24- def get_ratings (symbols , algo_time ):
24+ def get_ratings (api , algo_time ):
2525 assets = api .list_assets ()
2626 assets = [asset for asset in assets if asset .tradable ]
2727 ratings = pd .DataFrame (columns = ['symbol' , 'rating' , 'price' ])
@@ -31,22 +31,34 @@ def get_ratings(symbols, algo_time):
3131 formatted_time = None
3232 if algo_time is not None :
3333 # Convert the time to something compatable with the Alpaca API.
34+ start_time = (algo_time .date () -
35+ timedelta (days = window_size )).strftime (api_time_format )
3436 formatted_time = algo_time .date ().strftime (api_time_format )
3537 while index < len (assets ):
3638 symbol_batch = [
3739 asset .symbol for asset in assets [index :index + batch_size ]
3840 ]
3941 # Retrieve data for this batch of symbols.
40- barset = {}
41- for symbol in symbol_batch :
42- bars = api .get_bars (symbol ,
43- TimeFrame .Day ,
44- formatted_time - timedelta (days = window_size ),
45- formatted_time ,
46- limit = window_size ,
47- adjustment = 'raw' )
48- barset [symbol ] = bars
4942
43+ # note: soon get_barset() will be deprecated and you need to use the
44+ # commented out code instead
45+
46+ # barset = {}
47+ # bars = api.get_bars(symbol,
48+ # TimeFrame.Day,
49+ # start_time,
50+ # formatted_time,
51+ # limit=window_size,
52+ # adjustment='raw')
53+ # barset[symbol] = bars
54+
55+ barset = api .get_barset (
56+ symbols = symbol_batch ,
57+ timeframe = 'day' ,
58+ limit = window_size ,
59+ end = formatted_time
60+ )
61+ algo_time = pd .Timestamp ('now' , tz = timezone ('EST' ))
5062 for symbol in symbol_batch :
5163 bars = barset [symbol ]
5264 if len (bars ) == window_size :
@@ -102,10 +114,6 @@ def api_format(dt):
102114def backtest (api , days_to_test , portfolio_amount ):
103115 # This is the collection of stocks that will be used for backtesting.
104116 assets = api .list_assets ()
105- # Note: for longer testing windows, this should be replaced with a list
106- # of symbols that were active during the time period you are testing.
107- symbols = [asset .symbol for asset in assets ]
108-
109117 now = datetime .now (timezone ('EST' ))
110118 beginning = now - timedelta (days = days_to_test )
111119
@@ -129,7 +137,8 @@ def backtest(api, days_to_test, portfolio_amount):
129137 break
130138
131139 # Get the ratings for a particular day
132- ratings = get_ratings (symbols , timezone ('EST' ).localize (calendar .date ))
140+ ratings = \
141+ get_ratings (api , timezone ('EST' ).localize (calendar .date ))
133142 shares = get_shares_to_buy (ratings , portfolio_amount )
134143 for _ , row in ratings .iterrows ():
135144 # "Buy" our shares on that day and subtract the cost.
@@ -139,11 +148,11 @@ def backtest(api, days_to_test, portfolio_amount):
139148 cal_index += 1
140149
141150 # Print market (S&P500) return for the time period
142- sp500_bars = self . oapi .get_bars ('SPY' ,
143- TimeFrame .Day ,
144- api_format (calendars [0 ].date ),
145- api_format (calendars [- 1 ].date ),
146- adjustment = 'raw' )
151+ sp500_bars = api .get_bars ('SPY' ,
152+ TimeFrame .Day ,
153+ api_format (calendars [0 ].date ),
154+ api_format (calendars [- 1 ].date ),
155+ adjustment = 'raw' )
147156 sp500_change = (sp500_bars [- 1 ].c - sp500_bars [0 ].c ) / sp500_bars [0 ].c
148157 print ('S&P 500 change during backtesting window: {:.4f}%' .format (
149158 sp500_change * 100 )
@@ -161,15 +170,26 @@ def get_value_of_assets(api, shares_bought, on_date):
161170 total_value = 0
162171 formatted_date = api_format (on_date )
163172
164- barset = {}
165- for symbol in shares_bought .keys ():
166- bars = api .get_bars (symbol ,
167- TimeFrame .Day ,
168- on_date ,
169- on_date ,
170- limit = 1 ,
171- adjustment = 'raw' )
172- barset [symbol ] = bars
173+ # note: soon get_barset() will be deprecated and you need to use the
174+ # commented out code instead
175+
176+ # barset = {}
177+ # for symbol in shares_bought.keys():
178+ # bars = api.get_bars(symbol,
179+ # TimeFrame.Day,
180+ # on_date.date(),
181+ # on_date.date(),
182+ # limit=1,
183+ # adjustment='raw')
184+ # barset[symbol] = bars
185+
186+ barset = api .get_barset (
187+ symbols = shares_bought .keys (),
188+ timeframe = 'day' ,
189+ limit = 1 ,
190+ end = formatted_date
191+ )
192+
173193 for symbol in shares_bought :
174194 total_value += shares_bought [symbol ] * barset [symbol ][0 ].o
175195 return total_value
0 commit comments