股票模拟买卖
来源:互联网 发布:怎么利用淘宝达人赚钱 编辑:程序博客网 时间:2024/04/29 23:47
# -*- coding:cp936 -*-class stock: def __init__(self,capital=10000): self.base = capital self.capital = capital self.stock_holdings = 0 self.commission_rate = 0.0005 #佣金比例 self.stamp_tax = 0.001 #印花税 self.pay = 0 self.earn = 0 self.costing = 0 def __buy_fee(self,price,quantity): if price*quantity*self.commission_rate < 5: return 5 else: return price*quantity*self.commission_rate def __sell_fee(self,price,quantity): return self.__buy_fee(price,quantity) + price*quantity*self.stamp_tax def __show(self): print('\n当前本金:%.2f' % float(self.capital)) if self.stock_holdings != 0: print('当前持股:%d 持股成本:%.2f\n' % (self.stock_holdings,float(self.costing))) return True else: earn_temp = self.earn - self.pay earn_rate = earn_temp/self.pay*100 print('本次交易盈亏:%.2f 本次交易盈利率:%.2f%%' % (earn_temp,earn_rate)) earn_temp = self.capital - self.base earn_rate = earn_temp/self.base*100 print('总交易盈亏:%.2f 总交易盈利率:%.2f%%\n' % (earn_temp,earn_rate)) self.pay = 0 self.earn = 0 self.costing = 0 return True def buy(self,price,quantity): if price*quantity > self.capital: print "资金不足,买入失败" return False else: self.pay = self.pay + price*quantity + self.__buy_fee(price,quantity) self.stock_holdings = self.stock_holdings + quantity self.costing = (self.pay - self.earn)/self.stock_holdings self.capital = self.capital - price*quantity - self.__buy_fee(price,quantity) self.__show() return True def sell(self,price,quantity): if quantity > self.stock_holdings: print "卖出股份数量超过持有,卖出失败" return False else: self.earn = self.earn + price*quantity - self.__sell_fee(price,quantity) self.stock_holdings = self.stock_holdings - quantity self.capital = self.capital + price*quantity - self.__sell_fee(price,quantity) if self.stock_holdings != 0: self.costing = 1.0*(self.pay - self.earn)/self.stock_holdings self.__show() return True def share(self,quantity,dividend): temp = self.stock_holdings self.stock_holdings = temp + int(temp/10*quantity) print self.pay self.earn = self.earn + temp/10*dividend print self.earn self.costing = 1.0*(self.pay - self.earn)/self.stock_holdings self.__show() return Trues = stock()while True: select = raw_input("输入当前操作:1.买入 2.卖出 3.送配\n") if select=='1': price = input('输入买入价格:') print('当前可用资金:%.2f 可购买股份数:%d' % (s.capital,int(s.capital/price))) quantity = input('输入购买股份数:') s.buy(price,quantity) elif select=='2': print('当前持股成本:%.2f 可卖出股份数:%d' % (s.costing,s.stock_holdings)) price = input('输入卖出价格:') quantity = input('输入卖出股份数:') s.sell(price,quantity) elif select=='3': quantity = input('每10股送配股份数:') dividend = input('每10股分红金额:') s.share(quantity,dividend) else: break
0 0
- 股票模拟买卖
- 买卖股票
- 如何买卖股票
- 股票操作买卖守则
- 买卖股票计算公式
- 买卖股票之道
- 题目1537:买卖股票
- 买卖股票的最佳时机
- LeetCode之买卖股票
- 买卖股票的最佳时机
- 买卖股票的最佳时机
- Stock(买卖股票)
- 【DP?】买卖股票
- LintCode-买卖股票的最佳时机
- 题目:买卖股票的最佳时机
- 买卖股票最多K次
- LintCode--买卖股票的最佳时机
- 买卖股票收益最大问题
- Android 对Layout_weight属性完全解析以及使用ListView来实现表格(自定义适配器)
- N皇后问题的两个最高效的算法
- B/S 安全与基本攻击 / 防御模式
- 移动交换中心(MSC/VLR) .
- iOS逆向 - 工具篇
- 股票模拟买卖
- Error parsing DDL for `por`.`info`
- linux su和sudo命令的区别
- SQL 最日期
- 鉴权加密
- mfc static,checkbox,groupbox,redio 控件透明
- HTML5 web应用打包成android应用
- 全排列算法
- 开通csdn博客。记录自己点滴成长