Ejemplo n.º 1
0
    def ready_real_file(cls, path, real_date, file_date):
        """
        Prepare data by import file into database
        :param path: str 'real_path or test_path'
        :param real_date: str 'date format 2014-02-27'
        :param file_date: str 'date format 2014-02-28'
        """
        acc_data = open(
            os.path.join(path, real_date,
                         '%s-AccountStatement.csv' % file_date)).read()
        pos_data = open(
            os.path.join(path, real_date,
                         '%s-PositionStatement.csv' % file_date)).read()
        ta_data = open(
            os.path.join(path, real_date,
                         '%s-TradeActivity.csv' % file_date)).read()

        statement = Statement()
        statement.date = real_date
        statement.account_statement = acc_data
        statement.position_statement = pos_data
        statement.trade_activity = ta_data
        statement.save()

        SaveAccountStatement(date=file_date,
                             statement=statement,
                             file_data=acc_data).save_all()

        SavePositionStatement(date=file_date,
                              statement=statement,
                              file_data=pos_data).save_all()

        SaveTradeActivity(date=file_date,
                          statement=statement,
                          file_data=ta_data).save_all()
Ejemplo n.º 2
0
    def ready_real_file(cls, path, real_date, file_date):
        """
        Prepare data by import file into database
        :param path: str 'real_path or test_path'
        :param real_date: str 'date format 2014-02-27'
        :param file_date: str 'date format 2014-02-28'
        """
        acc_data = open(os.path.join(
            path, real_date, '%s-AccountStatement.csv' % file_date)
        ).read()
        pos_data = open(os.path.join(
            path, real_date, '%s-PositionStatement.csv' % file_date)
        ).read()
        ta_data = open(os.path.join(
            path, real_date, '%s-TradeActivity.csv' % file_date)
        ).read()

        statement = Statement()
        statement.date = real_date
        statement.account_statement = acc_data
        statement.position_statement = pos_data
        statement.trade_activity = ta_data
        statement.save()

        SaveAccountStatement(
            date=file_date,
            statement=statement,
            file_data=acc_data
        ).save_all()

        SavePositionStatement(
            date=file_date,
            statement=statement,
            file_data=pos_data
        ).save_all()

        SaveTradeActivity(
            date=file_date,
            statement=statement,
            file_data=ta_data
        ).save_all()
Ejemplo n.º 3
0
    def setUp(self):
        """
        ready up all variables and test class
        """
        TestUnitSetUp.setUp(self)

        self.spread = None

        # foreign key object
        self.statement = None
        self.trade_summary = None
        self.underlying = None

        # list use for determine spread
        self.spreads = list()
        self.sides = list()
        self.quantities = list()
        self.contracts = list()

        self.filled_order = None

        self.create_filled_order = create_filled_order

        # noinspection PyBroadException
        try:
            self.statement = Statement(
                date='2015-01-01',
                account_statement='',
                position_statement='',
                trade_activity=''
            )
            self.statement.save()

            self.trade_summary = TradeSummary(
                date='2015-01-01',
                statement=self.statement
            )
            self.trade_summary.save()

            self.assertTrue(self.statement.id)
            self.assertTrue(self.trade_summary.id)

            self.underlying = Underlying(
                symbol='UNG1',  # add 1 as a test symbol
                company='United States Natural Gas Fund'
            )
            self.underlying.save()

            self.future = Future(
                symbol='ZWH5',
                lookup='ZW',
                description='WHEAT FUTURE',
                spc='1/50'
            )
            self.future.save()

            self.forex = Forex(
                symbol='USD/OJY',  # test symbol usd convert to my money
                description='DOLLARS/JINYANG SPOT'
            )
            self.forex.save()

            print 'using statement: %s' % self.statement
            print 'using trade summary: %s' % self.trade_summary
            print 'using underlying: %s' % self.underlying
            print 'using future: %s' % self.future
            print 'using forex: %s' % self.forex
            print '-' * 80
        except Exception:
            print 'Statement, trade_summary, underlying already exists...'

            self.statement = Statement.objects.get(date='2015-01-01')
            self.trade_summary = TradeSummary.objects.get(date='2015-01-01')
            self.underlying = Underlying.objects.get(symbol='UNG1')
            self.future = Future.objects.get(symbol='NGX5')
            self.forex = Forex.objects.get(symbol='USD/OJY')
