英文:
Cant use RESTClient result
问题
使用RESTClient时,我在一个变量中接收结果,如何读取开盘价?
from polygon import RESTClient
client = RESTClient("xxxxxxxxx")
aggs = []
for a in client.list_aggs("SPY", 1, "minute", "2023-07-25", "2023-07-25", limit=50000):
aggs.append(a)
print(a)
print("开盘价:", a.open)
print(type(a))
输出结果中的开盘价是 a.open
。
英文:
When using RESTClient, I receive result in a variable, how to read Open price?
from polygon import RESTClient
client = RESTClient("xxxxxxxxx")
aggs = []
for a in client.list_aggs("SPY",1,"minute","2023-07-25","2023-07-25",limit=50000,):
aggs.append(a)
>>print(a)
Agg(open=455.34, high=455.34, low=455.3101, close=455.3101, volume=3797, vwap=455.3239,
timestamp=1690329540000, transactions=9, otc=None)
>>type(a)
Out[40]: polygon.rest.models.aggs.Agg
答案1
得分: 0
在你的for循环中,将aggs.append(a)
更新为aggs.append(a.open)
。这将在aggs
中构建你可以访问的开盘价列表。
我对Polygon库并不完全熟悉,但他们关于获取聚合数据的API指南可能是一个更好的选择。
在这里,你可以在身份验证后使用Requests或urllib3进行GET请求,然后解析来自“Results”部分的JSON响应。
{
"adjusted": true,
"next_url": "https://api.polygon.io/v2/aggs/ticker/AAPL/range/1/day/1578114000000/2020-01-10?cursor=bGltaXQ9MiZzb3J0PWFzYw",
"queryCount": 2,
"request_id": "6a7e466379af0a71039d60cc78e72282",
"results": [
{
"c": 75.0875,
"h": 75.15,
"l": 73.7975,
"n": 1,
"o": 74.06,
"t": 1577941200000,
"v": 135647456,
"vw": 74.6099
},
{
"c": 74.3575,
"h": 75.145,
"l": 74.125,
"n": 1,
"o": 74.2875,
"t": 1578027600000,
"v": 146535512,
"vw": 74.7026
}
],
"resultsCount": 2,
"status": "OK",
"ticker": "AAPL"
}
所以它会类似于这样:
import requests
open_prices = []
r = requests.get('https://api.polygon.io/v2/aggs/ticker/SPY/range/1/minute/2023-07-25/2023-07-25?apiKey=XXXXXXXXX')
for open_data in r['results']:
open_prices.append(open_data['o'])
print(open_prices)
你可以更清晰地将URL参数化,但如果这能帮助你开始,就让我知道。
英文:
EDIT:
In your for loop, update the aggs.append(a) to aggs.append(a.open). This will build your list of open prices within aggs that you can then access.
I'm not completely familiar with the Polygon library for their platform, but their API guide for pulling aggregates may be a better option.
Here you could utilize Requests or urllib3 with a GET after authenticating and then parse the JSON response from the Results section.
{
"adjusted": true,
"next_url": "https://api.polygon.io/v2/aggs/ticker/AAPL/range/1/day/1578114000000/2020-01-10?cursor=bGltaXQ9MiZzb3J0PWFzYw",
"queryCount": 2,
"request_id": "6a7e466379af0a71039d60cc78e72282",
"results": [
{
"c": 75.0875,
"h": 75.15,
"l": 73.7975,
"n": 1,
"o": 74.06,
"t": 1577941200000,
"v": 135647456,
"vw": 74.6099
},
{
"c": 74.3575,
"h": 75.145,
"l": 74.125,
"n": 1,
"o": 74.2875,
"t": 1578027600000,
"v": 146535512,
"vw": 74.7026
}
],
"resultsCount": 2,
"status": "OK",
"ticker": "AAPL"
}
So it would look something like this:
import requests
open_prices = []
r = requests.get('https://api.polygon.io/v2/aggs/ticker/SPY/range/1/minute/2023-07-25/2023-07-25?apiKey=XXXXXXXXX')
for open in r['results']:
open_prices.append(open['o'])
print(open_prices)
You could parameterize the URL a bit cleaner, but let me know if this gets you going.
答案2
得分: 0
我将print重定向到文件而不是屏幕,作为一种临时解决方案。
with open('filename.txt', 'a') as f:
sys.stdout = f
print(a)
我可以更轻松地处理文本文件。
英文:
As a temporary solution, I redirected print to file instead of screen.
with open('filename.txt', 'a') as f:
sys.stdout = f
print(a)
I can deal with text file easier.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论