def test_from_matrix_w_volume(self): values = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 0]] self.assertListEqual(OHLCVFactory.from_matrix(values), [ OHLCV(1, 2, 3, 4, 5), OHLCV(6, 7, 8, 9, 0), ])
def test_from_matrix2_wo_volume(self): values = [[1, 6], [2, 7], [3, 8], [4, 9]] self.assertListEqual(OHLCVFactory.from_matrix2(values), [ OHLCV(1, 2, 3, 4, None), OHLCV(6, 7, 8, 9, None), ])
def test_from_matrix2_w_time(self): now = datetime.now() values = [[1, 6], [2, 7], [3, 8], [4, 9], [5, 0], [now, now]] self.assertListEqual(OHLCVFactory.from_matrix2(values), [ OHLCV(1, 2, 3, 4, 5, now), OHLCV(6, 7, 8, 9, 0, now), ])
def test_from_matrix_w_time(self): now = datetime.now() values = [[1, 2, 3, 4, 5, now], [6, 7, 8, 9, 0, now]] self.assertListEqual(OHLCVFactory.from_matrix(values), [ OHLCV(1, 2, 3, 4, 5, now), OHLCV(6, 7, 8, 9, 0, now), ])
def test_from_dict_wo_volume(self): values = { 'open': [1, 6], 'high': [2, 7], 'low': [3, 8], 'close': [4, 9] } self.assertListEqual(OHLCVFactory.from_dict(values), [ OHLCV(1, 2, 3, 4, None), OHLCV(6, 7, 8, 9, None), ])
def test_from_dict_w_time(self): now = datetime.now() values = { 'open': [1, 6], 'high': [2, 7], 'low': [3, 8], 'close': [4, 9], 'volume': [5, 0], 'time': [now, now] } self.assertListEqual(OHLCVFactory.from_dict(values), [ OHLCV(1, 2, 3, 4, 5, now), OHLCV(6, 7, 8, 9, 0, now), ])
class TalippTest(unittest.TestCase): OHLCV_TMPL: List[OHLCV] = OHLCVFactory.from_dict({ 'low': [ 9.9, 9.78, 9.5, 10.47, 10.26, 10.4, 10.12, 9.91, 9.4, 9.11, 9.12, 8.5, 8.55, 8.21, 7.34, 7.53, 6.5, 7.04, 8.15, 8.72, 8.6, 8.89, 8.14, 8.24, 8.06, 7.7, 7.87, 7.94, 8.0, 7.37, 7.49, 7.38, 8.05, 8.79, 8.67, 9.16, 8.9, 9.17, 8.6, 8.92, 8.99, 9.11, 9.11, 8.43, 8.42, 9.26, 10.0, 10.19, 10.15, 9.62 ], 'high': [ 11.02, 10.74, 10.65, 11.05, 10.7, 10.73, 11.16, 10.86, 10.29, 10.8, 9.62, 9.35, 9.43, 8.91, 8.84, 7.82, 7.61, 8.84, 9.42, 9.5, 9.29, 9.4, 9.1, 8.51, 8.95, 8.7, 8.95, 8.75, 8.39, 8.28, 7.58, 8.17, 8.83, 9.2, 9.25, 10.1, 9.88, 9.65, 9.32, 9.4, 9.01, 9.36, 9.46, 9.34, 9.4, 10.5, 10.3, 10.86, 10.77, 10.39 ], 'open': [ 10.81, 10.58, 10.07, 10.58, 10.56, 10.4, 10.74, 10.16, 10.29, 9.4, 9.62, 9.35, 8.64, 8.8, 8.31, 7.56, 7.61, 7.04, 8.56, 9.26, 8.95, 9.31, 9.1, 8.51, 8.42, 8.3, 7.87, 7.94, 8.1, 8.08, 7.49, 7.4, 8.09, 8.86, 8.81, 9.16, 9.69, 9.45, 9.18, 9.4, 9.0, 9.11, 9.23, 9.34, 8.49, 9.3, 10.23, 10.29, 10.77, 10.28 ], 'close': [ 10.5, 9.78, 10.46, 10.51, 10.55, 10.72, 10.16, 10.25, 9.4, 9.5, 9.23, 8.5, 8.8, 8.33, 7.53, 7.61, 6.78, 8.6, 9.21, 8.95, 9.22, 9.1, 8.31, 8.37, 8.3, 7.78, 8.05, 8.1, 8.08, 7.49, 7.58, 8.17, 8.83, 8.91, 9.2, 9.76, 9.42, 9.3, 9.32, 9.04, 9.0, 9.33, 9.34, 8.49, 9.21, 10.15, 10.3, 10.59, 10.23, 10.0 ], 'volume': [ 55.03, 117.86, 301.04, 157.94, 39.96, 42.87, 191.95, 55.09, 131.58, 249.69, 77.75, 197.33, 107.93, 35.86, 269.05, 34.18, 209.1, 241.95, 162.86, 112.99, 66.53, 87.5, 349.14, 44.38, 45.79, 139.4, 46.49, 27.45, 16.44, 83.54, 15.08, 60.72, 140.22, 171.6, 209.26, 199.2, 165.77, 61.71, 29.73, 12.93, 4.14, 12.45, 42.23, 133.29, 120.02, 255.3, 111.55, 108.27, 48.29, 81.66 ] }) CLOSE_TMPL: List[float] = [ 10.5, 9.78, 10.46, 10.51, 10.55, 10.72, 10.16, 10.25, 9.4, 9.5, 9.23, 8.5, 8.8, 8.33, 7.53, 7.61, 6.78, 8.6, 9.21, 8.95, 9.22, 9.1, 8.31, 8.37, 8.3, 7.78, 8.05, 8.1, 8.08, 7.49, 7.58, 8.17, 8.83, 8.91, 9.2, 9.76, 9.42, 9.3, 9.32, 9.04, 9.0, 9.33, 9.34, 8.49, 9.21, 10.15, 10.3, 10.59, 10.23, 10.0 ] def assertIndicatorUpdate(self, indicator: Indicator, iterations_no: int = 20): last_indicator_value = indicator[-1] last_input_value = indicator.input_values[-1] for i in range(1, iterations_no): if isinstance(last_input_value, OHLCV): new_val = OHLCV(i + 1, i + 2, i + 3, i + 4, i + 5) else: new_val = i indicator.update_input_value(new_val) indicator.update_input_value(last_input_value) self.assertEqual(last_indicator_value, indicator[-1]) def assertIndicatorDelete(self, indicator: Indicator, iterations_no: int = 20): last_indicator_value = indicator[-1] last_input_value = indicator.input_values[-1] for i in range(1, iterations_no): if isinstance(last_input_value, OHLCV): new_val = OHLCV((i + 1)**2, (i + 3)**2, (i + 5)**2, (i + 7)**2, i**2) else: new_val = i indicator.add_input_value(new_val) for i in range(1, iterations_no): indicator.remove_input_value() # verify that adding and then removing X input values returns the original output value self.assertEqual(last_indicator_value, indicator[-1]) # delete the original last input value and add it back and check the original last output value is returned indicator.remove_input_value() indicator.add_input_value(last_input_value) self.assertEqual(last_indicator_value, indicator[-1]) def assertIndicatorPurgeOldest(self, indicator: Indicator): # purge oldest 5 values purge_size = 5 indicator_copy = indicator[:] indicator.purge_oldest(purge_size) self.assertSequenceEqual(indicator_copy[purge_size:], indicator) # purge all remaining values purge_size = len(indicator) indicator_copy = indicator[:] indicator.purge_oldest(purge_size) self.assertSequenceEqual(indicator_copy[purge_size:], indicator) self.assertSequenceEqual([], indicator)
# remove the last added value ema.remove_input_value() print(f'EMA after removing the last value: {ema}' ) # [3.0, 5.0, 7.0, 4.5, 4.25, 5.125, 6.5625, 8.28125] # purge the oldest input value ema.purge_oldest(1) print(f'EMA after purging the oldest value: {ema}' ) # [5.0, 7.0, 4.5, 4.25, 5.125, 6.5625, 8.28125] # STOCH indicator ([OHLCV] -> [composite]) stoch = Stoch( 5, 3, OHLCVFactory.from_dict({ 'high': [5, 10, 15, 20, 25, 30, 35], 'low': [1, 4, 7, 10, 13, 16, 19], 'close': [3, 9, 8, 19, 18, 17, 19] })) # print result as a list of composite values for 'k' and 'd' output parameters print( f'Stoch(5, 3) composite result: {stoch}' ) # [StochVal(k=70.83333333333333, d=None), StochVal(k=50.0, d=None), StochVal(k=42.857142857142854, d=54.563492063492056)] # print result as lists per output parameters print( f'Stoch(5, 3) decomposed result: {stoch.to_lists()}' ) # {'k': [70.83333333333333, 50.0, 42.857142857142854], 'd': [None, None, 54.563492063492056]} # Indicator chaining sma1 = SMA(3)
import random from talipp.ohlcv import OHLCVFactory from talipp.indicators import AccuDist, ADX, ALMA, AO, Aroon, ATR, BB, BOP, CCI, ChaikinOsc, ChandeKrollStop, CHOP, CoppockCurve, DEMA, DonchianChannels, DPO, EMA, EMV, ForceIndex, HMA, \ Ichimoku, KAMA, KeltnerChannels, KST, KVO, MACD, MassIndex, MeanDev, OBV, PivotsHL, ROC, RSI, ParabolicSAR, SFX, SMA, SMMA, SOBV, \ StdDev, Stoch, StochRSI, TEMA, TRIX, TSI, UO, VTX, VWAP, VWMA, WMA if __name__ == "__main__": close = random.sample(range(1, 10000), 1000) ohlcv = OHLCVFactory.from_matrix2([ random.sample(range(1, 10000), 1000), random.sample(range(1, 10000), 1000), random.sample(range(1, 10000), 1000), random.sample(range(1, 10000), 1000), random.sample(range(1, 10000), 1000) ]) print(f"Last OHLCV: {ohlcv[-1]}") print(f'AccuDist: {AccuDist(ohlcv)[-1]}') print(f'ADX: {ADX(14, 14, ohlcv)[-1]}') print(f'ALMA: {ALMA(9, 0.85, 6, close)[-1]}') print(f'AO: {AO(5, 34, ohlcv)[-1]}') print(f'Aroon: {Aroon(14, ohlcv)[-1]}') print(f'ATR: {ATR(14, ohlcv)[-1]}') print(f'BB: {BB(20, 2, close)[-1]}') print(f'BOP: {BOP(ohlcv)[-1]}') print(f'CCI: {CCI(20, ohlcv)[-1]}') print(f'ChaikinOsc: {ChaikinOsc(3, 10, ohlcv)[-1]}') print(f'ChandeKrollStop: {ChandeKrollStop(10, 2, 9, ohlcv)[-1]}') print(f'CHOP: {CHOP(14, ohlcv)[-1]}')