Beispiel #1
0
    def save_position(self):
        """
        save all into db
        """
        instrument_count = 0
        option_count = 0

        # now import all symbol position in files into db
        for symbol, items in self.position.items():
            instrument = items["Instrument"]
            stock = items["Stock"]
            options = items["Options"]

            # get company name
            company = stock["name"]

            # leg_identify insert position

            position = Position(symbol=symbol, company=company, date=self.date)
            position.save()

            # second insert instrument
            instrument_pos = InstrumentPos()
            instrument_pos.save_raw_data(position=position, raw_instrument=instrument)

            # third insert stock
            stock_pos = StockPos()
            stock_pos.save_raw_data(position=position, raw_stock=stock)

            # fourth and final insert option
            for option in options:
                option_pos = OptionPos()
                option_pos.save_raw_data(position=position, raw_option=option)

                option_count += 1

            # update insert count
            instrument_count += 1

        # after finish clear data
        self.position = None

        return instrument_count, option_count
Beispiel #2
0
    def test_format_saves(self):
        ids = []

        for raw_option in self.raw_options:
            # create new option position object
            op = OptionPos()

            # save into db
            op.save_raw_data(position=self.position, raw_option=raw_option)

            # get id
            ids.append(op.id)

        print 'Inserted IDs: %s\n' % ids

        print 'option row count in db: %d' % len(Option.objects.all())
        print 'option position row count in db: %d\n' % len(OptionPos.objects.all())

        print 'option rows:\n%s\n%s\n' % \
              (Option.objects.get(id=1), Option.objects.get(id=2))

        print 'option position rows:\n%s\n%s\n' % \
              (OptionPos.objects.get(id=1), OptionPos.objects.get(id=2))