首页 > 技术文章 > 好用的python项目

treasury-manager 2020-10-27 09:26 原文

数据分析

Numpy

numpy中与金融有关的函数 https://www.cnblogs.com/traditional/p/11113862.html
**numpy-financial **
https://pypi.org/project/numpy-financial/
https://github.com/numpy/numpy-financial
Numpy是高性能科学计算和数据分析的基础包。 NumPy系统是Python的一种开源的数值计算扩展。NumPy(Numeric Python)提供了许多高级的数值编程工具,如:矩阵数据类型、矢量处理,以及精密的运算库。专为进行严格的数字处理而产生。

Pandas

十分钟掌握Pandas(上)
https://www.cnblogs.com/littlepage/p/11964838.html

金融计算

quant-lib

下载:https://www.lfd.uci.edu/~gohlke/pythonlibs/#quantlib
在线文档:https://quantlib-python-docs.readthedocs.io/en/latest/
QuantLib Python Tutorials With Examples
(英文)http://gouthamanbalaraman.com/blog/quantlib-python-tutorials-with-examples.html
(中文)https://www.cnblogs.com/xuruilong100/p/8711520.html

相比TA-Lib在技术分析领域的地位,QuantLib在金融工程领域的地位可以说有过之而无不及。

参考其官方网站,QuantLib中包含的的模块如下(其中个人感觉国内比较有用的添加了中文注释):
Currencies and FX rates(货币相关)
Date and time calculations(日期和时间计算)
Calendars
Day counters
Design patterns
Financial instruments
Finite-differences framework
Lattice methods
Math tools
1-D Interpolations and corresponding traits
One-dimensional solvers
Optimizers(优化器)
Monte Carlo framework (蒙特卡洛模拟框架)
Numeric types
Output manipulators
Pricing engines(定价引擎)
Asian option engines(亚式期权)
Barrier option engines(障碍期权)
Basket option engines(篮子期权)
Cap/floor engines
Cliquet option engines
Forward option engines(远期期权)
Quanto option engines
Swaption engines
Vanilla option engines(普通期权)
QuantLib macros
Debugging macros
Numeric limits
Short-rate modelling framework(短期利率建模框架)
Stochastic processes(随机过程)
Term structures(期限结构)
Utilities

QuantLib实用例子:https://github.com/lballabio/QuantLib/tree/master/Examples

pyfinance

项目地址:https://github.com/bsolomon1124/pyfinance
https://pypi.org/project/pyfinance/
https://www.cnpython.com/pypi/pyfinance

pyfinance是为投资管理和证券收益分析而构建的Python分析包,主要是对面向定量金融的现有包进行补充,如pyfolio和pandas等。pyfinance包含六个模块,

datasets.py :金融数据下载(基于request进行数据爬虫,有些数据由于外网受限已经无法下载);
general.py:通用财务计算,例如主动份额计算,收益分配近似值和跟踪误差优化;
ols.py:回归分析,支持pandas滚动窗口回归;
options.py:期权衍生品计算和策略分析;
returns.py:通过CAPM框架对财务时间序列进行统计分析,旨在模拟FactSet Research Systems和Zephyr等软件的功能,并提高了速度和灵活性;
utils.py:基础架构。

Module Description

  • datasets.py Financial dataset download & assembly via requests.
  • general.py General-purpose financial computations, such as active share calculation, returns distribution approximation, and tracking error optimization.
  • ols.py Ordinary least-squares (OLS) regression, supporting static and rolling cases, built with a matrix formulation and implemented with NumPy.
  • options.py Vectorized option calculations, including Black-Scholes Merton European option valuation, Greeks, and implied volatility, as well as payoff determination for common money-spread option strategies.
  • returns.py Statistical analysis of financial time series through the CAPM framework, designed to mimic functionality of software such as FactSet Research Systems and Zephyr, with improved speed and flexibility.
  • utils.py Utilities not fitting into any of the above.

学习案例
【手把手教你】使用pyfinance进行证券收益分析
https://blog.csdn.net/ndhtou222/article/details/108544117

DROP

https://github.com/lakshmiDRIP/DROP
https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal

DROP is composed of three modules:

DROP implements libraries targeting analytics/risk, transaction cost analytics, asset liability analytics, capital, exposure, and margin analytics, valuation adjustment analytics, and portfolio construction analytics within and across fixed income, credit, commodity, equity, FX, and structured products. It also includes auxiliary libraries for graph algorithms, numerical analysis, numerical optimization, spline builder, model validation, statistical learning, and computational support

DROP is composed of three modules.
DROP Specifications
干货(太棒了):https://github.com/lakshmiDRIP/DROP/blob/master/Docs/Internal/FixedIncomeAnalytics/FixedIncomeAnalytics_v3.11.pdf
Main => https://lakshmidrip.github.io/DROP/
Wiki => https://github.com/lakshmiDRIP/DROP/wiki
GitHub => https://github.com/lakshmiDRIP/DROP
Repo Layout Taxonomy => https://lakshmidrip.github.io/DROP/Taxonomy.md
Javadoc => https://lakshmidrip.github.io/DROP/Javadoc/index.html
Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
Release Versions => https://lakshmidrip.github.io/DROP/version.html
Community Credits => https://lakshmidrip.github.io/DROP/credits.html
Issues Catalog => https://github.com/lakshmiDRIP/DROP/issues
JUnit => https://lakshmidrip.github.io/DROP/junit/index.html
Jacoco => https://lakshmidrip.github.io/DROP/jacoco/index.html

