如何在SAS中重新构建限价指令簿(Limit Order Book):使用HashTable

来源:互联网 发布:discuz源码安装 编辑:程序博客网 时间:2024/06/05 19:14

在之前的一篇日志里(http://blog.csdn.net/u010501526/article/details/8875446),我将重新构建LOB(Limit Order Book)分为了三步

1)如何用sas读取nasdaq total-view数据

2)根据每一个message的reference number找到这个message是属于哪只股票

3)利用某只股票的message来重构LOB

第一步已经有代码了,第二步可以自己构思一下,方法不是唯一的。第三步会很有意思,方法可能也不是唯一的,目前找到的效率最好的一种方法是用Hash Table来做,SAS在这块的功能很强大,有一篇文章就专门谈了这一点。

文章地址:http://www.nesug.org/Proceedings/nesug11/fi/fi04.pdf

这篇文章使用伦敦股票交易所的数据来重新构建LOB,步骤大致分为

1)先将数据处理成如下格式


列:每一行是一个新的message

行:从最右边开始,这样逻辑上清晰

第5列,lo_price表示的是价位,比如提单在多少价位,撤单在多少价位,成交在多少价位

第4列,event_date是时间戳

第3列,ohebs是买卖方向

第2列,sflag指令的变动数量

第1列,stamt是股票的变动数量

2)在得到上面这种数据格式之后就可以用下面的程序

/*Hashing programme to reconstruct the both sides of the LOB after each orderevent. For each price line, the reconstructed LOB contains the aggregatedvolume and the number of orders outstanding.*/data lob (keep = event_date lo_price ohebs depth ordcnt);/* initialization */if _n_ = 1 thendo;set book point = _n_;length depth 8 ordcnt 8;declare hash hh (hashexp: 10);/* Set up the hash table to recreate outstanding volume and number of orders at each price line in the buy and sell LOB*/hh.DefineKey ('lo_price', 'ohebs');hh.DefineData ('lo_price', 'ohebs', 'depth', 'ordcnt');DefineDone ();call missing (lo_price, ohebs, depth, ordcnt);declare hiter hiter ('hh');end;do until (last.event_date);set book;/* The objective is to reconstruct the LOB after each event time.*/by event_date; rc = hh.find ();/* If buy or sell limit price is found in the table, add aggregated size of the new orders to outstanding volume and add the number of new orders to number of outstanding orders at each price line.*/if (rc = 0) thendo;depth ++ stamt;ordcnt ++ sflag;hh.replace ();end;elsedo;depth = stamt;ordcnt = sflag;hh.add ();end;end;/* After each event time dump the content of the hash table.*/rc = hiter.first();do while (rc = 0);/* If the a table address is empty or size of the remaining order is zero then don't dump the table address.*/if depth GT 0 and ordcnt GT 0 then output;rc = hiter.next ();end;run;

得到的限价指令簿如下:



最后说一下用hash table的好处,由于sas在data步处理数据一般是从硬盘中读数据再放回,这样速度慢,但是hash table是在内存中存储数据,所以速度会快非常多,但也存在一个制约,即从硬盘读数据存数据意味着可以处理的数据量是硬盘的大小,而用内存处理数据那可以处理的数据量就只有内存的大小,所以在SAS中用hash table可能会出现内存不足,当然这是很极端的情况
原创粉丝点击