股票模拟买卖

# -*- 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 True

s = 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

你可能感兴趣的:(python,python)