コード例 #1
0
ファイル: test_regression.py プロジェクト: vikibytes/finance
def test_ridge_regression():
    stock_d = testdata()
    ti = TechnicalIndicators(stock_d)

    filename = 'test_N225_ridge.pickle'
    clffile = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..',
                           'clf', filename)

    if os.path.exists(clffile):
        os.remove(clffile)

    clf = Regression(filename)
    ti.calc_ret_index()
    ret = ti.stock['ret_index']
    base = ti.stock_raw['Adj Close'][0]

    train_X, train_y = clf.train(ret, regression_type="Ridge")

    test_y = clf.predict(ret, base)

    expected = 19177.97
    r = round(test_y[0], 2)
    eq_(r, expected)

    if os.path.exists(clffile):
        os.remove(clffile)
コード例 #2
0
def demo(code='N225',
         name='日経平均株価',
         start='2014-01-01',
         days=240,
         csvfile=os.path.join(os.path.dirname(os.path.abspath(__file__)), '..',
                              'test', 'stock_N225.csv'),
         update=False):

    # Handling ti object example.
    io = FileIO()
    stock_d = io.read_from_csv(code, csvfile)
    ti = TechnicalIndicators(stock_d)
    ti.calc_ret_index()

    print(ti.stock['ret_index'].tail(10))
    io.save_data(io.merge_df(stock_d, ti.stock), code, 'demo_')

    # Run analysis code example.
    analysis = Analysis(code=code,
                        name=name,
                        start=start,
                        days=days,
                        csvfile=csvfile,
                        update=True)
    return analysis.run()
コード例 #3
0
ファイル: demo.py プロジェクト: MovingAverage/finance
def demo(code='N225',
         name='日経平均株価',
         start='2014-01-01',
         days=240,
         csvfile=os.path.join(os.path.dirname(
             os.path.abspath(__file__)),
             '..',
             'test',
             'stock_N225.csv'),
         update=False):

    # Handling ti object example.
    io = FileIO()
    stock_d = io.read_from_csv(code,
                               csvfile)
    ti = TechnicalIndicators(stock_d)
    ti.calc_ret_index()

    print(ti.stock['ret_index'].tail(10))
    io.save_data(io.merge_df(stock_d, ti.stock),
                 code, 'demo_')

    # Run analysis code example.
    analysis = Analysis(code=code,
                        name=name,
                        start=start,
                        days=days,
                        csvfile=csvfile,
                        update=True)
    return analysis.run()
コード例 #4
0
ファイル: test_regression.py プロジェクト: hnjun7802/finance
def test_ridge_regression():
    stock_d = testdata()
    ti = TechnicalIndicators(stock_d)

    filename = "test_N225_ridge.pickle"
    clffile = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "clf", filename)

    if os.path.exists(clffile):
        os.remove(clffile)

    clf = Regression(filename)
    ti.calc_ret_index()
    ret = ti.stock["ret_index"]
    base = ti.stock_raw["Adj Close"][0]

    train_X, train_y = clf.train(ret, regression_type="Ridge")

    test_y = clf.predict(ret, base)

    expected = 19177.97
    r = round(test_y[0], 2)
    eq_(r, expected)

    if os.path.exists(clffile):
        os.remove(clffile)
コード例 #5
0
ファイル: test_classifier.py プロジェクト: vikibytes/finance
def test_classify_by_randomforest():
    stock_d = testdata()
    ti = TechnicalIndicators(stock_d)

    filename = 'test_N225_randomforest.pickle'
    clffile = os.path.join(os.path.dirname(
                           os.path.abspath(__file__)),
                           '..', 'clf',
                           filename)

    if os.path.exists(clffile):
        os.remove(clffile)

    clf = Classifier(filename)
    ti.calc_ret_index()
    ret = ti.stock['ret_index']

    train_X, train_y = clf.train(ret, classifier="Random Forest")

    eq_(filename, os.path.basename(clf.filename))

    r = round(train_X[-1][-1], 5)
    expected = 1.35486
    eq_(r, expected)

    r = round(train_X[0][0], 5)
    expected = 1.08871
    eq_(r, expected)

    expected = 14
    r = len(train_X[0])
    eq_(r, expected)

    expected = 120
    r = len(train_X)
    eq_(r, expected)

    expected = [1, 0, 0, 0, 1, 1, 0, 0, 0, 0,
                0, 0, 1, 0, 0, 1, 0, 1, 0, 1,
                1, 0, 1, 1, 1, 1, 1, 0, 1, 0,
                1, 1, 1, 1, 0, 1, 0, 1, 1, 0,
                1, 0, 0, 1, 1, 1, 1, 1, 1, 1,
                0, 0, 0, 1, 0, 0, 1, 1, 1, 1,
                1, 0, 1, 0, 0, 0, 0, 0, 0, 1,
                1, 1, 0, 0, 1, 0, 1, 1, 0, 1,
                1, 0, 1, 1, 0, 1, 0, 0, 1, 0,
                1, 1, 0, 0, 1, 0, 1, 0, 1, 1,
                1, 1, 1, 0, 1, 1, 1, 0, 0, 1,
                1, 0, 0, 1, 1, 1, 0, 1, 1, 0]

    for r, e in zip(train_y, expected):
        eq_(r, e)

    expected = 1
    test_y = clf.classify(ret)
    assert(test_y[0] == 0 or test_y[0] == 1)

    if os.path.exists(clffile):
        os.remove(clffile)
