Open pillajagadishwar opened 1 year ago
CREATE PROCEDURE staging.ni_load_prices_dom_legacy
@security_id_start BIGINT = NULL,
@security_id_end BIGINT = NULL,
@start_date DATETIME = NULL,
@end_date DATETIME = NULL
AS
/Begin parameter logging/
DECLARE @loggingParameters NVARCHAR(MAX)
DECLARE @loggingValues NVARCHAR(MAX)
DECLARE @loggingDatabaseName SYSNAME
DECLARE @loggingProcedureSchema SYSNAME
DECLARE @loggingProcedureName SYSNAME
DECLARE @callId INT
SELECT
@loggingDatabaseName = SPECIFIC_CATALOG,
@loggingProcedureSchema = SPECIFIC_SCHEMA,
@loggingProcedureName = SPECIFIC_NAME
FROM INFORMATION_SCHEMA.ROUTINES
WHERE OBJECT_ID(ROUTINE_SCHEMA + '.' + ROUTINE_NAME) = @@PROCID
SET @loggingParameters = '@security_id_start|@security_id_end|@start_date|@end_date'
SET @loggingValues = REPLACE(ISNULL(CAST(@security_id_start AS NVARCHAR(MAX)), 'XNULLX'),'|','
EXEC log.log_procedure_call @databaseName = @loggingDatabaseName, @procedureName = @loggingProcedureName, @procedureSchema = @loggingProcedureSchema, @loggingParameters = @loggingParameters, @loggingValues = @loggingValues, @callId = @callId OUTPUT
/Endof parameter logging/
BEGIN
set nocount on
set xact_abort on
DECLARE @max_price_date datetime
declare @error_msg varchar(max)
declare @NOW datetime
declare @PRC_DEFAULT_LOOKBACK int
declare @BATCH_SIZE int
declare @PROCEDURE_NAME varchar(50) = 'staging.ni_load_prices_dom_legacy'
DECLARE @PREV_BIZ_DAY datetime
set @NOW = getdate()
set @PRC_DEFAULT_LOOKBACK = convert(int, util.get_config_value('PRC_DEFAULT_LOOKBACK', null))
set @BATCH_SIZE = convert(int, util.get_config_value('PRC_BATCH_SIZE', null))
SET @PREV_BIZ_DAY = (SELECT prev_business_date FROM ref.ni_calendar_date WHERE calendar_code = 'INTL' AND obsdate = CONVERT(DATE, GETDATE()))
-- if @end_date is null, then set it to today's date
set @end_date = isnull(@end_date, convert(datetime, floor(convert(float, @NOW))))
-- set the start_date
set @start_date = isnull(@start_date, dateadd(day, abs(@PRC_DEFAULT_LOOKBACK) * -1, @end_date))
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': begin'
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': @PRC_DEFAULT_LOOKBACK=' + convert(varchar, @PRC_DEFAULT_LOOKBACK)
' @end_date=' + isnull(convert(varchar, @end_date, 112), 'null')
-- get the multi idc code map
if (object_id('tempdb..#idc_map') is not null) begin drop table #idc_map end
select security_id, start_date, end_date,
case when start_date < @start_date then @start_date else start_date end as prc_start_date,
case when end_date > @end_date then @end_date else end_date end as prc_end_date,
idc_region, idc_code, ri_adj_factor, cum_split_adj_factor
into #idc_map
from sec.ni_security_idc_code_map_view
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': created #idc_map, rows='+convert(varchar,@@rowcount)
if exists (select * from #idc_map where security_id between @security_id_start and @security_id_end and start_date <= @end_date and end_date >= @start_date and idc_region not in ('DOM', 'CAN'))
begin
select distinct s.security_id, ni_trading_symbol, security_name, trade_country
from sec.ni_security s
inner join #idc_map m on (s.security_id = m.security_id)
where s.security_id between @security_id_start and @security_id_end and m.start_date <= @end_date and m.end_date >= @start_date and idc_region not in ('DOM', 'CAN')
set @error_msg = @PROCEDURE_NAME + ': @security_id_start/@security_id_end cannot contain securities with idc_region not in (DOM, CAN).'
declare @security_id_start_varchar varchar(10)
, @security_id_end_varchar varchar(10)
select @security_id_start_varchar = isnull(@security_id_start, 0)
select @security_id_end_varchar = isnull(@security_id_end, 10000000)
select @error_msg = @error_msg + char(13) + char(10) + char(13) + char(10)
select @error_msg = @error_msg +
'--For more details, run below query:
select distinct sec.security_id
, sec.ni_trading_symbol
, sec.security_name
, sec.trade_country
, mv.start_date
, mv.end_date
, case
when start_date < ''' + convert(varchar, @start_date) + '''
then ''' + convert(varchar, @start_date) + '''
else start_date
end as prc_start_date
, case
when end_date > ''' + convert(varchar, @end_date) + '''
then ''' + convert(varchar, @end_date) + '''
else end_date
end as prc_end_date
, mv.idc_region
, mv.idc_code
, mv.ri_adj_factor
, mv.cum_split_adj_factor
from sec.ni_security_idc_code_map_view mv
join sec.ni_security sec
on mv.security_id = sec.security_id
where sec.security_id between ' + @security_id_start_varchar + ' and ' + @security_id_end_varchar + '
and mv.start_date <= ''' + convert(varchar, @start_date) + '''
and mv.end_date >= ''' + convert(varchar, @end_date) + '''
and mv.idc_region not in (''DOM'', ''CAN'')'
raiserror(@error_msg, 16, 1)
RETURN 10
END
-- need to pull in the override date if return index needs to adjust forward
update m
set m.prc_start_date = o.min_date
from #idc_map m
inner join
( select security_id, min(asof_date) min_date
from quote.ni_security_return_override
where override_status = 'ACTIVE' and adjust_forward = 'Y' group by security_id
) o on (m.security_id = o.security_id and o.min_date between m.start_date and m.end_date)
where o.min_date < m.prc_start_date
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': updated #idc_map (start_date, override), rows=' + convert(varchar, @@rowcount)
-- gather the securities that need to be processed
if (object_id('tempdb..#sec') is not null) begin drop table #sec end
select distinct s.security_id, s.security_state, s.security_type, s.trade_country, m.idc_region, m.idc_code, m.ri_adj_factor, m.cum_split_adj_factor, m.start_date, m.end_date, m.prc_start_date, m.prc_end_date
into #sec
from sec.ni_security s
inner join #idc_map m on (s.security_id = m.security_id)
where s.security_id between coalesce(@security_id_start, @security_id_end) and coalesce(@security_id_end, @security_id_start)
print @PROCEDURE_NAME+'<' + convert(varchar,getdate(),121) +'>: created #sec (@security_id_start, @security_id_end), rows=' +convert(varchar, @@rowcount)
-- if user creates a temp table #prc_securities_to_process, then add in the security_ids from that table to process
if (object_id('tempdb..#prc_securities_to_process') is not null)
begin
insert into #sec (security_id, security_state, security_type, trade_country, idc_region, idc_code, ri_adj_factor, cum_split_adj_factor, start_date, end_date, prc_start_date, prc_end_date)
select distinct s.security_id, s.security_state, s.security_type, s.trade_country, m.idc_region, m.idc_code, m.ri_adj_factor, m.cum_split_adj_factor, m.start_date, m.end_date, m.prc_start_date, m.prc_end_date
from sec.ni_security s
inner join #idc_map m on (s.security_id = m.security_id)
inner join #prc_securities_to_process p on (s.security_id = p.security_id)
where not exists (select 1 from #sec s2 where s2.security_id = s.security_id)
PRINT @PROCEDURE_NAME+'<' + CONVERT(VARCHAR,GETDATE(),121) +'>: inserted into #sec (#prc_securities_to_process), rows=' +CONVERT(VARCHAR, @@rowcount)
END
-- if the user provides no security_id start/end, and no temp table #prc_securities_to_process, then process all securities
ELSE IF @security_id_start IS NULL AND @security_id_end IS NULL
BEGIN
insert into #sec (security_id, security_state, security_type, trade_country, idc_region, idc_code, ri_adj_factor, cum_split_adj_factor, start_date, end_date, prc_start_date, prc_end_date)
select distinct s.security_id, s.security_state, s.security_type, s.trade_country, m.idc_region, m.idc_code, m.ri_adj_factor, m.cum_split_adj_factor, m.start_date, m.end_date, m.prc_start_date, m.prc_end_date
from sec.ni_security s
inner join #idc_map m on (s.security_id = m.security_id)
where m.idc_region in ('DOM', 'CAN') and
not exists (select 1 from #sec s2 where s2.security_id = s.security_id)
PRINT @PROCEDURE_NAME+'<' + CONVERT(VARCHAR,GETDATE(),121) +'>: inserted #sec (all), rows=' +CONVERT(VARCHAR, @@rowcount)
END
create clustered index ix_sec on #sec(idc_region, security_id, idc_code, prc_start_date)
delete from #sec where idc_region not in ('DOM', 'CAN')
print @PROCEDURE_NAME+'<' + convert(varchar,getdate(),121) +'>: deleted from #sec (idc_region not CAN/DOM), rows=' +convert(varchar, @@rowcount)
-- get max price dates
if (object_id('tempdb..#max_price_dates_by_country') is not null) begin drop table #max_price_dates_by_country end
CREATE TABLE #max_price_dates_by_country (country_code VARCHAR(2), max_price_date DATETIME)
INSERT INTO #max_price_dates_by_country (country_code, max_pricedate)
SELECT 'US', MAX(date) FROM qai.prc.PrcDly
INSERT INTO #max_price_dates_by_country (country_code, max_pricedate)
SELECT 'CA', MAX(date) FROM qai..cPrcDly
SELECT @max_price_date = MAX(max_price_date) FROM #max_price_dates_by_country
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': @max_price_date=' + ISNULL(convert(varchar, @max_price_date, 112), 'NULL')
INSERT INTO #max_price_dates_by_country (country_code, max_price_date)
SELECT DISTINCT trade_country, @max_price_date
FROM #sec s
WHERE s.trade_country NOT IN (SELECT country_code FROM #max_price_dates_by_country)
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': inserted into #max_price_dates_by_country(default)=' +convert(varchar, @@rowcount)
UPDATE #max_price_dates_by_country
SET max_price_date = @PREV_BIZ_DAY
WHERE max_price_date < @PREV_BIZ_DAY
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': updated #max_price_dates_by_country max_price_date = @PREV_BIZ_DAY where max_price_date less than @PREV_BIZ_DAY)=' +convert(varchar, @@rowcount)
-- Figure out the price dates to load for suspended stocks
if (object_id('tempdb..#suspended') is not null) begin drop table #suspended end
select h.security_id, s.idc_region, s.idc_code, h.start_date suspend_start_date,
case when h.end_date > d.max_price_date then d.max_price_date else h.end_date end suspend_end_date,
s.start_date, s.end_date, h.start_date derived_start_date, h.end_date derived_end_date
into #suspended
from sec.ni_security_his h
inner join #sec s on (h.security_id = s.security_id and h.start_date <= s.prc_end_date and h.end_date >= s.prc_start_date)
inner JOIN #max_price_dates_by_country d ON (s.trade_country = d.country_code)
where h.security_state = 'SUSPENDED'
print @PROCEDURE_NAME+'<' + convert(varchar,getdate(),121) +'>: created #suspended, rows=' +convert(varchar, @@rowcount)
update y
set y.derived_start_date =
case when x.prev_suspend_end_date is null then x.last_prc_date
when datediff(day, x.prev_suspend_end_date, x.suspend_start_date) > 1 and x.last_prc_date >= x.prev_suspend_end_date then x.last_prc_date
when x.last_prc_date < x.suspend_start_date then x.prev_suspend_end_date + 1
else y.suspend_start_date end
from
(
select s.security_id, s.suspend_start_date, s.suspend_end_date,
lag(suspend_end_date) over (partition by s.security_id order by suspend_end_date) prev_suspend_enddate,
max(p.date) last_prc_date
from #suspended s
inner join #idc_map m on (s.security_id = m.security_id and m.idc_region = 'CAN')
inner join qai..CPrcDly p on (m.idccode = p.code and p.date between m.start_date and m.enddate and p.volume != 0)
where p.Date < s.suspend_start_date
group by s.security_id, s.suspend_start_date, s.suspend_end_date
union
select s.security_id, s.suspend_start_date, s.suspend_end_date,
lag(suspend_end_date) over (partition by s.security_id order by suspend_end_date) prev_suspend_enddate,
max(p.date) last_prc_date
from #suspended s
inner join #idc_map m on (s.security_id = m.security_id and m.idc_region = 'DOM')
inner join qai.prc.PrcDly p on (m.idccode = p.code and p.date between m.start_date and m.enddate and p.volume != 0)
where p.Date < s.suspend_start_date
group by s.security_id, s.suspend_start_date, s.suspend_end_date
) x
inner join #suspended y on (x.security_id = y.security_id and x.suspend_start_date = y.suspend_start_date and x.suspend_end_date = y.suspend_end_date)
print @PROCEDURE_NAME+'<' + convert(varchar,getdate(),121) +'>: updated #suspended (derived_start_date), rows=' +convert(varchar, @@rowcount)
insert into #suspended
select s.security_id, s.idc_region, s.idc_code, h.start_date suspend_start_date, h.end_date suspend_end_date, s.start_date, s.end_date, s.derived_start_date, s.derived_end_date
from #suspended s
inner join sec.ni_security_his h on (s.security_id = h.security_id and s.derived_start_date <= h.end_date and s.derived_end_date >= h.start_date)
where h.security_state = 'SUSPENDED'
and not exists (select * from #suspended s2 where s2.security_id = h.security_id and s2.suspend_start_date = h.start_date)
print @PROCEDURE_NAME+'<' + convert(varchar,getdate(),121) +'>: inserted into #suspended, rows=' +convert(varchar, @@rowcount)
update a
set a.derived_end_date = b.next_derived_start_date - 1
from #suspended a
inner join
(
select *, lead(derived_start_date) over (partition by security_id order by derived_start_date) next_derived_start_date from #suspended
) b on (a.security_id = b.security_id and a.suspend_start_date = b.suspend_start_date and a.suspend_end_date = b.suspend_end_date)
where a.derived_end_date >= b.next_derived_start_date
print @PROCEDURE_NAME+'<' + convert(varchar,getdate(),121) +'>: updated #suspended (derived_end_date), rows=' +convert(varchar, @@rowcount)
update b
set b.prc_start_date = a.derived_start_date
from #suspended a
inner join #sec b on (a.security_id = b.security_id and a.derived_start_date between b.start_date and b.end_date)
where b.prc_start_date > a.derived_start_date
print @PROCEDURE_NAME+'<' + convert(varchar,getdate(),121) +'>: updated #sec (prc_start_date for suspended), rows=' +convert(varchar, @@rowcount)
-- figure out the load dates for each security
if (object_id('tempdb..#load_dates') is not null) begin drop table #load_dates end
create table #load_dates (security_id bigint, idc_region varchar(3), idc_code int, ri_adj_factor float, cum_split_adj_factor float, start_date datetime,
end_date datetime, prc_start_date datetime, prc_end_date datetime, min_date datetime, max_date datetime, tot_obs bigint, batch_num int, price_source varchar(10))
insert into #load_dates (security_id, idc_region, idc_code, ri_adj_factor, cum_split_adj_factor, start_date, end_date, prc_start_date, prc_end_date, min_date, max_date, price_source)
select s.security_id, s.idc_region, s.idc_code, s.ri_adj_factor, s.cum_split_adj_factor, s.start_date, s.end_date, s.prc_start_date, s.prc_enddate, min(p.date) mindate, max(p.date) max_date, 'IDC'
from #sec s
inner join qai..cprcdly p on (s.idccode = p.code and p.date between s.prc_start_date and s.prc_end_date)
where s.idcregion = 'CAN' --and p.date >= @startdate and p.date <= @enddate
and p.Volume > 0 and p.close is not null
group by s.security_id, s.idc_region, s.idc_code, s.ri_adj_factor, s.cum_split_adj_factor, s.start_date, s.end_date, s.prc_start_date, s.prc_end_date
print @PROCEDURE_NAME+'<' + convert(varchar,getdate(),121) +'>: inserted into #load_dates (CAN), rows=' +convert(varchar, @@rowcount)
insert into #load_dates (security_id, idc_region, idc_code, ri_adj_factor, cum_split_adj_factor, start_date, end_date, prc_start_date, prc_end_date, min_date, max_date, price_source)
select s.security_id, s.idc_region, s.idc_code, s.ri_adj_factor, s.cum_split_adj_factor, s.start_date, s.end_date, s.prc_start_date, s.prc_enddate, min(p.date) mindate, max(p.date) max_date, 'IDC'
from #sec s
inner join qai.prc.prcdly p on (s.idccode = p.code and p.date between s.prc_start_date and s.prc_end_date)
where s.idcregion = 'DOM' --and p.date >= @startdate and p.date <= @enddate
and p.Volume > 0 and p.close is not null
group by s.security_id, s.idc_region, s.idc_code, s.ri_adj_factor, s.cum_split_adj_factor, s.start_date, s.end_date, s.prc_start_date, s.prc_end_date
print @PROCEDURE_NAME+'<' + convert(varchar,getdate(),121) +'>: inserted into #load_dates (DOM), rows=' +convert(varchar, @@rowcount)
insert into #load_dates (security_id, idc_region, idc_code, ri_adj_factor, cum_split_adj_factor, start_date, end_date, prc_start_date, prc_end_date, min_date, max_date, price_source)
select s.security_id, s.idc_region, s.idc_code, s.ri_adj_factor, s.cum_split_adj_factor, s.start_date, s.end_date, s.prc_start_date, s.prc_end_date, min(p.MarketDate) min_date, max(p.MarketDate) max_date, 'DS2'
from #sec s
inner join sec.ni_security s2 on (s.security_id = s2.security_id)
inner join qai..DS2PrimQtPrc p on (s2.datastream_infocode = p.infocode and p.marketdate between s.prc_start_date and s.prc_enddate and p.Volume > 0 and p.close is not null)
where s2.trade_country = 'CA' and s2.currency_code = 'USD' --and p.marketdate >= @start_date and p.marketdate <= @end_date
and not exists (select 1 from #load_dates ld where ld.security_id = s.security_id)
group by s.security_id, s.idc_region, s.idc_code, s.ri_adj_factor, s.cum_split_adj_factor, s.start_date, s.end_date, s.prc_start_date, s.prc_end_date
print @PROCEDURE_NAME+'<' + convert(varchar,getdate(),121) +'>: inserted into #load_dates (CA, USD), rows=' +convert(varchar, @@rowcount)
-- figure out the rough date counts so that we can divide them into batches for processing
if (object_id('tempdb..#cnts') is not null) begin drop table #cnts end
select *, row_number() over (order by security_id, min_date) row_num, datediff(day, min_date, max_date) cnt into #cnts from #load_dates
;with calc as
(
select security_id, min_date, row_num, cnt, cnt as sum_cnt from #cnts where row_num = 1
union all
select a.security_id, a.min_date, a.row_num, a.cnt, a.cnt + b.sum_cnt from #cnts a
inner join calc b on a.row_num = (b.row_num + 1)
)
update b
set b.tot_obs = a.sum_cnt,
b.batch_num = (a.sum_cnt / @BATCH_SIZE) + 1
from calc a
inner join #load_dates b on (a.security_id = b.security_id and a.min_date = b.min_date)
option (maxrecursion 0)
print @PROCEDURE_NAME+'<' + convert(varchar,getdate(),121) +'>: updated #load_dates (batch_num), rows=' +convert(varchar, @@rowcount)
-- load prices
if (object_id('tempdb..#prc') is not null) begin drop table #prc end
create table #prc
(
security_id bigint,
obsdate datetime,
idc_region varchar(10),
idccode int,
open float,
high float,
low float,
close_ float,
close_usd float,
volume float,
shares float,
totret float,
cumspltfctr float,
ccfactor float,
adr_ratio float,
currency varchar(10),
bid float,
ask float,
vwap float,
avgtrade float,
blockvol float,
numtrades float,
vendor_totret float,
load_source varchar(20)
)
create index ix_prc on #prc (security_id asc, obsdate asc)
create index ix_load_dates on #load_dates (batch_num, idc_region, security_id)
if (object_id('tempdb..#sec_shares') is not null) begin drop table #sec_shares end
select a.securityid, b.date obsdate, b.Shares, 'DOM' idc_region
into #sec_shares
from #idc_map a
inner join #sec s on (a.security_id = s.securityid)
inner join qai.prc.prcshr b on (b.date between a.start_date and a.enddate and b.date between s.start_date and s.end_date and a.idc_code = b.Code and a.idc_region = 'DOM')
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': created #sec_shares (DOM), rows='+convert(varchar,@@rowcount)
insert into #sec_shares (security_id, obsdate, shares, idc_region)
select a.securityid, b.date obsdate, b.Shares, 'CAN'
from #idc_map a
inner join #sec s on (a.security_id = s.securityid)
inner join qai.dbo.CPRCSHR b on (b.date between a.start_date and a.enddate and b.date between s.start_date and s.end_date and a.idc_code = b.Code and a.idc_region = 'CAN')
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': inserted #sec_shares (CAN), rows='+convert(varchar,@@rowcount)
create index ix_sec_shares on #sec_shares(security_id, obsdate)
if (object_id('tempdb..#adr_ratio') is not null) begin drop table #adrratio end
select a.code, a.date startdate,
isnull((select min(date) -1 from qai.prc.prcadr c2 where c2.code = a.code and c2.date > a.date), '2049-06-06') end_date,
a.ratio
into #adr_ratio
from qai.prc.prcadr a
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': created #adr_ratio, rows='+convert(varchar,@@rowcount)
declare @max_batch_num int
select @max_batch_num = max(batch_num) from #load_dates
print @PROCEDURE_NAME+'<' + convert(varchar,getdate(),121) +'>: BATCH load begins. Total batches=' + isnull(convert(varchar, @max_batch_num), 'null')
declare @batch_num int
declare batch_cursor cursor for
select distinct batch_num from #load_dates order by batch_num
open batch_cursor
WHILE (0=0)
BEGIN
fetch next from batch_cursor into @batch_num
if(@@fetch_status <> 0) break
declare @min_security_id bigint, @max_security_id bigint
select @min_security_id=min(security_id), @max_security_id=max(security_id) from #load_dates where batch_num = @batch_num
print @PROCEDURE_NAME+'<' + convert(varchar,getdate(),121) +'>: PROCESSING --> @batch_num=' + convert(varchar, @batch_num) + ' (of ' + convert(varchar, @max_batch_num) + ')'
truncate table #prc
print @PROCEDURE_NAME+'<' + convert(varchar,getdate(),121) +'>: truncated #prc'
-- exception case: get securities traded on CAD exchanges in USD, with datastream pricing.
-- IDC does not consitently provide pricing for these, so we use Datastream as a secondary source
insert into #prc(security_id, obsdate, idc_region, idccode, open, high, low, close_, close_usd, volume,
shares, totret, cumspltfctr, ccfactor, adr_ratio, currency, bid, ask, vwap, avgtrade, blockvol, numtrades, vendor_totret, load_source)
select s.security_id, p.MarketDate, ld.idc_region, ld.idccode, p.Open, p.High, p.Low, p.Close, p.close, p.Volume,
isnull(sh.Shares, sh2.shares), r.ri, ad.CumAdjFactor, 1, null, p.ISOCurrCode, p.Bid, p.Ask, p.VWAP, null, null, null, r.ri, 'DS2'
from #load_dates ld
inner join sec.ni_security s on (ld.security_id = s.security_id)
inner join qai..DS2PrimQtPrc p on (s.datastream_infocode = p.InfoCode and p.MarketDate between ld.min_date and ld.max_date)
inner join qai..ds2primqtri r on (s.datastream_infocode = r.InfoCode and r.MarketDate = p.MarketDate)
left join qai..ds2adj ad on (ad.AdjDate = 2 and s.datastream_infocode = ad.InfoCode and p.MarketDate between ad.AdjDate and isnull(ad.EndAdjDate, '9999-12-31'))
left join qai..cprcshr sh on (sh.code = ld.idccode and sh.date = (select max(date) from qai..cprcshr sh2 where sh2.code = sh.code and sh2.date <= p.MarketDate))
left join #sec_shares sh2 on (ld.security_id = sh2.security_id and sh2.obsdate =
(select max(obsdate) from #sec_shares sh3 where sh3.security_id = sh2.security_id and sh3.obsdate <= p.MarketDate))
where ld.batch_num = @batch_num and ld.price_source = 'DS2' and s.trade_country = 'CA' and s.currency_code = 'USD'
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': inserted into #prc (ds2 prices for CA securities trading in USD, rows=' + convert(varchar, @@rowcount)
-- Canadian security prices
insert into #prc(security_id, obsdate, idc_region, idccode, open, high, low, close_, close_usd, volume,
shares, totret, cumspltfctr, ccfactor, adr_ratio, currency, bid, ask, vwap, avgtrade, blockvol, numtrades, vendor_totret, load_source)
select ld.securityid, p.date, ld.idc_region, ld.idccode, p.Open, p.high, p.low, p.close, p.close p.CCFactor, isnull(cv.composite_volume, p.volume),
isnull(s.Shares, sh.shares), p.totret ld.ri_adj_factor, a.Factor * ld.cum_split_adj_factor, 1.0 / p.CCFactor, null,
case i.SecType when 'A' then 'USD' else 'CAD' end,
v.bid, v.ask, v.Vwap, v.AvgTrade, v.BlockVol, v.NumTrades, p.TotRet, 'IDC'
from #load_dates ld
inner join qai..cprcdly p on (ld.idccode = p.code and p.date between ld.min_date and ld.maxdate and p.close is not null and p.volume > 0)
left join qai..cprcshr s on (ld.idccode = s.code and s.date = (select max(date) from qai..cprcshr s2 where s2.code = s.code and s2.date <= p.date))
left join qai..cprcadj a on (a.code = p.code and p.date between a.StartDate and a.EndDate and a.AdjType = 1)
left join qai..CPrcInfo i on (i.code = p.code)
--left join qai.prc.PrcCur c on (c.Country = i.Country)
left join qai..cprcvol v on (v.code = p.code and v.date = p.date)
left join quote.ni_composite_volume cv on (ld.security_id = cv.securityid and p.date = cv.obsdate and isnull(cv.composite_volume, 0) > 0)
left join #sec_shares sh on (ld.security_id= sh.security_id and sh.obsdate =
(select max(obsdate) from #sec_shares sh2 where sh2.security_id = sh.securityid and sh2.obsdate <= p.date))
where ld.batch_num = @batch_num and ld.idc_region = 'CAN' and ld.price_source = 'IDC'
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': inserted into #prc (CA securities) , rows=' + convert(varchar, @@rowcount)
-- US security prices
insert into #prc(security_id, obsdate, idc_region, idccode, open, high, low, close_, close_usd, volume,
shares, totret, cumspltfctr, ccfactor, adr_ratio, currency, bid, ask, vwap, avgtrade, blockvol, numtrades, vendor_totret, load_source)
select ld.securityid, p.date, ld.idc_region, ld.idccode, p.Open, p.high, p.low, p.close, p.close p.CCFactor, isnull(cv.composite_volume, p.volume),
isnull(s.Shares, sh.shares), p.totret ld.ri_adj_factor, a.Factor * ld.cum_split_adj_factor, 1.0 / p.CCFactor,
case when sec.security_type in ('ADR','GDR')
then adr.ratio
else NULL
end adr_ratio,
'USD',
v.bid, v.ask, v.Vwap, v.AvgTrade, v.BlockVol, v.NumTrades, p.TotRet, 'IDC'
from #load_dates ld
inner join sec.ni_security sec on (ld.security_id = sec.security_id)
inner join qai.prc.prcdly p on (ld.idccode = p.code and p.date between ld.min_date and ld.maxdate and p.close is not null and p.volume > 0)
left join qai.prc.prcshr s on (ld.idccode = s.code and s.date = (select max(date) from qai.prc.prcshr s2 where s2.code = s.code and s2.date <= p.date))
left join qai.prc.prcadj a on (a.code = p.code and p.date between a.StartDate and a.EndDate and a.AdjType = 1)
--left join qai.prc.PrcInfo i on (i.code = p.code)
left join qai.prc.prcvol v on (v.code = p.code and v.date = p.date)
left join quote.ni_composite_volume cv on (ld.security_id = cv.securityid and p.date = cv.obsdate and isnull(cv.composite_volume, 0) > 0)
left join #adr_ratio adr on (ld.idccode = adr.code and p.date between adr.start_date and adr.end_date)
left join #sec_shares sh on (ld.security_id = sh.security_id and sh.obsdate =
(select max(obsdate) from #sec_shares sh2 where sh2.security_id = sh.securityid and sh2.obsdate <= p.date))
where ld.batch_num = @batch_num and ld.idc_region = 'DOM' and ld.price_source = 'IDC'
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': inserted into #prc (US securities) , rows=' + convert(varchar, @@rowcount)
-- Override prices, cumulative adj factor, and return index
-- override price
update a
set a.close_ = isnull(po.overrideprice, a.close),
a.close_usd = isnull(po.override_price / fx.rate, a.close_usd),
a.volume = isnull(po.override_volume, a.volume),
a.currency = isnull(po.currency_code, a.currency),
a.CCFactor = isnull(fx.rate, a.ccfactor),
a.load_source = 'OVERRIDE'
from #prc a
inner join quote.ni_security_price_override po on (po.override_status = 'ACTIVE' and a.security_id = po.security_id and po.obsdate = a.obsdate)
inner join qai.ni_fx_rate_idc_view fx on (po.currency_code = fx.currencycode and po.obsdate = fx.date)
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': updated #security_pricing_dom_pending (price override), rows='+convert(varchar,@@rowcount)
-- add override price if doesn't exists
-- CA
insert into #prc (security_id, obsdate, idc_region, idccode, close, close_usd, Volume, Shares, cumSpltFctr, CCFactor, currency, totret, vendor_totret, load_source)
select a.security_id, po.obsdate, a.idc_region, a.idc_code, po.override_price, po.override_price / fx.rate as close_usd,
po.override_volume, s.Shares, aj.Factor * a.cum_split_adj_factor, fx.rate, po.currency_code, ro.override_totret, ro.override_totret, 'OVERRIDE'
from #load_dates a
inner join quote.ni_security_price_override po on (po.override_status = 'ACTIVE' and a.security_id = po.security_id and po.obsdate between a.start_date and a.end_date)
inner join qai.ni_fx_rate_idc_view fx on (po.currency_code = fx.currencycode and po.obsdate = fx.date)
left join quote.ni_security_return_override ro on (ro.override_status = 'ACTIVE' and a.security_id = ro.security_id and ro.asof_date = po.obsdate and ro.override_type in ('ADD_ONLY', 'OVERRIDE'))
left join qai.dbo.cprcadj aj on (aj.code = a.idc_code and po.obsdate between aj.StartDate and aj.EndDate and aj.adjtype = 1)
left join qai.dbo.CPRCSHR s on (s.Code = a.idccode and s.date = ( select max(date) from qai.dbo.cprcshr where Code = s.Code and Date <= po.obsdate))
left join #prc p on (a.security_id = p.security_id and po.obsdate = p.obsdate)
where a.idc_region = 'CAN' and p.security_id is null
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': inserted #prc (CA, price override), rows='+convert(varchar,@@rowcount)
insert into #prc (security_id, obsdate, idc_region, idccode, close, close_usd, Volume, shares, cumSpltFctr, CCFactor, currency, totret, vendor_totret, load_source)
select a.security_id, po.obsdate, a.idc_region, a.idc_code, po.override_price, po.override_price / fx.rate as close_usd,
po.override_volume, s.Shares, aj.Factor * a.cum_split_adj_factor, fx.rate, po.currency_code, ro.override_totret, ro.override_totret, 'OVERRIDE'
from #load_dates a
inner join quote.ni_security_price_override po on (po.override_status = 'ACTIVE' and a.security_id = po.security_id and po.obsdate between a.start_date and a.end_date)
inner join qai.ni_fx_rate_idc_view fx on (po.currency_code = fx.currencycode and po.obsdate = fx.date)
left join quote.ni_security_return_override ro on (ro.override_status = 'ACTIVE' and a.security_id = ro.security_id and ro.asof_date = po.obsdate and ro.override_type in ('ADD_ONLY', 'OVERRIDE'))
left join qai.prc.prcadj aj on (aj.code = a.idc_code and po.obsdate between aj.StartDate and aj.EndDate and aj.adjtype = 1)
left join qai.prc.PRCSHR s on (s.Code = a.idccode and s.date = ( select max(date) from qai.prc.prcshr where Code = s.Code and Date <= po.obsdate))
left join #prc p on (a.security_id = p.security_id and po.obsdate = p.obsdate)
where a.idc_region = 'DOM' and p.security_id is null
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': inserted #prc (US, price override), rows='+convert(varchar,@@rowcount)
-- override cumulative adj factor
update a
set a.cumSpltFctr = isnull(ao.factor, a.cumSpltFctr)
from #prc a
inner join qai.ni_prcadj_override ao on (ao.idc_region = a.idc_region and ao.idc_code= a.idc_code and ao.adjtype = 1 and a.obsdate between ao.start_date and ao.end_date)
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': updated #prc (cumspltfctr override), rows='+convert(varchar,@@rowcount)
-- override the return index
if (object_id('tempdb..#overrides') is not null) begin drop table #overrides end
select security_id, asof_date, override_type, vendor, currency_code, orig_vendor_totret, override_totret,
adjust_forward, override_status, row_number() over (partition by security_id order by asof_date) row_num
into #overrides
from quote.ni_security_return_override where override_type in ('REPLACE_ONLY', 'OVERRIDE') and vendor = 'IDC' and override_status = 'ACTIVE'
declare @pass int
declare ovd_cursor cursor for select distinct row_num from #overrides order by row_num
open ovd_cursor
while (0=0)
begin
fetch next from ovd_cursor into @pass
if(@@fetch_status <> 0) break
update a
set a.TotRet = a.totret * b.override_totret / a.vendor_totret
from #prc a
inner join #overrides b on (a.security_id = b.security_id and a.obsdate = b.asof_date)
where b.row_num = @pass and b.adjust_forward = 'N'
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': updated #prc (totret override), rows='+convert(varchar,@@rowcount)
update c
set c.totret = c.TotRet * b.override_totret / a.vendor_totret
from #prc a
inner join #overrides b on (a.security_id = b.security_id and a.obsdate = b.asof_date)
inner join #prc c on (c.security_id = b.security_id and c.obsdate >= b.asof_date)
where b.row_num = @pass and b.adjust_forward = 'Y'
PRINT @PROCEDURE_NAME + '<'+ CONVERT(VARCHAR, GETDATE(), 121) + '>' +': updated #prc (totret override - adjust forward), rows='+CONVERT(VARCHAR,@@rowcount)
END
close ovd_cursor
deallocate ovd_cursor
-- Carry forward prices for SUSPENDED securities
insert into #prc(security_id, obsdate, idc_region, idccode, open, high, low, close_, close_usd, volume,
shares, totret, cumspltfctr, ccfactor, adr_ratio, currency, bid, ask, vwap, avgtrade, blockvol, numtrades, vendor_totret, load_source)
select d.security_id, c.obsdate, d.idc_region, d.idccode, f.open, f.high, f.low, f.close_, f.close_usd, f.volume,
f.shares, f.totret, f.cumspltfctr, f.ccfactor, f.adr_ratio, f.currency, f.bid, f.ask, f.vwap, f.avgtrade, f.blockvol, f.numtrades, f.vendor_totret, 'CARRYOVER'
from #suspended d
inner join sec.ni_security s on (d.security_id = s.security_id)
left join ref.ni_exchange e on (s.exchange_id = e.exchange_id)
left join ref.ni_calendar_date c on c.calendar_code = isnull(e.trading_calendar_code, 'INTL')
and c.obsdate between d.suspend_start_date and d.suspend_end_date and c.seq != 0
and c.obsdate between d.start_date and d.end_date
left join #prc p on (d.security_id = p.security_id and c.obsdate = p.obsdate)
outer apply (
select p2.*, d2.last_prc_date
from #prc p2
inner join
(select pd.security_id, max(obsdate) last_prc_date
from #prc pd where pd.security_id = d.security_id and pd.obsdate <= c.obsdate
group by pd.security_id
) d2 on (p2.security_id = d2.security_id and p2.obsdate = d2.last_prc_date)
) f
where p.obsdate is null and f.last_prc_date is not null
print @PROCEDURE_NAME + '<'+ convert(varchar, getdate(), 121) + '>' +': inserted into #prc(CARRYOVER), rows='+convert(varchar,@@rowcount)
-- merge the prices into permanent table
BEGIN TRY
begin transaction
-- delete entries that are no longer valid
delete p
from quote.ni_price_dom_legacy p
inner join #load_dates d on (d.batch_num = @batch_num and p.uid = d.security_id and p.obsdate between d.prc_start_date and d.prc_end_date)
left outer join #prc s on (p.uid = s.security_id and p.obsdate = s.obsdate)
where s.security_id is null
print @PROCEDURE_NAME+'<' + convert(varchar,getdate(),121) +'>: deleted from quote.ni_price_dom_legacy, rows=' + convert(varchar, @@rowcount)
-- merge the rest
MERGE quote.ni_price_dom_legacy p
USING #prc t
ON t.securityid = p.uid AND t.obsdate = p.obsdate
WHEN MATCHED AND
(ISNULL(t.open, -1) != ISNULL(p.open, -1) OR
ISNULL(t.high, -1) != ISNULL(p.high, -1) OR
ISNULL(t.low, -1) != ISNULL(p.low, -1) OR
ISNULL(t.close, -1) != ISNULL(p.close_, -1) OR
ISNULL(t.close_usd, -1) != ISNULL(p.close_usd, -1) OR
ISNULL(t.volume, -1) != ISNULL(p.volume, -1) OR
ISNULL(t.shares, -1) != ISNULL(p.shares, -1) OR
ISNULL(t.totret, -1) != ISNULL(p.totret, -1) OR
ISNULL(t.cumspltfctr, -1) != ISNULL(p.cumspltfctr, -1) OR
ISNULL(t.ccfactor, -1) != ISNULL(p.ccfactor, -1) OR
ISNULL(t.adr_ratio, -1) != ISNULL(p.adr_ratio, -1) OR
ISNULL(t.currency, '') != ISNULL(p.price_curr, '') OR
ISNULL(t.bid, -1) != ISNULL(p.bid, -1) OR
ISNULL(t.ask, -1) != ISNULL(p.ask, -1) OR
ISNULL(t.vwap, -1) != ISNULL(p.vwap, -1) OR
ISNULL(t.avgtrade, -1) != ISNULL(p.avgtrade, -1) OR
ISNULL(t.blockvol, -1) != ISNULL(p.blockvol, -1) OR
ISNULL(t.numtrades, -1) != ISNULL(p.numtrades, -1) OR
ISNULL(t.load_source, '') != ISNULL(p.load_source, ''))
THEN
UPDATE
SET p.open = t.open,
p.high = t.high,
p.low = t.low,
p.close = t.close,
p.close_usd = t.close_usd,
p.volume = t.volume,
p.shares = t.shares,
p.totret = t.totret,
p.cumspltfctr = t.cumspltfctr,
p.ccfactor = t.ccfactor,
p.adr_ratio = t.adr_ratio,
p.price_curr = t.currency,
p.bid = t.bid,
p.ask = t.ask,
p.vwap = t.vwap,
p.avgtrade = t.avgtrade,
p.blockvol = t.blockvol,
p.numtrades = t.numtrades,
p.load_source= t.load_source,
p.loaddt = @NOW
WHEN NOT MATCHED BY TARGET
THEN
INSERT ( uid, obsdate, open, high, low, close_, close_usd, volume, shares, totret, cumspltfctr,
ccfactor, adr_ratio, price_curr, bid, ask, vwap, avgtrade, blockvol, numtrades, load_dt, load_source)
VALUES( t.securityid, t.obsdate, t.open, t.high, t.low, t.close_, t.close_usd, t.volume, t.shares, t.totret, t.cumspltfctr,
t.ccfactor, t.adr_ratio, t.currency, t.bid, t.ask, t.vwap, t.avgtrade, t.blockvol, t.numtrades, @NOW, t.load_source);
PRINT @PROCEDURE_NAME+'<' + CONVERT(VARCHAR,GETDATE(),121) +'>: merged #prc into quote.ni_price_dom_legacy, rows=' + CONVERT(VARCHAR, @@rowcount)
COMMIT
END TRY
BEGIN CATCH
SET @error_msg = ERROR_MESSAGE()
PRINT @error_msg
ROLLBACK
SET NOCOUNT OFF
RAISERROR(@error_msg,16,1)
RETURN -50
END CATCH
END
CLOSE batch_cursor
DEALLOCATE batch_cursor
PRINT @PROCEDURE_NAME+'<' + CONVERT(VARCHAR,GETDATE(),121) +'>: end batch_cursor.'
IF (OBJECT_ID('tempdb..#idc_map') IS NOT NULL) BEGIN DROP TABLE #idc_map END
IF (OBJECT_ID('tempdb..#sec') IS NOT NULL) BEGIN DROP TABLE #sec END
IF (OBJECT_ID('tempdb..#load_dates') IS NOT NULL) BEGIN DROP TABLE #load_dates END
IF (OBJECT_ID('tempdb..#cnts') IS NOT NULL) BEGIN DROP TABLE #cnts END
IF (OBJECT_ID('tempdb..#prc') IS NOT NULL) BEGIN DROP TABLE #prc END
IF (OBJECT_ID('tempdb..#overrides') IS NOT NULL) BEGIN DROP TABLE #overrides END
IF (OBJECT_ID('tempdb..#sec_shares') IS NOT NULL) BEGIN DROP TABLE #sec_shares END
IF (OBJECT_ID('tempdb..#suspended') IS NOT NULL) BEGIN DROP TABLE #suspended END
PRINT @PROCEDURE_NAME + '<'+ CONVERT(VARCHAR, GETDATE(), 121) + '>' +': end'
END
/Begin logging update/
EXEC log.update_logging_for_end_date @callId = @callId
/Endof logging update/
ALTER PROCEDURE [dbo].[DiskAlertOpsgenie] AS BEGIN DECLARE @RowCount INT
-- Execute your select query here and store the count in @RowCount SELECT @RowCount = COUNT(*) FROM tbl_diskreport WHERE isLatest = 1 and FreeSpacePercent < = 15
-- Check if the count is greater than or equal to 1 IF @RowCount >= 1 BEGIN DECLARE @Subject NVARCHAR(255) = 'Drive space alert' DECLARE @Body NVARCHAR(MAX) = 'Raise a Req immediately' --'Drive Space Alert ' + CAST(@RowCount AS NVARCHAR(10))
END END