예제 #1
0
파일: code.py 프로젝트: hoyajigi/yQuant
def _insert(cursor, type, code, name):
    if cursor.execute("SELECT code FROM Master WHERE code = :code", {'code': code}).fetchone() is not None:
        return

    cursor.execute("INSERT INTO Master (code, name, type, last_update) VALUES (:code, :name, :type, '2016-06-01')",
                   {'code': code, 'name': name, 'type': type})
    get_logger().debug('{} {} 종목을 Master 테이블에 추가했습니다.'.format(type, name))
예제 #2
0
파일: code.py 프로젝트: hoyajigi/yQuant
def insert_from_koscom(cursor, type, query):
    url = 'http://datamall.koscom.co.kr/servlet/infoService/SearchIssue'
    output = requests.post(url, data=query)
    soup = BeautifulSoup(output.text, 'html.parser')
    options = soup.find(attrs={'name': 'stockCodeList'}).find_all('option')

    get_logger().debug('코스콤에서 {} {}개의 종목코드를 파싱했습니다.'.format(type, len(options)));

    for item in options:
        code = item.text[1:7] + '.KS'  # .KS is prepared to query yahoo
        name = item.text[8:]
        if '폐지' in name: continue
        _insert(cursor, type, code, name)
예제 #3
0
def _insert(cursor, type, code, name):
    if cursor.execute("SELECT code FROM Master WHERE code = :code", {
            'code': code
    }).fetchone() is not None:
        return

    cursor.execute(
        "INSERT INTO Master (code, name, type, last_update) VALUES (:code, :name, :type, '2016-06-01')",
        {
            'code': code,
            'name': name,
            'type': type
        })
    get_logger().debug('{} {} 종목을 Master 테이블에 추가했습니다.'.format(type, name))
예제 #4
0
def insert_from_koscom(cursor, type, query):
    url = 'http://datamall.koscom.co.kr/servlet/infoService/SearchIssue'
    output = requests.post(url, data=query)
    soup = BeautifulSoup(output.text, 'html.parser')
    options = soup.find(attrs={'name': 'stockCodeList'}).find_all('option')

    get_logger().debug('코스콤에서 {} {}개의 종목코드를 파싱했습니다.'.format(
        type, len(options)))

    for item in options:
        code = item.text[1:7] + '.KS'  # .KS is prepared to query yahoo
        name = item.text[8:]
        if '폐지' in name: continue
        _insert(cursor, type, code, name)
예제 #5
0
파일: daily.py 프로젝트: hoyajigi/yQuant
def run():
    with sqlite3.connect("daily.db") as con:
        cursor = con.cursor()

        def _get_recent_date(cursor, table_name):
            row = cursor.execute("SELECT 1 FROM sqlite_master WHERE type='table' AND name='{}'".format(table_name)).fetchone()
            if row is None: return None

            row = cursor.execute("SELECT MAX(Date) FROM '{}'".format(table_name)).fetchone()
            return datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S')

        for code, name in get_code_list():
            table_name = code

            recent_date = _get_recent_date(cursor, table_name)
            if recent_date is None: recent_date = FETCHING_START

            # 오늘 가져온 가격정보가 있다면 새로 가져오지 않음
            if recent_date.date() == datetime.now().date(): continue

            # 주말에는 새로운 정보가 없으므로 월요일 16시 이전까진 새로 가져오지 않음
            if recent_date.weekday() == 4 and datetime.now() - recent_date < timedelta(hours=64): continue

            feeder = 'fred' if 'DEX' in code else 'yahoo' # fred : DEXKOUS 원화USD DEXJPUS 엔화USD
            get_logger().debug("{} ({}) 종목의 {:%Y-%m-%d} 이후 주가정보를 {}에서 가져옵니다.".format(name, code, recent_date, feeder))

            # 최근에 저장된 데이터의 다음 날부터 오늘까지
            start_date = (recent_date + timedelta(days=1))
            end_date = datetime.now()

            # 단, 16시 이전에는 주가정보가 바뀔 수 있으므로 어제까지 자료만 가져옴
            if datetime.now().hour < 16: end_date = datetime.now() - timedelta(days=1)

            try:
                df = web.DataReader(code, feeder, start_date, end_date)
                # get_logger().debug("{} 종목의 새로운 주가정보가 {}일치 있습니다.".format(name, len(df)))
                if len(df) == 0:
                    get_logger().debug("{} ({}) 종목의 새로운 주가정보가 없습니다.".format(name, code))
                else:
                    df.to_sql(table_name, con, if_exists='append', chunksize=1000)
                    get_logger().debug("{} ({}) 종목의 {}일치 주가정보를 {} 테이블에 저장했습니다.".format(name, code, len(df), table_name))

            except RemoteDataError:
                get_logger().warn("{} ({}) 종목의 주가정보를 가져오는데 실패했습니다.".format(name, code))