コード例 #6
0
def test_calc_rsi():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    rsi = ti.calc_rsi(timeperiod=14)

    expected = 74.98
    result = rsi.ix['2015-03-20', 'rsi14']
    result = round(result, 2)
    eq_(expected, result)
    return rsi
コード例 #7
0
def test_calc_ultosc():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    ultosc = ti.calc_ultosc()

    expected = 72.56
    result = ultosc.ix['2015-03-20', 'ultosc']
    result = round(result, 2)
    eq_(expected, result)
    return ultosc
コード例 #8
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_sar():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    sar = ti.calc_sar()

    expected = 18896.79
    result = sar.ix['2015-03-20', 'sar']
    result = round(result, 2)
    eq_(expected, result)
    return sar
コード例 #9
0
def test_calc_volume_rate():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    vr = ti.calc_volume_rate()

    expected = 21.84
    result = vr.ix['2015-03-19', 'v_rate']
    result = round(result, 2)
    eq_(expected, result)
    return vr
コード例 #10
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_rsi():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    rsi = ti.calc_rsi(timeperiod=14)

    expected = 74.98
    result = rsi.ix['2015-03-20', 'rsi14']
    result = round(result, 2)
    eq_(expected, result)
    return rsi
コード例 #11
0
def test_calc_momentum():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    mom = ti.calc_momentum(timeperiod=10)

    expected = 589.22
    result = mom.ix['2015-03-20', 'mom10']
    result = round(result, 2)
    eq_(expected, result)
    return mom
コード例 #12
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_natr():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    natr = ti.calc_natr()

    expected = 1.07
    result = natr.ix['2015-03-20', 'natr']
    result = round(result, 2)
    eq_(expected, result)
    return natr
コード例 #13
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_atr():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    atr = ti.calc_atr()

    expected = 208.40
    result = atr.ix['2015-03-20', 'atr']
    result = round(result, 2)
    eq_(expected, result)
    return atr
コード例 #14
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_ret_index():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    ret_index = ti.calc_ret_index()

    expected = 1.36
    result = ret_index.ix['2015-03-20', 'ret_index']
    result = round(result, 2)
    eq_(expected, result)
    return ret_index
コード例 #15
0
def test_calc_ret_index():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    ret_index = ti.calc_ret_index()

    expected = 1.36
    result = ret_index.ix['2015-03-20', 'ret_index']
    result = round(result, 2)
    eq_(expected, result)
    return ret_index
コード例 #16
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_roc():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    roc = ti.calc_roc(timeperiod=10)

    expected = 3.11
    result = roc.ix['2015-03-20', 'roc10']
    result = round(result, 2)
    eq_(expected, result)
    return roc
コード例 #17
0
def test_calc_roc():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    roc = ti.calc_roc(timeperiod=10)

    expected = 3.11
    result = roc.ix['2015-03-20', 'roc10']
    result = round(result, 2)
    eq_(expected, result)
    return roc
コード例 #18
0
def test_calc_willr():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    willr = ti.calc_willr(timeperiod=14)

    expected = -0.53
    result = willr.ix['2015-03-20', 'willr14']
    result = round(result, 2)
    eq_(expected, result)
    return willr
コード例 #19
0
def test_calc_sar():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    sar = ti.calc_sar()

    expected = 18896.79
    result = sar.ix['2015-03-20', 'sar']
    result = round(result, 2)
    eq_(expected, result)
    return sar
コード例 #20
0
def test_calc_natr():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    natr = ti.calc_natr()

    expected = 1.07
    result = natr.ix['2015-03-20', 'natr']
    result = round(result, 2)
    eq_(expected, result)
    return natr
コード例 #21
0
def test_calc_atr():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    atr = ti.calc_atr()

    expected = 208.40
    result = atr.ix['2015-03-20', 'atr']
    result = round(result, 2)
    eq_(expected, result)
    return atr
