Esempio n. 1
0
def test_unstable_period():
    a = np.arange(10, dtype=float)
    r = func.EMA(a, 3)
    assert_np_arrays_equal(r, [np.nan, np.nan, 1, 2, 3, 4, 5, 6, 7, 8])
    talib.set_unstable_period("EMA", 5)
    r = func.EMA(a, 3)
    assert_np_arrays_equal(r, [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 6, 7, 8])
    talib.set_unstable_period("EMA", 0)
Esempio n. 2
0
def test_CDL3BLACKCROWS():
    o = np.array([39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 40.32, 40.51, 38.09, 35.00, 27.66, 30.80])
    h = np.array([40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 41.69, 40.84, 38.12, 35.50, 31.74, 32.51])
    l = np.array([35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 39.26, 36.73, 33.37, 30.03, 27.03, 28.31])
    c = np.array([40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.46, 37.08, 33.37, 30.03, 31.46, 28.31])

    result = func.CDL3BLACKCROWS(o, h, l, c)
    assert_np_arrays_equal(result, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -100, 0, 0])
Esempio n. 3
0
def test_unstable_period():
    a = np.arange(10, dtype=float)
    r = func.EMA(a, 3)
    assert_np_arrays_equal(r, [np.nan, np.nan, 1, 2, 3, 4, 5, 6, 7, 8])
    talib.set_unstable_period('EMA', 5)
    r = func.EMA(a, 3)
    assert_np_arrays_equal(r, [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 6, 7, 8])
    talib.set_unstable_period('EMA', 0)
Esempio n. 4
0
def test_CDL3BLACKCROWS():
    o = np.array([39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 40.32, 40.51, 38.09, 35.00, 27.66, 30.80])
    h = np.array([40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 41.69, 40.84, 38.12, 35.50, 31.74, 32.51])
    l = np.array([35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 39.26, 36.73, 33.37, 30.03, 27.03, 28.31])
    c = np.array([40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.46, 37.08, 33.37, 30.03, 31.46, 28.31])

    result = func.CDL3BLACKCROWS(o, h, l, c)
    assert_np_arrays_equal(result, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -100, 0, 0])
Esempio n. 5
0
def test_compatibility():
    a = np.arange(10, dtype=float)
    talib.set_compatibility(0)
    r = func.EMA(a, 3)
    assert_np_arrays_equal(r, [np.nan, np.nan, 1, 2, 3, 4, 5, 6, 7, 8])
    talib.set_compatibility(1)
    r = func.EMA(a, 3)
    assert_np_arrays_equal(r, [np.nan, np.nan,1.25,2.125,3.0625,4.03125,5.015625,6.0078125,7.00390625,8.001953125])
    talib.set_compatibility(0)
Esempio n. 6
0
def test_call_doesnt_cache_parameters():
    sma = abstract.Function('SMA', timeperiod=10)

    expected = func.SMA(ford_2012['close'], 20)
    output = sma(ford_2012, timeperiod=20)
    assert_np_arrays_equal(expected, output)

    expected = func.SMA(ford_2012['close'], 10)
    output = sma(ford_2012)
    assert_np_arrays_equal(expected, output)
Esempio n. 7
0
def test_call_doesnt_cache_parameters():
    sma = abstract.Function('SMA', timeperiod=10)

    expected = func.SMA(ford_2012['close'], 20)
    output = sma(ford_2012, timeperiod=20)
    assert_np_arrays_equal(expected, output)

    expected = func.SMA(ford_2012['close'], 10)
    output = sma(ford_2012)
    assert_np_arrays_equal(expected, output)
Esempio n. 8
0
def test_MIN():
    result = func.MIN(series, timeperiod=4)
    i = np.where(~np.isnan(result))[0][0]
    assert len(series) == len(result)
    assert result[i + 1] == 93.780
    assert result[i + 2] == 93.780
    assert result[i + 3] == 92.530
    assert result[i + 4] == 92.530
    values = np.array([np.nan, 5., 4., 3., 5., 7.])
    result = func.MIN(values, timeperiod=2)
    assert_np_arrays_equal(result, [np.nan, np.nan, 4, 3, 3, 5])
Esempio n. 9
0
def test_MIN():
    result = func.MIN(series, timeperiod=4)
    i = np.where(~np.isnan(result))[0][0]
    assert_equals(len(series), len(result))
    assert_equals(result[i + 1], 93.780)
    assert_equals(result[i + 2], 93.780)
    assert_equals(result[i + 3], 92.530)
    assert_equals(result[i + 4], 92.530)
    values = np.array([np.nan, 5., 4., 3., 5., 7.])
    result = func.MIN(values, timeperiod=2)
    assert_np_arrays_equal(result, [np.nan, np.nan, 4, 3, 3, 5])
