def test_Ohlc(): tup = Ohlc(open=1, high=2, low=0, close=2) assert len(tup) == 4 vals = list(random_values_generator(count=10)) ohlcs = list(random_ohlc_generator(count=10)) vals5 = 5 * vals ohlcs5 = 5 * ohlcs assert len(vals) == 10 assert len(ohlcs) == 10 assert len(vals5) == 5 * 10 assert len(ohlcs5) == 5 * 10 assert len(Ohlc.from_ohlc_list(ohlcs)) == 4 assert len(Ohlc.from_ohlc_list(ohlcs5)) == 4 # ohlc computation is repearable assert Ohlc.from_values(vals) == Ohlc.from_values(vals5) assert Ohlc.from_ohlc_list(ohlcs) == Ohlc.from_ohlc_list(ohlcs5) # ohlc computation is idempotent for tup in ohlcs: assert Ohlc.from_values(list(tup)) == tup # no zero prices allowed assert 0.0 not in vals for tup in ohlcs: assert 0.0 not in tup
def redraw_candles(self, last_ohlc=None): if self.requires_redraw(): self.visible_ohlc = Ohlc.from_ohlc_list(self.visible_data) self.candles = list(self.draw_candles(self.visible_data)) elif last_ohlc is not None: ohlc = self.data[last_ohlc] self.candles = self.candles + list(self.draw_candles([ohlc])) self._candles_dirty = False
def test_candle_chart(): # test small values app = chart.CandleChart(h=15) n = 500 low = 1.0 high = 10.0 dv = (high - low) / n k = 8 def shuf(l): shuffle(l); return l up = [low + dv * pow(float(v),2.5) for v in range(n)] dn = [up[-1] - dv * pow(float(v),2.5) for v in range(n)] ups = [Ohlc.from_values(shuf(up[i:i+k])) for i in range(0,n,k)] downs = [Ohlc.from_values(shuf(dn[i:i+k])) for i in range(0,n,k)] for o in ups + downs: app.add_ohlc(o) print("printing candle chart:") app.print_lines()
def random_ohlc_generator(*, v_start=0.1, count=float('inf'), step=10, v_max=None, v_min=None): nums = random_values_generator(v_start=v_start, v_max=v_max, v_min=v_min) values = [float(v_start)] i = 0 o = None while i < count: # keep last values from prev. batch to allow open-close transition values = [values[-1]] + [next(nums) for _ in range(step)] i += 1 o = Ohlc.from_values(values, prev=o) yield o
def sBarDown(f, o: Ohlc): return o.close <= (o.low + (o.spread() * f.pctCs))
def sBarUp(f, o: Ohlc): return o.close >= (o.high - (o.spread() * f.pctCs))
def pBarDn(f, o: Ohlc): return o.open < o.high - ( o.spread() * f.pctCp) and o.close < o.high - ( o.spread() * f.pctCp) and o.high >= o.highest(f.pblb)
def pBarUp(f, o: Ohlc): return o.open > o.high - ( o.spread() * f.pctCPO) and o.close > o.high - ( o.spread() * f.pctCPO) and o.low <= o.lowest(f.pblb)
def fill(self, ohlc:Ohlc, height, scale=1.0, offset=0.0): if height <= 0: return [] if self.heikin: ohlc = ohlc.heikin() params = _fill_params(ohlc, scale, offset) chars = self._fill(params, height) return self.colorize(chars, params)