Product Core Module => Fixed Income Product Analytics, Loan Analytics, and Transaction Cost Analytics.
Portfolio Core Module => Portfolio Contruction and Asset Liability, along with Exposure, Margin, XVA, and Capital Analytics.
Computation Core Module => Algorithm/Computation Support, Function Analysis, Model Validation, Numerical Analysis, Numerical Optimizer, Spline Builder, Graph Algorithms, and Statistical Learning.

pyfolio

https://github.com/quantopian/pyfolio

financepy

项目地址:
https://github.com/domokane/FinancePy
学习案例
https://github.com/domokane/FinancePy-Conference-Notebooks

Overview
FinancePy is a python-based library that is currently in beta version. It covers the following functionality:

Valuation and risk models for a wide range of equity, FX, interest rate and credit derivatives.
Although it is written entirely in Python, it can achieve speeds comparable to C++ by using Numba. As a result the user has both the ability to examine the underlying code and the ability to perform pricing and risk at speeds which compare to a library written in C++.
Contact author at quant@financepy.com.

pyportfolioopt 资产组合优化

pip install PyPortfolioOpt
它的安装依赖于cvxpy等。
https://pyportfolioopt.readthedocs.io/en/latest/

TA-Lib

https://ta-lib.org/hdr_dw.html
如果直接pip install TA-Lib不成功,出现 Failed building wheel for TA-Lib
可以试试这个网址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#ta-lib
下载好相应的包之后,再用下面的命令安装
pip install C:\Users\Administrator\Downloads\TA_Lib-0.4.17-cp35-cp35m-win_amd64.whl

还有人说是因为缺了这个lxml包:
在这里下载对应的.whl文件,注意别改文件名!
http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml

Ctrl + F,输入lxml,找到下面这段

Lxml, a binding for the libxml2 and libxslt libraries.
lxml‑3.4.4‑cp27‑none‑win32.whl
lxml‑3.4.4‑cp27‑none‑win_amd64.whl
lxml‑3.4.4‑cp33‑none‑win32.whl
lxml‑3.4.4‑cp33‑none‑win_amd64.whl
lxml‑3.4.4‑cp34‑none‑win32.whl
lxml‑3.4.4‑cp34‑none‑win_amd64.whl
lxml‑3.4.4‑cp35‑none‑win32.whl
lxml‑3.4.4‑cp35‑none‑win_amd64.whl

cp后面是Python的版本号,27表示2.7,根据你的Python版本选择下载。

文本图形

Graphviz 绘制关系图

graphviz实际上是一个绘图工具,可以根据dot脚本画出树形图等。
windows安装

  1. 安装graphviz软件:https://graphviz.gitlab.io/_pages/Download/Download_windows.html
  2. 配置环境变量:把bin文件夹的路径加入到环境变量path里
  3. 安装python的graphviz模块:pip install graphviz
    http://www.graphviz.org/

python-docx 和 docxtpl

python读写doc文件的应用, python-docx 用于新生成文档比较方便, docxtpl 用于填写已有模板方便

pdfplumber

PDF文档读写应用

几个例子:
https://github.com/jsvine/pdfplumber/tree/stable/examples/notebooks
PDFPlumber使用入门
https://blog.csdn.net/weixin_48629601/article/details/107224376
https://cloud.tencent.com/developer/article/1709212

tesseract OCR文字识别

OCR,即Optical Character Recognition,光学字符识别,

https://digi.bib.uni-mannheim.de/tesseract/
https://github.com/tesseract-ocr/tesseract

开源数据

gopup 开源数据库

项目的地址为:
https://github.com/justinzm/gopup

GoPUP数据
指数数据:微博指数数据,百度指数数据,百度搜索数据,百度资讯指数,百度媒体指数,百度需求图谱,百度人群画像年龄分布,百度人群画像性别分布,百度人群画像兴趣分布;算数数据:算数指数数据,算数相关性分析,算数地域分析,算数城市分析,算数年龄分析,算数性别分析,算数用户阅读兴趣分类,谷歌指数数据,谷歌指数数据,谷歌事实查证;宏观数据:中国宏观数据,中国宏观杠杆率数据,货币汇率数据;利率数据:Shibor数据,Shibor报价数据,Shibor均值数据,LPR数据;新经济公司:千里马公司,独角兽公司,倒闭公司,商业特许经营公司;信息数据:新闻联播文字稿;生活数据:中国油价数据,汽柴油历史调价信息,调价日的地区油价历史数据;诗词数据:唐代诗人,唐诗数据;影视数据:实时电影票房数据,单日电影票房数据,单日影院票房数据,实时电视剧播映指数,实时综艺播映指数,艺人商业价值,艺人流量价值;全国高校:全国普通高等学校名单,全国成人高等学校名单,全国高等学校详情数据;疫情数据:网易疫情数据,丁香园疫情数据……

