-
Notifications
You must be signed in to change notification settings - Fork 3
/
PriceIterators.py
297 lines (252 loc) · 10 KB
/
PriceIterators.py
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
295
296
297
# PriceIterators.py
#
# Classes that can compute price levels (or other levels, e.g. RVI) as
# indexable sequences. E.g. logarithmically-spaced intervals where
# the index represetnts a logarithm of the price.
#
# Useage:
#
# import PriceIterators
#
# prices = PriceIterators.New(**args)
#
# Classes:
#
# o _LogSpacer
# o LogPrices
#
# Module Utils:
#
# o New(args...)
#
import math
import itertools
def New(**kwargs):
algorithm = kwargs.pop("iterator", "default")
if algorithm in ["logarithmic", "default"]:
newPI = LogPrices(**kwargs)
elif algorithm in ["interval"]:
newPI = IntervalPrices(**kwargs)
elif algorithm in ["plainoldlist"]:
newPI = PlainOldListPrices(**kwargs)
return newPI
class PlainOldListPrices:
"""Prices from a plain old list.
Note that :startprice: is ignored here. (It is used in the layers above
though just to get currency pair, where it is sufficient to pass in any
value, typically "0 BASE:QUOTE")
:param pricelist: List of prices, eg [12.345,67.890,...]. Note: List is
assumed to be ordered. Whether list is "ascending" or "descending" is
ascertained by looking at the first two elements. Lists of length 1 ARE
permitted.
"""
def __init__(self, startprice, pricelist, plane=1, flip=False):
self.startprice = pricelist[0]
self.pricelist = pricelist
self.interval = pricelist[1] - pricelist[0] if len(pricelist) > 1 else 0
self.steps = len(pricelist)
self.plane = plane # ignored
self.flip = flip
def __str__(self):
# String for appending to pretext header, e.g. "t32000:f2:s24:p2d"
# Format: (t|b)<startprice>:list:s<STEPS>[:p<PLANE>(u|d)]
descending = (self.interval <= 0) ^ self.flip
tailprice = self.pricelist[-1]
headerprice = self.startprice if not self.flip else tailprice
return "%s:%s:%s" % (
"%s%g"%("t" if descending else "b", headerprice),
"list",
"s%g"%(self.steps)
)
def getDescriptiveContext(self):
context = {}
context['steps'] = self.steps
context['plane'] = self.plane
context['structure'] = "explicit prices"
return context
def getPrices(self):
prices = [d for d in self.pricelist]
if self.flip:
prices.reverse()
return prices
@property
def prices(self):
return self.getPrices()
class IntervalPrices:
"""Prices at fixed intervals."""
def __init__(self, startprice, interval, steps, plane=1, flip=False):
self.startprice = startprice
self.interval = interval
self.steps = steps
self.plane = plane # ignored
self.flip = flip
def __str__(self):
# String for appending to pretext header, e.g. "t32000:f2:s24:p2d"
# Format: (t|b)<startprice>:v<INTERVAL>:s<STEPS>[:p<PLANE>(u|d)]
descending = (self.interval < 0) ^ self.flip
tailprice = self.startprice + (self.interval * self.steps)
headerprice = self.startprice if not self.flip else tailprice
norminterval = self.interval if (self.interval > 0) else -self.interval
return "%s:%s:%s" % (
"%s%g"%("t" if descending else "b", headerprice),
"v%g"%(norminterval),
"s%g"%(self.steps)
)
def getDescriptiveContext(self):
context = {}
context['steps'] = self.steps
context['plane'] = self.plane
context['structure'] = "fixed intervals"
return context
def getPrices(self):
deltas = [self.interval * m for m in range(self.steps+1)]
prices = [self.startprice + d for d in deltas]
if self.flip:
prices.reverse()
return prices
@property
def prices(self):
return self.getPrices()
class LogPrices:
"""Logarithmically indexed price sequence.
Sequence spans N multiplicative decades consisting of M steps each, for a
total of N*M+1 total price points. Price sequences are optionally
staggered into different interleaved "price planes" to allow multiple
coursely-spaced tables to combine into a finer coverage of price points.
The `plane` parameter indicates both which stagger plane is selected and
how many make up the set. I.e. the table is table n of m, with m indicated
by the most significant bit. Examples:
`plane` = 1 => table 1 of 1
2 => table 1 of 2
3 => table 2 of 2
4 => table 1 of 4
5 => table 2 of 4
8 => table 1 of 8
15 => table 7 of 8
Note that stagger planes 1, 2, 4, 8,... all produce the same price
sequence, but will produce different hash sequences due to the pretext
header being different.
"""
def __init__(self, startprice, factor, decades, steps, plane=1, flip=False):
self.startprice = startprice
self.factor = factor
self.decades = decades
self.steps = steps
self.plane = plane # Table n of m, (m indicated by MSB)
self.flip = flip
def __str__(self):
# String for appending to pretext header, e.g. "t32000:f2:s24:p2d"
# Format: (t|b)<startprice>:f<FACTOR>:s<STEPS>[:p<PLANE>(u|d)]
descending = (self.factor < 1) ^ self.flip
tailprice = self.startprice * (self.factor ** self.decades)
headerprice = self.startprice if not self.flip else tailprice
normfactor = self.factor if (self.factor > 1) else (1/self.factor)
return "%s:%s:%s%s" % (
"%s%g"%("t" if descending else "b", headerprice),
"f%g"%(normfactor),
"s%g"%(self.steps),
"" if self.plane == 1 else ":p%g%s"%(self.plane,
"u" if (self.factor > 1) else "d")
)
def getDescriptiveContext(self):
normfactor = self.factor if (self.factor > 1) else (1/self.factor)
context = {}
context['decades'] = self.decades
context['decadeword'] = "octaves" if normfactor==2 else "decades" if normfactor==10 else "multiples"
context['steps'] = self.steps
context['plane'] = self.plane
context['structure'] = "%(decades)s %(decadeword)s, %(steps)s steps each"%context
return context
def getPrices(self):
stagger_m = 2**int(math.log(self.plane,2))
stagger_n = self.plane - stagger_m # strip leading bit
multiples = _LogSpacer(self.factor, self.steps, stagger_n).getLevels(self.decades)
prices = [self.startprice * m for m in multiples]
if self.flip:
prices.reverse()
return prices
@property
def prices(self):
return self.getPrices()
####
## Class: _LogSpacer
##
## Generates sequences of factors where each factor is a constant
## ratio to the preceding one. Note we aren't dealing with "prices"
## here, just factors, so one end of the sequence will always be 1.00.
##
## Can generate the sequence either "forward" or "reverse".
##
## Handles "staggering", where multiple course-grained sequences, when
## combined together, make a fine-grained sequence.
##
class _LogSpacer:
def __init__(self, decadefactor=None, steps=None, stagger=None):
self.decadefactor = decadefactor
self.steps = steps
self.stagger = stagger
def getLevels(self, numDecades):
self._levels = [1.0]
decadeSteps = self.getDecadeSteps()
stepFactor = decadeSteps[0]
(n, d) = _LogSpacer.staggerRatioPair(self.stagger)
staggerFactor = stepFactor ** (n/d)
self._levels = [1.0 * staggerFactor]
for i in range(numDecades):
extend = [self._levels[-1] * ds for ds in decadeSteps]
self._levels.extend(extend)
return self._levels
def getDecadeSteps(self):
# Generate sequence of product multiplier that span a "decade" of
# proportionality given by 'factor' over a series of 'steps'
# intervals.
factor = float(self.decadefactor)
steps = int(self.steps)
stepmult = factor ** (1 / steps)
steppct = (stepmult-1)*100 if stepmult>1 else ((1/stepmult)-1)*100
result = [ stepmult ** i for i in range(1,1+steps)]
residual = abs(result[-1]-factor) # measure of accumulated error
if residual > 1e-12:
print ("Warning: Decade Residual of %g may be too large." % residual)
result[-1] = float(factor) # prevent accumulated error of many decades
return result
def staggerRatioPair(idx):
""" Compute power ratio used to compute the idx'th stagger of a series.
Returns (n, d) such that a price point in a series spaced with
stepfactor F is staggered by P(n, idx) = P(n, 0) * pow(F,(n/d)).
"""
if idx==0: return (0, 1)
msbidx = 2**int(math.log(idx,2)) # keeps only MSB of idx
d = msbidx*2
(n0, d0) = _LogSpacer.staggerRatioPair(idx-msbidx)
n = 1 + n0*d/d0
return (n, d)
if __name__ == '__main__':
print("Test 1:")
lines = ["", "", ""]
for i in range(0,33):
(n,d) = _LogSpacer.staggerRatioPair(i)
lines[0] += "%3d "%n
lines[1] += " -- "
lines[2] += "%3d "%d
#print("idx: %2d, (%2d, %2d)"%(i,n,d))
print(lines[0])
print(lines[1])
print(lines[2])
print("\nTest 2:")
LS = _LogSpacer(64, 3, 2)
for i in LS.getLevels(2):
print(i)
print("\nTest 3:\n")
P1 = LogPrices(32000,0.5,5,6,8,False)
P2 = LogPrices(32000,0.5,5,6,8,True)
P3 = LogPrices(32000,0.5,5,6,12,False)
P4 = LogPrices(32000,0.5,5,6,12,True)
P5 = LogPrices(32000,0.5,5,6,1,False)
P6 = LogPrices(32000,0.5,5,6,1,True)
P7 = LogPrices(1000,2,5,6,24,False)
P8 = LogPrices(1000,2,5,6,24,True)
print("%20s%20s%20s%20s%20s%20s%20s%20s"%(P1,P2,P3,P4,P5,P6,P7,P8))
for (a,b,c,d,f,g,h,i) in itertools.zip_longest(P1.prices,P2.prices,P3.prices,P4.prices,
P5.prices,P6.prices,P7.prices,P8.prices):
print("%20g%20g%20g%20g%20g%20g%20g%20g"%(a,b,c,d,f,g,h,i))