Cant use RESTClient result

186 Views Asked by At

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
2

There are 2 best solutions below

3
Douglas Korinke On BEST ANSWER

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.

https://polygon.io/docs/stocks/get_v2_aggs_ticker__stocksticker__range__multiplier___timespan___from___to

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.

0
Misha Zabrodin On

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.