安装方法:pip install gopup
使用文档:gopup-cookbook http://www.gopup.cn/login
当前版本:v0.3.0
https://github.com/cqkenuo/gopup

数学计算

SymPy

核心能力:SymPy是一个符号计算的Python库。它的目标是成为一个全功能的计算机代数系统,同时保持代码简洁、易于理解和扩展。它完全由Python写成,不依赖于外部库。
SymPy支持符号计算、高精度计算、模式匹配、绘图、解方程、微积分、组合数学、离散数学、几何学、概率与统计、物理学等方面的功能。
https://www.sympy.org/zh/index.html
https://github.com/sympy/sympy/
具体包括:多项式/演算/解方程/Combinatorics/离散数学/矩阵/几何学/绘图/物理学/统计/Cryptography/Parsing

from sympy import init_printing, integrate, log, sqrt, symbols
init_printing()
x = symbols('x')
g = sqrt(x + log(x))
f = (x ** 2 + 2 * x + 1 + (3 * x + 1) * g) / (x * g * (x + g))

输出(公式):

CVXOPT

CVXOPT is a free software package for convex optimization based on the Python programming language. It can be used with the interactive Python interpreter, on the command line by executing Python scripts, or integrated in other software via Python extension modules. Its main purpose is to make the development of software for convex optimization applications straightforward by building on Python’s extensive standard library and on the strengths of Python as a high-level programming language.
案例:
http://cvxopt.org/examples/index.html

cvxpy

它的安装依赖于MS Visual C++ 14.0 or greater, 需要下载 https://visualstudio.microsoft.com/zh-hans/visual-cpp-build-tools/

Scipy

pulp

爬虫

spider-flow 无需代码的爬虫

https://www.spiderflow.org/
https://github.com/ssssssss-team/spider-flow

杂项功能

鼠标和键盘控制 pyautogui

词云图 stylecloud

stylecloud 项目地址:https://github.com/minimaxir/stylecloud
pip3 install stylecloud
这款工具可以帮助生成风格多样的词云,包括梯度和图标形状!
stylecloud 是一个 Python 包,它基于流行的 word_cloud 包,并添加了一些有用的功能,从而创建出独特的词云。stylecloud 具备以下特点:
为词云提供(任意大小)的图标形状(通过 Font Awesome 5.11.2 获得);
支持高级调色板(通过 palettable 实现);
为上述调色板提供直接梯度;
支持读取文本文件,或预生成的 CSV 文件(包含单词和数字);
提供命令行接口。
更换中文字体,支持中文词云
stylecloud 包由数据科学家 Max Woolf 创建,是对其 2016 年风格化词云项目的较正式实现。
你可以在 stylecloud-examples repo 中查看 stylecloud 的更多示例,比如如何基于 Twitter 和 Reddit 数据制作 stylecloud。
地址:https://github.com/minimaxir/stylecloud-examples

wxpython GUI界面程序

https://wxpython.org/

pip install wxPython
pip install wxPython -i https://pypi.tuna.tsinghua.edu.cn/simple

baidu文库教程:
https://wenku.baidu.com/view/214bf71eec3a87c24028c4e7.html
易百教程
https://www.yiibai.com/wxpython/wxpython_hello_world.html

lunar-python

无第三方依赖的公历(阳历)和农历(阴历、老黄历)工具,支持节假日、星座、儒略日、干支、生肖、节气、节日、彭祖百忌、每日宜忌、吉神宜趋凶煞宜忌、吉神(喜神/福神/财神/阳贵神/阴贵神)方位、胎神方位、冲煞、纳音、星宿、八字、五行、十神、建除十二值星、青龙名堂等十二神、黄道黑道日及吉凶等。lunar is a calendar library for Solar and Chinese Lunar.

https://github.com/6tail/lunar-python
http://6tail.cn/calendar/api.html

示例

pip install lunar_python
from lunar_python import Lunar

# 通过指定年月日初始化阴历
lunar = Lunar.fromYmd(1986, 4, 21)
# 打印阴历
print(lunar.toFullString())
# 阴历转阳历并打印
print(lunar.getSolar().toFullString())

输出结果:
一九八六年四月廿一 丙寅(虎)年 癸巳(蛇)月 癸酉(鸡)日 子(鼠)时 纳音[炉中火 长流水 剑锋金 桑柘木] 星期四 北方玄武 星宿[斗木獬](吉) 彭祖百忌[癸不词讼理弱敌强 酉不会客醉坐颠狂] 喜神方位[巽](东南) 阳贵神方位[巽](东南) 阴贵神方位[震](正东) 福神方位[兑](正西) 财神方位[离](正南) 冲[(丁卯)兔] 煞[东]
1986-05-29 00:00:00 星期四 双子座

推荐阅读