Example #1
0
    def handle_data(self, data):
        """
        Wrapper around the handle_data method of each algo.

        Parameters
        ----------
        data

        """
        if not self.is_running:
            return

        # Resetting the frame stats every day to minimize memory footprint
        today = data.current_dt.floor('1D')
        if self.current_day is not None and today > self.current_day:
            self.frame_stats = list()

        new_transactions, new_commissions, closed_orders = \
            self.blotter.get_transactions(data)

        if len(new_transactions) > 0:
            self.perf_tracker.update_performance()

        cash, positions_value = self.synchronize_portfolio()
        log.info(
            'got totals from exchanges, cash: {} positions: {}'.format(
                cash, positions_value
            )
        )
        if self._handle_data:
            self._handle_data(self, data)

        # Unlike trading controls which remain constant unless placing an
        # order, account controls can change each bar. Thus, must check
        # every bar no matter if the algorithm places an order or not.
        self.validate_account_controls()

        try:
            self._save_stats_csv(self._process_stats(data))
        except Exception as e:
            log.warn('unable to calculate performance: {}'.format(e))

        # TODO: pickle does not seem to work in python 3
        try:
            save_algo_object(
                algo_name=self.algo_namespace,
                key='perf_tracker',
                obj=self.perf_tracker
            )
        except Exception as e:
            log.warn('unable to save minute perfs to disk: {}'.format(e))

        self.current_day = data.current_dt.floor('1D')
Example #2
0
    def _process_stats(self, data):
        today = data.current_dt.floor('1D')

        # Since the clock runs 24/7, I trying to disable the daily
        # Performance tracker and keep only minute and cumulative
        self.perf_tracker.update_performance()

        frame_stats = self.prepare_period_stats(
            data.current_dt, data.current_dt + timedelta(minutes=1))

        # Saving the last hour in memory
        self.frame_stats.append(frame_stats)

        self.add_pnl_stats(frame_stats)
        if self.recorded_vars:
            self.add_custom_signals_stats(frame_stats)
            recorded_cols = list(self.recorded_vars.keys())

        else:
            recorded_cols = None

        self.add_exposure_stats(frame_stats)

        log.info(
            'statistics for the last {stats_minutes} minutes:\n'
            '{stats}'.format(
                stats_minutes=self.stats_minutes,
                stats=get_pretty_stats(
                    stats=self.frame_stats,
                    recorded_cols=recorded_cols,
                    num_rows=self.stats_minutes
                )
            ))

        # Saving the daily stats in a format usable for performance
        # analysis.
        daily_stats = self.prepare_period_stats(
            start_dt=today,
            end_dt=data.current_dt
        )
        save_algo_object(
            algo_name=self.algo_namespace,
            key=today.strftime('%Y-%m-%d'),
            obj=daily_stats,
            rel_path='daily_perf'
        )

        return recorded_cols
    def handle_data(self, data):
        if not self.is_running:
            return

        self._synchronize_portfolio()

        transactions = self._check_open_orders()
        for transaction in transactions:
            self.perf_tracker.process_transaction(transaction)

        if self._handle_data:
            self._handle_data(self, data)

        # Unlike trading controls which remain constant unless placing an
        # order, account controls can change each bar. Thus, must check
        # every bar no matter if the algorithm places an order or not.
        self.validate_account_controls()

        try:
            # Since the clock runs 24/7, I trying to disable the daily
            # Performance tracker and keep only minute and cumulative
            self.perf_tracker.update_performance()

            minute_stats = self.prepare_period_stats(
                data.current_dt, data.current_dt + timedelta(minutes=1))
            # Saving the last hour in memory
            self.minute_stats.append(minute_stats)

            print_df = pd.DataFrame(list(self.minute_stats))
            log.debug(
                'statistics for the last {stats_minutes} minutes:\n{stats}'.
                format(stats_minutes=self.stats_minutes,
                       stats=get_pretty_stats(print_df, self.stats_minutes)))

            today = pd.to_datetime('today', utc=True)
            daily_stats = self.prepare_period_stats(
                start_dt=today, end_dt=pd.Timestamp.utcnow())
            save_algo_object(algo_name=self.algo_namespace,
                             key=today.strftime('%Y-%m-%d'),
                             obj=daily_stats,
                             rel_path='daily_perf')

        except Exception as e:
            log.warn('unable to calculate performance: {}'.format(e))

        try:
            save_algo_object(algo_name=self.algo_namespace,
                             key='perf_tracker',
                             obj=self.perf_tracker)
        except Exception as e:
            log.warn('unable to save minute perfs to disk: {}'.format(e))

        try:
            save_algo_object(algo_name=self.algo_namespace,
                             key='portfolio_{}'.format(self.exchange.name),
                             obj=self.exchange.portfolio)
        except Exception as e:
            log.warn('unable to save portfolio to disk: {}'.format(e))
Example #4
0
    def handle_data(self, data):
        """
        Wrapper around the handle_data method of each algo.

        Parameters
        ----------
        data

        """
        if not self.is_running:
            return

        self._synchronize_portfolio()

        transactions = self._check_open_orders()
        if len(transactions) > 0:
            for transaction in transactions:
                self.perf_tracker.process_transaction(transaction)

            self.perf_tracker.update_performance()

        if self._handle_data:
            self._handle_data(self, data)

        # Unlike trading controls which remain constant unless placing an
        # order, account controls can change each bar. Thus, must check
        # every bar no matter if the algorithm places an order or not.
        self.validate_account_controls()

        try:
            # Since the clock runs 24/7, I trying to disable the daily
            # Performance tracker and keep only minute and cumulative
            self.perf_tracker.update_performance()

            frame_stats = self.prepare_period_stats(
                data.current_dt, data.current_dt + timedelta(minutes=1))

            # Saving the last hour in memory
            self.frame_stats.append(frame_stats)

            self.add_pnl_stats(frame_stats)
            if self.recorded_vars:
                self.add_custom_signals_stats(frame_stats)
                recorded_cols = list(self.recorded_vars.keys())
            else:
                recorded_cols = None

            self.add_exposure_stats(frame_stats)

            print_df = pd.DataFrame(list(self.frame_stats))
            log.info(
                'statistics for the last {stats_minutes} minutes:\n{stats}'.
                format(stats_minutes=self.stats_minutes,
                       stats=get_pretty_stats(stats_df=print_df,
                                              recorded_cols=recorded_cols,
                                              num_rows=self.stats_minutes)))

            today = pd.to_datetime('today', utc=True)
            daily_stats = self.prepare_period_stats(
                start_dt=today, end_dt=pd.Timestamp.utcnow())
            save_algo_object(algo_name=self.algo_namespace,
                             key=today.strftime('%Y-%m-%d'),
                             obj=daily_stats,
                             rel_path='daily_perf')

        except Exception as e:
            log.warn('unable to calculate performance: {}'.format(e))

        # TODO: pickle does not seem to work in python 3
        try:
            save_algo_object(algo_name=self.algo_namespace,
                             key='perf_tracker',
                             obj=self.perf_tracker)
        except Exception as e:
            log.warn('unable to save minute perfs to disk: {}'.format(e))

        try:
            for exchange_name in self.exchanges:
                exchange = self.exchanges[exchange_name]
                save_algo_object(algo_name=self.algo_namespace,
                                 key='portfolio_{}'.format(exchange_name),
                                 obj=exchange.portfolio)
        except Exception as e:
            log.warn('unable to save portfolio to disk: {}'.format(e))