Beispiel #1
0
 def insert(self, table, fields):
     """inserts data in fields into table
     \param table 
     \param fields  must be hash like that {"field_name" : data_to_insert} or list of that hashes
     \return id of last inserted row
     """
     assert(isinstance(fields, dict) or hasattr(fields, "__iter__"))
     names = set()
     for kk in (isinstance(fields, dict) and [fields] or fields):
         for k in kk.keys():
             assert(isinstance(k, basestring))
             names.add(k)
     names = sorted(names)
                 
     if isinstance(fields, dict):
         data = map(lambda a: fields[a], names)
     else:
         data = map(lambda a: map(lambda x: gethash(a, x), names), fields)
     qqs = reduce_by_string(", ", map(lambda a: "?", names))
     nns = reduce_by_string(", ", names)
     query = "insert into {0}({1}) values ({2})".format(table, nns, qqs)
     if isinstance(fields, dict):
         return self.execute(query, data).lastrowid
     else:
         return self.executemany(query, data).lastrowid
Beispiel #2
0
 def get_micex_deals(self, domparsed):
     """\brief get deals from micex report
     \param domparsed
     \return list of \ref report_deal class instances
     """
     report = domparsed.getElementsByTagName('report')[0]
     deals = report.getElementsByTagName('common_deal')[0].getElementsByTagName('item')
     report_deals = []
     for deal in deals:
         d = report_deal()
         d.set_market_name('MICEX')
         d.set_stock_name(deal.getAttribute('security_name'))
         d.set_dtm(deal.getAttribute('deal_time'))
         d.set_count(deal.getAttribute('quantity'))
         d.set_points(deal.getAttribute('price'))
         d.set_direction(deal.getAttribute('deal_sign'))
         d.set_commission(float(deal.getAttribute('broker_comm')) +
                          float(deal.getAttribute('stock_comm')))
         d.set_sha1(hashlib.sha1(reduce_by_string('', [deal.getAttribute(attrname) for attrname in
                                                       ['deal_time',
                                                        'security_name',
                                                        'price',
                                                        'quantity',
                                                        'order_number',
                                                        'deal_number',
                                                        'deal_sign',
                                                        'board_name',
                                                        'broker_comm',
                                                        'stock_comm']])).hexdigest())
         report_deals.append(d)
     return report_deals
Beispiel #3
0
    def update(self, table, set_fields, where_part = None, where_arguments = tuple([])):
        """executes update on all `set_fields` where `where_part`
        \param table 
        \param set_fields hash like {"id" : value}
        \param where_part string
        \param where_arguments tuple with arguments or list with tuples with arguments
        """
        assert(isinstance(set_fields, dict))
        assert(isinstance(where_arguments, (tuple, list)))
        if is_null_or_empty(set_fields):
            return
        
        names = []
        data = []
        for k in set_fields.keys():
            names.append(k)
            data.append(set_fields[k])

        q = 'update {0} set {1}'.format(table, reduce_by_string(', ', map(lambda a: '{0} = ?'.format(a), names)))
        if not is_null_or_empty(where_part):
            q += ' where {0}'.format(where_part)
            wargs = (isinstance(where_arguments, tuple) and [where_arguments] or where_arguments)
            exargs = map(lambda warg: tuple(list(data) + list(warg)), wargs)
            self.executemany(q, exargs)
        else:
            self.execute(q, data)
Beispiel #4
0
 def get_repo_deals(self, source):
     """\brief get repo deals from micex report
     \param source
     \return list of \ref report_deal instances
     """
     parsed = minidom.parse(source.get_filename())
     repdeal = parsed.getElementsByTagName('report')[0].getElementsByTagName('repo_deal')
     if len(repdeal) == 0:
         return []
     repdeal = repdeal[0]
     items = repdeal.getElementsByTagName('item')
     report_type = self.get_report_type(parsed)
     if report_type not in ['micex', 'forts']:
         raise od_exception('report type must be micex or forts')
     ret = []
     for item in items:
         d = report_deal()
         d.set_market_name(('FORTS' if report_type == 'forts' else 'MICEX'))
         d.set_stock_name(item.getAttribute('security_name'))
         d.set_dtm(item.getAttribute('exec_date'))
         d.set_points(item.getAttribute('deal_price'))
         d.set_count(item.getAttribute('quantity'))
         d.set_direction(item.getAttribute('exec_sign'))
         try:
             d.set_commission(item.getAttribute('broker_comm'))
         except:
             pass
         d.set_attributes({'db.no_position' : 1,
                           'deal.repo' : int(item.getAttribute('repo_part'))})
         d.set_user_attributes({'REPO' : int(item.getAttribute('repo_part'))})
         d.set_sha1(hashlib.sha1(reduce_by_string('', [item.getAttribute(attrname) for attrname in
                                                       ['deal_time',
                                                        'security_type',
                                                        'security_name',
                                                        'grn_code',
                                                        'deal_price',
                                                        'exec_sign',
                                                        'repo_part',
                                                        'quantity']] + ['repo'])).hexdigest())
         ret.append(d)
     return ret
