Skip to content

歷史行情

Ticks

取得方式可以以一整天、某時間區段或是某天的最後幾筆。預設為商品最近交易日的Ticks。

取得特定日期 Ticks

In

var contract = _api.Contracts.Stocks["TSE"]["2330"];
Ticks ticks = _api.Ticks(contract, "2021-02-24");    
Console.WriteLine(ticks);

Out

Ticks(
    ts=[1614157201843172000,1614157201883723000,1614157201890433000,1614157201915257000,1614157201928901000], 
    close=[627,627,627,628,628],
    volume=[4507,133,2,1,5],
    bid_price=[627,627,627,627,627],
    bid_volume=[324,324,324,324,324],
    ask_price=[628,628,628,628,628],
    ask_volume=[261,261,261,261,261],
    tick_type=[2,1,2,2,2]
)

轉成 DataFrame

In

using System;
using Sinopac.Shioaji;
using Microsoft.Data.Analysis;

Shioaji _api = new Shioaji();
var contract = _api.Contracts.Stocks["TSE"]["2330"];
Ticks ticks = _api.Ticks(contract, "2021-02-24");

DataFrameColumn[] columns =
{
    new PrimitiveDataFrameColumn<long>("ts", ticks.ts.ToList()),
    new PrimitiveDataFrameColumn<long>("volume", ticks.volume.ToList()),
    new PrimitiveDataFrameColumn<double>("close", ticks.close.ToList()),
    new PrimitiveDataFrameColumn<double>("ask_price", ticks.ask_price.ToList()),
    new PrimitiveDataFrameColumn<double>("bid_price", ticks.bid_price.ToList()),
    new PrimitiveDataFrameColumn<long>("ask_volume", ticks.ask_volume.ToList()),
    new PrimitiveDataFrameColumn<long>("bid_volume", ticks.bid_volume.ToList()),
    new PrimitiveDataFrameColumn<long>("tick_type", ticks.tick_type.ToList()),
};
var df_ticks = new DataFrame(ts, volume, close, bid_price, bid_volume, ask_price, ask_volume);
Console.WriteLine(df_ticks);

Out

ts ask_volume ask_price close bid_price bid_volume volume tick_type
0 1614157201843172000 261 628 627 627 324 4507 2
1 1614157201883723000 261 628 627 627 324 133 1
2 1614157201890433000 261 628 627 627 324 2 2
3 1614157201915257000 261 628 628 627 324 1 2
4 1614157201928901000 261 628 628 627 324 5 2

取得特定時間區段 Ticks

In

var contract = _api.Contracts.Stocks["TSE"]["2330"];
Ticks ticks = _api.Ticks(
        contract:contract, 
        date:"2021-02-24", 
        query_type:TicksQueryType.RangeTime, 
        time_start:"09:00:00",
        time_end:"09:20:01"
    );    
Console.WriteLine(ticks);

取得最後數筆 Ticks

In

var contract = _api.Contracts.Stocks["TSE"]["2330"];
Ticks ticks = _api.Ticks(
        contract:contract, 
        date:"2021-02-24", 
        query_type:TicksQueryType.LastCount, 
        last_cnt:10
    );    
Console.WriteLine(ticks);

Ticks

ts (long): 時間
volume (long): 成交量
close (double): 成交價    
ask_price (double): 委賣價
bid_price (double): 委買價
ask_volume (long): 委賣量
bid_volume (long): 委買量        
tick_type (int): 內外盤別{1: 內盤, 2: 外盤, 0: 無法判定}

Kbars

In

using System;
using Sinopac.Shioaji;

Shioaji _api = new Shioaji();
var contract = _api.Contracts.Stocks["TSE"]["2330"];
Kbars kbars = _api.Kbars(contract, "2021-09-13", "2021-09-13");

Console.WriteLine(ticks);

Out

