Skip to content

Commit

Permalink
Support the production version of the JQuants API (#34)
Browse files Browse the repository at this point in the history
* feat: add DataScheme type for columns' type definitions

* feat: add PricesDailyQuotes data definition

* feat: complete dataschema for all API

* feat: add keyword argument 'types'

* fix: fix URI of API

* feat: add new endpoints

* fix: remove unused endpoint in prod

* doc: update README

* doc: replace 'get' to 'fetch'

* fix: remove ListedSections API

* feat: add each API struct with 'endpoint' method

* [WIP] feat: add constructors for API with params validation

* feat: create 'fetch' function for JQuants APIs

* feat: define dataschemes for all apis

* feat: move JSON parsing process from get/post

* doc: improve docstring

* fix: remove a unused variable

* fix: missing commas

* fix: change date format for the production API

* feat: add JQuantsInvalidParameterError

* fix: debug authorization and query in post

* fix: skip the coltype conversion if the column not found

* fix: fix typos

* feat: add FinsDividend parameter specs

* fix: clean utils.jl

* fix: fix the optional argument of `fetch` method

* feat: remove the old version script get_data.jl

* test: fix tests

* ci: update environment variables

* ci: small fix

* ci: update actions
  • Loading branch information
ki-chi authored May 2, 2023
1 parent 72cf587 commit b645cbe
Show file tree
Hide file tree
Showing 13 changed files with 906 additions and 869 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
version: '1'
arch: x64
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.version }}
Expand All @@ -35,17 +35,17 @@ jobs:
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
env:
JQUANTS_EMAIL_ADDRESS: ${{ secrets.JQUANTS_EMAIL_ADDRESS }}
JQUANTS_PASSWORD: ${{ secrets.JQUANTS_PASSWORD }}
JQUANTS_API_EMAIL: ${{ secrets.JQUANTS_API_EMAIL }}
JQUANTS_API_PASSWORD: ${{ secrets.JQUANTS_API_PASSWORD }}
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v1
- uses: codecov/codecov-action@v3
with:
file: lcov.info
docs:
name: Documentation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: julia-actions/cache@v1
- uses: julia-actions/julia-buildpkg@latest
- uses: julia-actions/julia-docdeploy@latest
Expand Down
32 changes: 14 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
[![CI](https://github.com/ki-chi/JQuants.jl/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/ki-chi/JQuants.jl/actions/workflows/ci.yml)
[![][docs-stable-img]][docs-stable-url] [![][docs-dev-img]][docs-dev-url]

[The J-Quants API](https://application.jpx-jquants.com/) wrapper for Julia.
[The J-Quants API](https://jpx-jquants.com/?lang=en) wrapper for Julia.

The J-Quants API is an distribution service that delivers historical stock prices and financial statements data through API,
provided by JPX Market Innovation & Research, Inc.
The API service is provided as a beta version, and the API specifications are subject to change in the future.

This client package helps you easily use the API from Julia.

Expand All @@ -29,9 +28,8 @@ julia> using Pkg; Pkg.add("JQuants")

## Authorization

You have to [register](https://application.jpx-jquants.com/register) to use the J-Quants API.
If you choose to authorize by using "Refresh token", you should get the token from [the portal of J-Quants API](https://application.jpx-jquants.com/).
You can also authorize using the email address and password registered for the J-Quants API.
You have to [register](https://jpx-jquants.com/auth/signup/?lang=en) to use the J-Quants API.
You may also grant authentication credentials through employment of a "Refresh token," or alternatively, by employing the email address and password that was previously registered for the J-Quants API.

```julia
julia> using JQuants
Expand All @@ -47,29 +45,27 @@ julia> authorize([YOUR EMAIL ADDRESS], [PASSWORD])
true
```

## Get market data
## Fetch market data

This package covers [all APIs](https://jpx.gitbook.io/j-quants-api-en/api-reference)
This package covers [APIs](https://jpx.gitbook.io/j-quants-en/api-reference)
for downloading data by the J-Quants API.

```julia
# Run after authorization

julia> getinfo(); # Get listed issues
julia> fetch(ListedInfo()); # Fetch listed issues

julia> getsections(); # Get definitions of sector codes (in Japanese)
julia> fetch(DailyQuotes(date="2022-09-09")); # Fetch daily stock prices

julia> getdailyquotes(date="2022-09-09"); # Get daily stock prices
julia> fetch(DailyQuotes(date=Date(2022, 9, 9))); # Dates.Date type is also OK

julia> getdailyquotes(date=Date(2022, 9, 9)); # Dates.Date type is also OK
julia> fetch(FinStatements(code="86970")); # Fetch financial statements

julia> getfinstatements(code="86970"); # Get financial statements
julia> fetch(FinAnnouncement()); # Fetch announcement of the next-day financial disclosure

julia> getfinannouncement(); # Get announcement of the next-day financial disclosure
julia> fetch(TradesSpec()); # Fetch investment trend statistics by investor types

julia> gettradesspecs(); # Get investment trend statistics by investor types

julia> gettopix(); # Get daily 'TOPIX' index data
julia> fetch(Topix()); # Fetch daily 'TOPIX' index data
```

See the [documentation][docs-stable-url] for detailed usage of the functions.
Expand All @@ -83,8 +79,8 @@ See the [documentation][docs-stable-url] for detailed usage of the functions.

# Reference

- [J-Quants API](https://application.jpx-jquants.com/)
- [J-Quants API Reference](https://jpx.gitbook.io/j-quants-api-en/api-reference)
- [J-Quants API](https://jpx-jquants.com/?lang=en)
- [J-Quants API Reference](https://jpx.gitbook.io/j-quants-en/api-reference)


# Acknowledgments
Expand Down
57 changes: 23 additions & 34 deletions src/JQuants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,39 @@ using JSON
using DataFrames
using Reexport

export authorize, getinfo, getsections, getdailyquotes,
getfinstatements, getfinannouncement, gettradesspecs,
gettopix



const JPX_URL = "https://api.jpx-jquants.com/v1"

# Endpoints
@enum EndPointKey begin
TokenAuthUser
TokenAuthRefresh
ListedInfo
ListedSections
PricesDailyQuotes
FinsStatements
FinsAnnouncement
MarketsTradeSpec
IndicesTopix
end

const endpoints = Base.ImmutableDict(
TokenAuthUser => "/token/auth_user",
TokenAuthRefresh => "/token/auth_refresh",
ListedInfo => "/listed/info",
ListedSections => "/listed/sections",
PricesDailyQuotes => "/prices/daily_quotes",
FinsStatements => "/fins/statements",
FinsAnnouncement => "/fins/announcement",
MarketsTradeSpec => "/markets/trades_spec",
IndicesTopix => "/indices/topix"
)
# Authorization function
export authorize

# APIs
export fetch
export TokenAuthUser, TokenAuthRefresh, ListedInfo,
PricesDailyQuotes, PricesAM, MarketsTradeSpec,
MarketsWeeklyMarginInterest, MarketsShortSelling,
MarketsBreakdown, IndicesTopix, FinsStatements,
FinsDividend, FinsAnnouncement, OptionIndexOption

const JQUANTS_URI = "https://api.jquants.com/v1"

# Errors
include("errors.jl")

# API specification
include("specs.jl")

# endpoints
include("endpoints.jl")

# Get & Post functions
include("http.jl")

# Authorization
include("auth.jl")

# For market data API
include("get_data.jl")
# Data types for the type conversion
include("datatypes.jl")

# Fetch market data
include("fetch.jl")

# Utility functions
include("utils.jl")
Expand Down
6 changes: 4 additions & 2 deletions src/auth.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ check_id_token() = ID_TOKEN[]

function update_ref_token(emailaddress, password)
body = JSON.json(Dict("mailaddress"=>emailaddress, "password"=>password))
resp = post(JQuants.TokenAuthUser, body=body)
resp = JSON.parse(post(TokenAuthUser(), body=body))
REFRESH_TOKEN[] = resp["refreshToken"]
end

function update_id_token()
resp = post(JQuants.TokenAuthRefresh, query=["refreshtoken"=>REFRESH_TOKEN[]])
resp = JSON.parse(
post(TokenAuthRefresh(), query=["refreshtoken"=>REFRESH_TOKEN[]])
)
ID_TOKEN[] = resp["idToken"]
end

Expand Down
Loading

0 comments on commit b645cbe

Please sign in to comment.