forked from mrdavidlaing/javascript-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCIAPI.services.js
More file actions
107 lines (97 loc) · 3.03 KB
/
CIAPI.services.js
File metadata and controls
107 lines (97 loc) · 3.03 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
var CIAPI = CIAPI || {};
/**
@namespace Test data
*/
CIAPI.__testData = (function() {
try {
var
_i, _j,
_marketList = [],
_priceBars = {},
_currentBar, _currentMarket,
/**
* @private
*/
_generateNextPrice = function (lastPrice) {
var direction = Math.random() > 0.5 ? 1 : -1;
return lastPrice.Close + (direction * lastPrice.Close * 0.05);
},
/**
* @private
*/
_createPriceBar = function(previousBar, interval) {
var intervalInMs = {
minute: 1000 * 60,
hour: 1000 * 60 * 60,
day: 1000 * 60 * 60 * 24
};
var theDate = new Date(previousBar.BarDate.getTime() - intervalInMs[interval]);
var close = _generateNextPrice(previousBar);
return {
"BarDate":theDate,
"Close": close,
"High": close * (Math.random() + 1),
"Low":close * (1- Math.random()),
"Open":previousBar.Close
};
};
for (_i = 0; _i <= 101; _i++) {
_currentBar = {
"BarDate":new Date(),
"Close":1.6283,
"High":1.6285,
"Low":1.6283,
"Open":1.6284
};
_currentMarket = {
"MarketId": _i,
"Name": "{marketName} CFD #" + (_i + 1),
"PriceHistory": {
minute: []
}
};
for (_j = 0; _j <= 1000; _j++) {
_currentMarket.PriceHistory.minute.push(_currentBar);
_currentBar = _createPriceBar(_currentBar, 'minute');
}
_marketList.push(_currentMarket);
}
return {
MarketList: _marketList
};
}
catch(error) {
console.log(error);
}
})();
/**
@namespace A collection of services that you can call
*/
CIAPI.services = (function() {
return {
GetPriceBars: function(marketId, priceBars, success, error) {
var idx, interval='minute', marketPriceBars=[];
if (marketId < 1 || marketId > 100) {
error({ message: "Only marketId's between 1 and 100 are available" });
return;
}
if (priceBars > 1000) {
error({ message: "Can only return a maximum of 1000 pricebars" });
return;
}
for(idx in CIAPI.__testData.MarketList)
{
if (CIAPI.__testData.MarketList[idx].MarketId == marketId){
marketPriceBars = CIAPI.__testData.MarketList[idx].PriceHistory;
break;
}
}
setTimeout(function() {
success({
PartialPriceBar: marketPriceBars.minute[0],
PriceBars: marketPriceBars.minute.slice(1, priceBars + 1)
});
}, 360);
}
};
})();