예제 #1
0
def calc_all_macd(table, fast=12, slow=26, sign=9):
    log.info('MACD generating for ' + table)

    db = SqliteDB().createIndicator(table, 'MACD', 'A', fast, slow, sign)
    prices = SqliteDB().getAllPrices(table)
    macds = calc_macd(prices, fast, slow, sign)

    for i in range(len(prices)):
        db.addIndicate(prices[i]['dtlong'], macds['macd'][i], macds['sign'][i],
                       macds['fast'][i], macds['slow'][i])
    db.commit()

    log.info('MACD done')
예제 #2
0
파일: rsi.py 프로젝트: zjw0358/pymisc
def calc_all_rsi(table, period=14):
    log.info('RSI generating for ' + table)

    db = SqliteDB().createIndicator(table, 'RSI', 'A', period)
    prices = SqliteDB().getAllPrices(table)
    rsis = calc_rsi(prices, period)

    for i in range(len(prices)):
        db.addIndicate(prices[i]['dtlong'], rsis['rsi'][i], rsis['up'][i],
                       rsis['down'][i])
    db.commit()

    log.info('RSI done')
예제 #3
0
def calc_all_kdj(table, kPeriod=5, dPeriod=3, slowing=3):
    log.info('KDJ generating for ' + table)

    db = SqliteDB().createIndicator(table, 'KDJ', 'A', kPeriod, dPeriod,
                                    slowing)
    prices = SqliteDB().getAllPrices(table)

    kds = calc_kd(prices, kPeriod, dPeriod, slowing)

    for i in range(len(prices)):
        db.addIndicate(prices[i]['dtlong'], kds['k'][i], kds['d'][i])
    db.commit()

    log.info('KDJ done')
예제 #4
0
def calc_all_bolling(table, period=20, deviate=2):
    log.info('Bolling generating for ' + table)

    db = SqliteDB().createIndicator(table, 'BOLLING', 'A', period, deviate)
    prices = SqliteDB().getAllPrices(table)
    bollings = calc_bolling(prices, period, deviate)

    for i in range(len(prices)):
        db.addIndicate(prices[i]['dtlong'], bollings['boll'][i],
                       bollings['mean'][i], bollings['upper'][i],
                       bollings['lower'][i], bollings['std'][i])

    db.commit()
    log.info('Bolling done')
예제 #5
0
def tally(ptype, price):
    db = SqliteDB()
    ret = ''

    dLong = time.mktime(price['dt'].timetuple())
    dDate = price['dt'].strftime('%Y-%m-%d')
    dTime = price['dt'].strftime('%H:%M:%S')

    db.addPrice((ptype, dLong, dDate, dTime, price['p'], price['per'],
                 price['p0'], '', dLong, dDate, dTime))

    # calculate the percentage
    percent0 = price['per']

    # get the price of 3 minutes ago
    price3 = db.getPrice(ptype, dLong - 180)
    percent3 = 0
    if price3:
        log.debug(ptype + ',price3,' + str(price3[3]) + ',' + str(price3[0]))
        price3 = price3[0]
        percent3 = round((price['p'] - price3) * 100 / price3, 3)

    # get the price of 30 minutes ago
    price30 = db.getPrice(ptype, dLong - 1800)
    percent30 = 0
    if price30:
        log.debug(ptype + ',price30,' + str(price30[3]) + ',' +
                  str(price30[0]))
        price30 = price30[0]
        percent30 = round((price['p'] - price30) * 100 / price30, 3)

    # get last message information
    notper0 = db.getNotice(ptype, 0, dDate)
    log.info(ptype + ', percentage 0: ' + str(percent0) + ', last notice: ' +
             str(notper0))
    #print notper0
    if abs(percent0 - notper0) >= 1:
        ret = ptype + '0,' + str(price['p']) + ',' + str(percent0) + '%;\n'
        db.addNotice(
            (ptype, 0, dLong, dDate, dTime, price['p'], percent0, ret, ''))

    notcount30 = db.getNoticeCount(ptype, 30, dLong - 1800)
    log.info(ptype + ', percentage 30: ' + str(percent30) +
             ', notice in 30 minutes: ' + str(notcount30))
    if notcount30 == 0 and abs(percent30) >= 1:
        ret = ret + ptype + '30,' + str(
            price['p']) + ',' + str(percent30) + '%;\n'
        db.addNotice(
            (ptype, 30, dLong, dDate, dTime, price['p'], percent30, ret, ''))

    notcount3 = db.getNoticeCount(ptype, 3, dLong - 180)
    log.info(ptype + ', percentage 3: ' + str(percent3) +
             ', notice in 3 minutes: ' + str(notcount3))
    if notcount3 == 0 and abs(percent3) >= 0.5:
        ret = ret + ptype + '3,' + str(
            price['p']) + ',' + str(percent3) + '%;\n'
        db.addNotice(
            (ptype, 3, dLong, dDate, dTime, price['p'], percent3, ret, ''))

    return ret
예제 #6
0
def latestPrices():
    msg = datetime.datetime.now().strftime('%m-%d %H:%M') + ':\n'
    dtypes = ['AGG', 'AGTD', 'SHDX']

    db = SqliteDB()
    dtLong = long(time.time())
    for dtype in dtypes:
        price = db.getPrice(dtype, dtLong)
        msg = msg + dtype + ':' + str(price[0]) + ',' + str(
            price[1]) + '%,' + str(price[2]) + ';\n'

    log.info('latest prices: ' + msg)
    return msg
예제 #7
0
def calc_all_ma(table, matype, period, weight=0):
    log.info('MA generating ' + matype + ' for ' + table)

    db = SqliteDB().createIndicator(table, 'MA', matype, period, weight)
    prices = SqliteDB().getAllPrices(table)
    ps = [p['close'] for p in prices]

    if matype == 'MA':
        mas = calc_ma(ps, period)
    elif matype == 'EMA':
        mas = calc_ema(ps, period)
    elif matype == 'SMA':
        mas = calc_sma(ps, period)
    elif matype == 'LWMA':
        mas = calc_lwma(ps, period)

    for i in range(len(prices)):
        db.addIndicate(prices[i]['dtlong'], mas[i])
    db.commit()

    log.info('MA done')
    return mas
예제 #8
0
def importTable(table):
    db = SqliteDB()
    prices = importToArray(table)
    for p in prices:
        db.addData(table, (
            p['dtlong'],
            p['date'],
            p['time'],
            p['open'],
            p['high'],
            p['low'],
            p['close'],
            p['vol'],
            p['rmb'],
            p['chan'],
            p['per'],
        ))

    db.commit()
예제 #9
0
파일: main.py 프로젝트: zjw0358/pymisc
def batchMonitor():
    try:
        db = SqliteDB()
        db.cleanOldData()
    except:
        log.exception('dayMonitor Exception Occured!')