Beispiel #1
0
def process(content_dict={"": ""}):
    INVALID_STOCK_CODE_MSG = 'You input invalid stock code, please input such as 600001.'
    _logger.info("process, entered..........")
    stock_code = content_dict['Content']
    if not check_valid(stock_code):
        content_dict['Content'] = INVALID_STOCK_CODE_MSG
        return
    db = DB(Constants.HIST_DATA_DB_NAME)
    hist_collector = HistDataCollector(stock_code, db)
    hist_collector.collect()
    realtime_collector = RTTickCollector()
    current_info = {}
    realtime_collector.collect(stock_code, current_info)
    strategy = Strategy(None, db)
    context = Context()
    context.set_realtime_data(stock_code, current_info)
    deal_type = strategy.decide(context, stock_code)

    result = ''
    if deal_type == 1:
        result = 'you should buy now.'
    elif deal_type == 2:
        result = 'you should sell now.'
    else:
        result = 'you hold, do nothing.'
    content_dict['Content'] = result
Beispiel #2
0
    def __init__(self):
        self.__db = DB("user")

        usr_colleciton = self.__db.get_collection('user_base')
        if usr_colleciton is None:
          usr_colleciton = self.__db.create_collection('user_base')
        self.__users = usr_colleciton
Beispiel #3
0
 def __init__(self):
     self.__mq_server = None
     self.__data_db = DB(Constants.HIST_DATA_DB_NAME)
     self.__config = Config()
     self.__tick_db = FileDB(
         self.__config.get_config('persistent', 'hist_tick_dir'))
     self.__trading_strategy = None
     self.__tick_collector = None
Beispiel #4
0
def process(content_dict={"":""}):
    INVALID_STOCK_CODE_MSG = 'You input invalid stock code, please input such as 600001.'
    _logger.info("process, entered..........")
    stock_code = content_dict['Content']
    if not check_valid(stock_code):
        content_dict['Content'] = INVALID_STOCK_CODE_MSG
        return
    db = DB(Constants.DB_NAME)
    hist_collector = HistoryCollector(stock_code, db)
    hist_collector.collect_history_data()
    realtime_collector = RealtimeCollector()
    current_info = {}
    realtime_collector.collect_rt_data(stock_code, current_info)
    strategy = Strategy(db)
    content_dict['Content'] = strategy.decide(current_info)
Beispiel #5
0
    def __get_ma5_price(self):
        if len(self.__hist_close_price) > 5:
            return np.mean(self.__hist_close_price[-5:])
        else:
            return np.mean(self.__hist_close_price)

    def __get_ma10_price(self):
        if len(self.__hist_close_price) > 10:
            return np.mean(self.__hist_close_price[-10:])
        else:
            return np.mean(self.__hist_close_price)

    def __get_ma20_price(self):
        if len(self.__hist_close_price) > 20:
            return np.mean(self.__hist_close_price[-20:])
        else:
            return np.mean(self.__hist_close_price)

    def __get_history_close_price(self, date_str):
        date = datetime.strptime(date_str, '%Y%m%d') - timedelta(days=40)
        conds = [('date', 'gt', date.strftime('%Y-%m-%d'))]
        result = self.__collection.findand(conds)
        for each in result:
            self.__hist_close_price.append(each['fqPrice'])


if __name__ == '__main__':
    db = DB(Constants.TEST_DATA_DB_NAME)
    collector = HistDataCollector('600036', db)
    collector.collect()
Beispiel #6
0
            begin_date = last_record['date']
            _logger.info('collect stock(%s) history data, begin date: %r.' %
                         (self.__stock_code, begin_date))

        end_date = Util.get_today()
        _logger.info('collect stock(%s) history data, end date: %r.' %
                     (self.__stock_code, end_date))
        if begin_date == end_date:
            return
        elif not begin_date or len(begin_date) == 0:
            result = ts.get_hist_data(self.__stock_code)
        else:
            result = ts.get_hist_data(code=self.__stock_code,
                                      start=begin_date,
                                      end=end_date)

        if result is None:
            _logger.warn('could get stock(%r) history data from tushare.' %
                         self.__stock_code)
            return
        datas = result.to_dict()
        for attr, data, in datas.iteritems():
            for date, value in data.iteritems():
                self.__collection.insert_and_update(date, attr, value)


if __name__ == '__main__':
    db = DB(Constants.DB_NAME)
    collector = HistoryCollector('600036', db)
    collector.collect_history_data()
Beispiel #7
0
def get_collection():
    db = DB('history_stock')
    collection = db.get_collection('history')
    return collection
Beispiel #8
0
from util.db import DB, Collection
from util.constants import Constants

import tushare as ts
import logging

_logger = logging.getLogger(__name__)

class StockBasicCollector(object):
    def __init__(self, db):
        self.__db = db
        self.__collection = Collection(Constants.BASIC_COLLECTION, self.__db)

    def collect(self):
        _logger.info('collect stock basic info, begin.....')
        result = ts.get_stock_basics()
        result['code'] = result.index
        for i in range(len(result)):
            record = result.iloc[i].to_dict()
            self.__collection.insert_and_update('code', record['code'], **record)
        _logger.info('collect stock basic info, end.....')

if __name__ == '__main__':
    db = DB(Constants.HIST_DATA_DB_NAME)
    collector = StockBasicCollector(db)
    collector.collect()
 def __init__(self):
     self.logger = Log.get_logger()
     self.http = Http()
     self.db = DB()
Beispiel #10
0
 def get_db_value(self, element_name, where_condition):
     db = DB()
     db_map = self.page['db_map'](element_name)
     return db.get(key=db_map[0],
                   table=db_map[1],
                   where_condition=where_condition)[0]
Beispiel #11
0
 def __init__(self):
     self.__user_db = DB(self.USER_DB_NAME)
     self.__usr_manager = Manager(self.__user_db)
     self.__trigger = TimeTrigger(self.__usr_manager, self.TRIGGER_INTERVAL)
     self.__stop = threading.Event()