1
- -- Cleanse
2
1
DROP TABLE IF EXISTS Users;
3
2
DROP TABLE IF EXISTS Stocks;
4
3
DROP TABLE IF EXISTS Orders;
5
4
DROP TABLE IF EXISTS Prices;
6
5
7
- -- Users
8
6
CREATE TABLE Users (
9
7
Id INT PRIMARY KEY IDENTITY (1 , 1 ),
10
8
FName VARCHAR (255 ) NOT NULL ,
11
- LName VARCHAR (255 ) NOT NULL
9
+ LName VARCHAR (255 ) NOT NULL ,
12
10
);
13
11
14
12
INSERT INTO Users (FName, LName)
15
13
VALUES
16
14
(' Loi' , ' Tran' ),
17
- (' Thao' , ' Tran' );
15
+ (' Tai' , ' Tran' ),
16
+ (' Thao' , ' Tran' ),
17
+ (' Hieu' , ' Tran' ),
18
+ (' Doug' , ' Tran' );
18
19
19
- -- Orders
20
20
CREATE TABLE Orders (
21
21
Expires DATETIME ,
22
- Shares INT NOT NULL ,
23
22
UserId INT NOT NULL ,
24
- StopPrice DECIMAL ( 10 , 2 ) ,
23
+ Shares INT NOT NULL ,
25
24
OrderableId INT NOT NULL ,
25
+ StopPrice DECIMAL (10 , 2 ),
26
26
Type VARCHAR (20 ) NOT NULL ,
27
27
LimitPrice DECIMAL (10 , 2 ),
28
28
Status VARCHAR (20 ) NOT NULL ,
29
29
Id INT PRIMARY KEY IDENTITY (1 , 1 ),
30
- OrderType VARCHAR (10 ) NOT NULL CHECK (OrderType IN (' Buy' , ' Sell' ))
31
- OrderableType VARCHAR (10 ) NOT NULL CHECK (OrderableType IN (' Stock' , ' Option' , ' Bond' )),
30
+ OrderType VARCHAR (10 ) NOT NULL CHECK (OrderType IN (' Buy' , ' Sell' )),
31
+ OrderableType VARCHAR (10 ) NOT NULL CHECK (OrderableType IN (' Stock' , ' Option' , ' Bond' ))
32
32
);
33
33
34
- -- Stocks
35
34
CREATE TABLE Stocks (
36
35
Id INT PRIMARY KEY IDENTITY (1 , 1 ),
37
36
Name NVARCHAR (30 ),
@@ -40,39 +39,74 @@ CREATE TABLE Stocks (
40
39
Symbol NVARCHAR (20 )
41
40
);
42
41
43
- -- Seed Stocks Data
44
42
INSERT INTO Stocks (Name , Price, Quantity, Symbol)
45
43
VALUES
46
- (' ABC Company ' , 150 . 00 , 100 , ' ABC ' ),
47
- (' XYZ Corporation ' , 75 . 50 , 50 , ' XYZ ' ),
48
- (' DEF Inc. ' , 120 . 25 , 75 , ' DEF ' );
44
+ (' AT&T ' , 15 . 81 , 75 , ' T ' ),
45
+ (' Coinbase ' , 64 . 21 , 100 , ' COIN ' ),
46
+ (' Bank of America ' , 28 . 5 , 100 , ' BAC ' );
49
47
50
- -- Seed Orders Data
51
48
INSERT INTO Orders (UserId, OrderableId, OrderableType, Status , Type , StopPrice, LimitPrice, Shares, Expires, OrderType)
52
49
VALUES
53
50
(1 , 1 , ' Stock' , ' Filled' , ' Buy' , NULL , 100 .00 , 10 , ' 2023-05-31 12:00:00' , ' Buy' ),
54
51
(2 , 2 , ' Option' , ' Non-filled' , ' Limit' , NULL , 50 .00 , 5 , ' 2023-06-01 15:30:00' , ' Buy' ),
55
52
(1 , 3 , ' Bond' , ' Cancelled' , ' Stop Loss' , 70 .00 , NULL , 8 , ' 2023-06-02 10:45:00' , ' Buy' );
56
53
57
- -- Prices
54
+
58
55
CREATE TABLE Prices (
56
+ Id INT PRIMARY KEY IDENTITY (1 , 1 ),
59
57
StockId INT NOT NULL ,
60
- DailyPrice DECIMAL (18 , 2 ),
61
- HourlyPrice DECIMAL (18 , 2 ),
62
- WeeklyPrice DECIMAL (18 , 2 ),
63
- YearlyPrice DECIMAL (18 , 2 ),
64
- MonthlyPrice DECIMAL (18 , 2 ),
65
- FiveYearsPrice DECIMAL (18 , 2 ),
66
58
TransactionCount INT NOT NULL ,
67
- FiveMinutePrice DECIMAL (18 , 2 ),
68
- Volume DECIMAL (18 , 2 ) NOT NULL ,
69
- ThreeMonthsPrice DECIMAL (18 , 2 ),
70
- Id INT PRIMARY KEY IDENTITY(1 , 1 ),
71
- LowPrice DECIMAL (18 , 2 ) NOT NULL ,
72
- OpenPrice DECIMAL (18 , 2 ) NOT NULL ,
73
- ClosePrice DECIMAL (18 , 2 ) NOT NULL ,
74
- HighPrice DECIMAL (18 , 2 ) NOT NULL ,
75
59
DateOfAggregation DATETIME NOT NULL ,
76
- VolumeWeightedAverage DECIMAL (18 , 2 ) NOT NULL
60
+ o DECIMAL (18 , 2 ) NOT NULL , -- open
61
+ l DECIMAL (18 , 2 ) NOT NULL , -- lo
62
+ h DECIMAL (18 , 2 ) NOT NULL , -- hi
63
+ c DECIMAL (18 , 2 ) NOT NULL , -- close
64
+ v DECIMAL (18 , 2 ) NOT NULL , -- volume
65
+ vwa DECIMAL (18 , 2 ) NOT NULL -- volume weighted average
77
66
);
78
67
68
+ DECLARE @count INT = 0 ;
69
+ DECLARE @weekAgo DATETIME = GETDATE () - 7 ;
70
+
71
+ DECLARE @price DECIMAL (10 , 2 ) = 27 .00 ;
72
+ DECLARE @open DECIMAL (10 , 2 ) = @price;
73
+ DECLARE @lo DECIMAL (10 , 2 ) = 0 .0 ;
74
+ DECLARE @hi DECIMAL (10 , 2 ) = 0 .0 ;
75
+ DECLARE @close DECIMAL (10 , 2 ) = 0 .0 ;
76
+
77
+ WHILE @count < 30
78
+ BEGIN
79
+ SET @price = @price - 0 .10 + (RAND () * (.10 - (- 0 .10 )));
80
+ SET @close = @price;
81
+
82
+ SET @lo = @price - 0 .05 + (RAND () * (- 0 .1 ));
83
+ SET @hi = @price - 0 .05 + (RAND () * (.1 ));
84
+
85
+ INSERT INTO Prices (
86
+ StockId,
87
+ TransactionCount,
88
+ DateOfAggregation,
89
+ o,
90
+ l,
91
+ v,
92
+ h,
93
+ c,
94
+ vwa
95
+ )
96
+ VALUES (
97
+ 1 ,
98
+ 100 ,
99
+ @weekAgo,
100
+ @open,
101
+ CASE WHEN @open < @lo THEN @open ELSE @lo END ,
102
+ 1000 ,
103
+ CASE WHEN @close > @hi THEN @close ELSE @hi END ,
104
+ @close,
105
+ 140
106
+ );
107
+
108
+ SET @open = @close;
109
+
110
+ SET @count = @count + 1 ;
111
+ SET @weekAgo = DATEADD (HOUR, 1 , @weekAgo);
112
+ END ;
0 commit comments