コード例 #22
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_momentum():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    mom = ti.calc_momentum(timeperiod=10)

    expected = 589.22
    result = mom.ix['2015-03-20', 'mom10']
    result = round(result, 2)
    eq_(expected, result)
    return mom
コード例 #23
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_ultosc():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    ultosc = ti.calc_ultosc()

    expected = 72.56
    result = ultosc.ix['2015-03-20', 'ultosc']
    result = round(result, 2)
    eq_(expected, result)
    return ultosc
コード例 #24
0
def test_calc_mfi():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    mfi = ti.calc_mfi()

    expected = 62.47
    result = mfi.ix['2015-03-20', 'mfi14']
    result = round(result, 2)
    eq_(expected, result)
    return mfi
コード例 #25
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_willr():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    willr = ti.calc_willr(timeperiod=14)

    expected = -0.53
    result = willr.ix['2015-03-20', 'willr14']
    result = round(result, 2)
    eq_(expected, result)
    return willr
コード例 #26
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_cci():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    cci = ti.calc_cci()

    expected = 104.27
    result = cci.ix['2015-03-20', 'cci14']
    result = round(result, 2)
    eq_(expected, result)
    return cci
コード例 #27
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_mfi():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    mfi = ti.calc_mfi()

    expected = 62.47
    result = mfi.ix['2015-03-20', 'mfi14']
    result = round(result, 2)
    eq_(expected, result)
    return mfi
コード例 #28
0
def test_calc_cci():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    cci = ti.calc_cci()

    expected = 104.27
    result = cci.ix['2015-03-20', 'cci14']
    result = round(result, 2)
    eq_(expected, result)
    return cci
コード例 #29
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_volume_rate():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    vr = ti.calc_volume_rate()

    expected = 21.84
    result = vr.ix['2015-03-19', 'v_rate']
    result = round(result, 2)
    eq_(expected, result)
    return vr
コード例 #30
0
def test_calc_vol():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    rets = ti.calc_ret_index()
    vol = ti.calc_vol(rets['ret_index'])

    expected = 1.56
    result = vol.ix['2015-03-20', 'vol']
    result = round(result, 2)
    eq_(expected, result)
    return vol
コード例 #31
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_vol():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    rets = ti.calc_ret_index()
    vol = ti.calc_vol(rets['ret_index'])

    expected = 1.56
    result = vol.ix['2015-03-20', 'vol']
    result = round(result, 2)
    eq_(expected, result)
    return vol
コード例 #32
0
def test_binary_class():
    stock_d = testdata()
    ti = TechnicalIndicators(stock_d)
    ti.calc_ret_index()

    ret_index = ti.stock['ret_index']
    f = Features()
    train_X, train_y = f.binary_class(ret_index, range=90)

    expected = [
        1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0,
        1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0,
        0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0,
        1, 1, 0
    ]
    for r, e in zip(train_y, expected):
        eq_(r, e)

    r = round(train_X[-1][-1], 5)
    expected = 1.35486
    eq_(r, expected)

    r = round(train_X[0][0], 5)
    expected = 1.19213
    eq_(r, expected)

    expected = 14
    r = len(train_X[0])
    eq_(r, expected)

    expected = 75
    r = len(train_X)
    eq_(r, expected)

    train_X, train_y = f.binary_class(ret_index)

    expected = 0
    eq_(train_y[0], expected)

    expected = 1
    eq_(len(train_y), expected)

    r = round(train_X[0][0], 5)
    expected = 1.30311
    eq_(r, expected)

    expected = 14
    r = len(train_X[0])
    eq_(r, expected)

    expected = 1
    r = len(train_X)
    eq_(r, expected)
コード例 #33
0
def test_calc_bbands():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    bbands = ti.calc_bbands()

    expected = [19661.0, 19436.0, 19210.0]
    result = (bbands.ix['2015-03-20', 'upperband'],
              bbands.ix['2015-03-20', 'middleband'], bbands.ix['2015-03-20',
                                                               'lowerband'])
    result = [round(x, 0) for x in result]
    eq_(expected, result)
    return bbands
コード例 #34
0
def test_binary_class():
    stock_d = testdata()
    ti = TechnicalIndicators(stock_d)
    ti.calc_ret_index()

    ret_index = ti.stock['ret_index']
    f = Features()
    train_X, train_y = f.binary_class(ret_index, range=90)

    expected = [1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1,
                0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1,
                0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1,
                1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0,
                1, 1, 0]
    for r, e in zip(train_y, expected):
        eq_(r, e)

    r = round(train_X[-1][-1], 5)
    expected = 1.35486
    eq_(r, expected)

    r = round(train_X[0][0], 5)
    expected = 1.19213
    eq_(r, expected)

    expected = 14
    r = len(train_X[0])
    eq_(r, expected)

    expected = 75
    r = len(train_X)
    eq_(r, expected)

    train_X, train_y = f.binary_class(ret_index)

    expected = 0
    eq_(train_y[0], expected)

    expected = 1
    eq_(len(train_y), expected)

    r = round(train_X[0][0], 5)
    expected = 1.30311
    eq_(r, expected)

    expected = 14
    r = len(train_X[0])
    eq_(r, expected)

    expected = 1
    r = len(train_X)
    eq_(r, expected)
