-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprice_gen.c
48 lines (41 loc) · 1.08 KB
/
price_gen.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
#include "price_gen.h"
float generateNewPrice(struct instrument* instrument)
{
int index = instrument->len;
float currentPrice = instrument->data[index];
generateNewDirection(instrument);
float maxDelta = 0.01f * instrument->riskLevel;
float deltaPercent = (float)pcg32_random() / (float)UINT32_MAX * maxDelta * (float)instrument->direction;
instrument->lastChange = deltaPercent;
return currentPrice + deltaPercent * currentPrice;
}
void generateNewDirection(struct instrument* instrument)
{
int degree, interval;
if(instrument->volatility == LOW)
{
if(instrument->directionChange < 5) return;
interval = instrument->directionChange - 4;
degree = 5;
}
else if(instrument->volatility == MEDIUM)
{
if(instrument->directionChange < 3) return;
interval = instrument->directionChange - 2;
degree = 4;
}
else //HIGH
{
degree = 3;
}
int denominator = degree - interval + 1;
if(pcg32_random()%denominator == 0) //change direction
{
instrument->directionChange = 1;
instrument->direction = 1 - instrument->direction;
}
else
{
instrument->directionChange++;
}
}