Beispiel #1
0
def test_creation_default_date_range():
    db = sdm.StockDBMgr('./stock_db/test')
    df = db.get_symbol_data('SPY')
    start = datetime.date(2020, 3, 16)
    stop = datetime.date(2020, 3, 20)
    assert df.iloc[0].name.date() < start
    assert df.iloc[-1].name.date() > stop
Beispiel #2
0
def test_get_symbol_data():
    db = sdm.StockDBMgr('./stock_db/test')
    df1 = db.get_symbol_data('SPY')
    df2 = db.get_symbol_data('SPY')
    assert (df1 == df2).all().all()
    assert len(df1) == len(df2)
    assert df1 is df2
Beispiel #3
0
def test_download_data():
    db = sdm.StockDBMgr('./stock_db/test')
    assert 'SPY' not in db._dic
    db.get_symbol_data('SPY')
    assert 'SPY' in db._dic
    db.download_data('SPY')
    assert 'SPY' not in db._dic
Beispiel #4
0
def test_creation_custom_date_range():
    start = datetime.date(2020, 3, 16)
    stop = datetime.date(2020, 3, 20)
    db = sdm.StockDBMgr('./stock_db/test', start, stop)
    df = db.get_symbol_data('SPY')
    assert df.iloc[0].name.date() == start
    assert df.iloc[-1].name.date() == stop
Beispiel #5
0
def test_update_all_symbols():
    db = sdm.StockDBMgr('./stock_db/empty')
    assert 'SPY' not in db._dic
    # Get a single symbol
    db.get_symbol_data('SPY')
    assert 'SPY' in db._dic
    db.update_all_symbols()
    assert 'SPY' not in db._dic
Beispiel #6
0
def test_get_all_symbol_single_data_item():
    db = sdm.StockDBMgr('./stock_db/test')
    df = db.get_all_symbol_single_data_item('Close')
    for s in db.get_all_symbols():
        assert s in df

    df = db.get_all_symbol_single_data_item('BadData')
    assert df is None
Beispiel #7
0
def load_data():
    print("load_data()")

    global dic
    global db

    db = sdm.StockDBMgr(data_dir, start_date, end_date)

    dic = db.get_all_symbol_data()
def test_get_all_symbol_single_data_item():
    db = sdm.StockDBMgr('./stock_db/test')
    df1 = db.get_symbol_data(db.get_all_symbols()[0])
    for p in df1.columns:
        df = db.get_all_symbol_single_data_item(p)
        for s in db.get_all_symbols():
            assert s in df

    df = db.get_all_symbol_single_data_item('BadData')
    assert df is None
Beispiel #9
0
def _main():
    db = sdm.StockDBMgr('../stock_db/test', datetime.date(2017, 1, 1),
                        datetime.date(2018, 1, 1))
    symbol_list = db.get_all_symbols()
    print(symbol_list)

    # Work with first symbol only
    s = symbol_list[0]

    if False:
        # To test caching
        df = db.get_symbol_data(s)
        df = db.get_symbol_data(s)
        db.download_data(s)
        df = db.get_symbol_data(s)
        db.update_all_symbols()
        df = db.get_symbol_data(s)

    if True:
        print("Validating symbols")
        for s in db.get_all_symbols():
            if not db.validate_symbol_data(s):
                print("{} failed validation".format(s))

    if True:
        print("Loading all symbols to a dict")
        # To test caching
        dd = db.get_all_symbol_data()
        dd = db.get_all_symbol_data()
        print(dd.keys())

        df = db.get_all_symbol_single_data_item('Close')
        print(df.head())
        df = db.get_all_symbol_single_data_item('Volume')
        print(df.head())

    if True:
        db = sdm.StockDBMgr('../stock_db/test', datetime.date(2017, 1, 1),
                            datetime.date(2018, 1, 1), False)
        df = db.get_symbol_data(symbol_list[0])
        print(df.describe())
Beispiel #10
0
def indicator_test():
    db = sdm.StockDBMgr('./stock_db/tsx', startdate, enddate)
    print("Loading all symbols...")
    df = db.get_all_symbol_single_data_item('Close')
    print("Loading done.")

    rp = (df.iloc[-1] - df.min()) / (df.max() - df.min())
    rps = 2.0 * rp - 1.0
    rr = (df.max() - df.min()) / df.max()

    t = rps * rr.pow(0.1)

    # Price in the floor (5% tolerance) & 15% drop
    print(t.loc[(rp < 0.05) & (rr > 0.15)])
Beispiel #11
0
def correlation_test():
    db = sdm.StockDBMgr('./stock_db/qt', startdate, enddate)
    df = db.get_all_symbol_single_data_item('Close')
    df.dropna(axis=1, how='any', inplace=True)
    symbols = list(df.columns)
    n = len(symbols)
    dfc = pd.DataFrame(index=symbols, data=np.zeros((n, n)), columns=symbols)

    for s1 in symbols:
        for s2 in symbols:
            c = sps.pearsonr(df.loc[:, s1], df.loc[:, s2])[0]
            dfc.loc[s1, s2] = c

    #print(dfc)

    # Find inverse correlation
    print(dfc.min())
    print(dfc.idxmin())
