Python Forum
Stock Rate of Change (based on open and current price) Not Working - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Stock Rate of Change (based on open and current price) Not Working (/thread-20128.html)



Stock Rate of Change (based on open and current price) Not Working - firebird - Jul-29-2019

Hello Everyone!
Would you lend me your help. I'm trying to write (probably very momentarily) a stock rate of change that is based on open price and current price. The reason i want it based on open and current price is to get a better sense of what a particular stock compare with others in terms of their actual (not historical) rate of change.
When running the script this is what i get
Error:
Traceback (most recent call last): File "C:\Users\bgeor\Desktop\ROCfunction.py", line 27, in <module> dF_1 = pd1.DataFrame(dSet1) File "C:\Users\bgeor\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 392, in __init__ mgr = init_dict(data, index, columns, dtype=dtype) File "C:\Users\bgeor\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\construction.py", line 212, in init_dict return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype) File "C:\Users\bgeor\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\construction.py", line 51, in arrays_to_mgr index = extract_index(arrays) File "C:\Users\bgeor\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\construction.py", line 308, in extract_index raise ValueError('If using all scalar values, you must pass' ValueError: If using all scalar values, you must pass an index
Any suggestion to remove any error messages, improve it and streamline it (make it simpler) is definitely welcomed. Thank you
This this my full script:
#import all modules
from yahoofinancials import YahooFinancials
import pandas as pd1
import pandas as pd2
#Stock stickers to get data
stocks = ['AAPL']

#Function to extract data
def openPrice():
            yahoo_financials = YahooFinancials(stocks)
            Open = yahoo_financials.get_open_price()
            return Open


def currentPrice():
            yahoo_financials = YahooFinancials(stocks)
            price = yahoo_financials.get_current_price()
            return price


def ROC(op_price, cur_price):
    rOchg = float((cur_price - op_price)/op_price)*100
    return rOchg

if __name__ == "__main__":
    dSet1 = openPrice()
    dF_1 = pd1.DataFrame(dSet1)
    for index, row1 in dF_1.iterrows():
        dict_price1 = {}
        dict_price1.update(row1)
        for item1 in dict_price1.values():
            dSet2 = currentPrice()
            dF_2 = pd2.DataFrame(dSet2)
            for index, row2 in dF_2.iterrows():
                dict_price2 = {}
                dict_price2.update(row2)
                for item2 in dict_price2.values():
                    rateChangeVal = ROC(item1, item2)
                    print(rateChangeVal)



RE: Stock Rate of Change (based on open and current price) Not Working - perfringo - Jul-29-2019

Some observations:

- Why you do this:

import pandas as pd1
import pandas as pd2
- It is always good to stick to PEP8 - Function and Variable naming conventions (openPrice -> open_price)

- maybe you should try .from_records method