简体   繁体   English

基本 python - 计算收盘价和开盘价之间的百分比变化

[英]Basic python - calculating %change between close and open price

I've got the following question and the challenging part of for me is to calculate the wanted value without using panda.我有以下问题,对我来说具有挑战性的部分是在不使用熊猫的情况下计算所需的价值。

Question:题:

  1. Please download a CSV file containing the stock history of some companies, for example from:请下载包含一些公司股票历史的 CSV 文件,例如:
    http://finance.yahoo.com/q/hp?s=GOOG http://finance.yahoo.com/q/hp?s=GOOG
    http://finance.yahoo.com/q/hp?s=IBM http://finance.yahoo.com/q/hp?s=IBM
    http://finance.yahoo.com/q/hp?s=MSFT http://finance.yahoo.com/q/hp?s=MSFT

(Download Data) (下载资料)
Save files giving them different names to a local folder on your drive将文件以不同的名称保存到驱动器上的本地文件夹中

  1. Write a program that searches for CSV files with stock rates in a given folder and for every one of them:编写一个程序,在给定文件夹中搜索包含股票价格的 CSV 文件,并搜索其中的每个文件:

  2. Calculates the percentage change between Close and Open price and adds these values as another column to this CSV file.计算收盘价和开盘价之间的百分比变化,并将这些值作为另一列添加到此 CSV 文件中。
    You can replace the old file or create a new one.您可以替换旧文件或创建新文件。

Change = (Close-Open)/Open变化 =(关闭-打开)/打开

  1. The output files can be stored in another folder输出文件可以存储在另一个文件夹中
  2. You can use Python to download files.您可以使用 Python 下载文件。 An example is given here:这里给出了一个例子:
    https://raw.githubusercontent.com/prubach/Python_Winter_2022_1/master/download_file.py https://raw.githubusercontent.com/prubach/Python_Winter_2022_1/master/download_file.py
  3. Please do not use pandas, or only use it as an alternative way of implementing it along a more "manual" way using just python without any libraries.请不要使用 pandas,或者仅使用它作为一种替代方式,以更“手动”的方式使用不带任何库的 python 实现它。
from urllib import request
# Define the remote file to retrieve
url = 'https://query1.finance.yahoo.com/v7/finance/download/IBM?period1=1639213520&period2=1670749520&interval=1d&events=history&includeAdjustedClose=true'
# Define the local filename to save data
file_IBM = 'IBM.csv'
# Download remote and save locally
request.urlretrieve(url, file_IBM)

class Change:
    def __init__(self,o,c):
        self.open = o
        self.close = c
    def calc_percentage(self):
        return (self.c - self.o)/self.o

    r = Change(IMB.csv)

print(r.calc_percentage)

I tried to make a new column using an object-oriented format but it's not working.我尝试使用面向对象的格式创建一个新专栏,但它不起作用。 And how I am writing my code is I have my data file on a separate python page and I am writing the code on another page and faced some issues connecting these 2 pages我是如何编写代码的,我将数据文件放在一个单独的 python 页面上,我在另一个页面上编写代码,但在连接这两个页面时遇到了一些问题

with open('IBM.csv', 'r') as file: reader = csv.reader(file) for column in reader: print(column) with open('IBM.csv', 'r') 作为文件:reader = csv.reader(file) for column in reader: print(column)

Okay, so far what I did was to use this code to read the data file I have.好的,到目前为止我所做的是使用这段代码来读取我拥有的数据文件。 now I should use this formula (close - open)/open Now my question is how should I implement this formula into this code?!现在我应该使用这个公式(关闭 - 打开)/打开现在我的问题是我应该如何将这个公式实现到这段代码中?!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM