def test_deep_bfs(self): a = ts.Const(1, count=1) b = ts.Random() c = ts.Curve([1, 2, 3]) d = a + b e = a + c f = b + c g = ts.Print(d) h = ts.Print(e) i = ts.Print(f) def _ids(lst): return set([elem._id for elem in lst]) def _ids_ids(lst_of_list): ret = [] for lst in lst_of_list: ret.append(_ids(lst)) return ret assert _ids(a._deep_bfs(tops_only=True)) == _ids([a, b, c]) assert _ids(b._deep_bfs(tops_only=True)) == _ids([a, b, c]) assert _ids(c._deep_bfs(tops_only=True)) == _ids([a, b, c]) assert _ids(d._deep_bfs(tops_only=True)) == _ids([a, b, c]) assert _ids(e._deep_bfs(tops_only=True)) == _ids([a, b, c]) assert _ids(f._deep_bfs(tops_only=True)) == _ids([a, b, c]) assert _ids(g._deep_bfs(tops_only=True)) == _ids([a, b, c]) assert _ids(h._deep_bfs(tops_only=True)) == _ids([a, b, c]) assert _ids(i._deep_bfs(tops_only=True)) == _ids([a, b, c]) for x in (a, b, c, d, e, f, g, h, i): for y in (a, b, c, d, e, f, g, h, i): assert _ids_ids(x._deep_bfs()) == _ids_ids(y._deep_bfs())
def test_merge(self): def func1(): yield 1 yield 3 def func2(): yield 2 yield 4 yield 6 out = ts.Merge(ts.Print(ts.Func(func1)), ts.Print(ts.Func(func2))) assert ts.run(out) == [(1, 2), (3, 4)]
def test_merge(self): def foo1(): yield 1 yield 3 def foo2(): yield 2 yield 4 yield 6 out = ts.Merge(ts.Print(ts.Foo(foo1)), ts.Print(ts.Foo(foo2))) assert ts.run(out) == [(1, 2), (3, 4)]
def test_construct_streaming(self): # adapted from https://gist.github.com/raddy/bd0e977dc8437a4f8276 # spot, strike, vol, days till expiry, interest rate, call or put (1,-1) spot, strike, vol, dte, rate, cp = sy.symbols('spot strike vol dte rate cp') T = dte / 260. N = syNormal('N', 0.0, 1.0) d1 = (sy.ln(spot / strike) + (0.5 * vol ** 2) * T) / (vol * sy.sqrt(T)) d2 = d1 - vol * sy.sqrt(T) TimeValueExpr = sy.exp(-rate * T) * (cp * spot * cdf(N)(cp * d1) - cp * strike * cdf(N)(cp * d2)) PriceClass = ts.construct_streaming(TimeValueExpr) def strikes(): strike = 205 while strike <= 220: yield strike strike += 2.5 price = PriceClass(spot=tss.Const(210.59), # strike=tss.Print(tss.Const(205), text='strike'), strike=tss.Foo(strikes, interval=1), vol=tss.Const(14.04), dte=tss.Const(4), rate=tss.Const(.2175), cp=tss.Const(-1)) ret = tss.run(tss.Print(price._node)) time.sleep(2) assert len(ret) == 7
def test_if(self): import tributary.streaming as ts assert ts.run( ts.Print( ts.If(ts.Foo(conditionals), ts.Foo(if_stream), ts.Foo(else_stream)))) == [1, 2, 3]
def test_function_order(self): def foo(*args): for _ in range(5): yield _ lazy_node = tl.Node(callable=foo) + 5 # 5 6 7 8 9 streaming_node = ts.Print(ts.Foo(foo) - 5, "streaming:") # -5 -4 -3 -2 -1 out = streaming_node + ts.Print(lazy_node, "lazy:") x = ts.run(out) # 0 2 4 6 8 print(x) assert x == [0, 2, 4, 6, 8]
def test_function(self): def foo(*args): for _ in range(5): yield _ lazy_node = tl.Node(callable=foo) + 5 # 5 6 7 8 9 streaming_node = ts.Print(ts.Foo(foo) - 5, 'streaming:') # -5 -4 -3 -2 -1 out = ts.Print(t.LazyToStreaming(lazy_node), 'lazy:') + streaming_node x = ts.run(out) # 0 2 4 6 8 print(x) assert x == [0, 2, 4, 6, 8]
def test_function(self): def func(*args): for _ in range(5): yield _ lazy_node = tl.Node(value=func) + 5 # 5 6 7 8 9 streaming_node = ts.Print(ts.Func(func) - 5, "streaming:") # -5 -4 -3 -2 -1 out = ts.Print(t.LazyToStreaming(lazy_node), "lazy:") + streaming_node x = ts.run(out) # 0 2 4 6 8 print(x) assert x == [0, 2, 4, 6, 8]
def test_value_order(self): lazy_node = tl.Node(value=0) # 5 6 7 8 9 def foo(lazy_node=lazy_node): for _ in range(5): yield _ lazy_node.setValue(_ + 1) streaming_node = ts.Print(ts.Foo(foo) - 5, "streaming:") # -5 -4 -3 -2 -1 out = streaming_node + ts.Print(lazy_node + 5, "lazy:") x = ts.run(out) # 0 2 4 6 8 print(x) assert x == [0, 2, 4, 6, 8]
def test_value(self): def foo(*args): for _ in range(5): lazy_node.setValue(_ + 1) yield _ lazy_node = tl.Node(value=0) # 5 6 7 8 9 streaming_node = ts.Print(ts.Foo(foo) - 5, "streaming:") # -5 -4 -3 -2 -1 out = ts.Print(t.LazyToStreaming(lazy_node) + 5, "lazy:") + streaming_node x = ts.run(out) # 0 2 4 6 8 print(x) assert x == [0, 2, 4, 6, 8]
def test_print_generator(self): def foo(): yield 1 yield 2 yield 3 yield 4 yield 5 assert ts.run(ts.Print(ts.Timer(foo, count=2))) == [1, 2]
def test_print(self): val = 0 def foo(): nonlocal val val += 1 return val assert ts.run(ts.Print(ts.Timer(foo, count=2))) == [1, 2]
def test_run_stop(self): import time import tributary.streaming as ts async def foo(): while True: yield 1 await asyncio.sleep(1) g = ts.run(ts.Print(ts.Foo(foo)), blocking=False) time.sleep(5) g.stop()
def test_file(self): file = os.path.abspath( os.path.join(os.path.dirname(__file__), "test_file_data.json")) out = ts.Print(ts.FileSource(filename=file, json=False)) assert ts.run(out) == [ '{"A":0.5202935277,"B":-0.1342029162,"C":-0.4063935124,"D":0.7683930309}\n', '{"A":2.4391663877,"B":-0.4531601906,"C":0.5504786851,"D":0.325227506}\n', '{"A":0.3086543475,"B":-0.4699086094,"C":0.030693103,"D":1.4332804114}\n', '{"A":1.0297827499,"B":-1.5501301415,"C":2.3629809104,"D":1.6914496686}\n', '{"A":-0.884150846,"B":-0.103603736,"C":-2.0112105601,"D":0.4678144406}\n', '{"A":-0.5774053104,"B":0.3085597903,"C":0.4117180955,"D":-1.2195844306}\n', '{"A":0.3986720589,"B":-0.7892302474,"C":-0.6073367673,"D":-0.0705578578}\n', '{"A":0.6379109681,"B":-2.8962345925,"C":0.5189689327,"D":0.4448039862}\n', '{"A":0.4370833908,"B":0.7522062612,"C":0.0451172889,"D":-0.8192326352}\n', '{"A":1.1929752083,"B":0.0977595985,"C":0.4772077299,"D":-0.148081604}', ]
def test_rsi(self): vals = pd.DataFrame(pd.util.testing.getTimeSeriesData(20)) adjust = False period = 14 delta = vals["A"].diff().shift(-1) up, down = delta.copy(), delta.copy() up[up < 0] = 0 down[down > 0] = 0 _gain = up.ewm(alpha=1.0 / period, adjust=adjust).mean() _loss = down.abs().ewm(alpha=1.0 / period, adjust=adjust).mean() RS = _gain / _loss rsi = pd.Series(100 - (100 / (1 + RS))).values curve = ts.Curve(vals["A"].tolist()) ret = ts.run(ts.Print(ts.RSI(curve, period=period), "rsi:")) for x, y in zip(ret, rsi): if pd.isna(y): continue print("({}, {})".format(x, y)) assert abs(x - y) < 0.001
def test_file(self): file = os.path.abspath( os.path.join(os.path.dirname(__file__), 'test_file_data.csv')) out = ts.Print(ts.FileSource(filename=file)) assert ts.run(out) == [1, 2, 3, 4]
def test_http(self): out = ts.Print(ts.HTTP(url='https://google.com')) ret = ts.run(out) print(ret) assert len(ret) == 1
def test_http(self): out = ts.Print(ts.HTTPSource(url="https://google.com")) ret = ts.run(out) assert len(ret) == 1
def test_file_json(self): file = os.path.abspath( os.path.join(os.path.dirname(__file__), "test_file_data.json")) out = ts.Print(ts.FileSource(filename=file, json=True)) assert ts.run(out) == _DATA
def test_dagre(self): def foo(): yield 1 assert ts.Dagre(ts.Print(ts.Timer(foo, count=2)))
def test_graphviz(self): def foo(): yield 1 assert ts.GraphViz(ts.Print(ts.Timer(foo, count=2)))