示例#1
0
    def getDataFromMongo(self):
        """
        from mongo get the currency_pairs and bank_rate
        """
        ## currency_pairs
        currency_pairs = list(set(map(lambda x: x['currency_pair'],
                                      self.data)))
        Time = self.Now.split()[0]
        #currency_dict = getcurrentrate(currency_pairs)
        currency_dict = gethistoryrate(currency_pairs, Time)

        ##bank_rate
        self.forwarddict = []
        for lst in self.data:

            forwarddict = {}
            sell_currency = lst['currency_pair'][:3]
            buy_currency = lst['currency_pair'][3:]
            currency_pair = lst['currency_pair']
            #ratetype = getRate((lst['delivery_date'] -lst['trade_date']).days)
            ratetype = '12月'

            #SellRate,BuyRate = getcurrentbankrate(sell_currency,buy_currency,ratetype)
            SellRate, BuyRate = gethistorybankrate(sell_currency, buy_currency,
                                                   ratetype,
                                                   Time)  ##获取银行历史拆借利率

            #sell_amount = float64(lst['sell_amount'])
            #buy_amount  = float64(lst['buy_amount'])
            ##获取厘定日的汇率,如果还未到厘定日,那么汇率返回None
            Setdate = dateTostr(lst['determined_date'])  ##厘定日
            if Setdate is None:
                Setdate = dateTostr(lst['delivery_date'])

            if Time < Setdate:
                SetRate = None
            else:
                SetRate = RateExchange(currency_pair).getdayMax(
                    Setdate)  ##厘定日汇率
            #SetRate = RateExchange(currency_pair).getdayMax(Setdate)##厘定日汇率

            if SetRate == []:
                SetRate = None  ## 还未到厘定日

            ## 执行汇率上下限
            strikeLowerRate = float64(lst['exe_doexrate'])
            strikeUpperRate = float64(lst['exe_upexrate'])

            currentRate = currency_dict.get(currency_pair)
            if currentRate is None:
                print "{ } not Fund!\n".format(currency_pair)
                continue

            deliverydate = dateTostr(lst['delivery_date'])

            #print 'CollaOptions',lst['id'],currency_pair,dateTostr(lst['trade_date']),deliverydate,
            temp = self.cumputeLost(Setdate, SetRate, deliverydate,
                                    strikeLowerRate, strikeUpperRate,
                                    currentRate, SellRate, BuyRate, self.delta)

            #if forwarddict[lst['id']] is not  None:

            ## 计算本金损益
            #forwarddict[lst['id']] = chooselocalmoney(lst,forwarddict[lst['id']])
            """
            'date','com_code','der_id','cap_currency','cap_amount','gal','exrate','data_date':
            日期,公司代码,衍生品编号,本金币种,本金金额,汇兑损益,当天的本金/人民币汇率,数据生成日期
            """
            if temp is not None:
                forwarddict['gal'], forwarddict['cap_amount'], forwarddict[
                    'cap_currency'] = chooselocalmoney(lst, temp)

            forwarddict['date'] = Time
            forwarddict['com_code'] = lst['consign_entity']
            forwarddict['der_id'] = lst['trade_type'] + '_' + str(lst['id'])
            currencypair = forwarddict['cap_currency'] + 'CNY'
            forwarddict['exrate'] = gethistoryrate(
                [currencypair],
                Time)[currencypair] if currencypair != 'CNYCNY' else 1.0
            forwarddict['data_date'] = getNow('%Y-%m-%d %H:%M:%S')
            logs('compute',
                 'CollaOptions',
                 'getDataFromMongo',
                 forwarddict,
                 infoFalg=True)

            self.forwarddict.append(forwarddict)
        #self.forwarddict = forwarddict
        self.updateData()