コード例 #35
0
def test_calc_macd():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    macd = ti.calc_macd()

    expected = [383., 346., 37.]
    result = (macd.ix['2015-03-20',
                      'macd'], macd.ix['2015-03-20',
                                       'macdsignal'], macd.ix['2015-03-20',
                                                              'macdhist'])
    result = [round(x, 0) for x in result]
    eq_(expected, result)
    return macd
コード例 #36
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_macd():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    macd = ti.calc_macd()

    expected = [383.,
                346.,
                37.]
    result = (macd.ix['2015-03-20', 'macd'],
              macd.ix['2015-03-20', 'macdsignal'],
              macd.ix['2015-03-20', 'macdhist'])
    result = [round(x, 0) for x in result]
    eq_(expected, result)
    return macd
コード例 #37
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_bbands():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    bbands = ti.calc_bbands()

    expected = [19661.0,
                19436.0,
                19210.0]
    result = (bbands.ix['2015-03-20', 'upperband'],
              bbands.ix['2015-03-20', 'middleband'],
              bbands.ix['2015-03-20', 'lowerband'])
    result = [round(x, 0) for x in result]
    eq_(expected, result)
    return bbands
コード例 #38
0
def test_calc_sma():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    sma = ti.calc_sma()
    sma = ti.calc_sma(timeperiod=25)
    sma = ti.calc_sma(timeperiod=75)

    expected = [19453., 18791., 17902.]
    result = (sma.ix['2015-03-20',
                     'sma5'], sma.ix['2015-03-20',
                                     'sma25'], sma.ix['2015-03-20', 'sma75'])
    result = [round(x, 0) for x in result]
    eq_(expected, result)
    return sma
コード例 #39
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_tr():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    tr = ti.calc_tr()

    expected = 148.81
    result = tr.ix['2015-03-20', 'tr']
    result = round(result, 2)
    eq_(expected, result)

    expected = 0.76
    result = tr.ix['2015-03-20', 'vl']
    result = round(result, 2)
    eq_(expected, result)
    return tr
コード例 #40
0
def test_calc_tr():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    tr = ti.calc_tr()

    expected = 148.81
    result = tr.ix['2015-03-20', 'tr']
    result = round(result, 2)
    eq_(expected, result)

    expected = 0.76
    result = tr.ix['2015-03-20', 'vl']
    result = round(result, 2)
    eq_(expected, result)
    return tr
コード例 #41
0
def test_calc_ewma():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    ewma = ti.calc_ewma()
    ewma = ti.calc_ewma(span=25)
    ewma = ti.calc_ewma(span=75)

    expected = [19429., 18821., 17991.]
    result = (ewma.ix['2015-03-20',
                      'ewma5'], ewma.ix['2015-03-20',
                                        'ewma25'], ewma.ix['2015-03-20',
                                                           'ewma75'])
    result = [round(x, 0) for x in result]
    eq_(expected, result)
    result = [round(x, 0) for x in result]
    return ewma
コード例 #42
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_sma():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    sma = ti.calc_sma()
    sma = ti.calc_sma(timeperiod=25)
    sma = ti.calc_sma(timeperiod=75)

    expected = [19453.,
                18791.,
                17902.]
    result = (sma.ix['2015-03-20', 'sma5'],
              sma.ix['2015-03-20', 'sma25'],
              sma.ix['2015-03-20', 'sma75'])
    result = [round(x, 0) for x in result]
    eq_(expected, result)
    return sma
コード例 #43
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_stochf():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    stochf = ti.calc_stochf()

    expected = 98.46
    result = stochf.ix['2015-03-20', 'fastk']
    result = round(result, 2)
    eq_(expected, result)

    expected = 93.79
    result = stochf.ix['2015-03-20', 'fastd']
    result = round(result, 2)
    eq_(expected, result)

    return stochf
コード例 #44
0
def test_calc_stoch():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    stoch = ti.calc_stoch()

    expected = 93.79
    result = stoch.ix['2015-03-20', 'slowk']
    result = round(result, 2)
    eq_(expected, result)

    expected = 93.32
    result = stoch.ix['2015-03-20', 'slowd']
    result = round(result, 2)
    eq_(expected, result)

    return stoch
