GithubHelp home page GithubHelp logo

dongtianqi1125 / futu-quant Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zhentaoo/futu-quant

0.0 1.0 0.0 2.76 MB

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

Python 9.03% JavaScript 90.97%

futu-quant's Introduction

使用富途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. 想法

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

  • 上升周期做止盈比定投收益低
  • 振荡周期做止盈比定投收益高
  • 下降周期一般操作都是亏损 而主观判断市场周期,更多的是政策面、消息面的判断...

futu-quant's People

Contributors

zhentaoo avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.