Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

python - Alpha Vantage: Iterate through list of stocks to get technical indicators

I have a list of 5 stocks for which I would like to obtain data using Alpha Vantage's TechIndicators. Below are what I have imported and defined:

from alpha_vantage.timeseries import TimeSeries
from alpha_vantage.foreignexchange import ForeignExchange
from alpha_vantage.cryptocurrencies import CryptoCurrencies
from alpha_vantage.techindicators import TechIndicators
from alpha_vantage.sectorperformance import SectorPerformances
import datetime
import numpy as np
import pandas as pd

top5 = ['CELH', 'MAXR', 'CD', 'WDC', 'IMAB']

Querying for technical indicators returns a dataframe (data) and a dictionary (meta_data)

data, meta_data = ti.get_sma(symbol=ticker, interval='weekly')

How do I run the query for my top5 list? I first thought it should look like this:

for ticker in top5:
    ticker_sma[ticker], ticker_meta_sma[ticker] = 
    ti.get_sma(symbol=ticker, interval='weekly')

or like this:

sma = {}
for ticker in top5gainers:
    gainers_sma[ticker], gainers_sma_data[ticker] = 
    ti.get_sma(symbol=ticker, interval='weekly')
gainers_sma.keys()
gainers_sma.values()

But I have had zero luck with that approach. I get a name error...

NameError: name 'ticker_sma' is not defined

...which is odd to me because I have success getting data for an individual stock, e.g., Microsoft, below:

msft_sma, msft_meta_sma = ti.get_sma(symbol='msft', interval='weekly')

If I remove [ticker], and just run this get request...

for ticker in top5:
    data_sma, meta_sma = ti.get_sma(symbol=ticker, interval='weekly')

...then according to the meta data in meta_sma, I've only obtained data for the 4th ticker out of the 5 tickers:

{'1: Symbol': 'IMAB', '2: Indicator': 'Simple Moving Average (SMA)', '3: Last Refreshed': '2020-12-31', '4: Interval': 'weekly', '5: Time Period': 20, '6: Series Type': 'close', '7: Time Zone': 'US/Eastern'}

Thank you for reading this question and for your answer!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are trying to dynamically create variables, which isn't exactly straightforward. The following shows you how to do that, but you may also want to consider just reading each symbol into its own dataframe and either concatenating them together or saving individually. That really just depends on what you are doing with the data (using live or saving for later).

This will create each variable in the loop and assign text to it, just so you can what's going on.

top5 = ['CELH', 'MAXR', 'CD', 'WDC', 'IMAB']

for ticker in top5:
    globals()['{}_sma'.format(ticker)] = ticker + 'sma_value'
    globals()['{}_meta_sma'.format(ticker)] = ticker + 'sma_meta_value'

Then print a few for proof:

In [6]: print(CELH_sma)
   ...: print(WDC_sma)
   ...: print(IMAB_meta_sma)
CELHsma_value
WDCsma_value
IMABsma_meta_value

I can't test but what I think will work for you is this:

for ticker in top5:
    globals()['{}_sma'.format(ticker)], globals()['{}_meta_sma'.format(ticker)] =
    ti.get_sma(symbol=ticker, interval='weekly')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...