forked from MrDrummer/core-coin-value-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
294 lines (229 loc) · 8.99 KB
/
server.js
File metadata and controls
294 lines (229 loc) · 8.99 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
var defaultPingsPerHour = 4; // 4 pings per hour
var defaultDeviation = 4; // 4% deviation
/**
starting value
which config (3 / 5) - to add more create a fresh file named config-?
pings per hour (null to use the above default)
deviation (% random fluctuation) (null to use the above default)
**/
var results = getPrices(500, "3", 2);
console.log(results);
function trackConfig(day) {
/*
This whole section is getting the config and parsing it into a chronological format that is easier to parse.
It's figuring out the "track" that is the important part, once we know that we can randomize the 4% lee-way.
*/
var projection = [];
var projectionTable = [];
var blankTracker = 0;
// For each day in the list...
for (var i in day) {
// For each item in the track
for (var j in day[i]["track"]) {
// Shorthand variable name
var track = day[i]["track"];
projectionTable.push(track[j]);
// If it's a $, add it to the internal counter.
if (track[j] == "$") {
blankTracker += 1;
continue
}
// if the blank tracker is over 0, push the counter to the array and reset the counter.
// It will only get to this point after it gets over 1. We make sure the very last few $ are added because the very last input (Friday, #7) isn't a $.
if (blankTracker > 0) {
projection.push("$" + blankTracker);
blankTracker = 0;
}
// If the item starts with a # it means it should not have the following pings in that hour counted.
if (track[j].startsWith("#")) {
var endItem = track[j].substring(1);
projection.push(("#" + (100 + parseInt(endItem))).toString());
// If the config option starts with + or -, add the int to 100 to get the overall percentage.
} else if (track[j].match(/^(\+|-)/i)) {
projection.push((100 + parseInt(track[j])).toString());
} else {
console.log("Seems to be an issue with the config syntax.");
}
}
}
return projection;
}
function trackConfigDetailed(projection, settings) {
var pings = settings.pingsPerHour;
var projectionDetailed = [];
var addOn = true;
// For each item in the simplified track array, we expand the array so that it accounts for the other
for (var i in projection) {
i = parseInt(i);
var item = projection[i];
// If the item starts with a $...
// $ means dynamic, as in the track value is calculated for us.
if (item.startsWith("$")) {
item = parseInt(item.substring(1));
var totalPings = (parseInt(item) * pings) + (pings - 1);
projectionDetailed.push("$" + totalPings);
} else {
projectionDetailed.push(item);
// If it doesn't start with a $ we need to check if the next item starts with a $ too, so we can add the pings - 1 to this item instead of the next (which would be $)
// If the next item doesn't also starts with a $...
if (projection[i+1]) {
if (!projection[i+1].startsWith("$")) {
if (!projection[i+1].startsWith("#")) {
projectionDetailed.push("$3");
}
}
}
}
}
return projectionDetailed
}
function trackDifference(projection) {
/* Calculating the true difference between entries - we only state the start of the hour in the config. We take 1 away from the multiplier because of the values already set.
So example:
500 * * * | 300 * * * | * * * * | * * * * | 200 * * * , where star is a filled in ping, | is splitting up each hour.
4 * means we've made that hour dynamic ("$"). We fill in the other 3 pings for that hour. It's dynamic so it could be 15 pings per hour and would still work - just means that the increment % would be much smaller.
*/
// Will hold all of the track projections.
var dayTrack = [];
for (var i in projection) {
i = parseInt(i);
var item = projection[i];
// If the item starts with a $...
// $ means dynamic, as in the track value is calculated for us.
if (item.startsWith("$")) {
item = parseInt(item.substring(1));
var prevItem = projection[i - 1];
// Remove the # if the next item starts with one.
var nextItem = projection[i + 1];
if (nextItem.startsWith("#")) {
item += 3;
nextItem = nextItem.substring(1);
}
// Get the difference between the last number and the next.
var diff = differencefunction(parseInt(prevItem), parseInt(nextItem));
var diffItems = trackPrice(prevItem, nextItem, item);
for (var k in diffItems) {
dayTrack.push(diffItems[k]);
}
} else {
if (!item.startsWith("#")) {
// item = item.substring(1);
dayTrack.push(parseInt(item));
} else {
dayTrack.push(parseInt(item.substring(1)));
}
}
}
return dayTrack
}
function trackGetRandom(percentageTrack, settings) {
var percentageValue = [];
var percentageRandom = [];
var differenceValue = [];
var deviation = settings.deviation;
var startPrice = settings.startPrice;
// For each element, push the values using the number input and % array.
percentageTrack.forEach(function(element) {
percentageValue.push(percentageOf(startPrice, element, true));
});
percentageValue.forEach(function(element) {
var rand = randomDeviation(element, startPrice, deviation);
percentageRandom.push(rand[0]);
differenceValue.push(rand[1]);
});
return [percentageRandom, percentageValue, differenceValue]
}
function trackToDays(track, settings) {
var hoursTest = track.length / settings.dayCount;
if (!(track.length) / settings.dayCount / settings.pingsPerHour == settings.hoursPerDay) {
console.error("Looks like there was an error in the matrix! (Number of items don't split up perfectly into " + settings.dayCount + " days.)")
return "Error. See console."
}
var trackSegmented = new Array(Math.ceil(track.length / hoursTest)).fill().map(_ => track.splice(0, hoursTest));
var trackObject = {}
for (let i=0; i < trackSegmented.length; i++) {
trackObject[i+1] = trackSegmented[i];
}
return trackObject
}
function differencefunction(a, b) {
return Math.abs(a - b);
}
function percentageOf(input, percentage, round) {
var out = (parseInt(input) / 100) * percentage;
if (round) {
return Math.floor(out);
} else {
return out;
}
}
// Returns an array of values equal difference, between the start and end values.
function trackPrice(startVal, endVal, runs) {
startVal = parseInt(startVal);
endVal = parseInt(endVal);
runs = parseInt(runs);
// How much we add the track by.
var addition = differencefunction(startVal, endVal) / runs;
var currentVal = startVal;
var toReturn = [];
var count = 1;
while (count <= runs) {
var val = 0;
// making sure we support negative values.
if (startVal < endVal) {
val = currentVal + addition;
} else {
val = currentVal - addition;
}
toReturn.push(parseInt(val));
currentVal = val;
count++;
}
return toReturn;
}
// base input, track change value, % deviation value
function randomDeviation(input, startVal, deviation) {
startVal = parseInt(startVal);
input = parseInt(input);
// x% of the starting value
// (gets the fixed deviation % to determine the max
var percent = (parseInt(startVal) / 100) * deviation;
var random = Math.floor(Math.random() * percent);
var upOrDown = Math.floor(Math.random() * 2);
// randomly adds or take away the determined value.
var out;
if (upOrDown == 1) {
out = input + random;
} else if (upOrDown == 0) {
out = input - random;
random = parseInt("-" + random);
} else {
console.error("Should never get here. upOrDown should only be 0 or 1.");
}
// the final value taking the original starting input and adding/subtracting the track value (incl random) with it., what the (total)random change was (incl -).
return [Math.floor(out), random];
}
function getPrices(startPrice=500, configToUse = "1", pingsPerHour = defaultPingsPerHour, deviation = defaultDeviation) {
var settings = {};
settings.config = require("./config-" + configToUse + ".js");
settings.days = settings.config.days;
settings.dayCount = settings.config.days.length;
settings.pingsPerHour = pingsPerHour;
// last part gets the number of hours in a day
settings.hoursPerDay = Object.keys(settings.days[0].track).length;
// % deviation of the determined value in the track.
settings.deviation = deviation;
settings.startPrice = startPrice;
console.error("===== Ran " + Date() + " =====");
startPrice = parseInt(startPrice);
// var newPrice = randomDeviation(startPrice);
var projection = trackConfig(settings.days);
var projectionDetailed = trackConfigDetailed(projection, settings);
var dayTrack = trackDifference(projectionDetailed);
var randomTrack = trackGetRandom(dayTrack, settings);
var trackRandom = randomTrack[0];
var trackPercentageValue = randomTrack[1];
var trackRandomDifferences = randomTrack[2];
var trackDays = trackToDays(trackRandom, settings);
return trackDays
}