Beispiel #5
0
 def get_forts_deals(self, domparsed):
     """\brief return list of deals for forts report type
     \param domparsed
     \return list of \ref report_deal objects
     """
     report = domparsed.getElementsByTagName('report')[0]
     deals = report.getElementsByTagName('common_deal')[0].getElementsByTagName('item')
     report_deals = []
     for deal in deals:
         d = report_deal()
         d.set_market_name('FORTS')
         d.set_stock_name(deal.getAttribute('security_name'))
         d.set_dtm(deal.getAttribute('deal_date'))
         d.set_points(deal.getAttribute('price'))
         d.set_count(deal.getAttribute('quantity'))
         d.set_direction(deal.getAttribute('deal_sign'))
         d.set_sha1(hashlib.sha1(reduce_by_string('', [deal.getAttribute(attrname) for attrname in
                                                       ['deal_date',
                                                        'security_name',
                                                        'expiration_date',
                                                        'price',
                                                        'quantity',
                                                        'order_number',
                                                        'deal_number',
                                                        'deal_sign']])).hexdigest())
         report_deals.append(d)
     atl = report.getElementsByTagName('account_totally_line')[0]
     atl_items = atl.getElementsByTagName('item')
     broker = find_in_list(lambda a: a.getAttribute(u'total_description') == u'Вознаграждение Брокера', atl_items)
     board = find_in_list(lambda a: a.getAttribute(u'total_description') == u'Биржевой сбор', atl_items)
     brcomm = (0 if broker == None else float(atl_items[broker].getAttribute('total_value')))
     boardcomm = (0 if board == None else float(atl_items[board].getAttribute('total_value')))
     summcomm = abs(brcomm) + abs(boardcomm) # summary commission from the report
     specific_comm = summcomm / sum([d.get_volume() for d in report_deals])
     for deal in report_deals:
         deal.set_commission(specific_comm * deal.get_volume()) # set commission to the deal
     return report_deals
Beispiel #6
0
 def get_account_ios(self, source):
     """\brief return list of account in out 
     \param source \ref open_ru_source instance
     \return list of \ref account_io
     """
     ret = []
     parsed = minidom.parse(source.get_filename())
     nontr = parsed.getElementsByTagName('report')[0].getElementsByTagName('nontrade_money_operation')
     if len(nontr) == 0:
         return []
     nontr = nontr[0]                # nontrade operations found
     items = nontr.getElementsByTagName('item')
     for it in items:
         aio = account_io()
         aio.set_dtm(it.getAttribute('operation_date'))
         aio.set_value(it.getAttribute('amount'))
         aio.set_comment(it.getAttribute('comment'))
         aio.set_sha1(hashlib.sha1(reduce_by_string('',[it.getAttribute(attrname) for attrname in
                                                        ['operation_date',
                                                         'amount',
                                                         'comment',
                                                         'ground']])).hexdigest())
         ret.append(aio)
     return ret