Beispiel #12
0
def check_db(path):
    # Create data base:
    db = sdm.StockDBMgr('./stock_db/' + path, startdate, enddate)

    # db.update_all_symbols()

    inv = []

    symbolList = db.get_all_symbols()
    print(len(symbolList))
    print(symbolList)

    for s in symbolList:
        if False:
            print("symbol:{}, yield:{}, name:{}".format(
                s, sq.get_dividend_yield(s), sq.get_name(s)))

        if not db.validate_symbol_data(s):
            inv.append(s)
            continue

        # Only applies if recent download...
        if True:
            df = db.get_symbol_data(s)

            t = startdate
            if df is not None and len(df) > 0:
                # t = r[-1].date
                t = df.iloc[-1].name.date()
            else:
                inv.append(s)
                continue

            if (today - t) > datetime.timedelta(4):
                inv.append(s)
                print("%s: len = %d" % (s, len(df)))

    print("Invalid list:")
    for s in inv:
        print(s)
Beispiel #13
0
import datetime
import numpy as np
import matplotlib.dates as mdates
import matplotlib.ticker as mticker
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager

import stock_db_mgr as sdm

startdate = datetime.date(2019, 1, 1)
enddate = datetime.date.today()
ticker = 'SPY'

db = sdm.StockDBMgr('../stock_db/test', startdate, enddate)


def moving_average(x, n, moving_average_type='simple'):
    """Compute an n period moving average.

    type is 'simple' | 'exponential'

    """
    x = np.asarray(x)
    if moving_average_type == 'simple':
        weights = np.ones(n)
    else:
        weights = np.exp(np.linspace(-1., 0., n))

    weights /= weights.sum()

    a = np.convolve(x, weights, mode='full')[:len(x)]
Beispiel #14
0
from pylab import figure, show
from matplotlib.dates import YearLocator, MonthLocator, DateFormatter
import datetime

import stock_db_mgr as sdm


# format the coords message box
def price(x):
    return '$%1.2f' % x


date1 = datetime.date(1995, 1, 1)
date2 = datetime.date(2004, 4, 12)

db = sdm.StockDBMgr('../stock_db/test', date1, date2)

years = YearLocator()  # every year
months = MonthLocator()  # every month
yearsFmt = DateFormatter('%Y')

df = db.get_symbol_data('SPY')

dates = [d for d in df.index]
opens = [o for o in df.loc[:, 'Open'].values]

fig = figure()
ax = fig.add_subplot(111)
ax.plot_date(dates, opens, '-')

# format the ticks
import virtual_account as va
import stock_db_mgr as sdm

db = sdm.StockDBMgr('./stock_db/test')


def test_virtual_account():
    assert db
    a = va.VirtualAccount(100000.0, db.get_all_symbol_data())
    assert a

    assert a.get_cash() == 100000.0
    a.delta_cash(200)
    assert a.get_cash() == 100200.0
    a.delta_cash(-100)
    assert a.get_cash() == 100100.0

    assert len(a.get_all_positions()) == 0
    assert len(a.get_open_positions()) == 0
    assert len(a.get_close_positions()) == 0
    assert len(a.get_all_positions('SPY')) == 0
    assert len(a.get_open_positions('SPY')) == 0
    assert len(a.get_close_positions('SPY')) == 0

    a.buy_at_market(0, 'SPY', 100)

    assert len(a.get_all_positions()) == 1
    assert len(a.get_open_positions()) == 1
    assert len(a.get_close_positions()) == 0
    assert len(a.get_all_positions('SPY')) == 1
    assert len(a.get_open_positions('SPY')) == 1
Beispiel #16
0
def test_get_all_symbol_data():
    db = sdm.StockDBMgr('./stock_db/test')
    dic = db.get_all_symbol_data()
    for s in db.get_all_symbols():
        assert s in dic
def test_get_symbol_data_bad():
    db = sdm.StockDBMgr('./stock_db/bad')
    df = db.get_symbol_data('BAD')
    assert (df is None)
def test_get_symbol_data_bad2():
    db = sdm.StockDBMgr('./stock_db/bad')
    df = db.get_symbol_data('XXXZZZ')  # Invalid ticker
    assert (df is None)
Beispiel #19
0
def test_validate():
    db = sdm.StockDBMgr('./stock_db/test')
    for s in db.get_all_symbols():
        assert db.validate_symbol_data(s)
Beispiel #20
0
def test_get_all_symbols():
    db = sdm.StockDBMgr('./stock_db/test', datetime.date(2017, 1, 1),
                        datetime.date(2018, 1, 1))
    symbol_list = db.get_all_symbols()
    assert len(symbol_list) > 3
Beispiel #21
0
from __future__ import print_function

import datetime
import stock_db_mgr as sdm

import tmxstockquote as tsq

startdate = datetime.date(1900, 1, 1)
today = datetime.date.today()

# Pick one:
#enddate = datetime.date(2018, 2, 22)
enddate = today

# Create data base:
db = sdm.StockDBMgr('./stock_db/qt', startdate, enddate)
#db = sdm.StockDBMgr('./stock_db/tsx')
#db = sdm.StockDBMgr('./stock_db/sp500')
#db = sdm.StockDBMgr('./stock_db/test')

#db.update_all_symbols()

inv = []

symbolList = db.get_all_symbols()
print(len(symbolList))
print(symbolList)

for s in symbolList:
    print("symbol:{}, yield:{}, name:{}".format(s, tsq.get_dividend_yield(s),
                                                tsq.get_name(s)))