Beispiel #1
0
def start_trader(ready_queue=None):
    print('start trader', datetime.now())
    market_code = []
    kosdaq_code = morning_client.get_market_code()
    kospi_code = morning_client.get_market_code(message.KOSPI)
    market_code.extend(kosdaq_code)
    market_code.extend(kospi_code)

    market_code = list(dict.fromkeys(market_code))
    market_code = list(
        filter(lambda x: len(x) > 0 and x[1:].isdigit(), market_code))
    yesterday_list = get_yesterday_data(datetime.now(), market_code)
    yesterday_list = sorted(yesterday_list,
                            key=lambda x: x['amount'],
                            reverse=True)
    yesterday_list = yesterday_list[:1000]

    for yesterday_data in yesterday_list:
        code = yesterday_data['code']
        sf = stock_follower.StockFollower(morning_client.get_reader(), code,
                                          yesterday_data, code in kospi_code)
        sf.subscribe_at_startup()
        followers.append(sf)

    if ready_queue is not None:
        ready_queue.put_nowait([yl['code'] for yl in yesterday_list])
    gevent.joinall([gevent.spawn(heart_beat), gevent.spawn(data_process)])
Beispiel #2
0
def get_yesterday_amount_rank(today):
    market_code = morning_client.get_market_code()
    yesterday = holidays.get_yesterday(today)
    yesterday_list = []
    for progress, code in enumerate(market_code):
        print('collect yesterday data',
              f'{progress+1}/{len(market_code)}',
              end='\r')
        data = morning_client.get_past_day_data(code, yesterday, yesterday)
        if len(data) == 1:
            data = data[0]
            data['code'] = code
            yesterday_list.append(data)
    print('')
    yesterday_list = sorted(yesterday_list,
                            key=lambda x: x['amount'],
                            reverse=True)
    # For testing
    yesterday_list = yesterday_list[:100]
    return yesterday_list
Beispiel #3
0
def start_vi_follower():
    global db_collection

    market_code = morning_client.get_market_code()
    today = datetime.now().date()
    #if holidays.is_holidays(today):
    #    print('today is holiday')
    #    sys.exit(1)

    yesterday = holidays.get_yesterday(today)
    db_collection = MongoClient(db.HOME_MONGO_ADDRESS).trade_alarm

    yesterday_list = []
    for progress, code in enumerate(market_code):
        print('collect yesterday data', f'{progress+1}/{len(market_code)}', end='\r')
        data = morning_client.get_past_day_data(code, yesterday, yesterday)
        if len(data) == 1:
            data = data[0]
            data['code'] = code
            yesterday_data[code] = data
            yesterday_list.append(data)
    print('')
    yesterday_list = sorted(yesterday_list, key=lambda x: x['amount'], reverse=True)

    yesterday_list = yesterday_list[:100]
    for ydata in yesterday_list:
        code = ydata['code']
        sf = stock_follower.StockFollower(morning_client.get_reader(), db_collection, code, yesterday_data[code])
        sf.subscribe_at_startup()
        subscribe_code[code] = sf

    print('Start Listening...')

    stock_api.subscribe_alarm(morning_client.get_reader(), vi_handler)
    watch_thread = gevent.spawn(start_watch)
    gevent.joinall([watch_thread])
Beispiel #4
0
    print('*' * 100)
    for ba in enumerate(by_amount[:10]):
        print(ba)
    print('*' * 100)

    print('amount rank1', by_amount[0]['amount'])
    for i, ba in enumerate(by_amount):
        if ba['code'] == code:
            print('by amount rank', i + 1)
            print(ba)
            break


if __name__ == '__main__':
    if len(sys.argv) < 3:
        print(sys.argv[0], 'code', 'time(2020-02-03 09:00)')
        sys.exit(1)

    code = sys.argv[1]
    search_time = sys.argv[2]
    print(search_time)
    kosdaq_market_code = morning_client.get_market_code()
    kospi_market_code = morning_client.get_market_code(message.KOSPI)
    kosdaq_market_code.extend(kospi_market_code)

    print(len(kosdaq_market_code))
    if code in kosdaq_market_code:
        start_search(code, search_time, kosdaq_market_code)
    else:
        print('Cannot find code', code)
