股票连续投资历史收益计算

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MengXP/article/details/79416817
假设每月有几千块钱投资某股票,在每月第一个星期五按收盘价买入,购买一定时间后全部卖出,计算收益率


由于有除权这种事件存在,所以不能简单的按照历史价格进行计算。
后复权:以时间起点往后复权,分红送股向上平移,用于计算历史收益,时间起点是股票发行日
前复权:以时间终点往前复权,分红送股向下平移,用于观察涨跌行情?


计算历史上某一段时间的收益,算法如下。
股票买入价:获取不复权的股价数据,寻找当日的股价A。
股票卖出价:获取后复权的股价数据,寻找买入日和卖出日的股价X和Y,做比例缩放即可。A/X*Y,这样就可以将中间的除权事件包含在内了。


python环境anaconda 5.1.0 
股票接口使用tushare


完整计算器代码如下:
# 测试环境 anaconda 5.1.0
# tushare 安装 pip install tushare
import sys
import datetime
import tushare as ts


def get_stock_price(pricedata, buydate):
  # pricedate 股价数据
  # buydate 买入日期,字符串 例 2012-01-01
  # 返回收盘价 如果停牌返回0
  for index, row in pricedata.iterrows():
    if row.date == buydate:
      return row.close
  return 0


def get_max_buy_count(price, fund):
  count = 100
  while count * price < fund:
    count += 100
  return count - 100


def get_last_trade_date(pricedata, selldate_exp):
  # pricedate 股价数据
  # selldate_exp 期望的卖出日期,字符串 例 2012-01-01
  # 返回距离期望最近的交易日期
  dsell = datetime.datetime.strptime(selldate_exp,'%Y-%m-%d')
  dret = 0
  for index, row in pricedata.iterrows():
    drow = datetime.datetime.strptime(row.date,'%Y-%m-%d')
    if drow <= dsell:
      dret = drow
    else:
      break
  if dret == 0:
    return 0
  return dret.strftime("%Y-%m-%d")


def calc_stock(price, price_hfq, buydate, selldate, fund):
  # price 股价数据 不复权
  # price_hfq 股价数据 后复权
  # buydate 买入日期
  # selldate 卖出日期
  # fund 购买资金
  # 返回 买入单价,买入单价后复权,卖出单价,购买数量
  buyprice = get_stock_price(price, buydate)
  buyprice_hfq = get_stock_price(price_hfq, buydate)
  sellprice_hfq = get_stock_price(price_hfq, selldate)
  if buyprice == 0 or buyprice_hfq == 0 or sellprice_hfq == 0:
    return 0,0,0,0
  buycount = get_max_buy_count(buyprice, fund)
  sellprice = buyprice / buyprice_hfq * sellprice_hfq
  return buyprice, buyprice_hfq, sellprice, buycount


def main(stock, start, end, fundpermonth):
  print('股票代码: ', stock)
  print('开始日期: ', start)
  print('卖出日期: ', end)
  print('每月资金: ', fundpermonth)


  # 获取不复权股价数据
  print('正在获取不复权股价数据')
  data_bfq = ts.get_k_data(stock,start,end,autype='none')
  print('正在获取后复权股价数据')
  data_hfq = ts.get_k_data(stock,start,end,autype='hfq')


  # 获取最后卖出日
  selldate = get_last_trade_date(data_bfq, end)
  if selldate == 0:
    print('卖出日期不存在')
    sys.exit(1)


  print('正在计算连续投资收益')
  # 遍历交易日 (每个月第一个星期五)
  dstart = datetime.datetime.strptime(start,'%Y-%m-%d')
  dend = datetime.datetime.strptime(end,'%Y-%m-%d')
  dtmp = dstart
  oneday = datetime.timedelta(days=1)
  month_buy = 0
  month_fund = 0
  total_buy = 0
  total_sell = 0
  total_stock = 0
  fund_pool = 0
  last_price_hfq = 0
  while dtmp <= dend:
    if dtmp.month != month_fund:
      # 新的月份,资金池累加
      fund_pool += int(fundpermonth)
      month_fund = dtmp.month
    if dtmp.month != month_buy:
      if dtmp.weekday() == 4:    #是个星期五
        # 检查是否可以购买
        print('=================================')
        buydate = dtmp.strftime("%Y-%m-%d")
        price = get_stock_price(data_bfq, buydate)
        if price > 0:
          # 可以购买 买入
          buyprice,buyprice_hfq,sellprice,buycount = calc_stock(data_bfq, data_hfq, buydate, selldate, fund_pool)
          if last_price_hfq > 0:
            gain = (buyprice_hfq - last_price_hfq) / last_price_hfq * 100
            print('同比增长: ', round(gain, 2), '%')
          print('买入日期: ', buydate)
          print('买入单价: ', round(buyprice, 2))
          print('卖出单价: ', round(sellprice, 2))
          print('买入数量: ', buycount)
          print('当月投资: ', round(buyprice * buycount, 2))
          total_buy += buyprice * buycount
          total_stock += buycount
          total_sell += sellprice * buycount
          fund_pool -= buyprice * buycount
          print('剩余资金: ', round(fund_pool, 2))
          print('当期总投资: ', round(total_buy, 2))
          print('当期总持股: ', total_stock)
          print('当期总收入: ', round(total_sell, 2))
          print('当期总收益: ', round((total_sell - total_buy) / total_buy * 100, 2), '%')
          last_price_hfq = buyprice_hfq
          month_buy = dtmp.month
        else:
          # 停牌 无法交易
          print('买入日期: ', buydate)
          print('停牌中,无法交易')
          
    dtmp += oneday
  
  rate = round((total_sell - total_buy) * 100 / total_buy, 2)
  print('=================================')
  print('总投资额: ', round(total_buy, 2))
  print('总持有量: ', total_stock)
  print('卖出金额: ', round(total_sell, 2))
  print('总收益率: ', rate, '%')


