|
|
楼主

楼主 |
发表于 2008-9-10 09:53:41
|
只看该作者
[转贴] 金融时间数据序列的相邻数据记录比较问题
现在将从Txt文本导入到数据库中的表中(高频金融时间数据,每天记录可能高达10-15万条记录),结构如下:
成交时间 成交价格 成交数量 成交金额
09:25:00 9.550 200 1910.000
09:30:00 9.540 500 4770.000
09:30:00 9.540 100 954.000
09:30:00 9.530 1000 9530.000
09:30:00 9.520 9000 85680.000
09:30:00 9.520 1000 9520.000
09:30:00 9.520 600 5712.000
09:30:00 9.530 2000 19060.000
09:30:00 9.540 1000 9540.000
09:30:01 9.540 900 8586.000
09:30:01 9.540 300 2862.000
09:30:01 9.540 500 4770.000
09:30:01 9.540 1500 14310.000
09:30:01 9.520 5000 47600.000
注意这个交易数据中的记录是按时间顺序排列,不能打乱,否则没有意义。
现在的要求是,要生成一个新的字段 :“实际成交”,实际成交值根据下面的原则产生:
1.比较时间相邻的二个交易价格,如果后一笔交易价格比前者高,那么实际成交=成交数量(此笔成交数量为正),
否则,实际成交=-成交数量(此笔成交数量为负)。
2.如果二者价格相等,则前面实际成交值为正,那么该笔实际成交也为正,如果为负,该笔实际成交也为负。
新表的根据上述原则,生成新字段如下:
成交时间 成交价格 成交数量 成交金额 实际成交
09:25:00 9.550 200 1910.000 200
09:30:00 9.540 500 4770.000 -500
09:30:00 9.540 100 954.000 -100
09:30:00 9.530 1000 9530.000 -1000
09:30:00 9.520 9000 85680.000 -9000
09:30:00 9.520 1000 9520.000 -1000
09:30:00 9.520 600 5712.000 -600
09:30:00 9.530 2000 19060.000 2000
09:30:00 9.540 1000 9540.000 1000
09:30:01 9.540 900 8586.000 900
09:30:01 9.540 300 2862.000 300
09:30:01 9.540 500 4770.000 500
09:30:01 9.540 1500 14310.000 1500
09:30:01 9.520 5000 47600.000 -5000
[code:1rvhyycc]/*ahuige的代码*/
data ahuige(drop=preprice presign);
input time time8. price amount total;
actual=amount*sign(price-preprice)+amount*presign*(price=preprice);
retain preprice presign 0;
preprice=price;
presign=sign(actual);
cards;
09:25:00 9.550 200 1910.000
09:30:00 9.540 500 4770.000
09:30:00 9.540 100 954.000
09:30:00 9.530 1000 9530.000
09:30:00 9.520 9000 85680.000
09:30:00 9.520 1000 9520.000
09:30:00 9.520 600 5712.000
09:30:00 9.530 2000 19060.000
09:30:00 9.540 1000 9540.000
09:30:01 9.540 900 8586.000
09:30:01 9.540 300 2862.000
09:30:01 9.540 500 4770.000
09:30:01 9.540 1500 14310.000
09:30:01 9.520 5000 47600.000
;
run;[/code:1rvhyycc]
[code:1rvhyycc]/*sxlion的代码*/
/*sas提供了另外一函数choosen()可使用,弥补了ifn()的不足。*/
data ahuige(drop=preprice presign);
input time time8. price amount total;
/* actual=amount*sign(price-preprice)+amount*presign*(price=preprice);*/
actual=choosen(sign(price-preprice)+2,-amount,amount*presign,amount);
retain preprice presign 0;
preprice=price;
presign=sign(actual);
cards;
09:25:00 9.550 200 1910.000
09:30:00 9.540 500 4770.000
09:30:00 9.540 100 954.000
09:30:00 9.530 1000 9530.000
09:30:00 9.520 9000 85680.000
09:30:00 9.520 1000 9520.000
09:30:00 9.520 600 5712.000
09:30:00 9.530 2000 19060.000
09:30:00 9.540 1000 9540.000
09:30:01 9.540 900 8586.000
09:30:01 9.540 300 2862.000
09:30:01 9.540 500 4770.000
09:30:01 9.540 1500 14310.000
09:30:01 9.520 5000 47600.000
;
proc print;
run;[/code:1rvhyycc] |
|