コード例 #45
0
def test_calc_stochf():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    stochf = ti.calc_stochf()

    expected = 98.46
    result = stochf.ix['2015-03-20', 'fastk']
    result = round(result, 2)
    eq_(expected, result)

    expected = 93.79
    result = stochf.ix['2015-03-20', 'fastd']
    result = round(result, 2)
    eq_(expected, result)

    return stochf
コード例 #46
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_stoch():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    stoch = ti.calc_stoch()

    expected = 93.79
    result = stoch.ix['2015-03-20', 'slowk']
    result = round(result, 2)
    eq_(expected, result)

    expected = 93.32
    result = stoch.ix['2015-03-20', 'slowd']
    result = round(result, 2)
    eq_(expected, result)

    return stoch
コード例 #47
0
ファイル: test_ti.py プロジェクト: MovingAverage/finance
def test_calc_ewma():
    stock = testdata()
    ti = TechnicalIndicators(stock)
    ewma = ti.calc_ewma()
    ewma = ti.calc_ewma(span=25)
    ewma = ti.calc_ewma(span=75)

    expected = [19429.,
                18821.,
                17991.]
    result = (ewma.ix['2015-03-20', 'ewma5'],
              ewma.ix['2015-03-20', 'ewma25'],
              ewma.ix['2015-03-20', 'ewma75'])
    result = [round(x, 0) for x in result]
    eq_(expected, result)
    result = [round(x, 0) for x in result]
    return ewma
コード例 #48
0
ファイル: analysis.py プロジェクト: hnjun7802/finance
    def run(self):
        io = FileIO()
        will_update = self.update

        if self.csvfile:
            stock_tse = io.read_from_csv(self.code, self.csvfile)

            msg = "".join(["Read data from csv: ", self.code, " Records: ", str(len(stock_tse))])
            print(msg)

            if self.update and len(stock_tse) > 0:
                index = pd.date_range(start=stock_tse.index[-1], periods=2, freq="B")
                ts = pd.Series(None, index=index)
                next_day = ts.index[1]
                t = next_day.strftime("%Y-%m-%d")
                newdata = io.read_data(self.code, start=t, end=self.end)

                msg = "".join(["Read data from web: ", self.code, " New records: ", str(len(newdata))])
                print(msg)
                if len(newdata) < 1:
                    will_update = False
                else:
                    print(newdata.ix[-1, :])

                stock_tse = stock_tse.combine_first(newdata)
                io.save_data(stock_tse, self.code, "stock_")
        else:
            stock_tse = io.read_data(self.code, start=self.start, end=self.end)

            msg = "".join(["Read data from web: ", self.code, " Records: ", str(len(stock_tse))])
            print(msg)

        if stock_tse.empty:
            msg = "".join(["Data empty: ", self.code])
            print(msg)
            return None

        if not self.csvfile:
            io.save_data(stock_tse, self.code, "stock_")

        try:
            stock_d = stock_tse.asfreq("B").dropna()[self.days :]

            ti = TechnicalIndicators(stock_d)

            ti.calc_sma()
            ti.calc_sma(timeperiod=5)
            ti.calc_sma(timeperiod=25)
            ti.calc_sma(timeperiod=50)
            ti.calc_sma(timeperiod=75)
            ewma = ti.calc_ewma(span=5)
            ewma = ti.calc_ewma(span=25)
            ewma = ti.calc_ewma(span=50)
            ewma = ti.calc_ewma(span=75)
            bbands = ti.calc_bbands()
            sar = ti.calc_sar()
            draw = Draw(self.code, self.name)

            ret = ti.calc_ret_index()
            ti.calc_vol(ret["ret_index"])
            rsi = ti.calc_rsi(timeperiod=9)
            rsi = ti.calc_rsi(timeperiod=14)
            mfi = ti.calc_mfi()
            roc = ti.calc_roc(timeperiod=10)
            roc = ti.calc_roc(timeperiod=25)
            roc = ti.calc_roc(timeperiod=50)
            roc = ti.calc_roc(timeperiod=75)
            roc = ti.calc_roc(timeperiod=150)
            ti.calc_cci()
            ultosc = ti.calc_ultosc()
            stoch = ti.calc_stoch()
            ti.calc_stochf()
            ti.calc_macd()
            willr = ti.calc_willr()
            ti.calc_momentum(timeperiod=10)
            ti.calc_momentum(timeperiod=25)
            tr = ti.calc_tr()
            ti.calc_atr()
            ti.calc_natr()
            vr = ti.calc_volume_rate()

            ret_index = ti.stock["ret_index"]
            clf = Classifier(self.clffile)
            train_X, train_y = clf.train(ret_index, will_update)
            msg = "".join(["Train Records: ", str(len(train_y))])
            print(msg)
            clf_result = clf.classify(ret_index)[0]
            msg = "".join(["Classified: ", str(clf_result)])
            print(msg)
            ti.stock.ix[-1, "classified"] = clf_result

            reg = Regression(self.regfile, alpha=1, regression_type="Ridge")
            train_X, train_y = reg.train(ret_index, will_update)
            msg = "".join(["Train Records: ", str(len(train_y))])
            base = ti.stock_raw["Adj Close"][0]
            reg_result = int(reg.predict(ret_index, base)[0])
            msg = "".join(["Predicted: ", str(reg_result)])
            print(msg)
            ti.stock.ix[-1, "predicted"] = reg_result

            if len(self.reference) > 0:
                ti.calc_rolling_corr(self.reference)
                ref = ti.stock["rolling_corr"]
            else:
                ref = []

            io.save_data(io.merge_df(stock_d, ti.stock), self.code, "ti_")

            draw.plot(
                stock_d,
                ewma,
                bbands,
                sar,
                rsi,
                roc,
                mfi,
                ultosc,
                willr,
                stoch,
                tr,
                vr,
                clf_result,
                reg_result,
                ref,
                axis=self.axis,
                complexity=self.complexity,
            )

            return ti

        except (ValueError, KeyError):
            msg = "".join(["Error occured in ", self.code])
            print(msg)
            return None
