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

Get ticks all day

In

ticks = api.ticks(
    contract=api.Contracts.Stocks["2330"], 
    date="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

Get ticks range time

In

ticks = api.ticks(
    contract=api.Contracts.Stocks["2330"], 
    date="2020-03-04", 
    query_type=sj.constant.TicksQueryType.RangeTime,
    time_start="09:00:00",
    time_end="09:20:01"
)
ticks

Out

Ticks(
    ts=[1621846804161286000, 1621846804261371000, 1621846808136002000, 1621846809658787000, 1621846809753537000], 
    close=[570.0, 570.0, 569.0, 569.0, 569.0],
    volume=[1121, 1, 1, 3, 1],
    bid_price=[569.0, 569.0, 569.0, 569.0, 569.0],
    bid_volume=[15, 15, 60, 79, 78],
    ask_price=[570.0, 570.0, 570.0, 570.0, 570.0],
    ask_volume=[27, 27, 59, 92, 92]
)

Get ticks last count

In

ticks = api.ticks(
    contract=api.Contracts.Stocks["2330"], 
    date="2020-03-04", 
    query_type=sj.constant.TicksQueryType.LastCount,
    last_cnt=5,
)
ticks

Out

Ticks(
    ts=[1621855608734606000, 1621855614383711000, 1621855616839500000, 1621855627880863000, 1621855640085704000], 
    close=[571.0, 570.0, 570.0, 571.0, 570.0],
    volume=[1, 1, 5, 1, 1],
    bid_price=[570.0, 570.0, 570.0, 570.0, 570.0],
    bid_volume=[99, 98, 93, 95, 98],
    ask_price=[571.0, 571.0, 571.0, 571.0, 571.0],
    ask_volume=[193, 197, 197, 195, 197]
)

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