-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstonks.c
123 lines (106 loc) · 2.97 KB
/
stonks.c
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
#include "stonks.h"
struct instrument instrument;
struct state state;
struct state* initState()
{
struct state* state = malloc(sizeof(struct state));
state->balance = INIT_BALANCE;
int i;
for(i = 0; i < NUM_INSTRUMENTS; i++)
{
struct instrument* instrument = malloc(sizeof(struct instrument));
instrument->len = -1;
instrument->held = 0;
instrument->directionChange = 0;
instrument->lastChange = 0.00;
state->instruments[i] = instrument;
}
return state;
}
void setRiskLevel(struct state* state, int index, int level)
{
state->instruments[index]->riskLevel = level;
}
void setVolatility(struct state* state, int index, int vol)
{
state->instruments[index]->volatility = vol;
}
void setDirection(struct state* state, int index, int direction)
{
state->instruments[index]->direction = direction;
}
void updateInstrument(struct state* state, int index, float price)
{
state->instruments[index]->len++;
int currentLen = state->instruments[index]->len;
state->instruments[index]->data[currentLen] = price;
}
void buyInstrument(struct state* state, int index, float amount)
{
int currentLen = state->instruments[index]->len;
float price = state->instruments[index]->data[currentLen] * amount;
if(price > state->balance) return; //not enough money
state->instruments[index]->held += amount;
state->balance -= price;
}
void buyInstrumentMoney(struct state* state, int index, float amount)
{
int len = state->instruments[index]->len;
float value = state->instruments[index]->data[len];
buyInstrument(state, index, amount/value);
}
void sellInstrumentMoney(struct state* state, int index, float amount)
{
int len = state->instruments[index]->len;
float value = state->instruments[index]->data[len];
sellInstrument(state, index, amount/value);
}
void sellInstrument(struct state* state, int index, float amount)
{
float held = state->instruments[index]->held;
if(amount > held) return; //cant sell that much
int currentLen = state->instruments[index]->len;
float price = state->instruments[index]->data[currentLen] * amount;
state->instruments[index]->held -= amount;
state->balance += price;
return;
}
float portfolioValue(struct state* state)
{
float total = state->balance;
int i;
for(i = 0; i < NUM_INSTRUMENTS; i++)
{
float amount = state->instruments[i]->held;
int index = state->instruments[i]->len;
if(index == -1) continue;
float price = state->instruments[i]->data[index];
total += price * amount;
}
return total;
}
void updateInstruments(struct state* state)
{
int i;
for(i = 0; i < NUM_INSTRUMENTS; i++)
{
float price = generateNewPrice(state->instruments[i]);
updateInstrument(state, i, price);
}
}
int maina()
{
struct state* state = initState();
updateInstrument(state, 3, 60);
setRiskLevel(state, 3, DYNAMIC);
setVolatility(state, 3, MEDIUM);
setDirection(state, 3, UP);
int i;
for(i = 0; i < 100; i++)
{
float newPrice = generateNewPrice(state->instruments[3]);
updateInstrument(state, 3, newPrice);
printf("%f\n", newPrice);
}
return 0;
}