Python Forum
creating a list of dictionaries from API calls
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
creating a list of dictionaries from API calls
#1
Hello,

I am having trouble formulating a list of dictionaries which incorporate information from an API call which will set the KEY:VALUE pairs.

I am trying to get this result:

Output:
MainList[ {'Asset_ID': 012334, 'AAPL': 238.75}, {'Asset_ID': 567890, 'FB': 28.50}, {'Asset_ID': 123345, 'BT': 203.55}, {'Asset_ID': 678901, 'C': 38.25} etc.............]
and this is my current code:

import requests
import json

BASE_URL = 'https://paper-api.alpaca.markets'
HEADERS = {'APCA-API-KEY-ID': "", 'APCA-API-SECRET-KEY': ""}
ACCOUNT_URL = '{}/v2/account'.format(BASE_URL)
ASSETS_URL = '{}/v2/assets'.format(BASE_URL)
POSITIONS_URL = '{}/v2/positions'.format(BASE_URL)

#asset API provides the stock symbol(AAPL,FB etc.)& the associated asset ID's.
assets_r = requests.get(ASSETS_URL, headers=HEADERS)
get_assets = json.loads(assets_r.content)

#positions API provides the Current_value
positions_r = requests.get(POSITIONS_URL, headers=HEADERS)
get_position = json.loads(positions_r.content)

dictionary = dict

Symbol_items = dictionary(get_assets)['symbol', range(1)]

Asset_ID_items = dictionary(get_assets)['id', range(1)]

price_items = dictionary(get_position)['current_price', range(1)]

dictionary = {'Asset_ID' : Asset_ID_items, Symbol_items: price_items}

print(dictionary)
--- when it runs I get this message,

Error:
Traceback (most recent call last): File "C:/Users/engli/PycharmProjects/tester01/rambo/first_Test.py", line 24, in <module> Symbol_items = dictionary(get_assets)['symbol', range(1)] ValueError: dictionary update sequence element #0 has length 10; 2 is required
Any help will be greatly appreciated!

Kind Regards,

Andrew
Reply
#2
What exactly are you trying to do with this line (and next 2):

Symbol_items = dictionary(get_assets)['symbol', range(1)]
and why you create alias for dict function before that?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
To get the assets_r as list:
assets_r = requests.get(ASSETS_URL, headers=HEADERS).json()
Then you have to access the symbols with POSITIONS_URL.
The URL is: https://paper-api.alpaca.markets/v2/positions/{symbol}

I guess you have to request each symbol... Very confusing.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
(Apr-03-2020, 05:00 AM)buran Wrote: What exactly are you trying to do with this line (and next 2):

Symbol_items = dictionary(get_assets)['symbol', range(1)]
and why you create alias for dict function before that?

Hi Buran,

Without the 'dictionary = dict' PyCharm was asking me to declare what dictionary was.

I am trying to create a function which will place 'symbol', 'id' and 'price' without actually stating what they are manually.

The range(1) was added in an attempt to resolve the Traceback error but nothing changed. The thought process was to take the first value from the api call 'symbol' as it provides a list of stock symbols.
This is the same thought process with 'ID' and 'Price'.

I need to also loop it for every value from the API call but Ia wanted to see if this process works before I attempted to loop it.

I hope this helped.

Kind Regards,

Andrew

(Apr-03-2020, 09:52 AM)DeaD_EyE Wrote: To get the assets_r as list:
assets_r = requests.get(ASSETS_URL, headers=HEADERS).json()
Then you have to access the symbols with POSITIONS_URL.
The URL is: https://paper-api.alpaca.markets/v2/positions/{symbol}

I guess you have to request each symbol... Very confusing.

Hi Deadeye,

Thank you for your advice, there are over 1000+ symbols so it would be rather impractical to request each symbol. unless it was possible to automate it with loop function?

I am basically trying to build a dictionary which inputs the 'KEY:VALUE' pairs from functions calling data from the API. i have declared the functions and place them into the dictionary itself. It may not be the most efficient way to do it but that is the direction I was heading with it?

I agree, it is confusing.. Wall

Kind Regards,

Andrew
Reply
#5
First of all, why would you requests the assets,
you can just request positions (there is all info you want - AssetID, symbol and price).
You can get what you want from there.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(Apr-03-2020, 01:53 PM)buran Wrote: First of all, why would you requests the assets,
you can just request positions (there is all info you want - AssetID, symbol and price).
You can get what you want from there.

Good question, I have thought about this. The 'Positions' only provide the current price of the asset you buy from Alpaca. The real-time pricing API call would be changed to 'Polygon.io' but his comes at a cost £+100 a month. - I cannot justify this at the moment. it is still early stages but once this code is resolved I will transfer the API call from Alpaca's position to Polygon.io's real-time pricing.

The Asset API call does provide all the AssetID's and Symbol's just not the Real-time pricing.

I have bought a share on this paper-trading platform so once this code is resolved it will show 1 'current_price' value for the time being but once the call is adapted to Polygon.io it will provide all real-time pricing.

I will have to match the stock symbol from Alpaca with Polygon to get the corresponding price. It has to match Alpaca since that is the trading platform.

Kind Regards,

Andrew
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
  Access list of dictionaries britesc 4 1,028 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,883 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  function that returns a list of dictionaries nostradamus64 2 1,701 May-06-2021, 09:58 PM
Last Post: nostradamus64
  Calls to Attributes of a Class SKarimi 3 3,339 Apr-22-2021, 04:18 PM
Last Post: SKarimi
  convert List with dictionaries to a single dictionary iamaghost 3 2,805 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  Creating list of lists from generator object t4keheart 1 2,162 Nov-13-2020, 04:59 AM
Last Post: perfringo
  Creating a dictionary from a list Inkanus 5 3,108 Nov-06-2020, 06:11 PM
Last Post: DeaD_EyE
  list call problem in generator function using iteration and recursive calls postta 1 1,863 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  Creating a list of dictionaries while iterating pythonnewbie138 6 3,195 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020