Python Forum
Stock Rate of Change (based on open and current price) Not Working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stock Rate of Change (based on open and current price) Not Working
#1
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)
Reply
#2
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Modify an Energy Model to account for year dependent interest rate rather than only t giovanniandrean 0 427 Oct-10-2023, 07:00 AM
Last Post: giovanniandrean
  How to do 100 runs simulation based on the current codes? dududada 6 988 Sep-03-2023, 01:43 PM
Last Post: deanhystad
  ANSI not working for change of text colors BliepMonster 10 3,409 Nov-10-2022, 09:28 AM
Last Post: BliepMonster
  Finding the price based on industry and number of transactions chandramouliarun 0 914 Jul-26-2022, 07:36 PM
Last Post: chandramouliarun
Question Change elements of array based on position of input data Cola_Reb 6 2,137 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  Openpyxl-change value of cells in column based on value that currently occupies cells phillipaj1391 5 9,849 Mar-30-2022, 11:05 PM
Last Post: Pedroski55
  time setup for realtime plotting of serial datas at high sampling rate alice93 6 3,753 Jan-07-2022, 05:41 PM
Last Post: deanhystad
  class - calculate total price of copies 3lnyn0 1 1,566 Dec-12-2021, 11:43 PM
Last Post: BashBedlam
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 3,175 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  An important question is how to create a zigzag in price data? epsilon 0 1,311 Nov-18-2020, 08:06 PM
Last Post: epsilon

Forum Jump:

User Panel Messages

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