Example #1
0
def load_contract_mar():
	file_name = "contract_mar.ini"
	if not os.path.exists(file_name):
		err = "file [%s] doesn't exist" %  file_name
		log.WriteError(err)
		raise Exception, err
		return

	file = open(file_name, "rt")
	if not file: 
		err = 'cant open file [%s]' %  file_name
		log.WriteError(err)
		raise Exception, err
		return

	global g_contract_mar
	line = file.readline()
	while line:
		try:
			ma = re.search("(\S+)\s+(\S+)", line)
			if ma:
				arrs = ma.groups()				
				contract = arrs[0]
				mar = float(arrs[1]) / 1000
				g_contract_mar[contract]=mar
			else:
				log.WriteError("%s line %d not match!" % (file_name, index))
		except Exception, e:
			print e

		line = file.readline()
Example #2
0
    def get_exg_info_by_contract(this, contract):
        if not this.contract_exchg_sub.has_key(contract):
            err = "can't find sub_exchange of contract[%s]" % contract
            log.WriteError(err)
            return None

        exg_sub = this.contract_exchg_sub[contract]
        if not this.exchg_info.has_key(exg_sub):
            err = "can't find exchange of sub_exchange[%s]" % exg_sub
            log.WriteError(err)
            return None

        return this.exchg_info[exg_sub]
Example #3
0
	def _request(this, url):
		try_time = 2

		for i in xrange(0, try_time):
			try:
				raw_str = this._request_requests(url)
				if not raw_str : raise Exception('空内容')
				return raw_str

			except Exception as e:
				desc = '%s' % e
				if i < try_time - 1: desc = desc + '.重试'
				log.WriteError(desc)

		log.WriteError('空内容')
		return ''
Example #4
0
	def __init__(this, host, user, passwd, db, table_name='', port=3306):		
		try:
			this.conn = MySQLdb.connect(host, user, passwd, db, port)
			this.cursor = this.conn.cursor()
			this.table_name = table_name
		except :
			log.WriteError ('Error: cant connect db names %s from host %s' % (db, host) )
Example #5
0
	def get_web_contents(this, url):
		url = this._get_full_url(url, this.set_base)
		if not isinstance(url, unicode):
			log.WriteError('连接不是纯字符串形式:%s' % url)
			return ''

		this.headers['Referer'] = this._get_full_url(this.get_base())
		strg = this._request(url)
		return this._decode(str(strg))
Example #6
0
    def __init__(this, path_ini):
        if not os.path.exists(path_ini):
            err = "file [%s] doesn't exist" % path_ini
            log.WriteError(err)
            raise Exception, err

        ini_parser = ConfigParser.ConfigParser()
        ini_parser.read(path_ini)

        this.exchg_info = {}
        this.contract_exchg_sub = {}

        for exg_id in xrange(1, 20):
            session_name = "Exchange%d" % exg_id
            if not ini_parser.has_section(session_name):
                break

            info = exchange_info()

            # 交易所名字
            info.name = ini_parser.get(session_name, "name")
            info.sub_name = ini_parser.get(session_name, "sub_name")

            for end_date_id in xrange(1, 10):
                option_name = "end_date%d" % end_date_id

                if not ini_parser.has_option(session_name, option_name):
                    break

                end_date = int(ini_parser.get(session_name, option_name))
                info.end_date_set.append(end_date)
                info.stage_time[end_date] = []
                info.stage_min_times[end_date] = []

                for time_id in xrange(1, 10):
                    sub_option_name = "time%d%d" % (end_date_id, time_id)

                    if not ini_parser.has_option(session_name,
                                                 sub_option_name):
                        break

                    time_str = ini_parser.get(session_name, sub_option_name)
                    time_arr = scanf.sscanf(time_str, "%d-%d")
                    info.stage_time[end_date].append(time_arr)

                    time_tick = this.make_min_time(time_arr[0], time_arr[1])
                    info.stage_min_times[end_date].extend(time_tick)

            this.exchg_info[info.sub_name] = info

            for var_id in xrange(1, 100):
                option_name = "var%d" % var_id
                if not ini_parser.has_option(session_name, option_name):
                    break

                var_name = ini_parser.get(session_name, option_name)
                this.contract_exchg_sub[var_name] = info.sub_name
Example #7
0
 def __connect(this):
     try:
         this.conn = MySQLdb.connect(this.host,
                                     this.user,
                                     this.passwd,
                                     this.db,
                                     this.port,
                                     charset=this.charset)
         this.cursor = this.conn.cursor()
     except:
         log.WriteError('Error: cant connect db names %s from host %s' %
                        (this.db, this.host))
