Use a single array for all lines

Speed up the lines array creation using proper slice assignment.
This gives another 10% speedup to the historical price rendering.
Drop ``_tina_mode`` support for now since we're not testing it.
its_happening
Tyler Goodlet 2020-06-16 14:24:24 -04:00
parent 11a7530d09
commit a92b53d2c1
1 changed files with 29 additions and 38 deletions

View File

@ -193,50 +193,41 @@ class BarItems(pg.GraphicsObject):
@timeit @timeit
def draw_from_data(self, data): def draw_from_data(self, data):
# XXX: overloaded method to allow drawing other candle types self.lines = lines = np.empty_like(data, shape=(data.shape[0]*3,), dtype=object)
# open_sticks = np.empty_like(data, dtype=object)
high_to_low = np.empty_like(data, dtype=object) # close_sticks = np.empty_like(data, dtype=object)
open_sticks = np.empty_like(data, dtype=object)
close_sticks = np.empty_like(data, dtype=object)
with self.painter() as p: with self.painter() as p:
import time # import time
start = time.time() # start = time.time()
for i, q in enumerate(data): for i, q in enumerate(data):
high_to_low[i] = QLineF(q['id'], q['low'], q['id'], q['high']) # indexing here is as per the below comments
open_sticks[i] = QLineF( lines[3*i:3*i+3] = (
q['id'] - self.w, q['open'], q['id'], q['open']) # high_to_low
close_sticks[i] = QtCore.QLineF( QLineF(q['id'], q['low'], q['id'], q['high']),
q['id'] + self.w, q['close'], q['id'], q['close']) # open_sticks
QLineF(q['id'] - self.w, q['open'], q['id'], q['open']),
# high_to_low = np.array( # close_sticks
# [QtCore.QLineF(q.id, q.low, q.id, q.high) for q in Quotes] QtCore.QLineF(q['id'] + self.w, q['close'], q['id'], q['close'])
# ) )
# open_sticks = np.array( # print(f"took {time.time() - start}")
# [QtCore.QLineF(q.id - self.w, q.open, q.id, q.open) # self.lines = lines = np.concatenate([high_to_low, open_sticks, close_sticks])
# for q in Quotes]
# )
# close_sticks = np.array(
# [
# QtCore.QLineF(q.id + self.w, q.close, q.id, q.close)
# for q in Quotes
# ]
# )
print(f"took {time.time() - start}")
self.lines = lines = np.concatenate([high_to_low, open_sticks, close_sticks])
if _tina_mode: if _tina_mode:
long_bars = np.resize(Quotes.close > Quotes.open, len(lines)) pass
short_bars = np.resize(Quotes.close < Quotes.open, len(lines)) # use traditional up/down green/red coloring
ups = lines[long_bars] # long_bars = np.resize(Quotes.close > Quotes.open, len(lines))
downs = lines[short_bars] # short_bars = np.resize(Quotes.close < Quotes.open, len(lines))
# draw "up" bars # ups = lines[long_bars]
p.setPen(self.bull_brush) # downs = lines[short_bars]
p.drawLines(*ups)
# draw "down" bars # # draw "up" bars
p.setPen(self.bear_brush) # p.setPen(self.bull_brush)
p.drawLines(*downs) # p.drawLines(*ups)
# # draw "down" bars
# p.setPen(self.bear_brush)
# p.drawLines(*downs)
else: # piker mode else: # piker mode
p.setPen(self.bull_brush) p.setPen(self.bull_brush)