Skip to content

Commit 63f465c

Browse files
committed
formatting
1 parent 7342229 commit 63f465c

13 files changed

+1084
-1088
lines changed

.JuliaFormatter.toml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
remove_extra_newlines = true
2+
whitespace_typedefs=true
3+
trailing_comma=false
4+
margin=1200

src/FinancialToolbox.jl

+27-29
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
__precompile__()
22
module FinancialToolbox
33

4-
using SpecialFunctions: erf
5-
using Dates,Requires
6-
include("dates.jl");
7-
function __init__()
8-
@require DualNumbers = "fa6b7ba4-c1ee-5f82-b5fc-ecf0adba8f74" include("financial_dual.jl")
9-
@require HyperDualNumbers = "50ceba7f-c3ee-5a84-a6e8-3ad40456ec97" include("financial_hyper.jl")
10-
end
11-
include("financial.jl")
12-
export
13-
normcdf,
14-
normpdf,
15-
blsprice,
16-
blkprice,
17-
blsdelta,
18-
blsgamma,
19-
blsvega,
20-
blsrho,
21-
blstheta,
22-
blslambda,
23-
blsimpv,
24-
blkimpv,
25-
## ADDITIONAL Function
26-
blspsi,
27-
blsvanna,
28-
#Dates
29-
fromExcelNumberToDate,
30-
daysact,
31-
yearfrac
32-
4+
using SpecialFunctions: erf
5+
using Dates, Requires
6+
include("dates.jl")
7+
function __init__()
8+
@require DualNumbers = "fa6b7ba4-c1ee-5f82-b5fc-ecf0adba8f74" include("financial_dual.jl")
9+
@require HyperDualNumbers = "50ceba7f-c3ee-5a84-a6e8-3ad40456ec97" include("financial_hyper.jl")
10+
end
11+
include("financial.jl")
12+
export normcdf,
13+
normpdf,
14+
blsprice,
15+
blkprice,
16+
blsdelta,
17+
blsgamma,
18+
blsvega,
19+
blsrho,
20+
blstheta,
21+
blslambda,
22+
blsimpv,
23+
blkimpv,
24+
## ADDITIONAL Function
25+
blspsi,
26+
blsvanna,
27+
#Dates
28+
fromExcelNumberToDate,
29+
daysact,
30+
yearfrac
3331

3432
end#End Module

src/dates.jl

+107-108
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
function dayNumber(inDate::Date)
3-
return Dates.date2epochdays(inDate)::Integer;
3+
return Dates.date2epochdays(inDate)::Integer
44
end
55

6-
excelcostant=693959;
6+
excelcostant = 693959;
77

88
"""
99
From Excel Number Format to Date
@@ -22,7 +22,7 @@ julia> fromExcelNumberToDate(45000)
2222
```
2323
"""
2424
function fromExcelNumberToDate(num::Integer)::Date
25-
return Dates.Dates.epochdays2date(excelcostant+num);
25+
return Dates.Dates.epochdays2date(excelcostant + num)
2626
end
2727

2828
"""
@@ -42,18 +42,18 @@ julia> daysact(Date(1996,10,12),Date(1998,1,10))
4242
455
4343
```
4444
"""
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
5050
end
5151

5252
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))
5454
end
5555

56-
currMaxImplemented=7;
56+
currMaxImplemented = 7;
5757

5858
"""
5959
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)
8181
1.2444444444444445
8282
```
8383
"""
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
182181
end

0 commit comments

Comments
 (0)