コード例 #49
0
ファイル: analysis.py プロジェクト: arippbbc/finance
    def run(self):
        io = FileIO()
        will_update = self.update

        if self.csvfile:
            stock_tse = io.read_from_csv(self.code, self.csvfile)

            msg = "".join([
                "Read data from csv: ", self.code, " Records: ",
                str(len(stock_tse))
            ])
            print(msg)

            if self.update and len(stock_tse) > 0:
                index = pd.date_range(start=stock_tse.index[-1],
                                      periods=2,
                                      freq='B')
                ts = pd.Series(None, index=index)
                next_day = ts.index[1]
                t = next_day.strftime('%Y-%m-%d')
                newdata = io.read_data(self.code, start=t, end=self.end)

                msg = "".join([
                    "Read data from web: ", self.code, " New records: ",
                    str(len(newdata))
                ])
                print(msg)
                if len(newdata) < 1:
                    will_update = False
                else:
                    print(newdata.ix[-1, :])

                stock_tse = stock_tse.combine_first(newdata)
                io.save_data(stock_tse, self.code, 'stock_')
        else:
            stock_tse = io.read_data(self.code, start=self.start, end=self.end)

            msg = "".join([
                "Read data from web: ", self.code, " Records: ",
                str(len(stock_tse))
            ])
            print(msg)

        if stock_tse.empty:
            msg = "".join(["Data empty: ", self.code])
            print(msg)
            return None

        if not self.csvfile:
            io.save_data(stock_tse, self.code, 'stock_')

        try:
            stock_d = stock_tse.asfreq('B').dropna()[self.days:]

            ti = TechnicalIndicators(stock_d)

            ti.calc_sma()
            ti.calc_sma(timeperiod=5)
            ti.calc_sma(timeperiod=25)
            ti.calc_sma(timeperiod=50)
            ti.calc_sma(timeperiod=75)
            ewma = ti.calc_ewma(span=5)
            ewma = ti.calc_ewma(span=25)
            ewma = ti.calc_ewma(span=50)
            ewma = ti.calc_ewma(span=75)
            bbands = ti.calc_bbands()
            sar = ti.calc_sar()
            draw = Draw(self.code, self.fullname)

            ret = ti.calc_ret_index()
            ti.calc_vol(ret['ret_index'])
            rsi = ti.calc_rsi(timeperiod=9)
            rsi = ti.calc_rsi(timeperiod=14)
            mfi = ti.calc_mfi()
            roc = ti.calc_roc(timeperiod=10)
            roc = ti.calc_roc(timeperiod=25)
            roc = ti.calc_roc(timeperiod=50)
            roc = ti.calc_roc(timeperiod=75)
            roc = ti.calc_roc(timeperiod=150)
            ti.calc_cci()
            ultosc = ti.calc_ultosc()
            stoch = ti.calc_stoch()
            ti.calc_stochf()
            ti.calc_macd()
            willr = ti.calc_willr()
            ti.calc_momentum(timeperiod=10)
            ti.calc_momentum(timeperiod=25)
            tr = ti.calc_tr()
            ti.calc_atr()
            ti.calc_natr()
            vr = ti.calc_volume_rate()

            ret_index = ti.stock['ret_index']
            clf = Classifier(self.clffile)
            train_X, train_y = clf.train(ret_index, will_update)
            msg = "".join(["Train Records: ", str(len(train_y))])
            print(msg)
            clf_result = clf.classify(ret_index)[0]
            msg = "".join(["Classified: ", str(clf_result)])
            print(msg)
            ti.stock.ix[-1, 'classified'] = clf_result

            reg = Regression(self.regfile, alpha=1, regression_type="Ridge")
            train_X, train_y = reg.train(ret_index, will_update)
            msg = "".join(["Train Records: ", str(len(train_y))])
            base = ti.stock_raw['Adj Close'][0]
            reg_result = int(reg.predict(ret_index, base)[0])
            msg = "".join(["Predicted: ", str(reg_result)])
            print(msg)
            ti.stock.ix[-1, 'predicted'] = reg_result

            if len(self.reference) > 0:
                ti.calc_rolling_corr(self.reference)
                ref = ti.stock['rolling_corr']
            else:
                ref = []

            io.save_data(io.merge_df(stock_d, ti.stock), self.code, 'ti_')

            draw.plot(stock_d,
                      ewma,
                      bbands,
                      sar,
                      rsi,
                      roc,
                      mfi,
                      ultosc,
                      willr,
                      stoch,
                      tr,
                      vr,
                      clf_result,
                      reg_result,
                      ref,
                      axis=self.axis,
                      complexity=self.complexity)

            return ti

        except (ValueError, KeyError):
            msg = "".join(["Error occured in ", self.code])
            print(msg)
            return None