Ejemplo n.º 4
0
class TestPrepareOrders(TestCase):
    def setUp(self):
        """
        ready up all variables and test class
        """
        print '=' * 100
        print "<%s> currently run: %s" % (self.__class__.__name__, self._testMethodName)
        print '-' * 100 + '\n'

        self.spread = None

        # foreign key object
        self.statement = None
        self.trade_summary = None
        self.underlying = None

        # list use for determine spread
        self.spreads = list()
        self.sides = list()
        self.quantities = list()
        self.contracts = list()

        self.option_order = None

        self.create_filled_order = create_filled_order

        # noinspection PyBroadException
        try:
            self.statement = Statement(
                date='2015-01-01',
                account_statement='',
                position_statement='',
                trade_activity=''
            )
            self.statement.save()

            self.trade_summary = TradeSummary(
                date='2015-01-01',
                statement=self.statement
            )
            self.trade_summary.save()

            self.assertTrue(self.statement.id)
            self.assertTrue(self.trade_summary.id)

            self.underlying = Underlying(
                symbol='UNG1',  # add 1 as a test symbol
                company='United States Natural Gas Fund'
            )
            self.underlying.save()

            self.future = Future(
                symbol='ZWH5',
                lookup='ZW',
                description='WHEAT FUTURE',
                spc='1/50'
            )
            self.future.save()

            self.forex = Forex(
                symbol='USD/OJY',  # test symbol usd convert to my money
                description='DOLLARS/JINYANG SPOT'
            )
            self.forex.save()

            """
            print 'using statement: %s' % self.statement
            print 'using trade summary: %s' % self.trade_summary
            print 'using underlying: %s' % self.underlying
            print 'using future: %s' % self.future
            print 'using forex: %s' % self.forex
            print '-' * 80
            """
        except Exception:
            print 'Statement, trade_summary, underlying already exists...'

            self.statement = Statement.objects.get(date='2015-01-01')
            self.trade_summary = TradeSummary.objects.get(date='2015-01-01')
            self.underlying = Underlying.objects.get(symbol='UNG1')
            self.future = Future.objects.get(symbol='NGX5')
            self.forex = Forex.objects.get(symbol='USD/OJY')

    # noinspection PyBroadException
    def tearDown(self):
        """
        remove variables after test
        """
        print '\n' + '=' * 100 + '\n\n'

        print '-' * 80
        print 'remove statement, trade_summary, underlying...'
        try:
            self.statement.delete()
            self.trade_summary.delete()
            self.underlying.delete()
            self.future.delete()
            self.forex.delete()
        except Exception:
            Statement.objects.filter(date='2015-01-01').delete()
            TradeSummary.objects.filter(date='2015-01-01').delete()
            Underlying.objects.filter(symbol='UNG1').delete()
            Future.objects.filter(symbol='NGX5').delete()
            Forex.objects.filter(symbol='USD/OJY').delete()
Ejemplo n.º 5
0
    def clean(self):
        """
        Validate date for file date and all import file name date
        """
        cleaned_data = super(PmsImportStatementsForm, self).clean()

        if not len(self._errors):
            # no error found for field
            real_date = cleaned_data.get("date").strftime('%Y-%m-%d')

            acc_date = datetime.strptime(
                cleaned_data.get("account_statement").name[:10], '%Y-%m-%d'
            )
            pos_date = datetime.strptime(
                cleaned_data.get("position_statement").name[:10], '%Y-%m-%d'
            )
            ta_date = datetime.strptime(
                cleaned_data.get("trade_activity").name[:10], '%Y-%m-%d'
            )

            acc_date = acc_date - BDay(1)
            acc_date = acc_date.strftime('%Y-%m-%d')
            pos_date = pos_date - BDay(1)
            pos_date = pos_date.strftime('%Y-%m-%d')
            ta_date = ta_date - BDay(1)
            ta_date = ta_date.strftime('%Y-%m-%d')

            if acc_date == pos_date == ta_date:
                if real_date != acc_date:
                    error_message = 'All date must have (-1 BDay, %s != %s).' % (real_date, acc_date)
                    self._errors['date'] = self.error_class([error_message])
            else:
                error_message = 'All file date must be same.'
                self._errors['date'] = self.error_class([error_message])

            if not len(self._errors):
                # save here so django can track error location
                acc_data = cleaned_data.get("account_statement")
                acc_data = acc_data.read()
                pos_data = cleaned_data.get("position_statement")
                pos_data = pos_data.read()
                ta_data = cleaned_data.get("trade_activity")
                ta_data = ta_data.read()

                statement = Statement()
                statement.date = real_date
                statement.account_statement = acc_data
                statement.position_statement = pos_data
                statement.trade_activity = ta_data
                statement.save()

                SaveAccountStatement(
                    date=real_date,
                    statement=statement,
                    file_data=acc_data
                ).save_all()

                SavePositionStatement(
                    date=real_date,
                    statement=statement,
                    file_data=pos_data
                ).save_all()

                trade_summary_id = SaveTradeActivity(
                    date=real_date,
                    statement=statement,
                    file_data=ta_data
                ).save_all()

                SaveStatDay(statement).save_all()

                # create or update position_set
                filled_orders = FilledOrder.objects.filter(trade_summary__id=trade_summary_id)
                controller = PositionSetController(filled_orders)
                controller.close_position_sets(date=real_date)
                controller.create_position_sets(date=real_date)
                controller.batch_update_foreign_keys(date=real_date)

                self.cleaned_data['statement_id'] = statement.id
                self.cleaned_data['statement_name'] = statement.__unicode__()

        return cleaned_data