# 命令行参数
# 股票代码 买入起始日期 卖出日期 每月资金
if __name__ == "__main__":
  if len(sys.argv) < 5:  
    print('Usage: calc [stockcode] [startdate] [selldate] [fundpermonth]')
    print('Exam:  calc 000002 2012-01-01 2016-01-01 8000')
    sys.exit()
  main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])


测试输出结果


(base) D:\stock>python main.py 000002 2012-01-01 2012-12-31 8000
股票代码:  000002
开始日期:  2012-01-01
卖出日期:  2012-12-31
每月资金:  8000
正在获取不复权股价数据
正在获取后复权股价数据
正在计算连续投资收益
=================================
买入日期:  2012-01-06
买入单价:  7.16
卖出单价:  10.27
买入数量:  1100
当月投资:  7876.0
剩余资金:  124.0
当期总投资:  7876.0
当期总持股:  1100
当期总收入:  11292.3
当期总收益:  43.38 %
=================================
同比增长:  8.8 %
买入日期:  2012-02-03
买入单价:  7.79
卖出单价:  10.27
买入数量:  1000
当月投资:  7790.0
剩余资金:  334.0
当期总投资:  15666.0
当期总持股:  2100
当期总收入:  21558.04
当期总收益:  37.61 %
=================================
同比增长:  10.78 %
买入日期:  2012-03-02
买入单价:  8.63
卖出单价:  10.27
买入数量:  900
当月投资:  7767.0
剩余资金:  567.0
当期总投资:  23433.0
当期总持股:  3000
当期总收入:  30797.2
当期总收益:  31.43 %
=================================
同比增长:  -2.9 %
买入日期:  2012-04-06
买入单价:  8.38
卖出单价:  10.27
买入数量:  1000
当月投资:  8380.0
剩余资金:  187.0
当期总投资:  31813.0
当期总持股:  4000
当期总收入:  41062.93
当期总收益:  29.08 %
=================================
同比增长:  10.02 %
买入日期:  2012-05-04
买入单价:  9.22
卖出单价:  10.27
买入数量:  800
当月投资:  7376.0
剩余资金:  811.0
当期总投资:  39189.0
当期总持股:  4800
当期总收入:  49275.52
当期总收益:  25.74 %
=================================
同比增长:  -0.76 %
买入日期:  2012-06-01
买入单价:  9.15
卖出单价:  10.27
买入数量:  900
当月投资:  8235.0
剩余资金:  576.0
当期总投资:  47424.0
当期总持股:  5700
当期总收入:  58514.68
当期总收益:  23.39 %
=================================
同比增长:  6.98 %
买入日期:  2012-07-06
买入单价:  9.65
卖出单价:  10.12
买入数量:  800
当月投资:  7720.0
剩余资金:  856.0
当期总投资:  55144.0
当期总持股:  6500
当期总收入:  66610.68
当期总收益:  20.79 %
=================================
同比增长:  -9.84 %
买入日期:  2012-08-03
买入单价:  8.7
卖出单价:  10.12
买入数量:  1000
当月投资:  8700.0
剩余资金:  156.0
当期总投资:  63844.0
当期总持股:  7500
当期总收入:  76730.68
当期总收益:  20.18 %
=================================
同比增长:  -1.15 %
买入日期:  2012-09-07
买入单价:  8.6
卖出单价:  10.12
买入数量:  900
当月投资:  7740.0
剩余资金:  416.0
当期总投资:  71584.0
当期总持股:  8400
当期总收入:  85838.68
当期总收益:  19.91 %
=================================
买入日期:  2012-10-05
停牌中,无法交易
=================================
同比增长:  -4.19 %
买入日期:  2012-10-12
买入单价:  8.24
卖出单价:  10.12
买入数量:  1000
当月投资:  8240.0
剩余资金:  176.0
当期总投资:  79824.0
当期总持股:  9400
当期总收入:  95958.68
当期总收益:  20.21 %
=================================
同比增长:  4.98 %
买入日期:  2012-11-02
买入单价:  8.65
卖出单价:  10.12
买入数量:  900
当月投资:  7785.0
剩余资金:  391.0
当期总投资:  87609.0
当期总持股:  10300
当期总收入:  105066.68
当期总收益:  19.93 %
=================================
同比增长:  6.36 %
买入日期:  2012-12-07
买入单价:  9.2
卖出单价:  10.12
买入数量:  900
当月投资:  8280.0
剩余资金:  111.0
当期总投资:  95889.0
当期总持股:  11200
当期总收入:  114174.69
当期总收益:  19.07 %
=================================
总投资额:  95889.0
总持有量:  11200
卖出金额:  114174.69
总收益率:  19.07 %




参考:http://tushare.org/
参考:https://xueqiu.com/3488649239/62074848

猜你喜欢

转载自blog.csdn.net/MengXP/article/details/79416817
今日推荐