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 Any suggestion to remove any error messages, improve it and streamline it (make it simpler) is definitely welcomed. Thank youThis 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 |