2 Star 0 Fork 0

向大圣/量化回测

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
broker.py 2.11 KB
一键复制 编辑 原始数据 按行查看 历史
xiangsheng 提交于 2021-11-21 21:44 . 第一次提交
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)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xiangdasheng/quantitative-back-test.git
git@gitee.com:xiangdasheng/quantitative-back-test.git
xiangdasheng
quantitative-back-test
量化回测
master

搜索帮助