def get_day(dmon, emon, day): '''根据交割月前emon个月,第day交易日返回日期''' #0代表第一个交易日,-1代表最后一个交易日 mon = int(dmon[-2:]) y = int(dmon[:-2]) oneday = datetime.timedelta(days=1) if y > 10: year = 2000 + y else: year = 2010 + y if mon + emon <= 0: year = year - 1 rmon = 12 + mon + emon elif mon + emon > 12: year += 1 rmon = mon + emon - 12 else: rmon = mon + emon if day == -1: temp = datetime.datetime(year, rmon, 1) temp2 = datetime.datetime(year, rmon, calendar.monthrange(year, rmon)[1]) n = fn.cal_tradeday(temp, temp2) res = get_day(dmon, emon, n) else: res = datetime.datetime(year, rmon, 1) temp = datetime.datetime(year, rmon, 1) while fn.cal_tradeday(temp, res) <> day: res = res + oneday return res
def client_pos_4(par): '''gp4的超仓监控''' #品种,合约,多头,空头,交易所,限额1,限额2,限额3,限额4 rule = re.compile(r'[^0-9]') dmon = rule.sub('', par[1]) now = datetime.datetime.now() l1, tt = client_pos_mon_par() l1 = l1['l4'] nT = 8 #提前多少个交易日 dl1 = get_day(dmon, l1[0][0], l1[0][1]) #限额日期1 dl2 = get_day(dmon, l1[1][0], l1[1][1]) #限额日期2 dl3 = get_day(dmon, l1[2][0], l1[2][1]) #限额日期2 is_exceed = False if fn.cal_tradeday(now, dl3) <= nT: if par[2] > par[-1] or par[3] > par[-1]: is_exceed = True return is_exceed, par[-1] if fn.cal_tradeday(now, dl2) <= nT: if par[2] > par[-2] or par[3] > par[-2]: is_exceed = True return is_exceed, par[-2] if fn.cal_tradeday(now, dl1) <= nT: if par[2] > par[-3] or par[3] > par[-3]: is_exceed = True return is_exceed, par[-3] if par[2] > par[-4] or par[3] > par[-4]: is_exceed = True return is_exceed, par[-4] return is_exceed, 0
def client_pos_3(par): '''gp3的超仓监控''' #品种,合约,多头,空头,市场持仓量,限额标准,限额百分比,限额1,限额2,限额3 rule = re.compile(r'[^0-9]') dmon = rule.sub('', par[1]) now = datetime.datetime.now() l1, tt = client_pos_mon_par() l1 = l1['l3'] nT = 8 #提前多少个交易日 dl1 = get_day(dmon, l1[0][0], l1[0][1]) #限额日期1 dl2 = get_day(dmon, l1[1][0], l1[1][1]) #限额日期2 is_exceed = False if fn.cal_tradeday(now, dl2) <= nT: if par[2] > par[-1] or par[3] > par[-1]: is_exceed = True return is_exceed, par[-1] if fn.cal_tradeday(now, dl1) <= nT: if par[2] > par[-2] or par[3] > par[-2]: is_exceed = True return is_exceed, par[-2] if par[4] > par[5]: if par[2] > par[4] * par[6] or par[3] > par[4] * par[6]: is_exceed = True return is_exceed, par[4] * par[6] elif par[2] > par[-3] or par[3] > par[-3]: is_exceed = True return is_exceed, par[-3] return is_exceed, 0
def client_pos_2(par): '''gp2的超仓监控''' #品种,合约,多头,空头,交易所,限额1,限额2,限额3 nT = 8 #提前多少个交易日 rule = re.compile(r'[^0-9]') dmon = rule.sub('', par[1]) now = datetime.datetime.now() dtpar, gppar = client_pos_mon_par() tl = False if par[0] in gppar['gp2_1']: tl = dtpar['l1'] elif par[0] in gppar['gp2_2']: tl = dtpar['l2'] elif par[0] in gppar['gp2_3']: tl = dtpar['l3'] if tl: dl1 = get_day(dmon, tl[0][0], tl[0][1]) #限额日期1 dl2 = get_day(dmon, tl[1][0], tl[1][1]) #限额日期2 elif par[4] in gppar['gp2_4']: dl1 = get_day(dmon, -1, 1) dl1 = datetime.datetime(dl1.year, dl1.month, 16) dl2 = get_day(dmon, 0, 1) else: dl1 = get_day(dmon, -1, 1) dl1 = datetime.datetime(dl1.year, dl1.month, 21) dl2 = get_day(dmon, 0, 1) is_exceed = False if fn.cal_tradeday(now, dl2) <= nT: if par[2] > par[-1] or par[3] > par[-1]: is_exceed = True return is_exceed, par[-1] if fn.cal_tradeday(now, dl1) <= nT: if par[2] > par[-2] or par[3] > par[-2]: is_exceed = True return is_exceed, par[-2] if par[2] > par[-3] or par[3] > par[-3]: is_exceed = True return is_exceed, par[-3] return is_exceed, 0
def compare_bu(self): ''' 函数说明:保证金率对比按钮''' import calendar res = [] for x in self.codeclass: if self.codeclass[x].Inf['Mrate'] <> self.variclass[ self.codeclass[x].Vari].Inf['Mrate']: res.append(x) res.sort() m = datetime.datetime.now() m1 = m.strftime('%y%m')[1:] m2 = datetime.datetime(m.year, m.month, 1) + datetime.timedelta( calendar.monthrange(m.year, m.month)[1]) m2 = m2.strftime('%y%m')[1:] czc = False dce = False if m.day >= 16: czc = True n = fn.cal_tradeday(datetime.datetime(m.year, m.month, 1), m) if n >= 15: dce = True tree = self.weight['tree'] items = tree.get_children() [tree.delete(item) for item in items] k = 1 for i in range(len(res)): if m1 in res[i]: continue if m2 in res[i]: if self.codeclass[res[i]].House == 'SHF': continue if self.codeclass[res[i]].House == 'CZC' and czc: continue if self.codeclass[res[i]].House == 'DCE' and dce: continue r = self.codeclass[res[i]].Inf['Mrate'] * 100 r1 = self.variclass[self.codeclass[res[i]].Vari].Inf['Mrate'] * 100 d = self.codeclass[res[i]].Inf['delta_rate'] * 100 v = [res[i], r1, d, r] tree.insert('', k, values=v) k += 1 tree.heading(u'原保证金率', text=u'品种保证金率') tree.heading(u'调整后保证金率', text=u'交易所保证金率') self.root.wm_attributes('-topmost', 1)
def delivery_mon(invclass): '''临近交割月持仓监控''' s1, s2, s3 = fn.get_lastmonth() colname = [ u'交易日', u'投资者代码', u'投资者名称', u'营业部名称', u'交易所代码', u'合约代码', u'多头持仓', u'空头持仓' ] colname1 = [ 'tr_date', 'invid', 'inv_name', 'invdepartment', 'house', 'code', 'longnums', 'shortnums' ] outdata = [] now = datetime.datetime.now() isremind = (fn.cal_tradeday(now, get_day(s2, 0, -1)) <= 10) for x in invclass: if invclass[x].InvInf['type'] <> u'自然人': continue for pos in invclass[x].Position: data = [] if len(pos['code']) <= 6: if pos['house'] not in [ 'CFFEX', 'SHFE' ] and pos['code'][-3:] in [s2[1:]] and isremind: data = [ now.strftime('%Y-%m-%d'), x, invclass[x].Name, invclass[x].InvInf['invdepartment'], pos['house'], pos['code'], pos['longnums'], pos['shortnums'] ] outdata.append(data) elif pos['house'] == 'SHFE' and pos['code'][-3:] in [s1[1:]]: data = [ now.strftime('%Y-%m-%d'), x, invclass[x].Name, invclass[x].InvInf['invdepartment'], pos['house'], pos['code'], pos['longnums'], pos['shortnums'] ] outdata.append(data) outdata.sort(key=itemgetter(2)) #,reverse=True) return colname, outdata, colname1
def client_pos_mon(invclass, ini_codeclass, datapath): '''公司客户超仓监控''' now = datetime.datetime.now() now_m = now.month ft = datetime.datetime(now.year, now.month, 1) now_d = fn.cal_tradeday(ft, now) dp = datapath + u'客户超仓参数.xls' book = xlrd.open_workbook(dp) sh = book.sheets()[0] n = sh.nrows vcol = {} for i in range(n): if sh.cell_value(i, 0) <> u'品种': vcol[sh.cell_value(i, 0).upper()] = i outdata = [] colname = [ u'交易日', u'投资者代码', u'投资者名称', u'营业部名称', u'交易所代码', u'合约代码', u'投保', u'多头持仓', u'空头持仓', u'持仓上限' ] colname1 = [ 'tr_date', 'invid', 'inv_name', 'invdepartment', 'house', 'code', 'posident', 'longnums', 'shortnums', 'pos_limit' ] dtpar, gppar = client_pos_mon_par() for x in invclass: for pos in invclass[x].Position: if len(pos['code']) <= 6 and pos['posident'] <> u'套保': is_exceed = False if pos['vari'] in dtpar['gp1']: par = [ pos['vari'], pos['code'], pos['longnums'], pos['shortnums'], ini_codeclass[pos['code']].Inf['open_interest'] ] par.append(sh.cell_value(vcol[pos['vari']], 1)) par.append(sh.cell_value(vcol[pos['vari']], 2)) par.append(sh.cell_value(vcol[pos['vari']], 4)) par.append(sh.cell_value(vcol[pos['vari']], 7)) is_exceed, limit = client_pos_1(par) if pos['vari'] in dtpar['gp2']: par = [ pos['vari'], pos['code'], pos['longnums'], pos['shortnums'], ini_codeclass[pos['code']].House ] par.append(sh.cell_value(vcol[pos['vari']], 1)) par.append(sh.cell_value(vcol[pos['vari']], 4)) par.append(sh.cell_value(vcol[pos['vari']], 7)) is_exceed, limit = client_pos_2(par) if pos['vari'] in dtpar['gp3']: par = [ pos['vari'], pos['code'], pos['longnums'], pos['shortnums'], ini_codeclass[pos['code']].Inf['open_interest'] ] par.append(sh.cell_value(vcol[pos['vari']], 1)) par.append(sh.cell_value(vcol[pos['vari']], 2)) par.append(sh.cell_value(vcol[pos['vari']], 3)) par.append(sh.cell_value(vcol[pos['vari']], 4)) par.append(sh.cell_value(vcol[pos['vari']], 7)) is_exceed, limit = client_pos_3(par) if pos['vari'] in dtpar['gp4']: par = [ pos['vari'], pos['code'], pos['longnums'], pos['shortnums'], ini_codeclass[pos['code']].House ] par.append(sh.cell_value(vcol[pos['vari']], 1)) par.append(sh.cell_value(vcol[pos['vari']], 4)) par.append(sh.cell_value(vcol[pos['vari']], 5)) par.append(sh.cell_value(vcol[pos['vari']], 7)) is_exceed, limit = client_pos_4(par) if is_exceed: data = [ now.strftime('%Y-%m-%d'), x, invclass[x].Name, invclass[x].InvInf['invdepartment'], pos['house'], pos['code'], pos['posident'], pos['longnums'], pos['shortnums'], limit ] outdata.append(data) outdata.sort(key=itemgetter(2)) return colname, outdata, colname1