Kbars(
    ts=[1631523660000000000,1631523720000000000,1631523780000000000,1631523840000000000,1631523900000000000], 
    Open=[619,617,618,619,619],
    High=[619,618,619,619,620],
    Low=[617,617,617,619,619],
    Close=[618,618,619,619,620],
    Volume=[1085,109,100,98,63],
    Amount=[671407000,67325000,61817000,60662000,39031000],
)

轉成 DataFrame

In

using System;
using Sinopac.Shioaji;
using Microsoft.Data.Analysis;

Shioaji _api = new Shioaji();
var contract = _api.Contracts.Stocks["TSE"]["2330"];
Kbars kbars = _api.Kbars(contract, "2021-09-13", "2021-09-13");
DataFrameColumn[] columns =
{
    new PrimitiveDataFrameColumn<double>("Open", kbars.Open.ToList()),
    new PrimitiveDataFrameColumn<double>("High", kbars.High.ToList()),
    new PrimitiveDataFrameColumn<double>("Low", kbars.Low.ToList()),
    new PrimitiveDataFrameColumn<double>("Close", kbars.Close.ToList()),
    new PrimitiveDataFrameColumn<long>("Volume", kbars.Volume.ToList()),
    new PrimitiveDataFrameColumn<double>("Amount", kbars.Amount.ToList()),
    new PrimitiveDataFrameColumn<long>("ts", kbars.ts.ToList())
};
Console.WriteLine(employeesDf);   

Out

ts Open High Low Close Volume Amount
0 1631523660000000000 619 619 617 618 1085 671407000
1 1631523720000000000 617 618 617 618 109 67325000
2 1631523780000000000 618 619 617 619 100 61817000
3 1631523840000000000 619 619 619 619 98 60662000
4 1631523900000000000 619 620 619 620 63 39031000

資料歷史期間

Historical Periods

Start Date End Date
Index 2020-03-02 Today
Stock 2020-03-02 Today
Futures 2020-03-22 Today

連續期貨合約

期貨合約一旦到期,合約即不再有效,亦即他將不會出現在您的_api.Contracts裡。為了取得到期的期貨合約歷史資料,我們提供連續期貨合約。R1, R2是近月及次月的連續期貨合約,他們會自動在結算日更換新的合約。您可以使用R1, R2合約來取得歷史資料,例如_api.Contracts.Futures.TXF.TXFR1。以下顯示如何使用R1, R2合約取得到期期貨的歷史TicksKbars

Ticks

Ticks

using System;
using Sinopac.Shioaji;
using Microsoft.Data.Analysis;

Shioaji _api = new Shioaji();
var contract = _api.Contracts.Futures["TXF"]["TXFR1"];
Kbars ticks = _api.Ticks(contract, "2023-03-22");
Console.WriteLine(ticks);

Out

Ticks(
    ts=[1679475600001000000,1679475600077000000,1679475600140000000,1679475600213000000,1679475600213000000], 
    close=[15582,15581,15582,15582,15581],
    volume=[1,1,3,4,14],
    bid_price=[15581,15581,15581,15582,15582],
    bid_volume=[6,6,7,4,4],
    ask_price=[15582,15582,15582,15583,15583],
    ask_volume=[4,4,3,2,2],
    tick_type=[1,2,1,1,2]
)

Kbars

Kbars

using System;
using Sinopac.Shioaji;
using Microsoft.Data.Analysis;

Shioaji _api = new Shioaji();
var contract = _api.Contracts.Futures["TXF"]["TXFR1"];
Kbars kbars = _api.Kbars(contract, "2023-03-22");
Console.WriteLine(kbars);

Out

Kbars(
    ts=[1679443200000000000,1679443260000000000,1679443320000000000,1679443380000000000],
    Open=[15509,15507,15510,15506],
    High=[15509,15512,15510,15511],
    Low=[15507,15505,15505,15506,],
    Close=[15507,15511,15506,15510],
    Volume=[12,105,87,44]
)