Skip to content

Historical Market Data

Ticks Data

Ticks Schema

In

from shioaji.data import Ticks
Ticks??

Out

Init signature: Ticks(**data:Any) -> None
Docstring:      <no docstring>
Source:        
class Ticks(BaseModel):
    ts: typing.List[int]
    close: typing.List[float]
    volume: typing.List[int]
    bid_price: typing.List[float]
    bid_volume: typing.List[int]
    ask_price: typing.List[float]
    ask_volume: typing.List[int]

File:           shioaji/data.py
Type:           ModelMetaclass

ticks function api

In

ticks = api.ticks(api.Contracts.Stocks["2330"], "2020-03-04")
ticks

Out

Ticks(
    ts=[1583312400821000000, 1583312405836000000, 1583312410849000000, 1583312415864000000, 1583312420877000000], 
    close=[322.0, 321.5, 321.0, 321.0, 321.0],
    volume=[5098, 91, 126, 59, 90],
    bid_price=[321.5, 321.0, 321.0, 321.0, 321.0],
    bid_volume=[5, 100, 94, 78, 20],
    ask_price=[322.0, 321.5, 321.5, 321.5, 321.5],
    ask_volume=[646, 13, 31, 86, 199]
)

Ticks to DataFrame

In

import pandas as pd
df = pd.DataFrame({**ticks})
df.ts = pd.to_datetime(df.ts)
df.head()

Out

ts ask_volume ask_price close bid_price bid_volume volume
0 2020-03-04 09:00:00.821 646 322.0 322.0 321.5 5 5098
1 2020-03-04 09:00:05.836 13 321.5 321.5 321.0 100 91
2 2020-03-04 09:00:10.849 31 321.5 321.0 321.0 94 126
3 2020-03-04 09:00:15.864 86 321.5 321.0 321.0 78 59
4 2020-03-04 09:00:20.877 199 321.5 321.0 321.0 20 90

KBar Data

In

from shioaji.data import Kbars
Kbars??

Out

Init signature: Kbars(**data: Any) -> None
Docstring:      <no docstring>
Source:        
class Kbars(BaseMapping):
    ts: typing.List[int]
    Open: typing.List[float]
    High: typing.List[float]
    Low: typing.List[float]
    Close: typing.List[float]
    Volume: typing.List[int]

In

kbars = api.kbars(api.Contracts.Stocks["2330"], start="2020-06-01", end="2020-07-01")
df = pd.DataFrame({**kbars})
df.ts = pd.to_datetime(df.ts)
df

Out

ts Close Open High Volume Low
0 2020-06-01 09:01:00 294.0 294.0 294.0 3187 293.5
1 2020-06-01 09:02:00 293.5 293.5 294.0 201 293.5
2 2020-06-01 09:03:00 294.0 293.5 294.0 267 293.5
3 2020-06-01 09:04:00 294.5 294.0 294.5 624 293.5
4 2020-06-01 09:05:00 294.0 294.0 294.5 419 293.5