-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathschema.sql
321 lines (297 loc) · 7.7 KB
/
schema.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
CREATE SCHEMA ibkr;
CREATE TYPE ibkr.action AS ENUM
('BUY',
'SELL',
'SSHORT');
CREATE TYPE ibkr.condition_comparator AS ENUM
('LESS THAN',
'GREATER THAN');
CREATE TYPE ibkr.condition_operator AS ENUM
('AND',
'OR');
CREATE TYPE ibkr.condition_trigger_method AS ENUM
('DEFAULT',
'DOUBLE BID/ASK',
'LAST',
'DOUBLE LAST',
'BID/ASK',
'LAST OF BID/ASK',
'MID POINT');
CREATE TYPE ibkr.condition_type AS ENUM
('PRICE',
'TIME',
'MARGIN',
'EXECUTION',
'VOLUME',
'PERCENT CHANGE');
CREATE TYPE ibkr.open_close AS ENUM
('OPEN',
'CLOSE',
'SAME');
CREATE TYPE ibkr.order_strategy AS ENUM
('LONG CALL',
'LONG PUT',
'BULL CALL VERTICAL SPREAD',
'BEAR CALL VERTICAL SPREAD',
'BULL PUT VERTICAL SPREAD',
'BEAR PUT VERTICAL SPREAD',
'LONG STRADDLE',
'LONG STRANGLE',
'CALL RATIO SPREAD',
'PUT RATIO SPREAD',
'CALL HORIZONTAL SPREAD',
'PUT HORIZONTAL SPREAD',
'CALL DIAGONAL SPREAD',
'PUT DIAGONAL SPREAD',
'CALL BUTTERFLY',
'PUT BUTTERFLY',
'CALL CONDOR',
'PUT CONDOR');
CREATE TYPE ibkr.pattern AS ENUM
('BULL PULLBACK',
'BEAR RALLY',
'HIGH BASE',
'LOW BASE',
'ASCENDING TRIANGLE',
'DESCENDING TRIANGLE',
'RANGE RALLY',
'RANGE PULLBACK',
'INCREASING RANK',
'DECREASING RANK',
'INCREASING VOL',
'DECREASING VOL');
CREATE TYPE ibkr."right" AS ENUM
('CALL',
'PUT');
CREATE TYPE ibkr.security_type AS ENUM
('STK',
'OPT',
'FUT',
'CASH',
'BOND',
'CFD',
'FOP',
'WAR',
'IOPT',
'FWD',
'BAG',
'IND',
'BILL',
'FUND',
'FIXED',
'SLB',
'NEWS',
'CMDTY',
'BSK',
'ICU',
'ICS');
CREATE TYPE ibkr.time_in_force AS ENUM
('DAY',
'GTC',
'OPG',
'IOC',
'GTD',
'GTT',
'AUC',
'FOK',
'GTX',
'DTC');
CREATE TABLE ibkr.closing_trade_association
(
opening_order_id int4 NOT NULL,
execution_id text NOT NULL,
CONSTRAINT closing_trade_association_execution_id_key UNIQUE (execution_id)
);
CREATE TABLE ibkr.contract
(
symbol text,
security_type ibkr.security_type,
expiry date,
strike numeric,
"right" ibkr."right",
exchange text,
currency text,
local_symbol text,
market_name text,
trading_class text,
contract_id bigint NOT NULL,
minimum_tick_increment numeric,
multiplier numeric,
price_magnifier integer,
underlying_contract_id bigint,
long_name text,
primary_exchange text,
contract_month text,
industry text,
category text,
subcategory text,
time_zone text,
ev_rule text,
ev_multiplier text,
CONSTRAINT contract_pkey PRIMARY KEY (contract_id)
);
CREATE TABLE ibkr.execution
(
order_id integer NOT NULL,
contract_id bigint,
execution_id text,
"timestamp" timestamp with time zone,
account text,
executing_exchange text,
side text,
shares numeric,
price numeric,
perm_id integer,
client_id integer,
liquidation integer,
cumulative_quantity integer,
average_price numeric,
order_reference text,
model_code text,
CONSTRAINT execution_execution_id_key UNIQUE (execution_id)
);
CREATE INDEX ON ibkr.execution ("timestamp");
CREATE TABLE ibkr.commission_report
(
execution_id text,
commission numeric,
currency text,
realized_pnl numeric,
yield numeric,
yield_redemption_date integer,
CONSTRAINT commission_report_execution_id_key UNIQUE (execution_id),
CONSTRAINT commission_report_execution_id_fkey FOREIGN KEY (execution_id)
REFERENCES ibkr.execution (execution_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
);
CREATE TABLE ibkr."order"
(
order_id integer NOT NULL,
contract_id bigint,
action ibkr.action,
total_quantity numeric,
order_type text,
limit_price numeric,
aux_price numeric,
time_in_force ibkr.time_in_force,
account text NOT NULL,
open_close ibkr.open_close,
order_ref text,
client_id integer,
perm_id integer,
"timestamp" timestamp with time zone,
CONSTRAINT order_pkey PRIMARY KEY (account, order_id)
);
CREATE TABLE ibkr.order_condition
(
account text NOT NULL,
order_id integer NOT NULL,
contract_id bigint,
type ibkr.condition_type NOT NULL,
operator ibkr.condition_operator,
comparator ibkr.condition_comparator,
value text,
exchange text,
trigger_method ibkr.condition_trigger_method,
CONSTRAINT order_condition_pkey PRIMARY KEY (account, order_id, type, comparator),
CONSTRAINT order_condition_order_id_fkey FOREIGN KEY (account, order_id)
REFERENCES ibkr."order" (account, order_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
);
CREATE TABLE ibkr.order_leg
(
account text NOT NULL,
order_id integer NOT NULL,
contract_id bigint NOT NULL,
ratio integer,
action ibkr.action,
exchange text,
open_close ibkr.open_close,
short_sale_slot smallint,
designated_location text,
exempt_code integer,
CONSTRAINT order_leg_pkey PRIMARY KEY (account, order_id, contract_id),
CONSTRAINT order_leg_order_id_fkey FOREIGN KEY (account, order_id)
REFERENCES ibkr."order" (account, order_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
);
CREATE TABLE ibkr.order_note
(
account text NOT NULL,
order_id integer NOT NULL,
order_strategy ibkr.order_strategy,
underlying_entry_price numeric,
underlying_stop_price numeric,
underlying_target_price numeric,
end_date date,
pattern ibkr.pattern,
CONSTRAINT order_note_order_id_key UNIQUE (account, order_id)
);
CREATE OR REPLACE VIEW ibkr."position"
AS SELECT execution.order_id,
execution.contract_id,
contract.symbol,
contract.security_type,
contract.expiry,
contract.strike,
contract."right",
execution.account,
sum(
CASE execution.side
WHEN 'BOT'::text THEN execution.shares
WHEN 'SLD'::text THEN execution.shares * '-1'::integer::numeric
ELSE NULL::numeric
END) AS signed_shares,
min(execution."timestamp") AS entry_timestamp
FROM ibkr.execution
JOIN ibkr.contract ON execution.contract_id = contract.contract_id
WHERE execution.order_id > 0
GROUP BY
execution.order_id,
execution.contract_id,
contract.symbol,
contract.security_type,
contract.expiry,
contract.strike,
contract."right",
execution.account;
CREATE SCHEMA renegade;
CREATE TABLE renegade.price_analysis (
"date" date NOT NULL,
market_act_symbol text NULL,
market_rating int2 NULL,
sector_act_symbol text NULL,
sector_vs_market numeric NULL,
sector_rating int2 NULL,
industry_act_symbol text NULL,
industry_rating int2 NULL,
stock_act_symbol text NOT NULL,
stock_vs_sector numeric NULL,
dividend_date date NULL,
earnings_date date NULL,
option_spread numeric NULL,
zacks_rank int2 NULL,
patterns text NULL,
CONSTRAINT price_analysis_pkey PRIMARY KEY (date, stock_act_symbol)
);
CREATE TABLE renegade.condor_analysis (
"date" date NOT NULL,
market_act_symbol text NULL,
market_rating numeric NULL,
market_risk_reward numeric NULL,
sector_act_symbol text NULL,
sector_rating numeric NULL,
sector_risk_reward numeric NULL,
industry_act_symbol text NULL,
industry_rating numeric NULL,
industry_risk_reward numeric NULL,
stock_act_symbol text NOT NULL,
stock_rating numeric NULL,
stock_risk_reward numeric NULL,
earnings_date date NULL,
option_spread numeric NULL,
CONSTRAINT condor_analysis_pkey PRIMARY KEY (date, stock_act_symbol)
);