Example #8
0
 def __connect(this):
     try:
         this.conn = MySQLdb.connect(this.host,
                                     this.user,
                                     this.passwd,
                                     this.db,
                                     this.port,
                                     charset=this.charset)
         this.cursor = this.conn.cursor()
         # this.cursor.execute("SET NAMES %s" % this.charset)
     except Exception, msg:
         log.WriteError('Error: cant connect db names %s from host %s %s' %
                        (this.db, this.host, msg))
Example #9
0
def query_close_price(database, contract, date):
    table_name = get_dbtable(contract)

    times = get_last_min(contract)
    rec = None
    for time in times:
        sql = "SELECT ClosePrice FROM %s WHERE time=%d%d && instrumentID='%s'" % (
            table_name, date % 1000000, time, contract)
        database.Query(sql)
        rec = database.FetchOne()
        if rec: break

    if not rec:
        log.WriteError('cant find %s close price %d [%s]' %
                       (contract, date, sql))
        # err = 'cant find %s close price %d [%s]' % (contract, date, sql)
        # raise Exception, err
        rec = [0]
    close_price = rec[0]
    return close_price
Example #10
0
def walk_sub_dir(cur_dir, sub_dir):
    map_report_per_area = {}
    sub_path = cur_dir + '\\' + sub_dir
    for root, dirs, files in os.walk(sub_path):
        for file_name in files:
            file_path = root + '\\' + file_name

            fp = open(file_path, "rb")
            if None == fp:
                log.WriteError('%s can\'t be opened' % file_path)
                continue

            empty_index_first = file_name.find(' ')
            empty_index_second = file_name.find(' ', empty_index_first + 1)
            date = file_name[empty_index_first + 1:empty_index_second]

            map_report_per_area[date] = {}

            line = fp.readline()
            while (len(line) > 0):
                user_info = scanf.sscanf(line, "%s %s %s %s %s %d %s %d %d %d")

                key = "android%u" % (user_info[7])
                if 53 == user_info[8]:
                    key = "java%u" % (user_info[7])

                if map_report_per_area[date].has_key(key):
                    map_report_per_area[date][
                        key] = map_report_per_area[date].get(key) + 1
                else:
                    map_report_per_area[date][key] = 1

                line = fp.readline()

            fp.close()

    return map_report_per_area
Example #11
0
	def get_picture(this, url_from, url_pic, file_save_to):
		if os.path.isfile(file_save_to) : 
			log.WriteDetail("已有:%s" % file_save_to)
			return True
		
		url_pic = this._get_full_url(url_pic, this.set_base)
		this.headers['Referer'] = this._get_full_url(url_from)
		
		raw = this._request(url_pic)

		if not raw :
			log.WriteError("失败无法下载:%s" % url_pic)
			return False

		fout = open(file_save_to, 'wb')
		fout.write(raw)
		fout.close()

		if not os.path.isfile(file_save_to) :
			log.WriteDetail("失败无法保存:%s <- %s" % (file_save_to, url_pic))
			return False

		log.WriteDetail('保存:%s' % file_save_to)
		return True
Example #12
0
def read_records(trade_rec_file):
    file_name = trade_rec_file
    list_data = []
    index = 0
    if not os.path.exists(file_name):
        err = "file [%s] doesn't exist" % file_name
        log.WriteError(err)
        raise Exception, err

    f = open(file_name, "rt")
    if not f:
        err = 'cant open file [%s]' % file_name
        log.WriteError(err)
        raise Exception
        return

    line = f.readline()
    while line:
        index += 1
        try:
            ma1 = re.search(
                "(\d+)\s+(\d+):(\d+):(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)",
                line)
            ma2 = re.search(
                "(\d+)\s+(\d+):(\d+):(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)",
                line)
            if ma1:
                arrs = ma1.groups()
                date = int(arrs[0])
                hh = int(arrs[1])
                mm = int(arrs[2])
                ss = int(arrs[3])
                contract = arrs[4]
                trade_dir = arrs[5].decode('gbk')
                trade_type = arrs[6].decode('gbk')
                price = float(arrs[7])
                trade_num = int(arrs[8])
                commission = float(arrs[9])
                touji = arrs[10]
                sysID = arrs[11]

                list_data.append([
                    contract, date, hh * 10000 + mm * 100 + ss, trade_dir,
                    trade_type, price, trade_num, commission, sysID
                ])
            elif ma2:
                arrs = ma2.groups()
                date = int(arrs[0])
                hh = int(arrs[1])
                mm = int(arrs[2])
                ss = int(arrs[3])
                contract = arrs[4]
                trade_dir = arrs[5].decode('gbk')
                trade_type = arrs[6].decode('gbk')
                price = float(arrs[7])
                trade_num = int(arrs[8])
                commission = float(arrs[9])
                sysID = arrs[10]

                list_data.append([
                    contract, date, hh * 10000 + mm * 100 + ss, trade_dir,
                    trade_type, price, trade_num, commission, sysID
                ])
            else:
                log.WriteError("trade_records line %d not match!" % (index))
        except Exception, e:
            print e

        line = f.readline()
