Search code examples
pythonstreamlityfinance

How to pass a variable as an argument of another function in python


I have a list (chart_list) and I want to call them one by one and plot the chart but I face an error. How can I deal with this problem? I know that they are strings but I don't know how to give it to the tickerDf.

    
    import streamlit as st
    import yfinance as yf
    import pandas as pd
    import datetime
    
    
      
    cols = st.columns(2)
    
    # define the ticker symbol
    tickerSymbol = cols[0].text_input("Symbol:", 'GOOG')
    st.markdown(f'Shown are the **stock closing** price and **volume** of **{tickerSymbol}**')
    
    # get data on this ticker
    tickerData = yf.Ticker(tickerSymbol)
    
    period_list = ['1d', '5d']
    selected_period = cols[0].selectbox("Period:", period_list)
    
    interval_list = ['1m', '2m', '5m', '15m', '30m', '60m', '90m', '1h', '1d', '5d', '1wk', '1mo', '3mo']
    selected_interval = cols[1].selectbox("Interval", interval_list)
    
    today = datetime.date.today()
    yesterday = today + datetime.timedelta(days=-10)
    start_date = cols[0].date_input('Start date', yesterday)
    end_date = cols[1].date_input('End date', today)
    if start_date > end_date:
        st.error("Error: End date must fall after start date")
    
    # get the historical prices for this ticker
    tickerDf = tickerData.history(interval=selected_interval, start=start_date, end=end_date)
    # Open High Low Close Volume Dividends Stock Splits
    
    chart_list = ['Open', 'High', 'Low', 'Close', 'Volume']
    selected_charts = st.multiselect("Charts", chart_list)
    
    if st.button("Show"):
        for chart in chart_list:
            st.line_chart(tickerDf.chart)
            st.write(f"## {chart}")

Solution

  • You can't index into a dataframe like this (st.line_chart(tickerDf.chart)), as this is a literal specification of the column name.

    Try st.line_chart(tickerDf[chart]) instead