Esempio n. 10
0
def test_MIN():
    result = func.MIN(series, timeperiod=4)
    i = np.where(~np.isnan(result))[0][0]
    assert_equals(len(series), len(result))
    assert_equals(result[i + 1], 93.780)
    assert_equals(result[i + 2], 93.780)
    assert_equals(result[i + 3], 92.530)
    assert_equals(result[i + 4], 92.530)
    values = np.array([np.nan, 5.0, 4.0, 3.0, 5.0, 7.0])
    result = func.MIN(values, timeperiod=2)
    assert_np_arrays_equal(result, [np.nan, np.nan, 4, 3, 3, 5])
Esempio n. 11
0
def test_MAVP():
    a = np.array(range(10), dtype=float)
    b = np.array([2, 4], dtype=float)
    result = func.MAVP(a, b)
    assert_np_arrays_equal(result, [
        np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan,
        np.nan
    ])
    result = func.MAVP(a, b, minperiod=2, maxperiod=4)
    assert_np_arrays_equal(
        result, [np.nan, np.nan, np.nan, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5])
Esempio n. 12
0
def test_call_first_exception():
    inputs = {'close': np.array([np.nan, np.nan, np.nan])}

    with pytest.raises(Exception, match="inputs are all NaN"):
        abstract.SMA(inputs, timeperiod=2)

    inputs = {'close': np.array([1.0, 2.0, 3.0])}

    output = abstract.SMA(inputs, timeperiod=2)
    expected = np.array([np.nan, 1.5, 2.5])
    assert_np_arrays_equal(expected, output)
Esempio n. 13
0
def test_RSI():
    a = np.array([0.00000024, 0.00000024, 0.00000024,
      0.00000024, 0.00000024, 0.00000023,
      0.00000024, 0.00000024, 0.00000024,
      0.00000024, 0.00000023, 0.00000024,
      0.00000023, 0.00000024, 0.00000023,
      0.00000024, 0.00000024, 0.00000023,
      0.00000023, 0.00000023], dtype='float64')
    result = func.RSI(a, 10)
    assert_np_arrays_equal(result, [np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,0,0,0,0,0,0,0,0,0,0])
    result = func.RSI(a * 100000, 10)
    assert_np_arrays_equal(result, [np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,33.333333333333329,51.351351351351347,39.491916859122398,51.84807024709005,42.25953803191981,52.101824405061215,52.101824405061215,43.043664867691085,43.043664867691085,43.043664867691085])
Esempio n. 14
0
def test_SMA():
    expected = func.SMA(ford_2012['close'], 10)
    assert_np_arrays_equal(expected, abstract.Function('sma', ford_2012, 10).outputs)
    assert_np_arrays_equal(expected, abstract.Function('sma')(ford_2012, 10, price='close'))
    assert_np_arrays_equal(expected, abstract.Function('sma')(ford_2012, timeperiod=10))
    expected = func.SMA(ford_2012['open'], 10)
    assert_np_arrays_equal(expected, abstract.Function('sma', ford_2012, 10, price='open').outputs)
    assert_np_arrays_equal(expected, abstract.Function('sma', price='low')(ford_2012, 10, price='open'))
    assert_np_arrays_not_equal(expected, abstract.Function('sma', ford_2012, 10, price='open')(timeperiod=20))
    assert_np_arrays_not_equal(expected, abstract.Function('sma', ford_2012)(10, price='close'))
    assert_np_arrays_not_equal(expected, abstract.Function('sma', 10)(ford_2012, price='high'))
    assert_np_arrays_not_equal(expected, abstract.Function('sma', price='low')(ford_2012, 10))
Esempio n. 15
0
def test_compatibility():
    a = np.arange(10, dtype=float)
    talib.set_compatibility(0)
    r = func.EMA(a, 3)
    assert_np_arrays_equal(r, [np.nan, np.nan, 1, 2, 3, 4, 5, 6, 7, 8])
    talib.set_compatibility(1)
    r = func.EMA(a, 3)
    assert_np_arrays_equal(r, [
        np.nan, np.nan, 1.25, 2.125, 3.0625, 4.03125, 5.015625, 6.0078125,
        7.00390625, 8.001953125
    ])
    talib.set_compatibility(0)