Example #13
0
    if len(records) == 0:
        raise Exception('cant read trading record!')
        return

    rec_buy, rec_sel = sort_rec(records)

    process_record(rec_buy)
    process_record(rec_sel)

    out = open("2.txt", "w")
    for r in rec_buy:
        out.write(" ".join(r))
        out.write("\n")

    b = ""
    for r in rec_sel:
        b = b.join(str(r))

    out.write(b)
    out.write("\n")

    out.close()


if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        log.WriteError(e)
        import traceback
        print(traceback.print_exc())
Example #14
0
def get_contract_mar(contract):
	symbol = get_contract_symbol(contract)
	if not g_contract_mar.has_key(symbol):
		log.WriteError("%s isn't in contract_mar.ini" % contract)
		return None
	return g_contract_mar[symbol]
Example #15
0
    def close(this, datetime, contract, dir, number, price, commission, today):
        if not this.contracts.has_key(contract):
            err = 'sell error, not match contract[%s] date[%s] dir[%s] number[%s] price[%.2f]' % (
                contract, datetime, dir, number, price)
            log.WriteError(err)
            raise Exception, err
            return
        date = datetime / 1000000

        if not this.date_profit.has_key(contract):
            this.date_profit[contract] = {}
        if not this.date_profit[contract].has_key(date):
            this.date_profit[contract][date] = 0

        total_profit_1 = 0
        total_sell = number
        positions = this.contracts[contract][1 - dir]
        index = 0
        this_profit = 0
        while True:
            if len(positions) == 0: break
            pos = positions[index]

            #print contract, datetime
            if today and pos.datetime / 1000000 != datetime / 1000000:  # 如果是平今,但是该仓非今仓
                loop_count = 0
                while True:
                    index += 1
                    loop_count += 1
                    pos = positions[index]
                    if pos.datetime / 1000000 == datetime / 1000000: break
                    if loop_count > 500:
                        err = 'sell error, cant find close today position [%s] date[%s] dir[%s] number[%s] price[%.2f]' % (
                            contract, datetime, dir, number, price)
                        raise Exception, err

            remove_number = min(pos.number, total_sell)
            profit = 0
            if dir == DIR_SELL:
                profit += remove_number * (
                    price -
                    pos.price) * contract_info.get_contract_mul(contract)
            else:
                profit += remove_number * (
                    pos.price -
                    price) * contract_info.get_contract_mul(contract)
            total_profit_1 += profit

            this_profit += profit

            total_sell -= remove_number
            pos.number -= remove_number

            if pos.number == 0: positions.remove(pos)
            if total_sell == 0: break

        this.total_profit[contract] += this_profit
        this.date_profit[contract][date] += this_profit

        if total_sell > 0:
            err = 'sell error, not match contract[%s] date[%s] dir[%s] number[%s] price[%.2f]' % (
                contract, datetime, dir, number, price)
            log.WriteError(err)
            raise Exception, err
            return
        # 手续费累加
        this.total_commission[contract] += commission

        return this_profit
Example #16
0
        db_profit.Execute(sql.encode('utf8'))

    db_profit.Commit()


def main():
    trade_rec_file = "trade_records.db"
    if len(sys.argv) > 1:
        trade_rec_file = sys.argv[1]

    db_profit = create_database("db_profit")

    sys_config = Sys_config("system.ini", "fund_info")

    # 读取交易记录
    records = read_records(trade_rec_file)
    if len(records) == 0:
        raise Exception, 'cant read trading record!'
        return

    send_record2db(sys_config, db_profit, records)


if __name__ == '__main__':
    try:
        main()
    except Exception, e:
        log.WriteError(e[0])
        import traceback
        print traceback.print_exc()
Example #17
0
#---------------------------------------------------------------
def str_middle(co, tuple_begin_anchol, str_end, case=False):
    c = co
    beg_off_set = 0

    if not case:
        c = co.lower()
        str_end = str_end.lower()
        tuple_begin_anchol = (x.lower() for x in tuple_begin_anchol)

    for anchol in tuple_begin_anchol:
        try:
            beg_off_set = c.find(anchol, beg_off_set)
        except:
            log.WriteError("find error")
            return ''

        if beg_off_set == -1: return ''
        beg_off_set += len(anchol)

    end_pt = c.find(str_end, beg_off_set)
    if end_pt == -1: return c[beg_off_set:]
    #return the original string, not the lower cased.
    return co[beg_off_set:end_pt]


#----------------------
def trim_to_unicode(strg):
    if strg is None: return ''
    if not isinstance(strg, basestring): return strg