コード例 #50
0
    def run(self):
        io = FileIO()
        will_update = self.update

        self.logger.info("".join(["Start Analysis: ", self.code]))

        if self.csvfile:
            stock_tse = io.read_from_csv(self.code, self.csvfile)

            self.logger.info("".join([
                "Read data from csv: ", self.code, " Records: ",
                str(len(stock_tse))
            ]))

            if self.update and len(stock_tse) > 0:
                index = pd.date_range(start=stock_tse.index[-1],
                                      periods=2,
                                      freq='B')
                ts = pd.Series(None, index=index)
                next_day = ts.index[1]
                t = next_day.strftime('%Y-%m-%d')
                newdata = io.read_data(self.code, start=t, end=self.end)

                self.logger.info("".join([
                    "Read data from web: ", self.code, " New records: ",
                    str(len(newdata))
                ]))

                if len(newdata) < 1:
                    will_update = False
                else:
                    print(newdata.ix[-1, :])

                stock_tse = stock_tse.combine_first(newdata)
                io.save_data(stock_tse, self.code, 'stock_')
        else:
            stock_tse = io.read_data(self.code, start=self.start, end=self.end)

            self.logger.info("".join([
                "Read data from web: ", self.code, " Records: ",
                str(len(stock_tse))
            ]))

        if stock_tse.empty:

            self.logger.warn("".join(["Data empty: ", self.code]))

            return None

        if not self.csvfile:
            io.save_data(stock_tse, self.code, 'stock_')

        try:
            stock_d = stock_tse.asfreq('B').dropna()[self.minus_days:]

            ti = TechnicalIndicators(stock_d)

            ti.calc_sma()
            ti.calc_sma(timeperiod=5)
            ti.calc_sma(timeperiod=25)
            ti.calc_sma(timeperiod=50)
            ti.calc_sma(timeperiod=75)
            ti.calc_sma(timeperiod=200)
            ewma = ti.calc_ewma(span=5)
            ewma = ti.calc_ewma(span=25)
            ewma = ti.calc_ewma(span=50)
            ewma = ti.calc_ewma(span=75)
            ewma = ti.calc_ewma(span=200)
            bbands = ti.calc_bbands()
            sar = ti.calc_sar()
            draw = Draw(self.code, self.fullname)

            ret = ti.calc_ret_index()
            ti.calc_vol(ret['ret_index'])
            rsi = ti.calc_rsi(timeperiod=9)
            rsi = ti.calc_rsi(timeperiod=14)
            mfi = ti.calc_mfi()
            roc = ti.calc_roc(timeperiod=10)
            roc = ti.calc_roc(timeperiod=25)
            roc = ti.calc_roc(timeperiod=50)
            roc = ti.calc_roc(timeperiod=75)
            roc = ti.calc_roc(timeperiod=150)
            ti.calc_cci()
            ultosc = ti.calc_ultosc()
            stoch = ti.calc_stoch()
            ti.calc_stochf()
            ti.calc_macd()
            willr = ti.calc_willr()
            ti.calc_momentum(timeperiod=10)
            ti.calc_momentum(timeperiod=25)
            tr = ti.calc_tr()
            ti.calc_atr()
            ti.calc_natr()
            vr = ti.calc_volume_rate()

            ret_index = ti.stock['ret_index']
            clf = Classifier(self.clffile)
            train_X, train_y = clf.train(ret_index, will_update)

            self.logger.info("".join(
                ["Classifier Train Records: ",
                 str(len(train_y))]))

            clf_result = clf.classify(ret_index)[0]

            self.logger.info("".join(["Classified: ", str(clf_result)]))

            ti.stock.ix[-1, 'classified'] = clf_result

            reg = Regression(self.regfile, alpha=1, regression_type="Ridge")
            train_X, train_y = reg.train(ret_index, will_update)

            self.logger.info("".join(
                ["Regression Train Records: ",
                 str(len(train_y))]))

            base = ti.stock_raw['Adj Close'][0]
            reg_result = int(reg.predict(ret_index, base)[0])

            self.logger.info("".join(["Predicted: ", str(reg_result)]))

            ti.stock.ix[-1, 'predicted'] = reg_result

            if will_update is True:
                io.save_data(io.merge_df(stock_d, ti.stock), self.code, 'ti_')

            if self.minus_days < -300:
                _prefix = 'long'
            elif self.minus_days >= -60:
                _prefix = 'short'
            else:
                _prefix = 'chart'

            draw.plot(stock_d,
                      _prefix,
                      ewma,
                      bbands,
                      sar,
                      rsi,
                      roc,
                      mfi,
                      ultosc,
                      willr,
                      stoch,
                      tr,
                      vr,
                      clf_result,
                      reg_result,
                      axis=self.axis,
                      complexity=self.complexity)

            self.logger.info("".join(["Finish Analysis: ", self.code]))

            return ti

        except (ValueError, KeyError) as e:
            self.logger.error("".join(
                ["Error occured in ", self.code, " at analysis.py"]))
            self.logger.error("".join(['ErrorType: ', str(type(e))]))
            self.logger.error("".join(['ErrorMessage: ', str(e)]))
            return None