Esempio n. 16
0
def test_SMA():
    expected = func.SMA(ford_2012["close"], 10)
    assert_np_arrays_equal(expected, abstract.Function("sma", ford_2012, 10).outputs)
    assert_np_arrays_equal(expected, abstract.Function("sma")(ford_2012, 10, price="close"))
    assert_np_arrays_equal(expected, abstract.Function("sma")(ford_2012, timeperiod=10))
    expected = func.SMA(ford_2012["open"], 10)
    assert_np_arrays_equal(expected, abstract.Function("sma", ford_2012, 10, price="open").outputs)
    assert_np_arrays_equal(expected, abstract.Function("sma", price="low")(ford_2012, 10, price="open"))
    assert_np_arrays_not_equal(expected, abstract.Function("sma", ford_2012, 10, price="open")(timeperiod=20))
    assert_np_arrays_not_equal(expected, abstract.Function("sma", ford_2012)(10, price="close"))
    assert_np_arrays_not_equal(expected, abstract.Function("sma", 10)(ford_2012, price="high"))
    assert_np_arrays_not_equal(expected, abstract.Function("sma", price="low")(ford_2012, 10))
Esempio n. 17
0
def test_SMA():
    expected = func.SMA(ford_2012['close'], 10)
    assert_np_arrays_equal(expected, abstract.Function('sma', ford_2012, 10).outputs)
    assert_np_arrays_equal(expected, abstract.Function('sma')(ford_2012, 10, price='close'))
    assert_np_arrays_equal(expected, abstract.Function('sma')(ford_2012, timeperiod=10))
    expected = func.SMA(ford_2012['open'], 10)
    assert_np_arrays_equal(expected, abstract.Function('sma', ford_2012, 10, price='open').outputs)
    assert_np_arrays_equal(expected, abstract.Function('sma', price='low')(ford_2012, 10, price='open'))
    assert_np_arrays_not_equal(expected, abstract.Function('sma', ford_2012, 10, price='open')(timeperiod=20))
    assert_np_arrays_not_equal(expected, abstract.Function('sma', ford_2012)(10, price='close'))
    assert_np_arrays_not_equal(expected, abstract.Function('sma', 10)(ford_2012, price='high'))
    assert_np_arrays_not_equal(expected, abstract.Function('sma', price='low')(ford_2012, 10))
Esempio n. 18
0
def test_MAXINDEX():
    import talib as func
    import numpy as np
    a = np.array(
        [1., 2, 3, 4, 5, 6, 7, 8, 7, 7, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 15])
    b = func.MA(a, 10)
    c = func.MAXINDEX(b, 10)
    assert_np_arrays_equal(
        c,
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 16, 16, 21])
    d = np.array([1., 2, 3])
    e = func.MAXINDEX(d, 10)
    assert_np_arrays_equal(e, [0, 0, 0])
Esempio n. 19
0
def test_call_supports_same_signature_as_func_module():
    adx = abstract.Function('ADX')

    expected = func.ADX(ford_2012['open'], ford_2012['high'], ford_2012['low'])
    output = adx(ford_2012['open'], ford_2012['high'], ford_2012['low'])
    assert_np_arrays_equal(expected, output)

    with assert_raises(TypeError) as e:
        adx(ford_2012['open'], ford_2012['high'], ford_2012['low'], ford_2012['close'])
        assert 'Too many price arguments: expected 3 (open, high, low)' in str(
            e.exception.message)

    with assert_raises(TypeError) as e:
        adx(ford_2012['open'], ford_2012['high'])
        assert 'Not enough price arguments: expected 3 (open, high, low)' in str(
            e.exception.message)
Esempio n. 20
0
def test_call_supports_same_signature_as_func_module():
    adx = abstract.Function('ADX')

    expected = func.ADX(ford_2012['open'], ford_2012['high'], ford_2012['low'])
    output = adx(ford_2012['open'], ford_2012['high'], ford_2012['low'])
    assert_np_arrays_equal(expected, output)

    with assert_raises(TypeError) as e:
        adx(ford_2012['open'], ford_2012['high'], ford_2012['low'],
            ford_2012['close'])
        assert 'Too many price arguments: expected 3 (open, high, low)' in str(
            e.exception.message)

    with assert_raises(TypeError) as e:
        adx(ford_2012['open'], ford_2012['high'])
        assert 'Not enough price arguments: expected 3 (open, high, low)' in str(
            e.exception.message)
Esempio n. 21
0
def test_MOM():
    values = np.array([90.0, 88.0, 89.0])
    result = func.MOM(values, timeperiod=1)
    assert_np_arrays_equal(result, [np.nan, -2, 1])
    result = func.MOM(values, timeperiod=2)
    assert_np_arrays_equal(result, [np.nan, np.nan, -1])
    result = func.MOM(values, timeperiod=3)
    assert_np_arrays_equal(result, [np.nan, np.nan, np.nan])
    result = func.MOM(values, timeperiod=4)
    assert_np_arrays_equal(result, [np.nan, np.nan, np.nan])