示例#2
0
    def getDataFromMongo(self):
        """
        from mongo get the currency_pairs and bank_rate
        """
        ## currency_pairs
        currency_pairs = list(set(map(lambda x: x['currency_pair'],
                                      self.data)))
        Time = self.Now.split()[0]
        #currency_dict = getcurrentrate(currency_pairs)
        currency_dict = gethistoryrate(currency_pairs, Time)
        ##--------------------获取汇率对的历史汇率

        ##bank_rate
        self.forwarddict = []
        for lst in self.data:
            forwarddict = {}
            sell_currency = lst['currency_pair'][:3]
            buy_currency = lst['currency_pair'][3:]
            currency_pair = lst['currency_pair']
            #ratetype = getRate((lst['delivery_date'] -lst['trade_date']).days)
            ratetype = '12月'

            #SellRate,BuyRate = getcurrentbankrate(sell_currency,buy_currency,ratetype)
            SellRate, BuyRate = gethistorybankrate(sell_currency, buy_currency,
                                                   ratetype,
                                                   Time)  ##获取银行历史拆借利率
            #sell_amount = float64(lst['sell_amount'])
            #buy_amount = float64(lst['buy_amount'])
            Setdate = dateTostr(lst['determined_date'])
            if Time < Setdate:
                SetRate = None
            else:
                SetRate = RateExchange(currency_pair).getdayMax(
                    Setdate)  ##厘定日汇率
            #SetRate = RateExchange(currency_pair).getdayMax(Setdate)##厘定日汇率
            kncockRate = float64(lst['knockout_exrate'])  ##敲出汇率

            LockedRate = float(lst['rate'])
            currentRate = currency_dict[currency_pair]
            deliverydate = dateTostr(lst['delivery_date'])
            #print 'knockoptions',lst['id'],currency_pair,dateTostr(lst['trade_date']),deliverydate,
            type = lst['type']
            temp = self.cumputeLost(Setdate, SetRate, deliverydate,
                                    currentRate, LockedRate, kncockRate,
                                    SellRate, BuyRate, self.delta, type)

            #print temp
            #print '\n'

            ## 计算本金损益
            #if forwarddict[lst['id']] is not  None:
            #forwarddict[lst['id']] = chooselocalmoney(lst,forwarddict[lst['id']])
            """
            'date','com_code','der_id','cap_currency','cap_amount','gal','exrate','data_date':
            日期,公司代码,衍生品编号,本金币种,本金金额,汇兑损益,当天的本金/人民币汇率,数据生成日期
            """
            if temp is not None:
                forwarddict['gal'], forwarddict['cap_amount'], forwarddict[
                    'cap_currency'] = chooselocalmoney(lst, temp)

            forwarddict['date'] = Time
            forwarddict['com_code'] = lst['consign_entity']
            forwarddict['der_id'] = lst['trade_type'] + '_' + str(lst['id'])
            currencypair = forwarddict['cap_currency'] + 'CNY'
            forwarddict['exrate'] = gethistoryrate(
                [currencypair],
                Time)[currencypair] if currencypair != 'CNYCNY' else 1.0
            forwarddict['data_date'] = getNow('%Y-%m-%d %H:%M:%S')
            logs('compute',
                 'knockoptions',
                 'getDataFromMongo',
                 forwarddict,
                 infoFalg=True)
            self.forwarddict.append(forwarddict)
        #self.forwarddict = forwarddict
        self.updateData()
