代码拉取完成,页面将自动刷新
import math
class Broker:
def __init__(self, cash=1000000, commission=0.001):
# 现金
self.cash = cash
# 持股
self.stock = {}
self.stock_price = {}
# 账户价值
self.value = cash
# 手续费
self.commission = commission
# 买入股票(根据剩余席位 平分cash)
def buy(self, code, price):
money = self.cash / (10 - len(self.stock))
by = money // (100 * price)
# 如果给的钱足够支付佣金和股票
whole = 100 * by * price * (1 + self.commission)
# 减少买入 直到能支付佣金
while money < whole:
by = by - 1
whole = 100 * by * price * (1 + self.commission)
if by > 0:
self.cash = self.cash - whole
self.value = self.value - 100 * by * price * self.commission
self.stock[code] = 100 * by
self.stock_price[code] = price
# 卖出股票
def sell(self, code, price):
self.cash = self.cash + self.stock[code] * price * (1 - self.commission)
self.value = self.value - self.stock[code] * price * self.commission
self.stock.pop(code)
self.stock_price.pop(code)
# 股价浮动 传入股票的价格 重新计算账户的价值
def float_price(self, price):
total_stock = 0
for key in self.stock.keys():
# 本次有股票信息
if price[key] != 0:
self.stock_price[key] = price[key]
total_stock = total_stock + self.stock_price[key] * self.stock[key]
self.value = self.cash + total_stock
return self.value
# 测试数据变动的过程中是否存在错误
def test(self):
count = 0
for key in self.stock.keys():
count = count + self.stock[key]*self.stock_price[key]
if count + self.cash != self.value:
print(count)
print(self.cash)
print(self.value)
return False
else:
return True
def print_log(self):
print("股票池:", self.stock)
print("值", self.value)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。