Esempio n. 22
0
def test_MOM():
    values = np.array([90.0, 88.0, 89.0])
    result = func.MOM(values, timeperiod=1)
    assert_np_arrays_equal(result, [np.nan, -2, 1])
    result = func.MOM(values, timeperiod=2)
    assert_np_arrays_equal(result, [np.nan, np.nan, -1])
    result = func.MOM(values, timeperiod=3)
    assert_np_arrays_equal(result, [np.nan, np.nan, np.nan])
    result = func.MOM(values, timeperiod=4)
    assert_np_arrays_equal(result, [np.nan, np.nan, np.nan])
Esempio n. 23
0
def test_input_nans():
    a1 = np.arange(10, dtype=float)
    a2 = np.arange(10, dtype=float)
    a2[0] = np.nan
    a2[1] = np.nan
    r1, r2 = func.AROON(a1, a2, 2)
    assert_np_arrays_equal(r1, [np.nan, np.nan, np.nan, np.nan, 0, 0, 0, 0, 0, 0])
    assert_np_arrays_equal(r2, [np.nan, np.nan, np.nan, np.nan, 100, 100, 100, 100, 100, 100])
    r1, r2 = func.AROON(a2, a1, 2)
    assert_np_arrays_equal(r1, [np.nan, np.nan, np.nan, np.nan, 0, 0, 0, 0, 0, 0])
    assert_np_arrays_equal(r2, [np.nan, np.nan, np.nan, np.nan, 100, 100, 100, 100, 100, 100])
Esempio n. 24
0
def test_input_nans():
    a1 = np.arange(10, dtype=float)
    a2 = np.arange(10, dtype=float)
    a2[0] = np.nan
    a2[1] = np.nan
    r1, r2 = func.AROON(a1, a2, 2)
    assert_np_arrays_equal(r1, [np.nan, np.nan, np.nan, np.nan, 0, 0, 0, 0, 0, 0])
    assert_np_arrays_equal(r2, [np.nan, np.nan, np.nan, np.nan, 100, 100, 100, 100, 100, 100])
    r1, r2 = func.AROON(a2, a1, 2)
    assert_np_arrays_equal(r1, [np.nan, np.nan, np.nan, np.nan, 0, 0, 0, 0, 0, 0])
    assert_np_arrays_equal(r2, [np.nan, np.nan, np.nan, np.nan, 100, 100, 100, 100, 100, 100])
Esempio n. 25
0
def test_call_supports_same_signature_as_func_module():
    adx = abstract.Function('ADX')

    expected = func.ADX(ford_2012['open'], ford_2012['high'], ford_2012['low'])
    output = adx(ford_2012['open'], ford_2012['high'], ford_2012['low'])
    assert_np_arrays_equal(expected, output)

    expected_error = re.escape(
        'Too many price arguments: expected 3 (high, low, close)')

    with pytest.raises(TypeError, match=expected_error):
        adx(ford_2012['open'], ford_2012['high'], ford_2012['low'],
            ford_2012['close'])

    expected_error = re.escape(
        'Not enough price arguments: expected 3 (high, low, close)')

    with pytest.raises(TypeError, match=expected_error):
        adx(ford_2012['open'], ford_2012['high'])
Esempio n. 26
0
def test_RSI():
    a = np.array([
        0.00000024, 0.00000024, 0.00000024, 0.00000024, 0.00000024, 0.00000023,
        0.00000024, 0.00000024, 0.00000024, 0.00000024, 0.00000023, 0.00000024,
        0.00000023, 0.00000024, 0.00000023, 0.00000024, 0.00000024, 0.00000023,
        0.00000023, 0.00000023
    ],
                 dtype='float64')
    result = func.RSI(a, 10)
    assert_np_arrays_equal(result, [
        np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan,
        np.nan, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    ])
    result = func.RSI(a * 100000, 10)
    assert_np_arrays_equal(result, [
        np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan,
        np.nan, 33.333333333333329, 51.351351351351347, 39.491916859122398,
        51.84807024709005, 42.25953803191981, 52.101824405061215,
        52.101824405061215, 43.043664867691085, 43.043664867691085,
        43.043664867691085
    ])
