Search code examples
pythonstock

How to insert the correct stock name into a list


I'm trying to create a stock screener and have no idea what I'm doing so please forgive my ignorance. I will try to explain my plan and please let me know if I'm totally off in my thinking!

import requests #The requests library for HTTP requests in Python
import csv
import pandas as pd
from secrets import IEX_CLOUD_API_TOKEN_TEST
from secrets import IEX_CLOUD_API_TOKEN_LIVE

Firstly I'm getting a list of symbols I want to screen:

   api_url = f'https://cloud.iexapis.com/stable/ref-data/iex/symbols?format=csv&token={IEX_CLOUD_API_TOKEN_LIVE}'
    tickers = []
    with requests.Session() as s:
        download = s.get(api_url)
    
        decoded_content = download.content.decode('utf-8')
    
        cr = csv.reader(decoded_content.splitlines(), delimiter=',')
        my_list = list(cr)
        for row in my_list:
            tickers.append(row[0])
    
    print(tickers)

This gives me:

['symbol', 'A', 'AA', 'AAA', 'AAAU', 'AAC', 'AAC+', 'AAC=', 'AACG', 'AACI', 'AACIU', 'AACIW', 'AADI', 'AADR', 'AAIC', 'AAIC-B', 'AAIC-C', 'AAIN', 'AAL', ...]

I'm then looping through the tickers and using the Alpha Vantage API to return the daily 1m data:

stocksIntraDay = []

def getIntraday():
    for ticker in tickers[2]:
        print(ticker)
        date = 'year1month1'
        apiKey = '5MACTR59FOK093UR'

        CSV_URL = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY_EXTENDED&symbol={ticker}&outputsize=full&interval=1min&slice={date}&apikey={apiKey}'

        with requests.Session() as s:
            download = s.get(CSV_URL)
            decoded_content = download.content.decode('utf-8')
            cr = csv.reader(decoded_content.splitlines(), delimiter=',')
            my_list = list(cr)
            for row in my_list:
                row.append(ticker)
                print(row)

getIntraday()

which (as an example) gives me:

['time', 'open', 'high', 'low', 'close', 'volume', 'A']
['2022-01-18 16:07:00', '141.32', '141.32', '141.32', '141.32', '100', 'A']
['2022-01-03 15:03:00', '63.385', '63.3864', '63.3507', '63.365', '52168', 'C']

From here I'm trying to make something like this:

[ticker] ['time', 'open', 'high', 'low', 'close', 'volume']
[A]['2022-01-18 16:07:00', '141.32', '141.32', '141.32', '141.32', '100']
...
[AA]['2022-01-18 15:03:00', '63.385', '63.3864', '63.3507', '63.365', '52168']
...
[AAA]['2022-01-18 15:03:00', '63.385', '63.3864', '63.3507', '63.365', '52168']
...
[AAAU]['2022-01-18 15:03:00', '63.385', '63.3864', '63.3507', '63.365', '52168']

I.e return a list of symbols with their intraday data AND their respective ticker name.

Hopefully I have explained my problem clearly. I am sorry I cannot share the entire logs they would make the question too big.

Alpha docs: https://www.alphavantage.co/documentation/


Solution

  • You can always just loop through your results to reorganize the data.

    If I understand correctly, you have the ticker in index 6 of the list. You results are a list of lists. Now, you need to add a return to your getIntraday() functions, like this...

    def getIntraday():
        for ticker in tickers[2]:
            print(ticker)
            date = 'year1month1'
            apiKey = '5MACTR59FOK093UR'
    
            CSV_URL = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY_EXTENDED&symbol={ticker}&outputsize=full&interval=1min&slice={date}&apikey={apiKey}'
    
            with requests.Session() as s:
                download = s.get(CSV_URL)
                decoded_content = download.content.decode('utf-8')
                cr = csv.reader(decoded_content.splitlines(), delimiter=',')
                my_list = list(cr)
                for row in len(my_list):
                    my_list[row].append(ticker)
                    print(row)
        return my_list
    
    

    After getting this, you would just need to reorganize your list, if you need, although your data is there, so more information would be needed for me to understand.