forked from DrPaulBrewer/market-pricing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (167 loc) · 5.77 KB
/
index.js
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
var findZeroRange = require('find-zero-range');
function walrasianCEPriceRange(pLow,pHigh,pTol,qDemand,qSupply){
'use strict';
var excessSupply = function(p){ return (qSupply(p)-qDemand(p)); };
var priceRange = findZeroRange(pLow, pHigh, pTol, excessSupply);
// correction for integer prices and quantities when a supply order is needed for CE
if ((priceRange.length===1) &&
(pTol===1) &&
(excessSupply(priceRange[0])<0) &&
(qSupply(priceRange[0])<qSupply(priceRange[0]+1)))
return [priceRange[0]+1];
return priceRange;
}
module.exports.walrasianCEPriceRange = walrasianCEPriceRange;
function demandFromQueue(buyQueue,bpCol,qCol){
return function(p){
'use strict';
var i=0,l=buyQueue.length,totalQ=0;
while ((i<l) && (p<=buyQueue[i][bpCol])){
totalQ += buyQueue[i][qCol];
++i;
}
return totalQ;
};
}
module.exports.demandFromQueue = demandFromQueue;
function supplyFromQueue(sellQueue,spCol,qCol){
return function(p){
'use strict';
var i=0,l=sellQueue.length,totalQ=0;
while ((i<l) && (p>=sellQueue[i][spCol])){
totalQ += sellQueue[i][qCol];
++i;
}
return totalQ;
};
}
module.exports.supplyFromQueue = supplyFromQueue;
function marshallianCEPriceRange(inframarginalBuyPrice,
inframarginalSellPrice,
extramarginalBuyPrice,
extramarginalSellPrice){
'use strict';
if (extramarginalBuyPrice > inframarginalBuyPrice)
throw new Error("marketPricing.marshallianCEPriceRange: invalid buy prices, extramarginal price must not be greater than inframarginal price");
if (extramarginalSellPrice < inframarginalSellPrice)
throw new Error("marketPricing.marshallianCEPriceRange: invalid sell prices, extramarginal price must not be less than inframarginal price");
if (extramarginalBuyPrice >= extramarginalSellPrice)
throw new Error("marketPricing.marshallianCEPriceRange: invalid extramarginal prices, extramarginal buy price should not equal or exceed extramarginal sell price");
var cePriceRange = [
(Math.max(extramarginalBuyPrice, inframarginalSellPrice) || inframarginalSellPrice),
(Math.min(extramarginalSellPrice, inframarginalBuyPrice) || inframarginalBuyPrice)
];
return cePriceRange;
}
module.exports.marshallianCEPriceRange = marshallianCEPriceRange;
function crossSingleUnitDemandAndSupply(buyPrices,sellPrices){
'use strict';
var l=Math.min(buyPrices.length,sellPrices.length),q0=0,q1=0;
var ceIntersection;
if (l>0){
while( (q0<l) && (buyPrices[q0]>sellPrices[q0]) )
q0++;
q1=q0;
while( (q1<l) && (buyPrices[q1]>=sellPrices[q1]) )
q1++;
if (q1===0){
ceIntersection = {
p: [buyPrices[0],sellPrices[0]],
q: 0
};
} else {
if (q1>q0){
/* in this case, buyPrices[j]===sellPrices[j] at q0<=j<q1 */
ceIntersection = {
p: buyPrices[q1-1],
q: [q0,q1]
};
} else if (q1===q0) {
ceIntersection = {
p: marshallianCEPriceRange(buyPrices[q1-1],sellPrices[q1-1],buyPrices[q1],sellPrices[q1]),
q: q0
};
if (ceIntersection.p[0]===ceIntersection.p[1])
ceIntersection.p = ceIntersection.p[0];
}
}
return ceIntersection;
}
}
module.exports.crossSingleUnitDemandAndSupply = crossSingleUnitDemandAndSupply;
function cross(buyQueue,sellQueue,bpCol,bqCol,spCol,sqCol){
'use strict';
if ((bpCol===undefined) ||
(bqCol===undefined) ||
(spCol===undefined) ||
(sqCol===undefined))
throw new Error("marketPricing.cross: missing 1 or more col parameters: "+([bpCol,bqCol,spCol,sqCol].join(",")));
var bidx=0,sidx=0,bl=buyQueue.length,sl=sellQueue.length;
if ((!bl) || (!sl)) return undefined;
var buyQ=[],sellQ=[],deltaQ=0,totalQ=0;
var ibp,isp,ebp=buyQueue[0][bpCol],esp=sellQueue[0][spCol];
if (ebp>=esp){
buyQ[0]=0;
sellQ[0]=0;
}
while ((ebp >= esp) &&
(bidx < bl) &&
(sidx < sl)){
deltaQ = Math.min(buyQueue[bidx][bqCol]-buyQ[bidx],sellQueue[sidx][sqCol]-sellQ[sidx]);
totalQ += deltaQ;
// increment or initialize quantity bought/sold by deltaQ
buyQ[bidx] += deltaQ;
sellQ[sidx] += deltaQ;
ibp = ebp;
isp = esp;
if (buyQueue[bidx][bqCol]===buyQ[bidx]){
bidx++;
buyQ[bidx]=0;
ebp = (bidx<bl)? (buyQueue[bidx][bpCol]) : undefined;
}
if (sellQueue[sidx][sqCol]===sellQ[sidx]){
sidx++;
sellQ[sidx]=0;
esp = (sidx<sl)? (sellQueue[sidx][spCol]) : undefined;
}
}
if (totalQ===0) return undefined;
var priceRange = marshallianCEPriceRange(ibp,isp,ebp,esp);
var price = (priceRange[0]+priceRange[1])/2;
if (buyQ[bidx]===0) buyQ.pop();
if (sellQ[sidx]===0) sellQ.pop();
return [price,totalQ,buyQ,sellQ];
}
module.exports.cross = cross;
function sequential(buyQueue,sellQueue,tCol,bpCol,bqCol,spCol,sqCol){
'use strict';
if ((tCol===undefined) ||
(bpCol===undefined) ||
(bqCol===undefined) ||
(spCol===undefined) ||
(sqCol===undefined))
throw new Error("marketPricing.sequential: missing 1 or more col parameters: "+([tCol,bpCol,bqCol,spCol,sqCol].join(",")));
var totalQ,prices,buyQ,sellQ,i,l,op;
if ((!buyQueue.length) ||
(!sellQueue.length) ||
(buyQueue[0][bpCol]<sellQueue[0][spCol])) return undefined;
var crossResult = cross(buyQueue,sellQueue,bpCol,bqCol,spCol,sqCol);
if (crossResult===undefined) return undefined;
totalQ = crossResult[1];
buyQ = crossResult[2];
sellQ = crossResult[3];
prices = [];
if (buyQueue[0][tCol]<sellQueue[0][tCol]){
op = 's';
if (sellQ[1]) throw new Error("marketPricing.sequential: non-sequential trades on sell side");
for(i=0,l=buyQ.length;i<l;++i)
prices[i] = buyQueue[i][bpCol];
} else {
op = 'b';
if (buyQ[1]) throw new Error("marketPricing.sequential: non-sequential trades on buy side");
for(i=0,l=sellQ.length;i<l;++i)
prices[i] = sellQueue[i][spCol];
}
return [op, prices, totalQ, buyQ, sellQ];
}
module.exports.sequential = sequential;