Esempio n. 27
0
def test_TEVA():
    size = 50
    df = pl.DataFrame({
        "open":
        np.random.uniform(low=0.0, high=100.0, size=size).astype("float32"),
        "high":
        np.random.uniform(low=0.0, high=100.0, size=size).astype("float32"),
        "low":
        np.random.uniform(low=0.0, high=100.0, size=size).astype("float32"),
        "close":
        np.random.uniform(low=0.0, high=100.0, size=size).astype("float32"),
        "volume":
        np.random.uniform(low=0.0, high=100.0, size=size).astype("float32")
    })
    tema1 = abstract.TEMA(df, timeperiod=9)
    assert isinstance(tema1, pl.Series)
    assert len(tema1) == 50
    inputs = abstract.TEMA.get_input_arrays()
    assert inputs.columns == df.columns
    for column in df.columns:
        assert_np_arrays_equal(inputs[column].to_numpy(),
                               df[column].to_numpy())

    tema2 = abstract.TEMA(df, timeperiod=9)
    assert isinstance(tema2, pl.Series)
    assert len(tema2) == 50
    inputs = abstract.TEMA.get_input_arrays()
    assert inputs.columns == df.columns
    for column in df.columns:
        assert_np_arrays_equal(inputs[column].to_numpy(),
                               df[column].to_numpy())

    assert_np_arrays_equal(tema1.to_numpy(), tema2.to_numpy())
Esempio n. 28
0
def test_MOM():
    values = pl.Series([90.0, 88.0, 89.0])
    result = talib.MOM(values, timeperiod=1)
    assert isinstance(result, pl.Series)
    assert_np_arrays_equal(result.to_numpy(), [np.nan, -2, 1])
    result = talib.MOM(values, timeperiod=2)
    assert isinstance(result, pl.Series)
    assert_np_arrays_equal(result.to_numpy(), [np.nan, np.nan, -1])
    result = talib.MOM(values, timeperiod=3)
    assert isinstance(result, pl.Series)
    assert_np_arrays_equal(result.to_numpy(), [np.nan, np.nan, np.nan])
    result = talib.MOM(values, timeperiod=4)
    assert isinstance(result, pl.Series)
    assert_np_arrays_equal(result.to_numpy(), [np.nan, np.nan, np.nan])
Esempio n. 29
0
def test_MAVP():
    a = np.array([1, 5, 3, 4, 7, 3, 8, 1, 4, 6], dtype=float)
    b = np.array([2, 4, 2, 4, 2, 4, 2, 4, 2, 4], dtype=float)
    result = func.MAVP(a, b, minperiod=2, maxperiod=4)
    assert_np_arrays_equal(
        result,
        [np.nan, np.nan, np.nan, 3.25, 5.5, 4.25, 5.5, 4.75, 2.5, 4.75])
    sma2 = func.SMA(a, 2)
    assert_np_arrays_equal(result[4::2], sma2[4::2])
    sma4 = func.SMA(a, 4)
    assert_np_arrays_equal(result[3::2], sma4[3::2])
    result = func.MAVP(a, b, minperiod=2, maxperiod=3)
    assert_np_arrays_equal(result, [
        np.nan, np.nan, 4, 4, 5.5, 4.666666666666667, 5.5, 4, 2.5,
        3.6666666666666665
    ])
    sma3 = func.SMA(a, 3)
    assert_np_arrays_equal(result[2::2], sma2[2::2])
    assert_np_arrays_equal(result[3::2], sma3[3::2])
Esempio n. 30
0
def test_STOCH():
    # check defaults match
    expected_k, expected_d = func.STOCH(ford_2012['high'], ford_2012['low'], ford_2012['close']) # 5, 3, 0, 3, 0
    got_k, got_d = abstract.Function('stoch', ford_2012).outputs
    assert_np_arrays_equal(expected_k, got_k)
    assert_np_arrays_equal(expected_d, got_d)

    expected_k, expected_d = func.STOCH(ford_2012['high'], ford_2012['low'], ford_2012['close'])
    got_k, got_d = abstract.Function('stoch', ford_2012)(5, 3, 0, 3, 0)
    assert_np_arrays_equal(expected_k, got_k)
    assert_np_arrays_equal(expected_d, got_d)

    expected_k, expected_d = func.STOCH(ford_2012['high'], ford_2012['low'], ford_2012['close'], 15)
    got_k, got_d = abstract.Function('stoch', ford_2012)(15, 5, 0, 5, 0)
    assert_np_arrays_not_equal(expected_k, got_k)
    assert_np_arrays_not_equal(expected_d, got_d)

    expected_k, expected_d = func.STOCH(ford_2012['high'], ford_2012['low'], ford_2012['close'], 15, 5, 1, 5, 1)
    got_k, got_d = abstract.Function('stoch', ford_2012)(15, 5, 1, 5, 1)
    assert_np_arrays_equal(expected_k, got_k)
    assert_np_arrays_equal(expected_d, got_d)
