Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
My First Notebook: Plotting SPY, DIA and QQQ Using .Apply on a DataFrame

Hi all,

This is my first notebook & forum post in Quantopian; while the idea is derived from the Getting Started modules, I did add a function - get_close - to apply to a DataFrame. This may not be necessary given the 'prices' feature; however, I need to practice writing my own functions so, no matter how basic, I may as well jump in and display for the world to see and provide feedback.

My end goal here is to create a dashboard of of sorts of relevant ETF & Equity prices and market indictors as a personal morning briefing, or at least serving as one of several notebooks dedicated to this purpose- as I see some need for not creating too large of a notebook to ensure clean viewing.

The below shown code are the standard imports to display price data using the research environment and matplotlib.pyplot. Additionally, I have imported timedelta to maintain a 365 day period when plotting the prices.

# Quantopian environment functions  
from quantopian.research import prices, symbols

# Pandas library: https://pandas.pydata.org/  
import pandas as pd  
import matplotlib.pyplot as plt  
from datetime import date, timedelta  

Here, I create a very short list and then a DataFrame from the list prior to the get_close function in the next sample code.

# create a df 'watchlist'  
watchlist = ['SPY', 'QQQ', 'DIA']  
df = pd.DataFrame(watchlist,columns=['Ticker'], index=watchlist)  

My function get_close is shown here. This function is derived from the 1st instructional notebook in the Getting Started modules. The only change made to the sample code from that notebook is, I have used timedelta to create the 365 day period.

# create a get_close function  
def get_close (row):  
    mkt_close = prices(  
    assets=symbols(row['Ticker']),  
    start= str(date.today() - timedelta(days=365)),  
    end= str(date.today()),)  
    return mkt_close  

I created a cell to display yesterday's close here until I learn how to display a realtime price, or last price. Additionally, in the absence of bokeh hovertools and plotyly cufflinks, I would like to learn how to write a mouseover function for the chart created from plot formatting.

prices(  
    assets=symbols(watchlist),  
    start= str(date.today() - timedelta(days=1)),  
    end= str(date.today()),)  

Shown below is the dot apply method of applying get_close to all rows of the DataFrame and the resulting image. Calling your attention to the .transpose() call; I had to do this to avoid having the figure display the referenced ETF symbols on the x-axis & displaying vertically the market prices.

df.apply(get_close, axis=1).transpose().plot()  

I hope this is a decent baseline for someone with no formal Python training outside of a few DataCamp Courses and I welcome feedback - more in the form of dropping hints vs full code suggestions.

Justin