Beispiel #7
0
 def check_file(self):
     if not (self.xml.childNodes.length == 1 and self.xml.childNodes[0].nodeName == "report"):
         raise od_exception_report_error(u'There is no tag report')
     self.report = self.xml.childNodes[0]
     for name in ["common_deal", "account_totally_line"]:
         if self.report.getElementsByTagName(name).length != 1:
             raise od_exception_report_error("there is no {0} in report or more that one found".format(name))
     cd = self.report.getElementsByTagName("common_deal")[0].getElementsByTagName("item") # list of 'item' elements in 'common_deal' tag
     ta = self.report.getElementsByTagName("account_totally_line")[0].getElementsByTagName("item") # list of 'item' elements in 'account_totally_line' tag
     self.total_account = ta
     if not (len(cd) > 0 and len(ta) > 1):
         raise od_exception_report_error(u'Странное количество тегов item в отчете, либо отчет битый, либо это вобще не отчет')
     attributes = map(lambda a: a.attributes, cd)
     report_type = set(map(lambda a: a['security_type'].nodeValue, attributes)) # all the posible 'security_type' attributes in the report
     if report_type == set(['FUT']): # if there is just 'FUT' type of the stock in the report
         prices = reduce(lambda a, b: a + b, # summary of prices for all deals
                         map(lambda b: float(b['price'].nodeValue), attributes))
         commission = reduce(lambda a, b: abs(float(a.attributes['total_value'].nodeValue)) +
                             abs(float(b.attributes['total_value'].nodeValue)), # sum of commission from 'account_totally_line' tag
                             filter(lambda c: c.attributes['total_description'].nodeValue in (u'Вознаграждение Брокера', u'Биржевой сбор'), ta))
         prices = commission / prices # now this is the commission value per one point
         self.papers = list(set(map(lambda a: hashed_dict({'name' : a['security_name'].nodeValue, # get unique paper records from the report
                                                           'type' : 'future',
                                                           'stock' : a['board_name'].nodeValue}), attributes)))
         for paper in self.papers: # fill each paper record with deals records
             deals = map(lambda a: {'sha1' : hashlib.sha1(reduce_by_string('', (a['deal_date'].nodeValue, a['security_name'].nodeValue, a['expiration_date'].nodeValue, a['price'].nodeValue, a['quantity'].nodeValue, a['order_number'].nodeValue, a['deal_number'].nodeValue, a['deal_sign'].nodeValue))).hexdigest(),
                                    'count' : math.trunc(float(a['quantity'].nodeValue)),
                                    'direction' : math.trunc(float(a['deal_sign'].nodeValue)),
                                    'points' : float(a['price'].nodeValue),
                                    'commission' : prices * float(a['price'].nodeValue), # this is because the commission is stored in 'total_account' tag of the report
                                    'datetime' : datetime.datetime.strptime(a['deal_date'].nodeValue, '%Y-%m-%dT%H:%M:%S')},
                         filter(lambda b: paper['name'] == b['security_name'].nodeValue and paper['stock'] == b['board_name'].nodeValue, attributes))
             paper['deals'] = deals
             
     elif report_type == set([u'Акции']): # if there is just 'Акции' type of the stock in the report
         self.papers = list(set(map(lambda a: hashed_dict({'name' : a['security_name'].nodeValue, # get unique paper records from the report
                                                           'stock' : a['board_name'].nodeValue,
                                                           'type' : 'stock'}),
                                    attributes)))
         repo_deals = self.report.getElementsByTagName('repo_deal')
         repo_attrs = None
         if len(repo_deals) == 1:
             repo = repo_deals[0].getElementsByTagName('item')
             repo_attrs = map(lambda a: a.attributes, repo)
             for repo1 in filter(lambda a: a['repo_part'].nodeValue == '1', repo_attrs):
                 for repo2 in filter(lambda a: a['repo_part'].nodeValue == '2' and a['grn_code'].nodeValue == repo1['grn_code'].nodeValue, repo_attrs):
                     repo2['deal_time'] = repo1['deal_time'].nodeValue
                     repo2['broker_comm'] = '0'
         
         for paper in self.papers: # fill each paper record with deal records
             deals = map(lambda a: {'sha1' : hashlib.sha1(reduce_by_string('', (a['deal_time'].nodeValue, a['security_name'].nodeValue, a['price'].nodeValue, a['quantity'].nodeValue, a['order_number'].nodeValue, a['deal_number'].nodeValue, a['deal_sign'].nodeValue, a['board_name'].nodeValue, a['broker_comm'].nodeValue, a['stock_comm'].nodeValue))).hexdigest(),
                                    'count' : math.trunc(float(a['quantity'].nodeValue)),
                                    'direction' : math.trunc(float(a['deal_sign'].nodeValue)),
                                    'points' : float(a['price'].nodeValue),
                                    'commission' : float(a['broker_comm'].nodeValue) + float(a['stock_comm'].nodeValue),
                                    'datetime' : datetime.datetime.strptime(a['deal_time'].nodeValue, '%Y-%m-%dT%H:%M:%S')},
                         filter(lambda b: paper['name'] == b['security_name'].nodeValue and paper['stock'] == b['board_name'].nodeValue, attributes))
             if repo_attrs != None:
                 try:
                     deals.extend(map(lambda a: {'sha1' : hashlib.sha1(reduce_by_string('', (a['deal_time'].nodeValue,
                                                                                             a['security_type'].nodeValue,
                                                                                             a['security_name'].nodeValue,
                                                                                             a['grn_code'].nodeValue,
                                                                                             a['deal_price'].nodeValue,
                                                                                             a['exec_sign'].nodeValue,
                                                                                             a['repo_part'].nodeValue,
                                                                                             a['quantity'].nodeValue,
                                                                                             'repo'))).hexdigest(),
                                                 'count' : math.trunc(float(a['quantity'].nodeValue)),
                                                 'direction' : math.trunc(float(a['exec_sign'].nodeValue)),
                                                 'points' : float(a['deal_price'].nodeValue),
                                                 'commission' : float(a['broker_comm'].nodeValue),
                                                 'datetime' : datetime.datetime.strptime(a['deal_time'].nodeValue, '%Y-%m-%dT%H:%M:%S'),
                                                 'user_attributes' : {'REPO' : a['repo_part'].nodeValue}},
                                      filter(lambda b: b['security_name'].nodeValue == paper['name'], repo_attrs)))
                 except Exception:
                     pass    # FIXME! we must do something here
             paper['deals'] = deals
         nontrade = self.report.getElementsByTagName('nontrade_money_operation')
         if len(nontrade) == 1:
             nt = nontrade[0].getElementsByTagName('item')
             nontrade_attrs = map(lambda a: a.attributes, nt)
             self.nontrade_operations = map(lambda a: {'datetime' : datetime.datetime.strptime(a['operation_date'].nodeValue, '%Y-%m-%dT%H:%M:%S'),
                                                       'sha1' : hashlib.sha1(reduce_by_string('', (a['operation_date'].nodeValue, a['amount'].nodeValue, a['comment'].nodeValue, a['ground'].nodeValue))).hexdigest(),
                                                       'money_count' : float(a['amount'].nodeValue),
                                                       'comment' : a['comment'].nodeValue},
                                            nontrade_attrs)
     else:
         raise od_exception_report_error('This report is strange, dont know what type of report is it futures or stocks ?')