Esempio n. 31
0
def test_doji_candle():
    expected = func.CDLDOJI(ford_2012["open"], ford_2012["high"], ford_2012["low"], ford_2012["close"])
    got = abstract.Function("CDLDOJI").run(ford_2012)
    assert_np_arrays_equal(got, expected)
Esempio n. 32
0
def test_doji_candle():
    expected = func.CDLDOJI(ford_2012['open'], ford_2012['high'],
                            ford_2012['low'], ford_2012['close'])
    got = abstract.Function('CDLDOJI').run(ford_2012)
    assert_np_arrays_equal(got, expected)
Esempio n. 33
0
def test_pandas():
    import pandas
    input_df = pandas.DataFrame(ford_2012)
    input_dict = dict((k, pandas.Series(v)) for k, v in ford_2012.items())

    expected_k, expected_d = func.STOCH(ford_2012['high'], ford_2012['low'],
                                        ford_2012['close'])  # 5, 3, 0, 3, 0
    output = abstract.Function('stoch', input_df).outputs
    assert_true(isinstance(output, pandas.DataFrame))
    assert_np_arrays_equal(expected_k, output['slowk'])
    assert_np_arrays_equal(expected_d, output['slowd'])
    output = abstract.Function('stoch', input_dict).outputs
    assert_true(isinstance(output, list))
    assert_np_arrays_equal(expected_k, output[0])
    assert_np_arrays_equal(expected_d, output[1])

    expected = func.SMA(ford_2012['close'], 10)
    output = abstract.Function('sma', input_df, 10).outputs
    assert_true(isinstance(output, pandas.Series))
    assert_np_arrays_equal(expected, output)
    output = abstract.Function('sma', input_dict, 10).outputs
    assert_true(isinstance(output, np.ndarray))
    assert_np_arrays_equal(expected, output)
Esempio n. 34
0
def test_doji_candle():
    expected = func.CDLDOJI(ford_2012['open'], ford_2012['high'], ford_2012['low'], ford_2012['close'])
    got = abstract.Function('CDLDOJI').run(ford_2012)
    assert_np_arrays_equal(got, expected)
Esempio n. 35
0
def test_STOCH():
    # check defaults match
    expected_k, expected_d = func.STOCH(ford_2012['high'], ford_2012['low'],
                                        ford_2012['close'])  # 5, 3, 0, 3, 0
    got_k, got_d = abstract.Function('stoch', ford_2012).outputs
    assert_np_arrays_equal(expected_k, got_k)
    assert_np_arrays_equal(expected_d, got_d)

    expected_k, expected_d = func.STOCH(ford_2012['high'], ford_2012['low'],
                                        ford_2012['close'])
    got_k, got_d = abstract.Function('stoch', ford_2012)(5, 3, 0, 3, 0)
    assert_np_arrays_equal(expected_k, got_k)
    assert_np_arrays_equal(expected_d, got_d)

    expected_k, expected_d = func.STOCH(ford_2012['high'], ford_2012['low'],
                                        ford_2012['close'], 15)
    got_k, got_d = abstract.Function('stoch', ford_2012)(15, 5, 0, 5, 0)
    assert_np_arrays_not_equal(expected_k, got_k)
    assert_np_arrays_not_equal(expected_d, got_d)

    expected_k, expected_d = func.STOCH(ford_2012['high'], ford_2012['low'],
                                        ford_2012['close'], 15, 5, 1, 5, 1)
    got_k, got_d = abstract.Function('stoch', ford_2012)(15, 5, 1, 5, 1)
    assert_np_arrays_equal(expected_k, got_k)
    assert_np_arrays_equal(expected_d, got_d)