示例#3
0
文件: forwards.py 项目: 12jk-M/test
    def getDataFromMongo(self):
        """
        from mongo get the currency_pairs and bank_rate
         #卖出币种如果是人民币(CNY/CNH),那么买入币种是本金,否则就是卖出币种
        """
        ## currency_pairs
        ##--------------------获取汇率对的实时汇率
        currency_pairs = list(set(map(lambda x: x['currency_pair'],
                                      self.data)))
        Time = self.Now.split()[0]
        #currency_dict = getcurrentrate(currency_pairs)
        currency_dict = gethistoryrate(currency_pairs, Time)
        ##--------------------获取汇率对的历史汇率

        self.forwarddict = []

        for lst in self.data:
            ##
            forwarddict = {}
            sell_currency = lst['currency_pair'][:3]
            buy_currency = lst['currency_pair'][3:]
            currency_pair = lst['currency_pair']
            #ratetype = getRate((lst['delivery_date'] -lst['trade_date']).days)
            ratetype = '12月'

            #SellRate,BuyRate = getcurrentbankrate(sell_currency,buy_currency,ratetype)##获取银行拆借利率
            SellRate, BuyRate = gethistorybankrate(sell_currency, buy_currency,
                                                   ratetype,
                                                   Time)  ##获取银行历史拆借利率

            LockedRate = float(lst['rate'])  ##锁定汇率

            currentRate = currency_dict.get(currency_pair)  ##即期实时汇率
            if currentRate is None:
                print "{} not fund!\n".format(currency_pair)
                continue

            deliverydate = dateTostr(lst['delivery_date'])  ##交割日期

            #print 'forwards',lst['id'],currency_pair,dateTostr(lst['trade_date']),deliverydate,

            temp = self.cumputeLost(SellRate, BuyRate, deliverydate,
                                    LockedRate, currentRate)
            #print temp

            ## 计算本金损益
            #if forwarddict[lst['id']] is not  None:
            #forwarddict[lst['id']] = chooselocalmoney(lst,forwarddict[lst['id']])
            """
            'date','com_code','der_id','cap_currency','cap_amount','gal','exrate','data_date':
            日期,公司代码,衍生品编号,本金币种,本金金额,汇兑损益,当天的本金/人民币汇率,数据生成日期
            """
            if temp is not None:
                forwarddict['gal'], forwarddict['cap_amount'], forwarddict[
                    'cap_currency'] = chooselocalmoney(lst, temp)

            forwarddict['date'] = Time
            forwarddict['com_code'] = lst['consign_entity']
            forwarddict['der_id'] = lst['trade_type'] + '_' + str(lst['id'])
            currencypair = forwarddict['cap_currency'] + 'CNY'
            forwarddict['exrate'] = gethistoryrate(
                [currencypair],
                Time)[currencypair] if currencypair != 'CNYCNY' else 1.0
            forwarddict['data_date'] = getNow('%Y-%m-%d %H:%M:%S')
            logs('compute',
                 'forwards',
                 'getDataFromMongo',
                 forwarddict,
                 infoFalg=True)
            #forwarddict[lst['consign_entity']]
            self.forwarddict.append(forwarddict)

        #self.forwarddict = forwarddict
        self.updateData()
