All Projects → zhentaoo → futu-quant

zhentaoo / futu-quant

Licence: other
量化:使用富途OpenApi,Python开启ETF量化交易之路

Programming Languages

javascript
184084 projects - #8 most used programming language
python
139335 projects - #7 most used programming language

使用富途open api,python开启量化之路

1. 官方文档 & 起步注意事项

2. 获取股票历史K线数据

  • 代码如下,使用futu open api: request_history_kline可以获取你想要的个股K线数据
  • futu api返回的数据结构是pandas,需要对应的py库做处理(我是转化成json,降低了学习成本)
  • 返回的数据不包括 5、10、20日均线,需要自己计算
from futu import *
import pandas as pd

quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)

print(data)

ret, data2, page_req_key = quote_ctx.request_history_kline('SH.510050', start='2005-09-30', end='2019-05-05',max_count=1000,page_req_key=page_req_key) #请求开头50个数据
print(data2)

3. 数据处理,从pandas到json

  • api返回的日线数据limit是1000,所以需要一些分页操作,page_req_key,是上一次查询返回的页码标识再下一次查询中带上就好,最后把几次结果合并一下(代码写的非常粗暴,for循环都没有,因为我是从文档中copy过来的)
ret, data, page_req_key = quote_ctx.request_history_kline('SH.510050', start='2005-09-30', end='2019-05-05',max_count=1000)
print(data)

ret, data2, page_req_key = quote_ctx.request_history_kline('SH.510050', start='2005-09-30', end='2019-05-05',max_count=1000,page_req_key=page_req_key) 
print(data2)

ret, data3, page_req_key = quote_ctx.request_history_kline('SH.510050', start='2005-09-30', end='2019-05-05',max_count=1000,page_req_key=page_req_key) 
print(data3)

result = pd.concat([data, data2, data3])
  • 拿到数据之后,转化为json格式,然后写入json文件
  • 由于本人更熟悉javascript & json操作,所以之后的逻辑策略用js处理;对于熟悉python的同学这里应该更加得心应手。
file = open('50ETF.json', 'w')
file.write(str(result.to_json(orient='records')))
file.close()

4. 策略-定投

配合代码&下图可以看出,从2013-05-01到2019-04-30,每20个交易日定投沪深300,10000元最终可盈利130.488%

5. 策略-定投止盈(待续...)

注意:不可在盈利后彻底清仓,会损失很多

6. 想法

在做量化的时候,会发现任何策略都不是万能的,

  • 上升周期做止盈比定投收益低
  • 振荡周期做止盈比定投收益高
  • 下降周期一般操作都是亏损 而主观判断市场周期,更多的是政策面、消息面的判断...
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].