Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
list of stock price in new dataframe

hi,

i'm trying to get price from a list of stock and create a dataframe with the price of each stock in different columns, the code is the following.

import pandas as pd
import numpy as np

this is the list of stock i want the price in the new dataframe

universe = ['AAPL','KO','TSLA','PFE','GOLD','WFC','ABEV','DIS','VIST','HMY','AMGN','INTC','BBD','VALE','AUY','AMZN','QCOM']

for item in range(len(universe)):

stock = get_pricing(universe[item],start_date='2010-01-01',end_date='2020-9-14',frequency='daily')  

 #for each stock of the list i create a new column on the dataframe cedears  
 cedears[universe[item]] = pd.DataFrame(stock['price'])

cedears.head()

AAPL    KO  TSLA    PFE GOLD    WFC ABEV    DIS VIST    HMY AMGN    INTC    BBD VALE    AUY AMZN    QCOM  

0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
3 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
4 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

the dataframe as no date in the index and all the values are NaN. i cant find whats missing.

can you help me ??

2 responses

No need to loop through the list to get te prices in the format you want. The get_pricing method accepts a list or 'list like' iterable of assets or symbols. So something like this will work

# this is the list of stock symbols to get prices for  
universe = ['AAPL','KO','TSLA','PFE','GOLD','WFC','ABEV','DIS','VIST','HMY','AMGN','INTC','BBD','VALE','AUY','AMZN','QCOM']

# the get_pricing method will return a row for each date and a column for each stock  
stock_prices = get_pricing(universe,  
                           start_date='2010-01-01',  
                           end_date='2020-9-14',  
                           frequency='daily',  
                           fields='price') 

The key here is to specify fields='price'. Otherwise, the default is for the method to return the open, high, low, close, price, and volume in a pandas panel. When only a single field is requested, the get_pricing method returns a dataframe with a column for each stock .

Check out the attached notebook.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

thanks you Dan for your help!!