Esempio n. 36
0
def test_MAVP():
    a = pd.Series([1,5,3,4,7,3,8,1,4,6], index=range(10, 20), dtype=float)
    b = pd.Series([2,4,2,4,2,4,2,4,2,4], index=range(20, 30), dtype=float)
    result = talib.MAVP(a, b, minperiod=2, maxperiod=4)
    assert_is_instance(result, pd.Series)
    assert_np_arrays_equal(result.values, [np.nan,np.nan,np.nan,3.25,5.5,4.25,5.5,4.75,2.5,4.75])
    assert_np_arrays_equal(result.index, range(10, 20))
    sma2 = talib.SMA(a, 2)
    assert_is_instance(sma2, pd.Series)
    assert_np_arrays_equal(sma2.index, range(10, 20))
    assert_np_arrays_equal(result.values[4::2], sma2.values[4::2])
    sma4 = talib.SMA(a, 4)
    assert_is_instance(sma4, pd.Series)
    assert_np_arrays_equal(sma4.index, range(10, 20))
    assert_np_arrays_equal(result.values[3::2], sma4.values[3::2])
    result = talib.MAVP(a, b, minperiod=2, maxperiod=3)
    assert_is_instance(result, pd.Series)
    assert_np_arrays_equal(result.values, [np.nan,np.nan,4,4,5.5,4.666666666666667,5.5,4,2.5,3.6666666666666665])
    assert_np_arrays_equal(result.index, range(10, 20))
    sma3 = talib.SMA(a, 3)
    assert_is_instance(sma3, pd.Series)
    assert_np_arrays_equal(sma3.index, range(10, 20))
    assert_np_arrays_equal(result.values[2::2], sma2.values[2::2])
    assert_np_arrays_equal(result.values[3::2], sma3.values[3::2])
Esempio n. 37
0
def test_MAVP():
    a = pl.Series([1, 5, 3, 4, 7, 3, 8, 1, 4, 6], dtype=pl.Float64)
    b = pl.Series([2, 4, 2, 4, 2, 4, 2, 4, 2, 4], dtype=pl.Float64)
    result = talib.MAVP(a, b, minperiod=2, maxperiod=4)
    assert isinstance(result, pl.Series)
    assert_np_arrays_equal(
        result.to_numpy(),
        [np.nan, np.nan, np.nan, 3.25, 5.5, 4.25, 5.5, 4.75, 2.5, 4.75])
    sma2 = talib.SMA(a, 2)
    assert isinstance(sma2, pl.Series)
    assert_np_arrays_equal(result.to_numpy()[4::2], sma2.to_numpy()[4::2])
    sma4 = talib.SMA(a, 4)
    assert isinstance(sma4, pl.Series)
    assert_np_arrays_equal(result.to_numpy()[3::2], sma4.to_numpy()[3::2])
    result = talib.MAVP(a, b, minperiod=2, maxperiod=3)
    assert isinstance(result, pl.Series)
    assert_np_arrays_equal(result.to_numpy(), [
        np.nan, np.nan, 4, 4, 5.5, 4.666666666666667, 5.5, 4, 2.5,
        3.6666666666666665
    ])
    sma3 = talib.SMA(a, 3)
    assert isinstance(sma3, pl.Series)
    assert_np_arrays_equal(result.to_numpy()[2::2], sma2.to_numpy()[2::2])
    assert_np_arrays_equal(result.to_numpy()[3::2], sma3.to_numpy()[3::2])
Esempio n. 38
0
def test_MAVP():
    a = pd.Series([1, 5, 3, 4, 7, 3, 8, 1, 4, 6],
                  index=range(10, 20),
                  dtype=float)
    b = pd.Series([2, 4, 2, 4, 2, 4, 2, 4, 2, 4],
                  index=range(20, 30),
                  dtype=float)
    result = talib.MAVP(a, b, minperiod=2, maxperiod=4)
    assert isinstance(result, pd.Series)
    assert_np_arrays_equal(
        result.values,
        [np.nan, np.nan, np.nan, 3.25, 5.5, 4.25, 5.5, 4.75, 2.5, 4.75])
    assert_np_arrays_equal(result.index, range(10, 20))
    sma2 = talib.SMA(a, 2)
    assert isinstance(sma2, pd.Series)
    assert_np_arrays_equal(sma2.index, range(10, 20))
    assert_np_arrays_equal(result.values[4::2], sma2.values[4::2])
    sma4 = talib.SMA(a, 4)
    assert isinstance(sma4, pd.Series)
    assert_np_arrays_equal(sma4.index, range(10, 20))
    assert_np_arrays_equal(result.values[3::2], sma4.values[3::2])
    result = talib.MAVP(a, b, minperiod=2, maxperiod=3)
    assert isinstance(result, pd.Series)
    assert_np_arrays_equal(result.values, [
        np.nan, np.nan, 4, 4, 5.5, 4.666666666666667, 5.5, 4, 2.5,
        3.6666666666666665
    ])
    assert_np_arrays_equal(result.index, range(10, 20))
    sma3 = talib.SMA(a, 3)
    assert isinstance(sma3, pd.Series)
    assert_np_arrays_equal(sma3.index, range(10, 20))
    assert_np_arrays_equal(result.values[2::2], sma2.values[2::2])
    assert_np_arrays_equal(result.values[3::2], sma3.values[3::2])