Beispiel #5
0
                        (tick['current_price'] - code_dict[code]['buy_price'])
                        / code_dict[code]['buy_price'] * 100, 'time',
                        tick['time'])
                    break


if __name__ == '__main__':
    db_collection = MongoClient(db.HOME_MONGO_ADDRESS).trade_alarm
    alarm_data = list(db_collection['alarm'].find({
        'date': {
            '$gte': target_date,
            '$lte': target_date + timedelta(days=1)
        }
    }))
    alarm_data = sorted(alarm_data, key=lambda x: x['date'])
    market_code = morning_client.get_market_code()
    yesterday = holidays.get_yesterday(target_date.date())
    done_codes = []
    #alarm_data = list(filter(lambda x: x['3'] == 'A017890', alarm_data))
    print(len(market_code))

    for code in market_code:
        code_dict[code] = {
            'state': STATE_NONE,
            'yesterday_data': None,
            'today_min_data': None,
            'vi': True,
            'today_tick_data': None,
            'vi_highest': 0,
            'bottom_price': 0,
            'buy_price': 0,
Beispiel #6
0
from configs import db
from pymongo import MongoClient
from morning.pipeline.converter import dt
import numpy as np
from scipy.signal import find_peaks, peak_prominences
import pandas as pd


def wait_loop():
    while True:
        gevent.sleep(0.03)


def tick_data_handler(code, data):
    if len(data) != 1:
        return
    print(data[0])


market_code = morning_client.get_market_code()  # KOSDAQ
kospi_market_code = morning_client.get_market_code(message.KOSPI)

market_code.extend(kospi_market_code)
print('all', len(market_code))

for code in market_code:
    stock_api.subscribe_stock(morning_client.get_reader(), code,
                              tick_data_handler)

gevent.joinall([gevent.spawn(wait_loop)])
Beispiel #7
0
def start_validation(codes=[]):
    global db_collection

    if len(codes) > 0:
        market_code = codes
    else:
        market_code = morning_client.get_market_code()

    today = datetime.now().date()
    yesterday = holidays.get_yesterday(today)
    db_collection = MongoClient(db.HOME_MONGO_ADDRESS).trade_alarm

    yesterday_list = []
    for progress, code in enumerate(market_code):
        print('collect yesterday data',
              f'{progress+1}/{len(market_code)}',
              end='\r')
        data = morning_client.get_past_day_data(code, yesterday, yesterday)
        if len(data) == 1:
            data = data[0]
            data['code'] = code
            #yesterday_data[code] = data
            yesterday_list.append(data)
    print('')
    yesterday_list = sorted(yesterday_list,
                            key=lambda x: x['amount'],
                            reverse=True)
    yesterday_list = yesterday_list[:1000]
    failed_tick_codes = []
    failed_ba_tick_codes = []
    # 1. validate yesterday_list :100's today tick data
    for ydata in yesterday_list:
        if not validate_tick_data(ydata['code'], today):
            failed_tick_codes.append(ydata['code'])
        if not validate_ba_tick_data(ydata['code'], today):
            failed_ba_tick_codes.append(ydata['code'])

    if len(failed_tick_codes) > 0:
        print('FAILED TICK', failed_tick_codes)
    else:
        print('TICK ALL SUCCESS')

    if len(failed_ba_tick_codes) > 0:
        print('FAILED BA TICK', failed_ba_tick_codes)
    else:
        print('TICK BA ALL SUCCESS')

    # 2. validate today alarm tick data
    alarm_list = get_alarm_list(today)
    alarm_failed_codes = []
    if len(alarm_list) > 0:
        for ac in alarm_list:
            if len(codes) > 0 and ac['3'] not in codes:
                continue

            if not validate_alarm_data(ac['3'], today, ac['date']):
                alarm_failed_codes.append(ac['3'])

        if len(alarm_failed_codes) > 0:
            print('FAILED ALARM CODES', alarm_failed_codes,
                  len(alarm_failed_codes), '/', len(alarm_list))
        else:
            print('ALARM ALL SUCCESS', len(alarm_list))
    else:
        print('NO ALARM TODAY')