示例#4
0
 def getDataFromMongo(self):
     """
     from mongo get the currency_pairs and bank_rate
     """
     ## currency_pairs
     currency_pairs = list(set(map(lambda x:x['currency_pair'],self.data)))
     Time = self.Now.split()[0]
     #currency_dict = getcurrentrate(currency_pairs)
     currency_dict = gethistoryrate(currency_pairs,Time)
     
     ##bank_rate
     self.forwarddict = []        
     for lst in self.data:
         forwarddict= {}
         ##交易本币与外币
         ##支付固定利率
         #print '计算订单{}\n'.format(lst)
         payFixRate = None if lst['pay_fix_rate'] is None else float64(lst['pay_fix_rate']) ##支付固定利率
         chargeFixRate  =  None if lst['charge_fix_rate'] is None else  float64(lst['charge_fix_rate'])##收取浮动利息固定部分
         value_date = dateTostr(lst['value_date'])
         delivery_date = dateTostr(lst['delivery_date'])
         trade_date = dateTostr(lst['trade_date'])
         #print lst['id']
         if lst['interest_pay_way'] == '3':
             ##到期一次性支付
             br = BankRate(lst['buy_currency'],interestway(lst['interest_pay_way'],lst['charge_float_libor']))
             if  delivery_date> Time:
                  date = {dateTostr(lst['delivery_date']):br.getday(Time)}
             else:
                  
                date = {dateTostr(lst['delivery_date']):br.getday(delivery_date)}
                
         elif lst['interest_pay_way']== '0' or lst['trade_type']=='4':
             ##提前支付
             br = BankRate(lst['buy_currency'],interestway(lst['interest_pay_way'],lst['charge_float_libor']))
             if (lst['trade_date'] - lst['value_date']).days>180:
                 
                  date = {dateTostr(lst['delivery_date']):br.getday(trade_date)}## value_date trade_date
             else:
                 date = {dateTostr(lst['delivery_date']):br.getday(value_date)}## value_date trade_date
         else:
             #print lst
             if (lst['trade_date'] - lst['value_date']).days>180:
                   date = getbankrateoneday(lst['buy_currency'],trade_date,delivery_date,lst['interest_pay_way'],lst['charge_float_libor'],Time)
             else:
                 date = getbankrateoneday(lst['buy_currency'],value_date,delivery_date,lst['interest_pay_way'],lst['charge_float_libor'],Time)
         #payFixRateDict = {}              
         if  chargeFixRate is  None:
             chargeFixRate=0.0
         
         for t in date:
             
                 date[t] = date[t]/100.0+chargeFixRate##货币浮动利率及支付利息时间
                
         Fix = array(sorted(date.items(),key=lambda x:x[0]))
         FixDate = Fix[:,0]
         FixRate =  Fix[:,1][:]
         FixRate = FixRate.astype(float64)
         Rate = (FixDate,payFixRate,FixRate)##利息支付日期,支付卖出货币利息,收取浮动利率利息
         
         
    
         currency_pair = lst['currency_pair']
         
         ##货币对之间本币与外币
         sellCurrency  = currency_pair[:3]
         buyCurrency  = currency_pair[3:]
         ratetype ='12月'
         #SellRate,BuyRate = getcurrentbankrate(sellCurrency,buyCurrency,ratetype)
         SellRate,BuyRate = gethistorybankrate(sellCurrency,buyCurrency,ratetype,Time)##获取银行历史拆借利率
         
             
         LockedRate = float64(lst['exe_exrate'])##执行汇率
         capped_exrate  = None if lst['capped_exrate'] is None else float64(lst['capped_exrate']) ##封顶汇率
         ##获取厘定日汇率
         ##获取厘定日的汇率,如果还未到厘定日,那么汇率返回None
         Setdate = dateTostr(lst['determined_date'])##厘定日
         if Time<Setdate:
             SetRate = None
         else:
             SetRate = RateExchange(currency_pair).getdayMax(Setdate)##厘定日汇率
         #SetRate = RateExchange(currency_pair).getdayMax(Setdate)##厘定日汇率
         if SetRate ==[]:
             SetRate = None## 还未到厘定日
         
         currentRate = currency_dict.get(currency_pair) ## 实时汇率
         if currentRate is None:
             print "{} not fund!".format(currency_pair)
             continue
         #deliverydate = dateTostr(lst['delivery_date'])## 交割日期
         trade_type = lst['trade_type']##交易类型
         rateway = lst['interest_pay_way']##利息支付方式
         #sell_amount  = float64(lst['sell_amount'])
         #buy_amount  = float64(lst['buy_amount'])
         #2,3,4分别表示:区间式货币掉期(利率进行互换+固定补贴)、货币掉期(利率互换)、封顶式期权(固定补贴)
         #print 'SwapOptions', lst['id'],currency_pair,rateway,'区间式货币掉期' if trade_type=='2' else ('货币掉期' if trade_type=='3' else '封顶式期权'),value_date,delivery_date,
         temp = self.cumputeLost(Setdate,SetRate,value_date,delivery_date,currentRate,SellRate,BuyRate,LockedRate,rateway,self.delta,capped_exrate,trade_type,Rate)
         #if sell_currency+buy_currency !=currency_pair:
         #    forwarddict[lst['id']] = forwarddict[lst['id']]/currentRate
         
         #if forwarddict[lst['id']] is not  None:
             #forwarddict[lst['id']] = chooselocalmoney(lst,forwarddict[lst['id']])
         """
         'date','com_code','der_id','cap_currency','cap_amount','gal','exrate','data_date':
         日期,公司代码,衍生品编号,本金币种,本金金额,汇兑损益,当天的本金/人民币汇率,数据生成日期
         """        
         if temp is not  None:
             forwarddict['gal'],forwarddict['cap_amount'],forwarddict['cap_currency'] = chooselocalmoney(lst,temp)
         
         forwarddict['date'] = Time
         forwarddict['com_code'] = lst['consign_entity']
         forwarddict['der_id'] = lst['trade_type']+'_'+str(lst['id'])
         currencypair = forwarddict['cap_currency']+'CNY'
         forwarddict['exrate'] = gethistoryrate([currencypair],Time)[currencypair] if currencypair!='CNYCNY' else 1.0
         forwarddict['data_date'] = getNow('%Y-%m-%d %H:%M:%S') 
         logs('compute','swaoption','getDataFromMongo',forwarddict,infoFalg=True)
         #if lst['id']==1170:
             #print Setdate,SetRate,value_date,delivery_date,currentRate,SellRate,BuyRate,LockedRate,rateway,self.delta,capped_exrate,trade_type,Rate,forwarddict['gal']
         self.forwarddict.append(forwarddict)
         #print forwarddict
     #self.forwarddict = forwarddict
     self.updateData()