WU Trading Library 0.2.0
A backtesting and trading strategy library
Loading...
Searching...
No Matches
positionvector.c
Go to the documentation of this file.
1#define _POSIX_C_SOURCE 200809L
2#include <stdlib.h>
3#include <string.h>
4#include "wu.h"
5
6// Forward declaration
8
9static void add_position(WU_PositionVector* vec, WU_Position pos) {
10 if (!vec || !pos) return;
11 int slot = -1;
12 for (int i = 0; i < vec->capacity; i++) {
13 if (!vec->active[i]) {
14 slot = i;
15 break;
16 }
17 }
18 if (slot == -1) {
19 int new_capacity = vec->capacity == 0 ? 4 : vec->capacity * 2;
20 vec->positions = realloc(vec->positions, new_capacity * sizeof(struct WU_Position_));
21 vec->active = realloc(vec->active, new_capacity * sizeof(bool));
22 for (int i = vec->capacity; i < new_capacity; i++) {
23 vec->active[i] = false;
24 }
25 slot = vec->capacity;
26 vec->capacity = new_capacity;
27 }
28 // Copy the position value into the array
29 vec->positions[slot] = *pos;
30 vec->active[slot] = true;
31 vec->count++;
32}
33
34static void remove_position(WU_PositionVector* vec, int index) {
35 if (!vec || index < 0 || index >= vec->capacity) return;
36 if (!vec->active[index]) return;
37 vec->active[index] = false;
38 vec->count--;
39}
40
42 if (!vec) return;
43 for (int i = 0; i < vec->capacity; i++) {
44 vec->active[i] = false;
45 }
46 vec->count = 0;
47}
48
49static struct WU_Position_ get_position(WU_PositionVector* vec, int index, bool* found) {
50 struct WU_Position_ empty = {0};
51 if (!vec || index < 0 || index >= vec->capacity || !vec->active[index]) {
52 if (found) *found = false;
53 return empty;
54 }
55 if (found) *found = true;
56 return vec->positions[index];
57}
58
60 if (!vec) return 0.0;
61 double total = 0.0;
62 for (int i = 0; i < vec->capacity; i++) {
63 if (vec->active[i]) {
64 total += vec->positions[i].quantity;
65 }
66 }
67 return total;
68}
69
71 WU_PositionVector* vec = malloc(sizeof(WU_PositionVector));
72 if (!vec) return NULL;
73 vec->symbol = strndup(symbol ? symbol : "", WU_SYMBOL_MAX_LEN);
74 vec->last_price = 0.0;
75 vec->positions = NULL;
76 vec->active = NULL;
77 vec->count = 0;
78 vec->capacity = 0;
79 vec->add = add_position;
82 vec->get = get_position;
85 return vec;
86}
87
89 if (!vec) return;
90 free(vec->symbol);
91 free(vec->positions);
92 free(vec->active);
93 free(vec);
94}
#define WU_SYMBOL_MAX_LEN
Definition positions.h:8
WU_PositionVector * wu_position_vector_new(const char *symbol)
static void add_position(WU_PositionVector *vec, WU_Position pos)
static void remove_position(WU_PositionVector *vec, int index)
static void clear_positions(WU_PositionVector *vec)
static struct WU_Position_ get_position(WU_PositionVector *vec, int index, bool *found)
static double get_total_quantity(WU_PositionVector *vec)
static void wu_position_vector_free(WU_PositionVector *vec)
WU_PositionVector is a data structure that holds multiple positions for a single asset.
Definition positions.h:24
struct WU_Position_(* get)(struct WU_PositionVector *vec, int index, bool *found)
Definition positions.h:28
void(* remove)(struct WU_PositionVector *vec, int index)
Definition positions.h:26
struct WU_Position_ * positions
Definition positions.h:34
void(* delete)(struct WU_PositionVector *vec)
Definition positions.h:31
void(* clear)(struct WU_PositionVector *vec)
Definition positions.h:27
double(* total_quantity)(struct WU_PositionVector *vec)
Definition positions.h:30
void(* add)(struct WU_PositionVector *vec, WU_Position pos)
Definition positions.h:25
WU_Position represents an open position in the portfolio.
Definition positions.h:13
double quantity
Definition positions.h:15