Python Forum
Comparing Values Resulting from Function Outputs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comparing Values Resulting from Function Outputs
#1
Hello Everyone!
I have some functions both pulling data, stocks for one and indices for the other, from yahoo finance. The output of the function looks something like this:

{'AAPL': 208.67, 'MSFT': 140.72}<--current price
{'AAPL': 207.67, 'MSFT': 138.9}<--open price
{'AAPL': 209.15, 'MSFT': 140.74}<--high
{'AAPL': 207.17, 'MSFT': 138.85}<--low
{'AAPL': -0.00081401155, 'MSFT': 0.010266409}<--percentage change
{'AAPL': 14991567, 'MSFT': 20738275}<--volume

What's attempted is to compare the percentage changes of elements of stocks = ['AAPL','MSFT','TSLA'] against that of indices = ['^IXIC','^DJI','^GSPC'] and then do something from there (such as print something for example).
Thank you for the help!
The error i get when running the script is:
Error:
Traceback (most recent call last): File "C:\Users\bgeor\Desktop\valChg_test1.py", line 38, in <module> if percent_stock < percent_index: File "C:\Users\bgeor\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\ops.py", line 2096, in f raise ValueError('Can only compare identically-labeled ' ValueError: Can only compare identically-labeled DataFrame objects
The script:
from yahoofinancials import YahooFinancials
#import all modules
from yahoofinancials import YahooFinancials
import pandas as pd1
import pandas as pd2
#Stock stickers to get data - declared globally
stocks = ['AAPL','MSFT','TSLA']
#NASDAQ Composite (^IXIC), Dow Jones Industrial Average (^DJI), S&P 500 (^GSPC)
indices = ['^IXIC','^DJI','^GSPC']


#Function to extract stock data
def getStockData():
            yahoo_financials = YahooFinancials(stocks)#stocks = ['AAPL','MSFT','TSLA']
            price = yahoo_financials.get_current_price()
            Open = yahoo_financials.get_open_price()
            High = yahoo_financials.get_daily_high()
            Low = yahoo_financials.get_daily_low()
            perChg1 = yahoo_financials.get_current_percent_change()
            return(price, Open, High, Low, perChg1)
#Function to extract index data
def getIndexData():
            yahoo_financials = YahooFinancials(indices)#indices = ['^IXIC','^DJI','^GSPC']
            price = yahoo_financials.get_current_price()
            Open = yahoo_financials.get_open_price()
            High = yahoo_financials.get_daily_high()
            Low = yahoo_financials.get_daily_low()
            perChg2 = yahoo_financials.get_current_percent_change()
            return(price, Open, High, Low, perChg2)
#Here we want to access the directional movement of stocks = ['AAPL','MSFT','TSLA']
#by comparing it to that of perccentage change of indices = ['^IXIC','^DJI','^GSPC']
for row in getStockData():
    percent_stock = pd1.DataFrame([{'perChg1' : row}])
    #carry on a logic here
    for val in getIndexData():
        percent_index = pd2.DataFrame([{'perChg2' : val}])
#compare percentage change of (stocks = ['AAPL','MSFT','TSLA']) against that of (indices = ['^IXIC','^DJI','^GSPC'])
        if percent_stock < percent_index:
            print(percent_stock)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python outputs to Excel NewBiee 3 812 Nov-26-2023, 07:25 AM
Last Post: DPaul
  Outputs "NaN" after "DataFrame columns" function? epsilon 7 3,572 Jan-27-2021, 10:59 AM
Last Post: epsilon
  Basic storage of function values for monte carlo simulation glidecode 1 1,713 Apr-15-2020, 01:41 AM
Last Post: jefsummers
  how to get x values based on y-axis values from curvefit function python_newbie09 1 3,232 Sep-19-2019, 02:09 AM
Last Post: scidam
  How get values from ACF function franromanos6 2 5,311 Mar-13-2018, 03:44 PM
Last Post: franromanos6

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020