コード例 #51
0
ファイル: test_draw.py プロジェクト: misper/finance
def test_plot():
    stock = testdata()
    draw = Draw("N225", "日経平均株価")

    ti = TechnicalIndicators(stock)

    rsi = ti.calc_rsi(timeperiod=9)
    rsi = ti.calc_rsi(timeperiod=14)
    roc = ti.calc_roc()
    roc = ti.calc_roc(timeperiod=25)
    mfi = ti.calc_mfi()
    ultosc = ti.calc_ultosc()
    stoch = ti.calc_stoch()
    willr = ti.calc_willr()
    tr = ti.calc_tr()
    vr = ti.calc_volume_rate()

    ewma = ti.calc_ewma(span=5)
    ewma = ti.calc_ewma(span=25)
    ewma = ti.calc_ewma(span=50)
    ewma = ti.calc_ewma(span=75)
    bbands = ti.calc_bbands()
    sar = ti.calc_sar()

    draw.plot(stock,
              ewma,
              bbands,
              sar,
              rsi,
              roc,
              mfi,
              ultosc,
              willr,
              stoch,
              tr,
              vr,
              clf_result=0,
              reg_result=5000,
              ref=[])

    eq_(draw.code, 'N225')
    eq_(draw.name, '日経平均株価')

    filename = 'chart_N225.png'
    expected = True
    eq_(expected, os.path.exists(filename))

    if os.path.exists(filename):
        os.remove(filename)
コード例 #52
0
ファイル: test_draw.py プロジェクト: MovingAverage/finance
def test_plot():
    stock = testdata()
    draw = Draw("N225", "日経平均株価")

    ti = TechnicalIndicators(stock)

    rsi = ti.calc_rsi(timeperiod=9)
    rsi = ti.calc_rsi(timeperiod=14)
    roc = ti.calc_roc()
    roc = ti.calc_roc(timeperiod=25)
    mfi = ti.calc_mfi()
    ultosc = ti.calc_ultosc()
    stoch = ti.calc_stoch()
    willr = ti.calc_willr()
    tr = ti.calc_tr()
    vr = ti.calc_volume_rate()

    ewma = ti.calc_ewma(span=5)
    ewma = ti.calc_ewma(span=25)
    ewma = ti.calc_ewma(span=50)
    ewma = ti.calc_ewma(span=75)
    bbands = ti.calc_bbands()
    sar = ti.calc_sar()

    draw.plot(stock, ewma, bbands, sar,
              rsi, roc, mfi, ultosc, willr,
              stoch, tr, vr,
              clf_result=0, reg_result=5000,
              ref=[])

    eq_(draw.code, 'N225')
    eq_(draw.name, '日経平均株価')

    filename = 'chart_N225.png'
    expected = True
    eq_(expected, os.path.exists(filename))

    if os.path.exists(filename):
        os.remove(filename)