-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_structures.h
More file actions
356 lines (310 loc) · 9.64 KB
/
Copy pathdata_structures.h
File metadata and controls
356 lines (310 loc) · 9.64 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* ============================================================================
* File: data_structures.h
* Description: Core data structures for algorithmic trading engine
* Author: Trading Engine Team
* Date: 2024
* ============================================================================
*/
#ifndef DATA_STRUCTURES_H
#define DATA_STRUCTURES_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
// ============================================================================
// CONSTANTS AND MACROS
// ============================================================================
#define MAX_SYMBOL_LENGTH 10
#define MAX_DATE_LENGTH 20
#define INITIAL_CAPACITY 100
#define HASH_TABLE_SIZE 101
#define MAX_STOCKS 50
// ============================================================================
// BASIC STRUCTURES
// ============================================================================
/**
* Date structure for time-series data
*/
typedef struct {
int year;
int month;
int day;
} Date;
/**
* Price data for a single trading day (OHLCV)
*/
typedef struct {
Date date;
double open;
double high;
double low;
double close;
long volume;
} PriceData;
/**
* Stock information and price history
*/
typedef struct {
char symbol[MAX_SYMBOL_LENGTH];
PriceData *prices;
int size;
int capacity;
} Stock;
// ============================================================================
// DYNAMIC ARRAY FOR TIME-SERIES DATA
// ============================================================================
/**
* Dynamic array structure with automatic resizing
*/
typedef struct {
double *data;
int size;
int capacity;
} DynamicArray;
// Function prototypes for DynamicArray
DynamicArray* create_dynamic_array(int initial_capacity);
void push_back(DynamicArray *arr, double value);
double get_at(DynamicArray *arr, int index);
void set_at(DynamicArray *arr, int index, double value);
void free_dynamic_array(DynamicArray *arr);
void resize_dynamic_array(DynamicArray *arr);
// ============================================================================
// CIRCULAR QUEUE FOR MOVING AVERAGES
// ============================================================================
/**
* Circular queue for efficient moving window calculations
* Provides O(1) insertion and deletion
*/
typedef struct {
double *data;
int front;
int rear;
int size;
int capacity;
double sum; // Running sum for quick average calculation
} CircularQueue;
// Function prototypes for CircularQueue
CircularQueue* create_circular_queue(int capacity);
int enqueue(CircularQueue *queue, double value);
double dequeue(CircularQueue *queue);
int is_queue_full(CircularQueue *queue);
int is_queue_empty(CircularQueue *queue);
double get_queue_average(CircularQueue *queue);
void free_circular_queue(CircularQueue *queue);
// ============================================================================
// MIN-MAX HEAP FOR PRICE TRACKING
// ============================================================================
/**
* Min heap node structure
*/
typedef struct {
double price;
int timestamp;
} HeapNode;
/**
* Min heap structure for finding minimum prices quickly
*/
typedef struct {
HeapNode *nodes;
int size;
int capacity;
} MinHeap;
/**
* Max heap structure for finding maximum prices quickly
*/
typedef struct {
HeapNode *nodes;
int size;
int capacity;
} MaxHeap;
// Function prototypes for MinHeap
MinHeap* create_min_heap(int capacity);
void insert_min_heap(MinHeap *heap, double price, int timestamp);
HeapNode extract_min(MinHeap *heap);
HeapNode peek_min(MinHeap *heap);
void min_heapify(MinHeap *heap, int index);
void free_min_heap(MinHeap *heap);
// Function prototypes for MaxHeap
MaxHeap* create_max_heap(int capacity);
void insert_max_heap(MaxHeap *heap, double price, int timestamp);
HeapNode extract_max(MaxHeap *heap);
HeapNode peek_max(MaxHeap *heap);
void max_heapify(MaxHeap *heap, int index);
void free_max_heap(MaxHeap *heap);
// ============================================================================
// LINKED LIST FOR TRADE HISTORY
// ============================================================================
/**
* Trade information
*/
typedef struct {
char symbol[MAX_SYMBOL_LENGTH];
char action[10]; // BUY or SELL
Date date;
double price;
int quantity;
double total_value;
double commission;
} Trade;
/**
* Linked list node for trade history
*/
typedef struct TradeNode {
Trade trade;
struct TradeNode *next;
} TradeNode;
/**
* Trade history linked list
*/
typedef struct {
TradeNode *head;
TradeNode *tail;
int count;
} TradeList;
// Function prototypes for TradeList
TradeList* create_trade_list();
void add_trade(TradeList *list, Trade trade);
void print_trade_list(TradeList *list);
void free_trade_list(TradeList *list);
int get_trade_count(TradeList *list);
// ============================================================================
// HASH TABLE FOR STOCK LOOKUP
// ============================================================================
/**
* Hash table entry
*/
typedef struct HashNode {
char symbol[MAX_SYMBOL_LENGTH];
Stock *stock;
struct HashNode *next;
} HashNode;
/**
* Hash table for O(1) stock lookup
*/
typedef struct {
HashNode **table;
int size;
} HashTable;
// Function prototypes for HashTable
HashTable* create_hash_table(int size);
unsigned int hash_function(const char *symbol);
void insert_stock(HashTable *ht, Stock *stock);
Stock* lookup_stock(HashTable *ht, const char *symbol);
void free_hash_table(HashTable *ht);
// ============================================================================
// BINARY SEARCH TREE FOR ORDERED PRICES
// ============================================================================
/**
* BST node for ordered price levels
*/
typedef struct BSTNode {
double price;
int count;
struct BSTNode *left;
struct BSTNode *right;
} BSTNode;
/**
* Binary Search Tree for price ordering
*/
typedef struct {
BSTNode *root;
int size;
} BST;
// Function prototypes for BST
BST* create_bst();
BSTNode* insert_bst_node(BSTNode *root, double price);
BSTNode* search_bst(BSTNode *root, double price);
BSTNode* find_min_bst(BSTNode *root);
BSTNode* find_max_bst(BSTNode *root);
void inorder_traversal(BSTNode *root);
void free_bst(BSTNode *root);
// ============================================================================
// SEGMENT TREE FOR RANGE QUERIES
// ============================================================================
/**
* Segment tree for efficient range min/max queries
* Time Complexity: O(log n) for queries and updates
*/
typedef struct {
double *min_tree;
double *max_tree;
int *base_array_size;
int tree_size;
} SegmentTree;
// Function prototypes for SegmentTree
SegmentTree* create_segment_tree(double *arr, int n);
void build_segment_tree(double *arr, int node, int start, int end,
double *min_tree, double *max_tree);
double query_min(SegmentTree *st, int node, int start, int end,
int left, int right);
double query_max(SegmentTree *st, int node, int start, int end,
int left, int right);
void free_segment_tree(SegmentTree *st);
// ============================================================================
// PORTFOLIO STRUCTURE
// ============================================================================
/**
* Portfolio holding information
*/
typedef struct {
char symbol[MAX_SYMBOL_LENGTH];
int quantity;
double average_price;
double current_price;
double total_invested;
double current_value;
double profit_loss;
double profit_loss_percentage;
} Holding;
/**
* Portfolio structure
*/
typedef struct {
Holding *holdings;
int num_holdings;
int capacity;
double cash_balance;
double initial_capital;
double total_value;
double total_return;
double total_return_percentage;
} Portfolio;
// Function prototypes for Portfolio
Portfolio* create_portfolio(double initial_capital);
void add_holding(Portfolio *portfolio, const char *symbol, int quantity,
double price);
void update_holding(Portfolio *portfolio, const char *symbol,
double current_price);
void remove_holding(Portfolio *portfolio, const char *symbol);
Holding* get_holding(Portfolio *portfolio, const char *symbol);
void calculate_portfolio_value(Portfolio *portfolio);
void print_portfolio(Portfolio *portfolio);
void free_portfolio(Portfolio *portfolio);
// ============================================================================
// UTILITY FUNCTIONS
// ============================================================================
// Date manipulation
Date create_date(int year, int month, int day);
int compare_dates(Date d1, Date d2);
void print_date(Date date);
Date string_to_date(const char *date_str);
// Stock functions
Stock* create_stock(const char *symbol);
void add_price_data(Stock *stock, PriceData data);
PriceData* get_price_at_date(Stock *stock, Date date);
void free_stock(Stock *stock);
// Statistical functions
double calculate_mean(double *data, int size);
double calculate_std_dev(double *data, int size);
double calculate_variance(double *data, int size);
double calculate_covariance(double *data1, double *data2, int size);
double calculate_correlation(double *data1, double *data2, int size);
// Array operations
void copy_array(double *dest, double *src, int size);
double find_max(double *arr, int size);
double find_min(double *arr, int size);
int find_max_index(double *arr, int size);
int find_min_index(double *arr, int size);
#endif // DATA_STRUCTURES_H