2013年5月13日 星期一

備份歷史報價-排程設定

 

整合取得期交所每日行情資料備份期交所歷史報價的內容,再來作些設計,讓全部作業自動化吧。

 

建立一個CMD檔,路徑請自行更改
@echo off
D:\期貨DB建置資料\ExtractorProcess\ExtractorProcess\bin\Debug\ExtractorProcess.exe
cd D:\期貨DB建置資料\期貨Tick\zip
D:
unzip -d D:\期貨DB建置資料\期貨Tick Daily.zip
del Daily.zip
cd..
copy *.rpt D:\期貨DB建置資料\期貨Tick\BACK_new
rename *.rpt Daily.rpt

指令簡單說明,一開始執行 ExtractorProcess.exe 將檔案下載回來,利用unzip這個執行檔把檔案解壓縮,把ZIP檔刪除,然後把解壓縮完的rpt檔複製備份起來,再將這個rpt檔重新命名為Daily.rpt。



然後在SQL SERVER設定排程,如圖,

 

排程設定

 

第一步是執行將Daily.rpt轉檔到TABLE Daily的封裝,第二步是執行Daily備份到各TABLE的SQL語法,完整SQL程式如下,

 
insert Tick select * from Daily
go

select top 1 expireDate,count(*) num
into #tmp
from daily
where commodity = 'TX'
group by expiredate
order by num desc

select Date,dealtime,dealprice,sum(cast(dealvolume as int)) dealvolume
into #tmp1
from Daily
where commodity = 'TX' and NearPrice = '-'
and expiredate = (select expireDate from #tmp)
and dealtime = '084500'
group by Date,Commodity,expiredate,dealtime,dealprice
order by Date,Commodity,expiredate,dealtime,dealvolume desc

insert #tmp1
select Date,dealtime,dealprice,sum(cast(dealvolume as int)) dealvolume
from Daily
where commodity = 'TX' and NearPrice = '-'
and expiredate = (select expireDate from #tmp)
and dealtime > '084500'
group by Date,Commodity,expiredate,dealtime,dealprice
order by Date,Commodity,expiredate,dealtime,dealvolume

insert TX_Tick(Date,DealTime,dealPrice,Dealvolume)
select * from #tmp1 order by dealtime

insert TE_Tick(Date,DealTime,dealPrice,Dealvolume)
select Date,dealtime,dealprice,sum(cast(dealvolume as int)) dealvolume
from Daily
where commodity = 'TE' and NearPrice = '-'
and expiredate = (select expireDate from #tmp)
group by Date,Commodity,expiredate,dealtime,dealprice
order by Date,Commodity,expiredate,dealtime

insert TF_Tick(Date,DealTime,dealPrice,Dealvolume)
select Date,dealtime,dealprice,sum(cast(dealvolume as int)) dealvolume
from Daily
where commodity = 'TF' and NearPrice = '-'
and expiredate = (select expireDate from #tmp)
group by Date,Commodity,expiredate,dealtime,dealprice
order by Date,Commodity,expiredate,dealtime

insert MTX_Tick(Date,DealTime,dealPrice,Dealvolume)
select Date,dealtime,dealprice,sum(cast(dealvolume as int)) dealvolume
from Daily
where commodity = 'MTX' and NearPrice = '-'
and expiredate = (select expireDate from #tmp)
group by Date,Commodity,expiredate,dealtime,dealprice
order by Date,Commodity,expiredate,dealtime

drop table #tmp
go

drop table #tmp1
go

truncate table Daily
go

 

除了第一步L還是有把所有TICK資料存到一個超大的TICK TABLE裡,及後面的TE、TF和台指類似就沒提到之外,其它的相關內容都有在之前說明過了。

 

這個SQL排程執行完後,資料庫就已經備份好歷史資料,最後再一個小小的CMD,把Daily.rpt刪除掉。

 
cd D:\期貨DB建置資料\期貨Tick

D:

del Daily.rpt

 

以上三個步驟,設定排程在每日的下午四點半之後,

1.取得期交所報價檔案的 cmd

2.資料庫排程備份歷史報價

3.清除暫存檔

就自動完成了每日備份囉。

 

 

7 則留言:

  1. 版主你寫的東西對我來說根本是有字的天書,也太專業了~!!

    回覆刪除
  2. 好說,好說。小弟也是持續在學習中。

    回覆刪除
  3. L 你好,
    你的文章說明十分專業且清楚,對初學者如我十分受用,但小弟有幾點疑惑:
    1. 建立一個資料庫來維護歷史資料的用途在哪呢?QuoteManager不是已經有database並且會自動回補歷史資料了嗎?還是因為你並沒有使用數據源呢?
    2. 您用Multichart回測的時候又是如何調用database裡的資料呢?
    3. 國外的資料又是如何蒐集呢?

    還請釋疑,謝謝!

    回覆刪除
  4. Hello~
    1. 這幾篇資料庫的應用是從期交所下載每日的行情檔再轉存到資料庫中,目的就是取得最源頭的、最正確的行情資料,用途是在往後回測時使用的資料。而QuoteManager裡頭是凱衛的行情,它的行情資料和期交所的一定有誤差。若是未用凱衛行情源也是一樣的問題,假設是接api或dde串接的行情,那和原始期交所的行情也一定不同,也不是說不能用,只是如果能有最正確的資料來測試策略應該比較好囉~

    2.每日行情轉檔之後會將資料存在table裡,需要時匯出能餵進QuoteManager的文字檔,就可以用這份行情回測~

    3.若是使用券商版的Multicharts,那會有國外期貨的歷史報價資料,但資料長度和正確性需要仔細看看,再來就是自己持續接報價來存,或開國外戶用國外券商軟體來下載,不過這部份我就不是很清楚了^^

    不客氣~

    回覆刪除
  5. 你這一系列取資料做備份的文章,我是看過最好的,對我幫忙最大
    非常感謝你

    認真研究程式交易已經有一年了
    做了很多的策略測試,但是總是因為資料的不足而無法做到自已想要的測試
    很冒昧的請問您,不知您是否願意分享期交所2005年以來選擇權 Tick 交易資料呢?

    若造成你的困擾,在此說聲抱歉

    p.s. 多策略組合這幾篇文章,我也受益良多,謝謝!

    回覆刪除
  6. hi,
    謝謝您的鼓勵,
    我自己也沒有選擇權的tick資料,不好意思,只有期貨的。
    歡迎持續留言討論。

    回覆刪除
  7. 謝謝你,沒想到選擇權 tick 這麼難找
    真不行就要下手跟期交所買了

    最近要開始參考你的文章,建立自動抓取及備份期交所的資料
    繼續期待你的好文分享,謝謝!

    回覆刪除