Reputation: 13
from datetime import datetime, time, timedelta
from alpaca.trading.client import TradingClient
from alpaca.trading.requests import MarketOrderRequest, LimitOrderRequest, StopLimitOrderRequest, StopLossRequest, TrailingStopOrderRequest, GetOrdersRequest
from alpaca.trading.enums import OrderSide, TimeInForce, OrderType, QueryOrderStatus
from alpaca.data import StockHistoricalDataClient
from alpaca.data.requests import StockBarsRequest, StockLatestBarRequest
from alpaca.data.timeframe import TimeFrame
from alpaca.data.models.bars import Bar, BarSet
# Variables required for trading with Alpaca API
API_KEY = "PKFQ83SMC9BBSLD0AHE8"
API_SECRET = "8BaacD3BIvnYbDF9l62c8LtWIv008ToyecfNYv8t"
ENDPOINT = "https://paper-api.alpaca.markets/v2"
trading_client = TradingClient(API_KEY, API_SECRET)
data_client = StockHistoricalDataClient(API_KEY, API_SECRET, raw_data = True)
# Trigger variables
MACD = 0
# Market open and close times
market_open = time(9, 30, 0)
market_close = time(16, 0, 0)
# if((datetime.now().time() >= market_open) and (datetime.now().time() <= market_close)):
# print()
# trading_client.submit_order(
# order_data = MarketOrderRequest(
# symbol = "TSLA",
# qty = 1,
# side = OrderSide.BUY,
# time_in_force = TimeInForce.GTC
# )
# )
# trading_client.close_all_positions()
day = datetime(2024, 6, 3, 0, 0)
request = StockBarsRequest(symbol_or_symbols = "TSLA", start = day, end = (day + timedelta(days = 1)), timeframe = TimeFrame.Day, limit = 1)
data1 = data_client.get_stock_bars(request)
print(data1.getclose())
The way the method works doesn't allow to get specific data on closing prices. How can I do this? I'm trying to get specific historical closing prices for TSLA using the Alpaca API. I tried getting stock bars for TSLA and specifically targeting closing price but it is only able to print all bar data at once rather then separating the data.
Upvotes: 0
Views: 943
Reputation: 51
Getting the historical close prices is a fairly simple task. You are on the right track with your code, it just needs to be adjusted.
Here is the code you should be using to get historical close prices:
day = datetime(2024, 6, 3, 0, 0)
request = StockBarsRequest(symbol_or_symbols="TSLA", start=day, end=(day + timedelta(days=5)), timeframe=TimeFrame.Day, limit=10)
data1 = data_client.get_stock_bars(request)
final_data = [{x['t'], x['c']} for x in data1['TSLA']]
print(final_data)
And when this code is run, it returns the following:
[{'2024-06-03T04:00:00Z', 176.29}, {'2024-06-04T04:00:00Z', 174.77}, {'2024-06-05T04:00:00Z', 175}, {177.94, '2024-06-06T04:00:00Z'}, {177.48, '2024-06-07T04:00:00Z'}]
In the original code, I am unsure where you got the getclose()
method. I did not see that anywhere in the alpaca-py docs.
Each element of data1['TSLA']
has the stock bar data for the day (day, high, low, close, etc.), so with some filtering and list comprehension, you can get the time and close price.
If you want to adjust the date range you look at, make sure to adjust the start/end date and also the limit
. Originally, the limit was set to 1 and that will only return one days worth of data.
Upvotes: 1