I have several tables in SQL Server 2019 with this kind of stock price data
Stock A
CREATE TABLE dbo.StockA_DailyPrice(
Date DATE NOT NULL PRIMARY KEY
,Openprice DECIMAL(16,2) NOT NULL
,Highprice DECIMAL(16,2) NOT NULL
,Lowprice DECIMAL(16,2) NOT NULL
,Closeprice DECIMAL(16,2) NOT NULL
,DailyChange DECIMAL(16,4) NOT NULL
);
INSERT INTO StockA_DailyPrice(Date,Openprice,Highprice,Lowprice,Closeprice,DailyChange) VALUES ('2021-08-25','155.5','159.35','155.5','158.7','0.0205');
INSERT INTO StockA_DailyPrice(Date,Openprice,Highprice,Lowprice,Closeprice,DailyChange) VALUES ('2021-08-24','154.5','156.95','154.3','155.5','0.0064');
INSERT INTO StockA_DailyPrice(Date,Openprice,Highprice,Lowprice,Closeprice,DailyChange) VALUES ('2021-08-23','150','154.55','150','154.45','0.0296');
INSERT INTO StockA_DailyPrice(Date,Openprice,Highprice,Lowprice,Closeprice,DailyChange) VALUES ('2021-08-20','147.4','149.4','145.6','149.15','0.01187');
INSERT INTO StockA_DailyPrice(Date,Openprice,Highprice,Lowprice,Closeprice,DailyChange) VALUES ('2021-08-19','147.8','149.05','144','147.4','-0.0027');
Stock B
CREATE TABLE StockB_DailyPrice(
Date Date NOT NULL PRIMARY KEY
,Openprice DECIMAL(16,2) NOT NULL
,Highprice DECIMAL(16,2) NOT NULL
,Lowprice DECIMAL(16,2) NOT NULL
,Closeprice DECIMAL(16,2) NOT NULL
,DailyChange DECIMAL(16,4) NOT NULL
);
INSERT INTO StockB_DailyPrice(Date,Openprice,Highprice,Lowprice,Closeprice,DailyChange) VALUES ('2021-08-24','79.1','80.6','79','80.1','0.0126');
INSERT INTO StockB_DailyPrice(Date,Openprice,Highprice,Lowprice,Closeprice,DailyChange) VALUES ('2021-08-23','79','80.1','77.9','79.1','0.0012');
INSERT INTO StockB_DailyPrice(Date,Openprice,Highprice,Lowprice,Closeprice,DailyChange) VALUES ('2021-08-20','77.8','79.7','77.7','78.9','0.0141');
INSERT INTO StockB_DailyPrice(Date,Openprice,Highprice,Lowprice,Closeprice,DailyChange) VALUES ('2021-08-19','79.1','79.3','77.3','78.5','-0.0075');
INSERT INTO StockB_DailyPrice(Date,Openprice,Highprice,Lowprice,Closeprice,DailyChange) VALUES ('2021-08-18','77.4','79.7','77.3','79.6','0.0284');
INSERT INTO StockB_DailyPrice(Date,Openprice,Highprice,Lowprice,Closeprice,DailyChange) VALUES ('2021-08-17','77.4','78.6','77.2','77.3','-0.0012');
I want to create an index (not SQL-index but a stock index, a calculation of multiple values) from 2 or more of these tables with historical prices for a stock. Perhaps with the data inserted into another table?
The starting dates of the historic stock prices vary so I'd like to choose which start date to start calculating the merger from. And the start date should have a starting index number, like 100.
The result of the merge between A and B (but It would be nice if the merger code is scalable to merge multiple stocks) should be.
StockA_StockB_Merge
| Date | Index |
|---|---|
| 2021-08-23 | (IndexTheDayBefore)*(1+((StockA.DailyChange+StockB.DailyChange)/NumberOfMergedStocks)) |
| 2021-08-20 | (IndexTheDayBefore)*(1+((StockA.DailyChange+StockB.DailyChange)/NumberOfMergedStocks)) |
| 2021-08-19 | 100*(1+((StockA.DailyChange+StockB.DailyChange)/NumberOfMergedStocks)) |
Which would output these numbers
| Date | Index |
|---|---|
| 2021-08-23 | 100.78*(1+((0.0296+0.0012)/2)) =102.34 |
| 2021-08-20 | 99.49*(1+((0.0141+0.0119)/2)) =100.78 |
| 2021-08-19 | 99.49 |
The calculation for 2021-08-19 is 100*(1+((-0.0075+-0.0027)/2)) which I couldn't for the life of me get into the table. The editor said it was wrongly formated code.
The basic idea is that if one stock goes up 3% and the other 1%, the average for that pair is 2% (for example, stocks in the same industry).
The exact calculation of the daily change can of course vary. The tricky part is choosing the start date and start index value and the calculation to go from there.
Sorry if this is a tricky question :) I'm new to T-SQL.