-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstruments.js
More file actions
80 lines (67 loc) · 2.2 KB
/
instruments.js
File metadata and controls
80 lines (67 loc) · 2.2 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
var affirm = require('affirm.js')
var _ = require('lodash')
module.exports = (function () {
var instruments = {}
var contracts = {}
var symbols = []
var configs = {}
instruments.type = {
inverse: require("./instruments/inverse"),
quanto : require("./instruments/quanto")
}
instruments.init = function (configs) {
affirm(configs, 'Invalid configs')
var symbols = Object.keys(configs)
affirm(symbols && Array.isArray(symbols), 'Invalid configs')
for (var i = 0; i < symbols.length; i++) {
var symbol = symbols[i];
var config = configs[symbol]
instruments.add(config)
}
}
instruments.expire = function(symbol) {
affirm(symbol, 'Invalid symbol ' + symbol)
affirm(configs[symbol], 'Config missing for symbol: ' + symbol + ". All Configs:" + Object.keys(configs))
configs[symbol].status = 'expired'
}
instruments.add = function (config) {
affirm(config.type === 'inverse' || config.type === 'quanto', 'Invalid instrument type')
if (contracts[config.symbol]) return console.log(config.symbol, 'is already present')
contracts[config.symbol] = instruments.type[config.type](config)
symbols.push(config.symbol)
configs[config.symbol] = config
}
instruments.get = function (symbol) {
affirm(symbol, 'Invalid symbol')
return contracts[symbol]
}
instruments.getConfigs = function () {
return configs
}
instruments.getConfig = function (symbol) {
instruments.affirmSymbol(symbol)
return configs[symbol]
}
instruments.symbols = function () {
return symbols
}
instruments.affirmSymbol = function (symbol) {
affirm(configs[symbol], 'Invalid symbol:' + symbol, 422)
}
instruments.affirmActiveSymbol = function(symbol){
instruments.affirmSymbol(symbol)
affirm(! instruments.get(symbol).isExpired(), "Instrument " + symbol + " has expired.")
}
instruments.remove = function (symbol) {
affirm(symbol, 'Invalid symbol')
delete contracts[symbol]
delete configs[symbol]
_.remove(symbols, function (ele) {
return ele === symbol
})
}
instruments.reset = function(){
_.clone(symbols).forEach(instruments.remove)
}
return instruments
})()