Ejemplo n.º 1
0
def stats_to_algo_folder(stats, algo_namespace, recorded_cols=None):
    """
    Saves the performance stats to the algo local folder.

    Parameters
    ----------
    stats: list[Object]
    algo_namespace: str
    recorded_cols: list[str]

    Returns
    -------
    str

    """
    bytes_to_write = get_csv_stats(stats, recorded_cols=recorded_cols)

    timestr = time.strftime('%Y%m%d')
    folder = get_algo_folder(algo_namespace)

    stats_folder = os.path.join(folder, 'stats')
    ensure_directory(stats_folder)

    filename = os.path.join(stats_folder, '{}.csv'.format(timestr))

    with open(filename, 'wb') as handle:
        handle.write(bytes_to_write)

    return bytes_to_write
Ejemplo n.º 2
0
    def get_frame_stats(self):
        """
        preparing the stats before analyze
        :return: stats: pd.Dataframe
        """
        # add the last day stats which is not saved in the directory
        current_stats = pd.DataFrame(self.frame_stats)
        current_stats.set_index('period_close', drop=False, inplace=True)

        # get the location of the directory
        algo_folder = get_algo_folder(self.algo_namespace)
        folder = join(algo_folder, 'frame_stats')

        if exists(folder):
            files = [f for f in listdir(folder) if isfile(join(folder, f))]

            period_stats_list = []
            for item in files:
                filename = join(folder, item)

                with open(filename, 'rb') as handle:
                    perf_period = pickle.load(handle)
                    period_stats_list.extend(perf_period)

            stats = pd.DataFrame(period_stats_list)
            stats.set_index('period_close', drop=False, inplace=True)

            return pd.concat([stats, current_stats])
        else:
            return current_stats
Ejemplo n.º 3
0
def stats_to_algo_folder(stats, algo_namespace, recorded_cols=None):
    """
    Saves the performance stats to the algo local folder.

    Parameters
    ----------
    stats: list[Object]
    algo_namespace: str
    recorded_cols: list[str]

    Returns
    -------
    str

    """
    bytes_to_write = get_csv_stats(stats, recorded_cols=recorded_cols)

    timestr = time.strftime('%Y%m%d')
    folder = get_algo_folder(algo_namespace)

    stats_folder = os.path.join(folder, 'stats')
    ensure_directory(stats_folder)

    filename = os.path.join(stats_folder, '{}.csv'.format(timestr))

    with open(filename, 'wb') as handle:
        handle.write(bytes_to_write)

    return bytes_to_write
Ejemplo n.º 4
0
    def interrupt_algorithm(self):
        self.is_running = False

        if self._analyze is None:
            log.info('Exiting the algorithm.')

        else:
            log.info('Exiting the algorithm. Calling `analyze()` '
                     'before exiting the algorithm.')

            algo_folder = get_algo_folder(self.algo_namespace)
            folder = join(algo_folder, 'daily_performance')
            files = [f for f in listdir(folder) if isfile(join(folder, f))]

            daily_perf_list = []
            for item in files:
                filename = join(folder, item)

                with open(filename, 'rb') as handle:
                    perf_period = pickle.load(handle)
                    perf_period_dict = perf_period.to_dict()
                    daily_perf_list.append(perf_period_dict)

            stats = pd.DataFrame(daily_perf_list)
            stats.set_index('period_close', drop=False, inplace=True)

            self.analyze(stats)

        sys.exit(0)
Ejemplo n.º 5
0
    def interrupt_algorithm(self):
        self.is_running = False

        if self._analyze is None:
            log.info('Exiting the algorithm.')

        else:
            log.info('Exiting the algorithm. Calling `analyze()` '
                     'before exiting the algorithm.')

            algo_folder = get_algo_folder(self.algo_namespace)
            folder = join(algo_folder, 'daily_perf')
            files = [f for f in listdir(folder) if isfile(join(folder, f))]

            daily_perf_list = []
            for item in files:
                filename = join(folder, item)
                with open(filename, 'rb') as handle:
                    daily_perf_list.append(pickle.load(handle))

            stats = pd.DataFrame(daily_perf_list)

            self.analyze(stats)

        sys.exit(0)
Ejemplo n.º 6
0
    def interrupt_algorithm(self):
        """

        when algorithm comes to an end this function is called.
        extracts the stats and calls analyze.
        after finishing, it exits the run.

        Parameters
        ----------

        Returns
        -------

        """
        self.is_running = False

        if self._analyze is None:
            log.info('Exiting the algorithm.')

        else:
            log.info('Exiting the algorithm. Calling `analyze()` '
                     'before exiting the algorithm.')

            # add the last day stats which is not saved in the directory
            current_stats = pd.DataFrame(self.frame_stats)
            current_stats.set_index('period_close', drop=False, inplace=True)

            # get the location of the directory
            algo_folder = get_algo_folder(self.algo_namespace)
            folder = join(algo_folder, 'frame_stats')

            if exists(folder):
                files = [f for f in listdir(folder) if isfile(join(folder, f))]

                period_stats_list = []
                for item in files:
                    filename = join(folder, item)

                    with open(filename, 'rb') as handle:
                        perf_period = pickle.load(handle)
                        period_stats_list.extend(perf_period)

                stats = pd.DataFrame(period_stats_list)
                stats.set_index('period_close', drop=False, inplace=True)

                stats = pd.concat([stats, current_stats])
            else:
                stats = current_stats

            self.analyze(stats)

        sys.exit(0)