|
4 | 4 | import alpaca_trade_api as tradeapi |
5 | 5 | import time |
6 | 6 | from alpaca_trade_api.rest import TimeFrame |
7 | | - |
| 7 | +import pandas as pd |
8 | 8 | API_KEY = "YOUR_API_KEY_HERE" |
9 | 9 | API_SECRET = "YOUR_API_SECRET_HERE" |
10 | 10 | APCA_API_BASE_URL = "https://paper-api.alpaca.markets" |
@@ -273,13 +273,17 @@ def rerank(self): |
273 | 273 | # Get the total price of the array of input stocks. |
274 | 274 | def getTotalPrice(self, stocks, resp): |
275 | 275 | totalPrice = 0 |
| 276 | + |
276 | 277 | for stock in stocks: |
277 | | - bars = self.alpaca.get_bars(stock, TimeFrame.Minute, |
| 278 | + bars = self.alpaca.get_bars(stock[0], TimeFrame.Minute, |
278 | 279 | pd.Timestamp('now').date(), |
279 | 280 | pd.Timestamp('now').date(), limit=1, |
280 | 281 | adjustment='raw') |
281 | 282 |
|
282 | | - totalPrice += bars[stock][0].c |
| 283 | + if len(bars) != 0: |
| 284 | + totalPrice += bars[0].c |
| 285 | + |
| 286 | + |
283 | 287 | resp.append(totalPrice) |
284 | 288 |
|
285 | 289 | # Submit a batch order that returns completed and uncompleted orders. |
@@ -322,7 +326,9 @@ def getPercentChanges(self): |
322 | 326 | pd.Timestamp('now').date(), |
323 | 327 | pd.Timestamp('now').date(), limit=length, |
324 | 328 | adjustment='raw') |
325 | | - self.allStocks[i][1] = (bars[stock[0]][len(bars[stock[0]]) - 1].c - bars[stock[0]][0].o) / bars[stock[0]][0].o |
| 329 | + if len(bars) != 0: |
| 330 | + self.allStocks[i][1] = (bars[len(bars) - 1].c - bars[0].o) / bars[0].o |
| 331 | + |
326 | 332 |
|
327 | 333 | # Mechanism used to rank the stocks, the basis of the Long-Short Equity Strategy. |
328 | 334 | def rank(self): |
|
0 commit comments