1
1
2
2
function dayNumber (inDate:: Date )
3
- return Dates. date2epochdays (inDate):: Integer ;
3
+ return Dates. date2epochdays (inDate):: Integer
4
4
end
5
5
6
- excelcostant= 693959 ;
6
+ excelcostant = 693959 ;
7
7
8
8
"""
9
9
From Excel Number Format to Date
@@ -22,7 +22,7 @@ julia> fromExcelNumberToDate(45000)
22
22
```
23
23
"""
24
24
function fromExcelNumberToDate (num:: Integer ):: Date
25
- return Dates. Dates. epochdays2date (excelcostant+ num);
25
+ return Dates. Dates. epochdays2date (excelcostant + num)
26
26
end
27
27
28
28
"""
@@ -42,18 +42,18 @@ julia> daysact(Date(1996,10,12),Date(1998,1,10))
42
42
455
43
43
```
44
44
"""
45
- function daysact (Date1:: Date ,Date2:: Date ):: Integer
46
- D1 = dayNumber (Date1);
47
- D2 = dayNumber (Date2);
48
- dayCount= D2 - D1;
49
- return dayCount;
45
+ function daysact (Date1:: Date , Date2:: Date ):: Integer
46
+ D1 = dayNumber (Date1)
47
+ D2 = dayNumber (Date2)
48
+ dayCount = D2 - D1
49
+ return dayCount
50
50
end
51
51
52
52
function isLastOfFebruary (inDate:: Date ):: Bool
53
- return (Dates. isleapyear (Dates. year (inDate))&& (Dates. day (inDate)== 29 )&& (Dates. month (inDate)== 2 ))|| ((! Dates. isleapyear (Dates. year (inDate)))&& (Dates. day (inDate)== 28 )&& (Dates. month (inDate)== 2 ));
53
+ return (Dates. isleapyear (Dates. year (inDate)) && (Dates. day (inDate) == 29 ) && (Dates. month (inDate) == 2 )) || ((! Dates. isleapyear (Dates. year (inDate))) && (Dates. day (inDate) == 28 ) && (Dates. month (inDate) == 2 ))
54
54
end
55
55
56
- currMaxImplemented= 7 ;
56
+ currMaxImplemented = 7 ;
57
57
58
58
"""
59
59
Fraction of year between two Dates according the following convention
@@ -81,102 +81,101 @@ julia> yearfrac(Date(1996,10,12),Date(1998,1,10),1)
81
81
1.2444444444444445
82
82
```
83
83
"""
84
- function yearfrac (startDate:: Date ,endDate:: Date ,convention:: Integer ):: Real
85
- if (convention< 0 )
86
- error (" Negative basis are not defined, check the help" )
87
- end
88
- if (convention> currMaxImplemented)
89
- error (" Convention not implemented yet" )
90
- end
91
- if (startDate> endDate)
92
- return - yearfrac (endDate,startDate,convention);
93
- elseif (startDate== endDate)
94
- return 0.0 ;
95
- end
96
- yearFrac= 0.0 ;
97
- y1= Dates. year (startDate);
98
- m1= Dates. month (startDate);
99
- y2= Dates. year (endDate);
100
- m2= Dates. month (endDate);
101
- d1= Dates. day (startDate);
102
- d2= Dates. day (endDate);
103
- if (convention== 0 )# (ACT/ACT)
104
- Nday= daysact (startDate,endDate);
105
- EndOFYear= Date (1 );
106
- if (isLastOfFebruary (startDate)&& (Dates. day (startDate)== 29 ))
107
- EndOFYear= Date (Dates. year (startDate)+ 1 ,3 ,1 );
108
- else
109
- EndOFYear= Date (Dates. year (startDate)+ 1 ,Dates. month (startDate),Dates. day (startDate));
110
- end
111
- yearFrac= (Nday)/ daysact (startDate,EndOFYear);
112
- elseif (convention== 1 ) # (30/360 SIA)
113
- if (isLastOfFebruary (startDate)&& isLastOfFebruary (endDate))
114
- d2= 30 ;
115
- end
116
- if (isLastOfFebruary (startDate)|| Dates. day (startDate)== 31 )
117
- d1= 30 ;
118
- end
119
- if (d1== 30 && d2== 31 )
120
- d2= 30 ;
121
- end
122
- dy= y2- y1;
123
- dm= m2- m1;
124
- dd= d2- d1;
125
- yearFrac= (360.0 * dy+ 30.0 * dm+ dd)/ 360.0 ;
126
- elseif (convention== 2 ) # (ACT/360)
127
- Nday= daysact (startDate,endDate);
128
- yearFrac= (Nday)/ 360.0 ;
129
- elseif (convention== 3 ) # (ACT/365)
130
- Nday= daysact (startDate,endDate);
131
- yearFrac= ( Nday)/ 365.0 ;
132
- elseif (convention== 4 ) # (30/360 PSA)
133
- if ((Dates. day (startDate)== 31 )|| isLastOfFebruary (startDate))
134
- d1= 30 ;
135
- end
136
- if ((Dates. day (startDate)== 30 || isLastOfFebruary (startDate))&& Dates. day (endDate)== 31 )
137
- d2= 30 ;
138
- end
139
- dy= y2- y1;
140
- dm= m2- m1;
141
- dd= d2- d1;
142
- yearFrac= (360.0 * dy+ 30.0 * dm+ dd)/ 360.0 ;
143
-
144
- elseif (convention== 5 ) # (30/360 ISDA)
145
- y1= Dates. year (startDate);
146
- m1= Dates. month (startDate);
147
- y2= Dates. year (endDate);
148
- m2= Dates. month (endDate);
149
- if (Dates. day (startDate)< 31 )
150
- d1= Dates. day (startDate);
151
- else
152
- d1= 30 ;
153
- end
154
- if ((Dates. day (endDate)== 31 )&& (d1> 29 ))
155
- d2= 30 ;
156
- else
157
- d2= Dates. day (endDate);
158
- end
159
- yearFrac= (360.0 * ((y2- y1))+ 30.0 * ((m2- m1))+ (d2- d1))/ 360.0 ;
160
-
161
- elseif (convention== 6 ) # (30E/360)
162
- if (d1== 31 )
163
- d1= 30 ;
164
- end
165
- if (d2== 31 )
166
- d2= 30 ;
167
- end
168
- dy= y2- y1;
169
- dm= m2- m1;
170
- dd= d2- d1;
171
- yearFrac= (360.0 * dy+ 30.0 * dm+ dd)/ 360.0 ;
172
- elseif (convention== 7 ) # (ACT/365 JPN)
173
- daydistance = [0 ;31 ;59 ;90 ;120 ;151 ;181 ;212 ;243 ;273 ;304 ;334 ];
174
- dy= y2- y1;
175
- dd= d2- d1;
176
- dayCount= (365.0 * dy+ daydistance[m2]- daydistance[m1]+ dd)
177
- yearFrac = dayCount/ 365.0 ;
178
- end
179
-
180
- return yearFrac;
181
-
84
+ function yearfrac (startDate:: Date , endDate:: Date , convention:: Integer ):: Real
85
+ if (convention < 0 )
86
+ error (" Negative basis are not defined, check the help" )
87
+ end
88
+ if (convention > currMaxImplemented)
89
+ error (" Convention not implemented yet" )
90
+ end
91
+ if (startDate > endDate)
92
+ return - yearfrac (endDate, startDate, convention)
93
+ elseif (startDate == endDate)
94
+ return 0.0
95
+ end
96
+ yearFrac = 0.0
97
+ y1 = Dates. year (startDate)
98
+ m1 = Dates. month (startDate)
99
+ y2 = Dates. year (endDate)
100
+ m2 = Dates. month (endDate)
101
+ d1 = Dates. day (startDate)
102
+ d2 = Dates. day (endDate)
103
+ if (convention == 0 )# (ACT/ACT)
104
+ Nday = daysact (startDate, endDate)
105
+ EndOFYear = Date (1 )
106
+ if (isLastOfFebruary (startDate) && (Dates. day (startDate) == 29 ))
107
+ EndOFYear = Date (Dates. year (startDate) + 1 , 3 , 1 )
108
+ else
109
+ EndOFYear = Date (Dates. year (startDate) + 1 , Dates. month (startDate), Dates. day (startDate))
110
+ end
111
+ yearFrac = (Nday) / daysact (startDate, EndOFYear)
112
+ elseif (convention == 1 ) # (30/360 SIA)
113
+ if (isLastOfFebruary (startDate) && isLastOfFebruary (endDate))
114
+ d2 = 30
115
+ end
116
+ if (isLastOfFebruary (startDate) || Dates. day (startDate) == 31 )
117
+ d1 = 30
118
+ end
119
+ if (d1 == 30 && d2 == 31 )
120
+ d2 = 30
121
+ end
122
+ dy = y2 - y1
123
+ dm = m2 - m1
124
+ dd = d2 - d1
125
+ yearFrac = (360.0 * dy + 30.0 * dm + dd) / 360.0
126
+ elseif (convention == 2 )# (ACT/360)
127
+ Nday = daysact (startDate, endDate)
128
+ yearFrac = (Nday) / 360.0
129
+ elseif (convention == 3 )# (ACT/365)
130
+ Nday = daysact (startDate, endDate)
131
+ yearFrac = (Nday) / 365.0
132
+ elseif (convention == 4 )# (30/360 PSA)
133
+ if ((Dates. day (startDate) == 31 ) || isLastOfFebruary (startDate))
134
+ d1 = 30
135
+ end
136
+ if ((Dates. day (startDate) == 30 || isLastOfFebruary (startDate)) && Dates. day (endDate) == 31 )
137
+ d2 = 30
138
+ end
139
+ dy = y2 - y1
140
+ dm = m2 - m1
141
+ dd = d2 - d1
142
+ yearFrac = (360.0 * dy + 30.0 * dm + dd) / 360.0
143
+
144
+ elseif (convention == 5 )# (30/360 ISDA)
145
+ y1 = Dates. year (startDate)
146
+ m1 = Dates. month (startDate)
147
+ y2 = Dates. year (endDate)
148
+ m2 = Dates. month (endDate)
149
+ if (Dates. day (startDate) < 31 )
150
+ d1 = Dates. day (startDate)
151
+ else
152
+ d1 = 30
153
+ end
154
+ if ((Dates. day (endDate) == 31 ) && (d1 > 29 ))
155
+ d2 = 30
156
+ else
157
+ d2 = Dates. day (endDate)
158
+ end
159
+ yearFrac = (360.0 * ((y2 - y1)) + 30.0 * ((m2 - m1)) + (d2 - d1)) / 360.0
160
+
161
+ elseif (convention == 6 )# (30E/360)
162
+ if (d1 == 31 )
163
+ d1 = 30
164
+ end
165
+ if (d2 == 31 )
166
+ d2 = 30
167
+ end
168
+ dy = y2 - y1
169
+ dm = m2 - m1
170
+ dd = d2 - d1
171
+ yearFrac = (360.0 * dy + 30.0 * dm + dd) / 360.0
172
+ elseif (convention == 7 )# (ACT/365 JPN)
173
+ daydistance = [0 ; 31 ; 59 ; 90 ; 120 ; 151 ; 181 ; 212 ; 243 ; 273 ; 304 ; 334 ]
174
+ dy = y2 - y1
175
+ dd = d2 - d1
176
+ dayCount = (365.0 * dy + daydistance[m2] - daydistance[m1] + dd)
177
+ yearFrac = dayCount / 365.0
178
+ end
179
+
180
+ return yearFrac
182
181
end
0 commit comments