使用百分比法计算股票变化值
百分比法是计算股票变化值的常用方法。具体操作是:将当前股票价格与前一交易日的股票价格进行比较,计算出价格变动的百分比。
公式为:(当前价格 - 前一交易日价格) / 前一交易日价格 × 100%。
这种方法简单明了,可以快速得出股票变化的百分比。
print(f"This stock previous closing price is {base_date.previousClosingPrice()}")
TypeError: ‘float’ object is not callable
浮动对象不可调用?????
发现了。我没有更新代码,应该是getCurrentPr()
代码如下:
import exCode07
def main():
symbolStock = "INTC"
nameStock = "Inter Corporation"
previousNum = 20.5
currentPriceNum = 20.35
# symbolStock = input("Enter stock symbol is: ")
# nameStock = input("Enter stock name is: ")
# previousNum = eval(input("Enter stock previous closing price: "))
# currentPriceNum = eval(input("Enter stock current price: "))
base_date = exCode07.Stock(symbolStock, nameStock, previousNum, currentPriceNum)
print(f"This stock symbol is {base_date.getSymbol()}")
print(f"This stock name is {base_date.getName()}")
print(f"This stock previous closing price is {base_date.getPrevious()}")
print(f"This stock current price is {base_date.getCurrentPr()}")
print(f"This stock current price - previous closing price is {base_date.getChangePercent()}")
main()
原谅我的懒惰哈哈哈
然后是stock类的代码,还是存放在exCode07文件里。当然您也可以 把两段代码放一起。都行
class Stock:
def __init__(self, symbol, name, previousClosingPrice, 1currentPrice):
self.symbol = symbol
self.name = name
self.previousClosingPrice = previousClosingPrice
self.currentPrice = currentPrice
# 利用百分比计算法 计算股票变化值
def getChangePercent(self):
aTemp = (self.currentPrice - self.previousClosingPrice) / (self.previousClosingPrice * 100)
return round(aTemp, 4)
def setName(self, name):
self.name = name
def getName(self):
return self.name
def setSymbol(self, symbol):
self.symbol = symbol
def getSymbol(self):
return self.symbol
def getPrevious(self):
return self.previousClosingPrice
def getCurrentPr(self):
return self.currentPrice
标签:TypeError,name,创建,self,price,symbol,def,stock
From: https://blog.csdn.net/m0_37228426/article/details/141194453