Exemple #1
0
def checkDB_for_period():
    conf = Config.Config()
    dbConf = DbConfig.DbConfig()
    connect = psycopg2.connect(database=dbConf.dbname, user=dbConf.user, host=dbConf.address, password=dbConf.password)
    cursor = connect.cursor()
    cursor.itersize = 1000

    candleDiff = conf.candleDiff
    if conf.candlePeriod == 'M':
        candleDiff = candleDiff * 60
    if conf.candlePeriod == 'H':
        candleDiff = candleDiff * 3600

    print('Successfully connected')
    tName = conf.insName.lower()

    cmd = 'SELECT * FROM {0} ORDER BY datetimestamp;'.format(tName)
    cursor.execute(cmd)

    lastTimeStamp = datetime.min
    error = False
    for row in cursor:
        timeStamp = row[0]
        if lastTimeStamp!=datetime.min:
            delta = timeStamp - lastTimeStamp
            if delta != timedelta(seconds=candleDiff):
                print('Error: difference in time is ', delta, row)
                error = True
                break
        lastTimeStamp = timeStamp

    connect.close()
    return error
Exemple #2
0
def fix_missing(delta, row):
    conf = Config.Config()
    dbConf = DbConfig.DbConfig()
    candleDiff = conf.candleDiff
    if conf.candlePeriod == 'M':
        candleDiff = candleDiff * 60
    if conf.candlePeriod == 'H':
        candleDiff = candleDiff * 3600

    tName = conf.insName.lower()
    cmd = ('INSERT INTO {0} VALUES').format(tName)
    cmd_bulk = ''
    dumpback = delta
    mcount = 0
    md = row[0]

    while dumpback > timedelta(seconds=candleDiff):
        #cmdel = ('DELETE FROM {0} WHERE ').format(tName)
        md -= timedelta(seconds=candleDiff)
        #cmdel = cmdel + ("(datetimestamp) = '{0}';".format(md))
        cmd_bulk = cmd_bulk + ("(TIMESTAMP '{0}',{1},{2},{3}),\n".format(
            md, row[1], row[2], row[3]))
        print(md)
        #connect = psycopg2.connect(database=dbConf.dbname, user=dbConf.user, host=dbConf.address, password=dbConf.password)
        #curdel = connect.cursor()
        #print(cmdel)
        #curdel.execute(cmdel)
        #connect.close()

        dumpback -= timedelta(seconds=candleDiff)
        mcount += 1

    connect = psycopg2.connect(database=dbConf.dbname,
                               user=dbConf.user,
                               host=dbConf.address,
                               password=dbConf.password)
    cursor = connect.cursor()

    if len(cmd_bulk) > 0:
        cmd = cmd + cmd_bulk[:-2] + ';'
        cursor.execute(cmd)
        print("Вставка пропушенных. Количество: ", mcount)
        print("Цикл на ", row[0])
        connect.commit()
        connect.close()
    else:
        print("Нет пропущенных")
    print("Вставка пропущенных завершена")
Exemple #3
0
def get_patterns_for_window_and_num(window, length, limit=None):
    conf = Config.Config()
    dbConf = DbConfig.DbConfig()
    connect = psycopg2.connect(database=dbConf.dbname,
                               user=dbConf.user,
                               host=dbConf.address,
                               password=dbConf.password)
    cursor = connect.cursor()

    print('Successfully connected')
    tName = conf.insName.lower()
    cmd = 'SELECT COUNT(*) FROM {0};'.format(tName)
    cursor.execute(cmd)
    totalCount = cursor.fetchone()[0]
    print('Total items count {0}'.format(totalCount))
    cmd = 'SELECT * FROM {0} ORDER BY open_time'.format(tName)
    if limit is None:
        cmd = '{0};'.format(cmd)
    else:
        cmd = '{0} LIMIT {1};'.format(cmd, limit)
    cursor.execute(cmd)

    wl = list()
    patterns = list()
    profits = list()
    indicies = list()
    i = 1
    for row in cursor:
        nextCandle = Candle(open_price=row[0],
                            high_price=row[1],
                            low_price=row[2],
                            close_price=row[3],
                            volume=row[4],
                            open_time=row[5])
        wl.append(nextCandle)
        print('Row {0} of {1}, {2:.3f}% total'.format(
            i, totalCount, 100 * (float(i) / float(totalCount))))
        if len(wl) == window + length:
            # find pattern of 0..length elements
            # that indicates price falls / grows
            # in the next window elements to get profit
            candle = wl[length - 1]
            ind = length + 1
            # take real data only
            if candle.volume != 0:
                while ind <= window + length:
                    iCandle = wl[ind - 1]
                    # define patterns for analyzing iCandle
                    if iCandle.volume != 0:
                        # if iCandle.low_price > candle.high_price:
                        if iCandle.open_price > candle.close_price:
                            # buy pattern
                            p = Pattern(wl[:length], 'buy')
                            patterns.append(p)
                            indicies.append(ind - length)
                            # profits.append(iCandle.low_price - candle.high_price)
                            profits.append(iCandle.open_price -
                                           candle.close_price)
                            break
                        # if iCandle.high_price < candle.low_price:
                        if iCandle.close_price < candle.open_price:
                            # sell pattern
                            p = Pattern(wl[:length], 'sell')
                            patterns.append(p)
                            indicies.append(ind - length)
                            # profits.append(candle.low_price - iCandle.high_price)
                            profits.append(candle.open_price -
                                           iCandle.close_price)
                            break
                    ind = ind + 1
            wl.pop(0)
        i = i + 1
    print('Total patterns: {0}'.format(len(patterns)))
    print('Mean index[after]: {0}'.format(numpy.mean(indicies)))
    print('Mean profit: {0}'.format(numpy.mean(profits)))
    connect.close()
    return patterns
Exemple #4
0
import psycopg2
from StockDataDownloader import StockDataDownloader
from Conf import DbConfig, Config
from datetime import datetime, timedelta
import oandapyV20
import re

step = 60 * 360  # download step, s
daysTotal = 500  # download period, days
dbConf = DbConfig.DbConfig()
conf = Config.Config()
connect = psycopg2.connect(database=dbConf.dbname,
                           user=dbConf.user,
                           host=dbConf.address,
                           password=dbConf.password)
cursor = connect.cursor()

print 'Successfully connected'
cursor.execute("SELECT * FROM pg_tables WHERE schemaname='public';")
tables = list()
for row in cursor:
    tables.append(row[1])
for name in tables:
    cmd = "DROP TABLE " + name
    print cmd
    cursor.execute(cmd)
connect.commit()

tName = conf.insName.lower()
cmd = ('CREATE TABLE public."{0}" (' \
       'datetimestamp TIMESTAMP WITHOUT TIME ZONE NOT NULL,' \