Beispiel #1
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 #2
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 #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
# 开启cors
CORS(app)

# 初使化SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://' + Config.MYSQL_USER + ':' + Config.MYSQL_PASSWORD + '@' + \
                                        Config.MYSQL_HOST+ '/' + Config.MYSQL_DBNAME
# 不配置会报错提示。如果设置成 True (默认情况),Flask-SQLAlchemy 将会追踪对象的修改并且发送信号。这需要额外的内存, 如果不必要的可以禁用它。
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
# 密钥,不配置无法对session字典赋值
app.config['SECRET_KEY'] = os.urandom(24)
# 显示SQLAlchemy的sql语句日志
app.config["SQLALCHEMY_ECHO"] = True

# 缓存全局db对象,每次重新创建会导致事务不是同一个
DB.init(app)

# 自定义返回,flask_restful默认只支持json
api = Api(app, default_mediatype='application/json')
# 根据请求头的的Accept参数,返回对应的格式
api.representations = {
    'application/json': output_json,
}
#
api.add_resource(ValidImageCreate, '/common/validImage/create')  # 生成图片验证码
api.add_resource(QCloudCosSign, '/common/QCloud/sign')  # 腾讯云COS多次签名
api.add_resource(MerchantReg, '/merchant/reg')
api.add_resource(MerchantLogin, '/merchant/login')
api.add_resource(MerchantInfo, '/merchant/info')
api.add_resource(MerchantInfoSave, '/merchant/info/save')
api.add_resource(ProductAdd, '/product/add')
Beispiel #9
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()
class Case():
    def __init__(self):
        self.logger = Log.get_logger()
        self.http = Http()
        self.db = DB()

    def load_data(self, data_file):
        self.data = Data(data_file)

    def run_case(self, sheet_name, case_name, vars={}):
        self.case_data = self.data.get_test_case(sheet_name, case_name)
        self.http.set_url(self.case_data.get('Request Url'))
        self.http.set_headers(self.case_data.get('Request Data Type'))
        self.http.set_data(self.case_data.get('Request Data') % vars)
        if self.case_data.get('Request Method').lower() == 'get':
            self.resp = self.http.get()
        else:
            self.resp = self.http.post()
        return self.resp

    def run(self, vars={}):
        case_name = inspect.stack()[1][3].strip()
        sheet_name = case_name.split("_")[2]
        self.run_case(sheet_name, case_name, vars)
        try:
            assert self.http_code_200
        except AssertionError:
            self.logger.debug(self.resp.status_code)

        if self.case_data.get('Response Contains'):
            try:
                assert self.response_content
            except AssertionError:
                self.logger.debug(self.resp.text)

        if self.case_data.get('Response Code'):
            try:
                assert self.response_code
            except AssertionError:
                self.logger.debug(self.resp.json().get('code'))

        if self.case_data.get('Response Msg'):
            try:
                assert self.response_msg
            except AssertionError:
                self.logger.debug(self.resp.json().get('code'))

    @property
    def http_code_200(self):
        return True if self.resp.status_code == 200 else False

    @property
    def response_content(self):
        return True if self.case_data.get(
            'Response Contains') in self.resp.text else False

    @property
    def response_code(self):
        return True if self.resp.json().get('code') == self.case_data.get(
            'Response Code') else False

    @property
    def response_msg(self):
        return True if self.resp.json().get('msg') == self.case_data.get(
            'Response Msg') else False

    def check_db(self, sql_name, vars={}):
        sql = self.data.get_sql(sql_name) % vars
        result = self.db.exec_sql(sql)
        return True if result else False
 def __init__(self):
     self.logger = Log.get_logger()
     self.http = Http()
     self.db = DB()
Beispiel #12
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 #13
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()
Beispiel #14
0
class Manager(object):
    USER_INFO_CFG_PATH = "usr_cfg"

    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

    def __is_user_already_exist(self, name):
        result = self.__users.find_one({"UserName" : name})
        if result is not None:
            return True

        return False

    def __get_user_cfg_file(self, name):
        file_name = name + '.json'
        cfg_file = os.path.join(self.USER_INFO_CFG_PATH, file_name)
        print cfg_file
        return cfg_file

    def __get_user_info_from_stock_seller(self, name, stock_seller):
        user = easytrader.use(stock_seller)
        cfg_file = self.__get_user_cfg_file(name) 

        try:
            print cfg_file
            user.prepare(cfg_file)
            user.login()
        except Exception as e:
            print e
            print "login stock seller failed"
            _logger.exception("User login failed")
            return None, None

        _logger.debug("%s login success" % name)

        balance = user.get_balance()
        position = user.get_position()

        return balance, position    

    def __encode_user_db_info(self, user, balance, position):
        user_info = {}
        user_info["UserName"] =  user["UserName"]
        user_info["StockSeller"] = user["StockSeller"]
        user_info["Balance"] = balance
        user_info["Postion"] = position
        return user_info

    def add_user(self, user):
        name = user['UserName']
        if self.__is_user_already_exist(name):
            _logger.warn("add user is already exist, user name is %s" % name)
            return self.get_user(name)

        balance, position = self.__get_user_info_from_stock_seller(name, user["StockSeller"])
        if balance is None or position is None:
            _logger.error("Get user info from stocker seller failed")

        db_user_info = self.__encode_user_db_info(user, balance, position)
        try :
            self.__users.insert_one(db_user_info)
        except Exception as e:
            _logger.exception("Insert user in db is exception")

    def delete_user(self, name):
        result = self.__users.delete_one({'UserName' : name})
        if result.deleted_count != 1:
            print 'delete unexist user'
            _logger.warn('Delete user failed, user name is %s' % name)

    def get_user(self, name):
        user = self.__users.find_one({"UserName" : name})

        return user

    def concern_stock(self, user_name, stock_code):
        pass

    def unconcern_stock(self, user_name, stock_code):
        pass
    
    def add_fake_user(self, user):
        name = user['UserName']
        balance, position = None, None

        db_user_info = self.__encode_user_db_info(user, balance, position)
        try :
            self.__users.insert_one(db_user_info)
        except Exception as e:
            _logger.exception("Insert user in db is exception")

    def get_all_user(self):
        users = []
        for user in self.__users.find():
            print user
            users.append(user)

        return users

    def delete_all_fake(self, fake_user):
        self.__users.delete_many({"UserName" : fake_user["UserName"]})