Skip to content

Commit 4e65915

Browse files
authored
Fixes Incorrect Access Issue on Long-Short Example (#560)
* fixes 422 Issue with get_bars * reverted timestamp changes
1 parent 9528725 commit 4e65915

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

examples/long-short.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import alpaca_trade_api as tradeapi
55
import time
66
from alpaca_trade_api.rest import TimeFrame
7-
7+
import pandas as pd
88
API_KEY = "YOUR_API_KEY_HERE"
99
API_SECRET = "YOUR_API_SECRET_HERE"
1010
APCA_API_BASE_URL = "https://paper-api.alpaca.markets"
@@ -273,13 +273,17 @@ def rerank(self):
273273
# Get the total price of the array of input stocks.
274274
def getTotalPrice(self, stocks, resp):
275275
totalPrice = 0
276+
276277
for stock in stocks:
277-
bars = self.alpaca.get_bars(stock, TimeFrame.Minute,
278+
bars = self.alpaca.get_bars(stock[0], TimeFrame.Minute,
278279
pd.Timestamp('now').date(),
279280
pd.Timestamp('now').date(), limit=1,
280281
adjustment='raw')
281282

282-
totalPrice += bars[stock][0].c
283+
if len(bars) != 0:
284+
totalPrice += bars[0].c
285+
286+
283287
resp.append(totalPrice)
284288

285289
# Submit a batch order that returns completed and uncompleted orders.
@@ -322,7 +326,9 @@ def getPercentChanges(self):
322326
pd.Timestamp('now').date(),
323327
pd.Timestamp('now').date(), limit=length,
324328
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+
326332

327333
# Mechanism used to rank the stocks, the basis of the Long-Short Equity Strategy.
328334
def rank(self):

0 commit comments

Comments
 (0)