恆生和摩台指的最後交易日為每月的倒數第二個工作天,要寫結算語法的話比寫台指、小道瓊、那斯達克、小SP等困難得多了! 以下我示範怎麼寫:
Vars:x(0),y(0);
x = dayofweek(date);
y = dayofmonth(date);
If date <> date[1] then SecondLastWorkingDay = false;
If Month(date) = 2 or Month(date) = 4 or Month(date) = 6 or Month(date) = 9 or Month(date) = 11 then begin
If Month(date) = 2 and Mod(year(date)+1900,4) = 0 then value1 = 29;
If Month(date) = 2 and Mod(year(date)+1900,4) <> 0 then value1 = 28;
If Month(date) = 4 or Month(date) = 6 or Month(date) = 9 or
Month(date) = 11 then value1 = 30;
end else begin
value1 = 31;
end;
Value2 = Mod(value1-y,7);
If x + value2 = 6 or x + value2 = 7 or x + value2 = 0 then begin
If x + value2 = 6 then value3 = value1-1;
If x + value2 = 7 or x + value2 = 0 then value3 = value1-2;
end else begin
value3 = value1;
end;
If dayofweek(date) = 1 and dayofmonth(date) = value3-1 then SecondLastWorkingDay = true;
If dayofweek(date) = 2 and dayofmonth(date) = value3-1 then SecondLastWorkingDay = true;
If dayofweek(date) = 3 and dayofmonth(date) = value3-1 then SecondLastWorkingDay = true;
If dayofweek(date) = 4 and dayofmonth(date) = value3-1 then SecondLastWorkingDay = true;
If dayofweek(date) = 5 and dayofmonth(date) = value3-3 then SecondLastWorkingDay = true;
在Powerlanguage Editor 裡新增一個函數SecondLastWorkingDay,函數回傳類型為True/False,儲存類型為時序,再定義兩個變數 Vars:x(0),y(0);
首先計算當月有幾多天數,儲存於value1,月份1,3,5,7,8,10,12 有31日,月份4,6,9,11 則有30日,至於2 月,可能是28日或29日。每四年便會出現一次2 月是29日的,如果年份可以被4整除的話,當年的2 月就有29日。例如今年2020 年,2020 可被4整除,所以今年的2 月有29 日。用Mod 這個關鍵字就能算出年份可不可以被4整除,這個關鍵字的語法是mod (被除數,除數)。Year 這個關鍵字則傳回年份,它傳回的格式是ELDate的年份,例如Year(1200617)
傳回120。
算完當月有幾多天數後,下一步計算當月最後的工作天是幾號,儲存於value3。如果當月最後一日是星期一至星期五,那麼當日便是最後的工作天 ( value1 = value3)。 如果當月最後一日是星期六或星期日,最後的工作天就會提早一天 (value3
= value1-1) 或兩天(value3 =
value1-2)。
這一部份的計算會用到變數x和y,x = dayofweek(date); y = dayofmonth(date); dayofweek(date)傳回當天是星期幾,dayofmonth(date)傳回當天是幾號,Value2是計算當月剩餘的日子被 7 除之後的餘數,x + value2 可得出當月最後一日是星期幾,1 至 7 代表星期一至星期日。如果算出來 x + value2 等於1、2、3、4 或 5,當月最後一日是星期一至五,即是當月最後一日是工作天。如果算出來 x + value2 等於 6,當月最後一日是星期六,當天不是工作天,上一天才是工作天,所以value3 = value1-1。如果算出來 x + value2 等於 7,當月最後一日是星期天,當天不是工作天,上兩天才是工作天,所以value3 = value1-2。
到這裡還未完結啊! 還要算出最後交易日,最後交易日為每月的倒數第二個工作天,value3 再減1。可是這只是星期一至星期四正確,星期五的話value3 要減3。因為如果最後的工作天是星期一的話,最後交易日會是3日前。
如果看完後還是不明白,可以嘗試另一個寫法,就是把所有的可能性一一列出來。
語法如下,新增一個函數HSIsettlement,函數回傳類型為True/False,儲存類型為時序
var: M(0),DM(0),DW(0);
If date <> date[1] then HSIsettlement = false;
M=Month(Date);
DM=DayOfMonth(Date);
DW=DayOfWeek(Date);
If M=1 or M=3 or M=5 or M=7 or M=8 or M=10 or M=12 then begin
If DM=30 and DW=4 then HSIsettlement = true;
If DM=30 and DW=3 then HSIsettlement = true;
If DM=30 and DW=2 then HSIsettlement = true;
If DM=30 and DW=1 then HSIsettlement = true;
If DM=29 and DW=4 then HSIsettlement = true;
If DM=28 and DW=5 then HSIsettlement = true;
If DM=28 and DW=4 then HSIsettlement = true;
end;
If M=4 or M=6 or M=9 or M=11 then begin
If DM=29 and DW=4 then HSIsettlement = true;
If DM=29 and DW=3 then HSIsettlement = true;
If DM=29 and DW=2 then HSIsettlement = true;
If DM=29 and DW=1 then HSIsettlement = true;
If DM=28 and DW=4 then HSIsettlement = true;
If DM=27 and DW=5 then HSIsettlement = true;
If DM=27 and DW=4 then HSIsettlement = true;
end;
If M=2 and Mod(Year(Date)+1900,4) <> 0 then begin
If DM=27 and DW=4 then HSIsettlement = true;
If DM=27 and DW=3 then HSIsettlement = true;
If DM=27 and DW=2 then HSIsettlement = true;
If DM=27 and DW=1 then HSIsettlement = true;
If DM=26 and DW=4 then HSIsettlement = true;
If DM=25 and DW=5 then HSIsettlement = true;
If DM=25 and DW=4 then HSIsettlement = true;
end;
If M=2 and Mod(Year(Date)+1900,4) = 0 then begin
If DM=28 and DW=4 then HSIsettlement = true;
If DM=28 and DW=3 then HSIsettlement = true;
If DM=28 and DW=2 then HSIsettlement = true;
If DM=28 and DW=1 then HSIsettlement = true;
If DM=27 and DW=4 then HSIsettlement = true;
If DM=26 and DW=5 then HSIsettlement = true;
If DM=26 and DW=4 then HSIsettlement = true;
end;