Ejemplo n.º 6
0
def statement_import_all(request):
    """
    import all statements in real_path folder
    :param request: request
    :return: render
    """
    template = 'tos_import/statement_import_all.html'
    real_files_folder = glob.glob('%s/*' % import_path)

    imported_logs = list()
    error_logs = list()
    for folder in real_files_folder:
        if os.path.isdir(folder):
            real_date = os.path.basename(folder)

            try:
                datetime.strptime(real_date, '%Y-%m-%d')
            except ValueError:
                error_logs.append(
                    {
                        'path': folder,
                        'date': real_date.split(' ')[0],
                        'note': real_date.split(' ')[1],
                        'error': 'Invalid filename'
                    }
                )
            else:
                # only import error free folder
                # get file inside and get date
                # skip date exist in db
                if not Statement.objects.filter(date=real_date).exists():
                    statement = glob.glob('%s/*.csv' % folder)[0]
                    file_date = os.path.basename(statement)[0:10]

                    acc_data = pos_data = ta_data = ''
                    acc_file = os.path.join(folder, '%s-AccountStatement.csv' % file_date)
                    pos_file = os.path.join(folder, '%s-PositionStatement.csv' % file_date)
                    ta_file = os.path.join(folder, '%s-TradeActivity.csv' % file_date)

                    statement = Statement()
                    statement.date = real_date

                    if os.path.exists(acc_file):
                        acc_data = open(acc_file).read()
                        statement.account_statement = acc_data
                    if os.path.exists(pos_file):
                        pos_data = open(pos_file).read()
                        statement.position_statement = pos_data
                    if os.path.exists(ta_file):
                        ta_data = open(ta_file).read()
                        statement.trade_activity = ta_data

                    statement.save()

                    if acc_data:
                        account_summary_id = SaveAccountStatement(
                            date=real_date,
                            statement=statement,
                            file_data=acc_data
                        ).save_all()

                    if pos_data:
                        position_summary_id = SavePositionStatement(
                            date=real_date,
                            statement=statement,
                            file_data=pos_data
                        ).save_all()

                    trade_summary_id = 0
                    if ta_data:
                        trade_summary_id = SaveTradeActivity(
                            date=real_date,
                            statement=statement,
                            file_data=ta_data
                        ).save_all()

                    # save stat day
                    SaveStatDay(statement).save_all()

                    # create or update position_set
                    filled_orders = FilledOrder.objects.filter(trade_summary__id=trade_summary_id)
                    controller = PositionSetController(filled_orders)
                    controller.close_position_sets(date=real_date)
                    controller.create_position_sets(date=real_date)
                    controller.batch_update_foreign_keys(date=real_date)

                    imported_logs.append({
                        'statement': {
                            'id': statement.id,
                            'change_url': reverse(
                                'admin:tos_import_statement_change',
                                args={statement.id}),
                            'delete_url': reverse(
                                'admin:tos_import_statement_delete',
                                args={statement.id}),
                            'date': statement.date
                        },
                        'account_statement': {
                            'id': account_summary_id,
                            'change_url': reverse(
                                'admin:statement_account_accountsummary_change',
                                args={account_summary_id}),
                            'delete_url': reverse(
                                'admin:statement_account_accountsummary_delete',
                                args={account_summary_id}),
                        },
                        'position_statement': {
                            'id': position_summary_id,
                            'change_url': reverse(
                                'admin:statement_position_positionsummary_change',
                                args={position_summary_id}),
                            'delete_url': reverse(
                                'admin:statement_position_positionsummary_delete',
                                args={position_summary_id}),
                        },
                        'trade_activity': {
                            'id': trade_summary_id,
                            'change_url': reverse(
                                'admin:statement_trade_tradesummary_change',
                                args={trade_summary_id}),
                            'delete_url': reverse(
                                'admin:statement_trade_tradesummary_delete',
                                args={trade_summary_id}),
                        },
                    })

                    # log entry
                    LogEntry.objects.log_action(
                        user_id=request.user.id,
                        content_type_id=ContentType.objects.get_for_model(Statement).id,
                        object_id=statement.id,
                        object_repr=statement.__unicode__(),
                        action_flag=ADDITION
                    )

    parameters = dict(
        imported_logs=imported_logs,
        error_logs=error_logs
    )

    # testing page view, delete all after done...
    # Statement.objects.all().delete()

    return render(request, template, parameters)