项目作者: doncat99

项目描述 :
Fetching Finance Data (US/China)
高级语言: Python
项目地址: git://github.com/doncat99/FinanceCenter.git
创建时间: 2020-09-15T13:44:31Z
项目社区:https://github.com/doncat99/FinanceCenter

开源协议:MIT License

下载


Financial Center: open financial data framework

Python 3.8
Platform
License: MIT
Maintenance
Contributions welcome


Overview

Running Shot

logo

Logging Shot

logo

Database Shot

logo

Flask Demo Shot

logo

Description

Based on registry framework, project can easily compat various of thrid-party data sources.

Below shows all data sources that project already supported.





























Third Party Data Sources Support Table
China Market US Market HK Market
BaoStock Yahoo
JoinQuant alpaca
Tushare
EastMoney

To add more third party data sources, please reference (findy/database/plugins/***) modules for modification.

logo


Installation guide

The FinDy installation consists of setting up the following components:

1. Packages / Dependencies

Command line tools

  1. xcode-select --install #xcode command line tools

Homebrew

  1. /bin/bash -c "$(curl -fsSL https://gitee.com/ineo6/homebrew-install/raw/master/install.sh)"
  2. echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/huangdon/.zprofile
  3. eval "$(/opt/homebrew/bin/brew shellenv)"
  4. brew install git cmake pkg-config openssl
  5. brew link openssl --force

2. Database

FinDy recommends using a PostgreSQL database. But you can use MySQL too, see MySQL setup guide.

  1. # Install package
  2. brew install postgres
  3. postgres -V
  4. # Start PostgreSQL server
  5. pg_ctl -D /usr/local/var/postgres start
  6. #Or you can start the PostgreSQL server and have it start up at login automatically
  7. brew services start postgresql
  8. # Stop PostgreSQL server
  9. pg_ctl -D /usr/local/var/postgres stop
  10. #To make it stop starting up automatically at login
  11. brew services stop postgresql
  12. # Login to PostgreSQL
  13. psql -U XXX(superuser role) -d postgres
  14. # Create a user for FinDy
  15. CREATE USER postgres with NOSUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;
  16. # Create the FinDy production database & grant all privileges on database
  17. CREATE DATABASE findy OWNER postgres;
  18. # Quit the database session
  19. \q
  20. # Try connecting to the new database with the new user
  21. sudo -u git -H psql -d findy
  22. # tuning postgres (optional)
  23. cpan -i DBD::Pg
  24. cd pgtune
  25. chmod +x postgresqltuner.pl
  26. ./pg_tune.sh

For MacOS, if you wish to enlarge system share memory from 4MB to 512 MB, make following:

  1. Disable SIP: Restart computer to Recovery mode, Select “Terminal” and run command csrutil disable, then restart computer
  2. Create /Library/LaunchDaemons/com.startup.sysctl.plist
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    3. <!-- DISABLE SIP TO USE: macOS Recovery > Utilities > Terminal > `csrutil disable` > Reboot -->
    4. <plist version="1.0">
    5. <dict>
    6. <key>Label</key>
    7. <string>com.startup.sysctl</string>
    8. <key>LaunchOnlyOnce</key>
    9. <true></true>
    10. <key>StandardErrorPath</key>
    11. <string>/private/tmp/sysctl.err</string>
    12. <key>StandardOutPath</key>
    13. <string>/private/tmp/sysctl.out</string>
    14. <key>ProgramArguments</key>
    15. <array>
    16. <string>/usr/sbin/sysctl</string>
    17. <string>-w</string>
    18. <string>kern.sysv.shmmax=16777216</string>
    19. <string>kern.sysv.shmmin=1</string>
    20. <string>kern.sysv.shmmni=128</string>
    21. <string>kern.sysv.shmseg=512</string>
    22. <string>kern.sysv.shmall=4096</string>
    23. </array>
    24. <key>RunAtLoad</key>
    25. <true></true>
    26. </dict>
    27. </plist>
  3. Load the PLIST after a few housekeeping items

    1. # sanity check
    2. sysctl -a | grep shm
    3. # set permissions
    4. sudo chown root:wheel /Library/LaunchDaemons/com.startup.sysctl.plist
    5. # validate key-value pairs
    6. plutil /Library/LaunchDaemons/com.startup.sysctl.plist
    7. # load plist
    8. sudo launchctl bootstrap system /Library/LaunchDaemons/com.startup.sysctl.plist
    9. # check logs
    10. tail -f /tmp/sysctl.out
    11. tail -f /tmp/sysctl.err
    12. # recheck sysctl values
    13. sysctl -a | grep shm

3. Redis

  1. brew install redis
  2. ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents

Redis config is located in /usr/local/etc/redis.conf. Make a copy:

  1. cp /usr/local/etc/redis.conf /usr/local/etc/redis.conf.orig

Disable Redis listening on TCP by setting ‘port’ to 0

  1. sed 's/^port .*/port 0/' /usr/local/etc/redis.conf.orig | sudo tee /usr/local/etc/redis.conf

Edit file (nano /usr/local/etc/redis.conf) and uncomment:

  1. unixsocket /tmp/redis.sock
  2. unixsocketperm 777

Start Redis

  1. launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

4. Kafka

  1. # Install package
  2. brew install zookeeper
  3. brew install kafka
  4. modify file /opt/homebrew/etc/kafka/server.properties, active line listeners=PLAINTEXT://:9092 as below:
  5. ############################# Socket Server Settings #############################
  6. # The address the socket server listens on. It will get the value returned from
  7. # java.net.InetAddress.getCanonicalHostName() if not configured.
  8. # FORMAT:
  9. # listeners = listener_name://host_name:port
  10. # EXAMPLE:
  11. # listeners = PLAINTEXT://your.host.name:9092
  12. listeners=PLAINTEXT://localhost:9092
  13. # Start kafka server
  14. brew services start zookeeper
  15. brew services start kafka
  16. pip install kafka_python

5. Pip Packages

  1. pip install -r requirements_mac_arm64.txt
  2. # optional, generate requirement.txt file from project imports
  3. $ pipreqs ./ --force


Configure FinDy Settings

Default (config.json) setting

  1. {
  2. "version": "0.0.2",
  3. "debug": 0,
  4. "processes": 1,
  5. "batch_size": 10000,
  6. "db_name": "findy",
  7. "db_host": "192.168.1.133",
  8. "db_port": "15432",
  9. "db_user": "",
  10. "db_pass": "",
  11. "redis_pass": "",
  12. "kafka": "localhost:9092",
  13. "jq_username": "",
  14. "jq_password": "",
  15. "tushare_token": "",
  16. "trade_host": "127.0.0.1",
  17. "trade_port": "11111",
  18. "trade_socket_port": "33333",
  19. "trade_socket_key": "",
  20. "http_proxy": "127.0.0.1:1087",
  21. "https_proxy": "127.0.0.1:1087",
  22. "smtp_host": "smtpdm.aliyun.com",
  23. "smtp_port": "80",
  24. "email_username": "",
  25. "email_password": "",
  26. "wechat_app_id": "",
  27. "wechat_app_secrect": "",
  28. "wechat_agent_id": ""
  29. }

change database host address to following setting if you install your database locally:

  1. "db_host": "127.0.0.1",
  2. "db_port": "5432",

set database user and password to your custom settings:

  1. "db_user": "xxx",
  2. "db_pass": "xxx",

chinese stock market user are required to obtain joinquant and tushare authentication.

JoinQuant

TuShare


Updates:

(25 Dec 2021)

  • rewrite yahoo finance lib (yfinance) to support aiohttp requests
  • remove proxy pool
  • add wechat notify (see utils/notify.py for more details)
  • dev/async_db branch: add async sqlalchemy ORM
  • many bugfix

(01 Mar 2021)

  • database session decoupling
  • entity provider removed
  • database querying speed boost up
  • asyncio logic framework (implementations requires rewrite request logic)
  • multiprocessing logic rewrite, use processes to replace pool
  • add proxy pool (see proxy_pool)
  • add baostock thrid party data source

(16 Sep 2020):

  • Switch Sql to Postgresql, which allow remote database access, separate fetching and analysis models to two individal project(see FinanceAnalysis project for more detail).
  • Centralized http request interface,add Session and retry logic (Rxpy may be involved to rewrite request model pretty soon).
  • multiprocessing is involved to boost up request speed.
  • request progress display.
  • auto joinquant account switching (banned)
  • log to console -> log to file.
  • US stock market data.
  • global timezone support.
  • tiny bug fix.


How to use

Fetch data

  1. from findy.interface import Region
  2. from findy.interface.fetch import fetching
  3. fetching(Region.CHN) # Region.US

Read data

  1. from datetime import datetime, timedelta
  2. from findy.interface import Region, Provider, EntityType, SP_500_TICKER
  3. from findy.interface.reader import DataReader
  4. from findy.database.schema.meta.stock_meta import Stock, StockDetail
  5. from findy.database.schema.quotes.stock.stock_1d_kdata import Stock1dKdata
  6. from findy.database.schema.register import get_entity_schema_by_type
  7. from findy.database.context import get_db_session
  8. from findy.database.quote import get_entities
  9. end = datetime.now()
  10. start = end - timedelta(days=365)
  11. entity_schema = get_entity_schema_by_type(EntityType.StockDetail)
  12. db_session = get_db_session(Region.US, Provider.Yahoo, entity_schema)
  13. entities, column_names = get_entities(
  14. region=Region.US,
  15. provider=Provider.Yahoo,
  16. entity_schema=entity_schema,
  17. db_session=db_session,
  18. codes=SP_500_TICKER,
  19. columns=[StockDetail.code],
  20. filters=[StockDetail.market_cap > 10_000_000_000])
  21. codes_cap = [entity.code for entity in entities]
  22. stock_reader = DataReader(region=Region.US,
  23. provider=Provider.Yahoo,
  24. data_schema=Stock1dKdata,
  25. entity_schema=Stock,
  26. codes=codes_cap,
  27. start_timestamp=start.strftime('%Y-%m-%d'),
  28. end_timestamp=end.strftime('%Y-%m-%d'))
  29. stock_reader.load_data()
  30. df = stock_reader.data_df.reset_index(drop=True)
  31. gb = df.groupby('code', sort=False)
  32. stocks = [(code, gb.get_group(code)) for code in gb.groups]


Docker Usage

Docker Execute

  1. docker-compose stop; docker-compose rm -f ; docker-compose build --no-cache
  2. docker-compose up -d

View Log

  1. docker-compose logs -f

To Do

  1. rewrite vendor/baostock package to support async logic
  2. add more 3th-party data source packages
  3. dev/async_db branch optimization