Esempio n. 39
0
def test_MOM():
    values = pd.Series([90.0, 88.0, 89.0], index=[10, 20, 30])
    result = talib.MOM(values, timeperiod=1)
    assert isinstance(result, pd.Series)
    assert_np_arrays_equal(result.values, [np.nan, -2, 1])
    assert_np_arrays_equal(result.index, [10, 20, 30])
    result = talib.MOM(values, timeperiod=2)
    assert isinstance(result, pd.Series)
    assert_np_arrays_equal(result.values, [np.nan, np.nan, -1])
    assert_np_arrays_equal(result.index, [10, 20, 30])
    result = talib.MOM(values, timeperiod=3)
    assert isinstance(result, pd.Series)
    assert_np_arrays_equal(result.values, [np.nan, np.nan, np.nan])
    assert_np_arrays_equal(result.index, [10, 20, 30])
    result = talib.MOM(values, timeperiod=4)
    assert isinstance(result, pd.Series)
    assert_np_arrays_equal(result.values, [np.nan, np.nan, np.nan])
    assert_np_arrays_equal(result.index, [10, 20, 30])
Esempio n. 40
0
def test_MAVP():
    a = np.array([1,5,3,4,7,3,8,1,4,6], dtype=float)
    b = np.array([2,4,2,4,2,4,2,4,2,4], dtype=float)
    result = func.MAVP(a, b, minperiod=2, maxperiod=4)
    assert_np_arrays_equal(result, [np.nan,np.nan,np.nan,3.25,5.5,4.25,5.5,4.75,2.5,4.75])
    sma2 = func.SMA(a, 2)
    assert_np_arrays_equal(result[4::2], sma2[4::2])
    sma4 = func.SMA(a, 4)
    assert_np_arrays_equal(result[3::2], sma4[3::2])
    result = func.MAVP(a, b, minperiod=2, maxperiod=3)
    assert_np_arrays_equal(result, [np.nan,np.nan,4,4,5.5,4.666666666666667,5.5,4,2.5,3.6666666666666665])
    sma3 = func.SMA(a, 3)
    assert_np_arrays_equal(result[2::2], sma2[2::2])
    assert_np_arrays_equal(result[3::2], sma3[3::2])
Esempio n. 41
0
def test_pandas():
    import pandas

    input_df = pandas.DataFrame(ford_2012)
    input_dict = dict((k, pandas.Series(v)) for k, v in ford_2012.items())

    expected_k, expected_d = func.STOCH(ford_2012["high"], ford_2012["low"], ford_2012["close"])  # 5, 3, 0, 3, 0
    output = abstract.Function("stoch", input_df).outputs
    assert_true(isinstance(output, pandas.DataFrame))
    assert_np_arrays_equal(expected_k, output["slowk"])
    assert_np_arrays_equal(expected_d, output["slowd"])
    output = abstract.Function("stoch", input_dict).outputs
    assert_true(isinstance(output, list))
    assert_np_arrays_equal(expected_k, output[0])
    assert_np_arrays_equal(expected_d, output[1])

    expected = func.SMA(ford_2012["close"], 10)
    output = abstract.Function("sma", input_df, 10).outputs
    assert_true(isinstance(output, pandas.Series))
    assert_np_arrays_equal(expected, output)
    output = abstract.Function("sma", input_dict, 10).outputs
    assert_true(isinstance(output, np.ndarray))
    assert_np_arrays_equal(expected, output)
Esempio n. 42
0
def test_MOM():
    values = pd.Series([90.0,88.0,89.0], index=[10, 20, 30])
    result = talib.MOM(values, timeperiod=1)
    assert_is_instance(result, pd.Series)
    assert_np_arrays_equal(result.values, [np.nan, -2, 1])
    assert_np_arrays_equal(result.index, [10, 20, 30])
    result = talib.MOM(values, timeperiod=2)
    assert_is_instance(result, pd.Series)
    assert_np_arrays_equal(result.values, [np.nan, np.nan, -1])
    assert_np_arrays_equal(result.index, [10, 20, 30])
    result = talib.MOM(values, timeperiod=3)
    assert_is_instance(result, pd.Series)
    assert_np_arrays_equal(result.values, [np.nan, np.nan, np.nan])
    assert_np_arrays_equal(result.index, [10, 20, 30])
    result = talib.MOM(values, timeperiod=4)
    assert_is_instance(result, pd.Series)
    assert_np_arrays_equal(result.values, [np.nan, np.nan, np.nan])
    assert_np_arrays_equal(result.index, [10, 20, 30])