-
Notifications
You must be signed in to change notification settings - Fork 14
/
3-upload-short-front-sinking-complex-bond-insturments.sql
155 lines (125 loc) · 4.76 KB
/
3-upload-short-front-sinking-complex-bond-insturments.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
-- ============================================================
-- Description:
-- In this query, we run an ETL process on some complex bonds.
-- 1. First, we load an Excel file of complex bond data from Drive.
-- 2. Next, we transform the core data and create schedule json fields.
-- 3. Then we combine the schedules json fields into a single column.
-- 4. Finally we upload the instrument data into LUSID.
-- ============================================================
----------------------
-- Define constants --
----------------------
@@data = select 'fixed-stubs-sinking-bond.xlsx';
@@file = select 'luminesce-examples';
@@levelType = select 'Absolute';
@@stepScheduleType = select 'Notional';
@@scope = select 'complexBondTesting';
--------------------------------
-- 1. Extract Instrument Data --
--------------------------------
-- Extract bond data from LUSID Drive
@extractCmplxBondAssetData = use Drive.Excel with @@data, @@file
--file={@@file}
--folderFilter={@@data}
--worksheet=bond-data
enduse;
-- Extract step schedule data from LUSID Drive
@extractCmplxBondStepsData = use Drive.Excel with @@data, @@file
--file={@@file}
--folderFilter={@@data}
--worksheet=steps-data
enduse;
---------------------------------
-- 2. Transform data using SQL --
---------------------------------
-- Transform core complex bond fields
@coreCmplxBondData = select
[Name] as [DisplayName],
[ISIN] as [Isin],
[AssetID] as [ClientInternal],
'Standard' as [CalculationType]
from @extractCmplxBondAssetData;
-- Transform front fixed schedule data
@formatFrontStubFixedScheduleData = select
[AssetID] as [AssetID],
[Interest Start Date] as [StartDate],
[First Coupon Date] as [Maturitydate],
[Currency] as [Currency],
[DCC] as [DayCountConvention],
[Payment Frequency] as [PaymentFrequency],
'None' as [RollConvention],
[Currency] as[PaymentCalendars],
[Currency] as [ResetCalendars],
[Settle Days] as [SettleDays],
0 as [ResetDays],
true as [LeapDaysIncluded],
[Notional] as [Notional],
[Coupon] as [CouponRate],
[Currency] as [PaymentCurrency],
[Front Stub Type] as [StubType]
from @extractCmplxBondAssetData;
-- Add front fixed schedule JSON fields to core fields
@addFrontStubSchedule = select d.*, d.ClientInternal, results.*
from @coreCmplxBondData d
outer apply (
select JsonString as [FixedScheduleJSONFront] from Schedules.Fixed_schedule fs where
fs.AssetID = d.ClientInternal and fs.FixedTable = @formatFrontStubFixedScheduleData and d.ClientInternal = fs.AssetID
) results ;
-- Transform back fixed schedule data
@formatBackStubFixedScheduleData = select
[AssetID] as [AssetID],
[First Coupon Date] as [StartDate],
[Maturity Date] as [Maturitydate],
[Currency] as [Currency],
[DCC] as [DayCountConvention],
[Payment Frequency] as [PaymentFrequency],
'None' as [RollConvention],
[Currency] as[PaymentCalendars],
[Currency] as [ResetCalendars],
[Settle Days] as [SettleDays],
0 as [ResetDays],
true as [LeapDaysIncluded],
[Notional] as [Notional],
[Coupon] as [CouponRate],
[Currency] as [PaymentCurrency],
[Back Stub Type] as [StubType]
from @extractCmplxBondAssetData;
-- Add back fixed schedule JSON fields to core fields
@addBackStubSchedule = select d.*, d.ClientInternal, results.*
from @addFrontStubSchedule d
outer apply (
select JsonString as [FixedScheduleJSONBack] from Schedules.Fixed_schedule fs where
fs.AssetID = d.ClientInternal and fs.FixedTable = @formatBackStubFixedScheduleData and d.ClientInternal = fs.AssetID
) results ;
-- Transform step schedule data
@formatStepScheduleData = select
[AssetID] as [AssetID],
[Date] as [Date],
[Value] as [Quantity]
from @extractCmplxBondStepsData;
-- Add step schedule JSON fields to core fields
@addStepSchedule = select d.*, d.ClientInternal, results.*
from @addBackStubSchedule d
outer apply (
select JsonString as [StepScheduleJSON] from Schedules.Step_schedule ss where
ss.StepsTable = @formatStepScheduleData and ss.LevelType = @@levelType and ss.StepScheduleType = @@stepScheduleType and ss.AssetFilter = d.ClientInternal and d.ClientInternal = ss.AssetID
) results ;
-------------------------------
-- 3. Combine schedules JSON --
-------------------------------
-- Format full complex bond data with combined schedules list column
@complexBondDataToLoad = select
[DisplayName],
[Isin],
[ClientInternal],
[CalculationType],
json_array(
json(FixedScheduleJSONFront), json(FixedScheduleJSONBack), json(StepScheduleJSON)
) as [SchedulesJson]
from @addStepSchedule;
-----------------------------------------
-- 4. Load formatted complex bond data --
-----------------------------------------
@load = select * from Lusid.Instrument.ComplexBond.Writer where
ToWrite = @complexBondDataToLoad;
select * from @load;