返回列表 发布新帖

请问我在miniQMT中把K线数据和复权数据下载下来保存在外部数据库中后,怎么计算前复权

1211 5
发表于 2023-12-25 20:57:54 | 显示全部楼层 阅读模式
请问我在miniQMT中把K线数据和复权数据下载下来保存在外部数据库中后,怎么计算前复权和后复权

评论5

rzp
发表于 2023-12-26 09:23:11 | 显示全部楼层
使用xtdata的接口时可以指定复权方式获取k线
*******3497楼主
发表于 2023-12-26 09:47:35 | 显示全部楼层
rzp 发表于 2023-12-26 09:23
使用xtdata的接口时可以指定复权方式获取k线

我现在是把数据存到外部的数据库里面,没有在QMT里面,调xtdata的接口没用,需要自己算,想知道这个计算公式是怎么样的
张三
发表于 2023-12-26 11:58:45 | 显示全部楼层
本帖最后由 张三 于 2023-12-26 12:04 编辑
  1. #coding:utf-8

  2. import numpy as np
  3. import pandas as pd

  4. from xtquant import xtdata

  5. #def gen_divid_ratio(quote_datas, divid_datas):
  6. #    drl = []
  7. #    for qi in range(len(quote_datas)):
  8. #        q = quote_datas.iloc[qi]
  9. #        dr = 1.0
  10. #        for di in range(len(divid_datas)):
  11. #            d = divid_datas.iloc[di]
  12. #            if d.name <= q.name:
  13. #                dr *= d['dr']
  14. #        drl.append(dr)
  15. #    return pd.DataFrame(drl, index = quote_datas.index, columns = quote_datas.columns)

  16. def gen_divid_ratio(quote_datas, divid_datas):
  17.     drl = []
  18.     dr = 1.0
  19.     qi = 0
  20.     qdl = len(quote_datas)
  21.     di = 0
  22.     ddl = len(divid_datas)
  23.     while qi < qdl and di < ddl:
  24.         qd = quote_datas.iloc[qi]
  25.         dd = divid_datas.iloc[di]
  26.         if qd.name >= dd.name:
  27.             dr *= dd['dr']
  28.             di += 1
  29.         if qd.name <= dd.name:
  30.             drl.append(dr)
  31.             qi += 1
  32.     while qi < qdl:
  33.         drl.append(dr)
  34.         qi += 1
  35.     return pd.DataFrame(drl, index = quote_datas.index, columns = quote_datas.columns)

  36. def process_forward_ratio(quote_datas, divid_datas):
  37.     drl = gen_divid_ratio(quote_datas, divid_datas)
  38.     drlf = drl / drl.iloc[-1]
  39.     result = (quote_datas * drlf).apply(lambda x: round(x, 2))
  40.     return result

  41. def process_backward_ratio(quote_datas, divid_datas):
  42.     drl = gen_divid_ratio(quote_datas, divid_datas)
  43.     result = (quote_datas * drl).apply(lambda x: round(x, 2))
  44.     return result

  45. def process_forward(quote_datas1, divid_datas):
  46.     quote_datas = quote_datas1.copy()
  47.     def calc_front(v, d):
  48.         return round(
  49.             (v - d['interest'] + d['allotPrice'] * d['allotNum'])
  50.             / (1 + d['allotNum'] + d['stockBonus'] + d['stockGift'])
  51.             , 2
  52.         )
  53.     for qi in range(len(quote_datas)):
  54.         q = quote_datas.iloc[qi]
  55.         for di in range(len(divid_datas)):
  56.             d = divid_datas.iloc[di]
  57.             if d.name <= q.name:
  58.                 continue
  59.             q.iloc[0] = calc_front(q.iloc[0], d)
  60.     return quote_datas

  61. def process_backward(quote_datas1, divid_datas):
  62.     quote_datas = quote_datas1.copy()
  63.     def calc_front(v, d):
  64.         return round(
  65.             (v * (1 + d['stockGift'] + d['stockBonus'] + d['allotNum'])
  66.             + d['interest'] - d['allotNum'] * d['allotPrice'])
  67.             , 2
  68.         )
  69.     for qi in range(len(quote_datas)):
  70.         q = quote_datas.iloc[qi]
  71.         for di in range(len(divid_datas)):
  72.             d = divid_datas.iloc[di]
  73.             if d.name > q.name:
  74.                 continue
  75.             q.iloc[0] = calc_front(q.iloc[0], d)
  76.     return quote_datas


  77. #--------------------------------

  78. s = '002594.SZ'

  79. #xtdata.download_history_data(s, '1d', '20100101', '')

  80. dd = xtdata.get_divid_factors(s)
  81. print(dd)

  82. #复权计算用于处理价格字段
  83. field_list = ['open', 'high', 'low', 'close']
  84. datas_ori = xtdata.get_market_data(field_list, [s], '1d', dividend_type = 'none')['close'].T
  85. #print(datas_ori)

  86. #等比前复权
  87. datas_forward_ratio = process_forward_ratio(datas_ori, dd)
  88. print('datas_forward_ratio', datas_forward_ratio)

  89. #等比后复权
  90. datas_backward_ratio = process_backward_ratio(datas_ori, dd)
  91. print('datas_backward_ratio', datas_backward_ratio)

  92. #前复权
  93. datas_forward = process_forward(datas_ori, dd)
  94. print('datas_forward', datas_forward)

  95. #后复权
  96. datas_backward = process_backward(datas_ori, dd)
  97. print('datas_backward', datas_backward)

复制代码
张三
发表于 2023-12-26 12:05:50 | 显示全部楼层
*******3497楼主
发表于 2023-12-26 13:45:21 | 显示全部楼层

好的,感谢大佬

回复

您需要登录后才可以回帖 登录 | 立即注册

客服专线

400-080-8112

用思考的速度交易,用真诚的态度合作,我们是认真的!
  • 关注公众号
  • 添加微信客服
Copyright © 2001-2025 迅投QMT社区 版权所有 All Rights Reserved. 蜀ICP备19002686号-2
关灯 快速发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表