Ejemplo n.º 1
0
def analyze(context, perf):
    stats = get_pretty_stats(perf)
    print('the algo stats:\n{}'.format(stats))
    # Get the base_currency that was passed as a parameter to the simulation
    exchange = list(context.exchanges.values())[0]
    base_currency = exchange.quote_currency.upper()

    # First chart: Plot portfolio value using base_currency
    ax1 = plt.subplot(411)
    perf.loc[:, ['portfolio_value']].plot(ax=ax1)
    ax1.legend_.remove()
    ax1.set_ylabel('Portfolio Value\n({})'.format(base_currency))
    start, end = ax1.get_ylim()
    ax1.yaxis.set_ticks(np.arange(start, end, (end - start) / 5))

    ax2 = plt.subplot(412, sharex=ax1)
    perf.loc[:, ['btc_price']].plot(ax=ax2, label='PriceB', color='orange')
    ax2.legend_.remove()
    ax2.set_ylabel('BTC USDT')
    start, end = ax2.get_ylim()
    ax2.yaxis.set_ticks(np.arange(0, end, (end - start) / 5))

    transaction_df = extract_transactions(perf)
    if not transaction_df.empty:
        ax2.scatter(transaction_df.index.to_pydatetime(),
                    perf.loc[transaction_df.index, 'btc_price'],
                    marker='x',
                    s=150,
                    c='black',
                    label='')
    plt.show()
Ejemplo n.º 2
0
def analyze(context, perf):
    import matplotlib.pyplot as plt
    log.info('the stats: {}'.format(get_pretty_stats(perf)))

    # The base currency of the algo exchange
    base_currency = list(context.exchanges.values())[0].base_currency.upper()

    # Plot the portfolio value over time.
    ax1 = plt.subplot(611)
    perf.loc[:, 'portfolio_value'].plot(ax=ax1)
    ax1.set_ylabel('Portfolio Value ({})'.format(base_currency))

    # Plot the price increase or decrease over time.
    ax2 = plt.subplot(612, sharex=ax1)
    perf.loc[:, 'price'].plot(ax=ax2, label='Price')

    ax2.set_ylabel('{asset} ({base})'.format(
        asset=context.asset.symbol, base=base_currency
    ))

    transaction_df = extract_transactions(perf)
    if not transaction_df.empty:
        buy_df = transaction_df[transaction_df['amount'] > 0]
        sell_df = transaction_df[transaction_df['amount'] < 0]
        ax2.scatter(
            buy_df.index.to_pydatetime(),
            perf.loc[buy_df.index, 'price'],
            marker='^',
            s=100,
            c='green',
            label=''
        )
        ax2.scatter(
            sell_df.index.to_pydatetime(),
            perf.loc[sell_df.index, 'price'],
            marker='v',
            s=100,
            c='red',
            label=''
        )

    ax4 = plt.subplot(613, sharex=ax1)
    perf.loc[:, 'cash'].plot(
        ax=ax4, label='Base Currency ({})'.format(base_currency)
    )
    ax4.set_ylabel('Cash ({})'.format(base_currency))

    perf['algorithm'] = perf.loc[:, 'algorithm_period_return']

    ax5 = plt.subplot(614, sharex=ax1)
    perf.loc[:, ['algorithm', 'price_change']].plot(ax=ax5)
    ax5.set_ylabel('Percent Change')

    plt.legend(loc=3)

    # Show the plot.
    plt.gcf().set_size_inches(18, 8)
    plt.show()
    pass
Ejemplo n.º 3
0
def analyze(context, perf):
    import matplotlib.pyplot as plt
    log.info('the stats: {}'.format(get_pretty_stats(perf)))

    # The quote currency of the algo exchange
    quote_currency = list(context.exchanges.values())[0].quote_currency.upper()

    # Plot the portfolio value over time.
    ax1 = plt.subplot(611)
    perf.loc[:, 'portfolio_value'].plot(ax=ax1)
    ax1.set_ylabel('Portfolio Value ({})'.format(quote_currency))

    # Plot the price increase or decrease over time.
    ax2 = plt.subplot(612, sharex=ax1)
    perf.loc[:, 'price'].plot(ax=ax2, label='Price')

    ax2.set_ylabel('{asset} ({quote})'.format(
        asset=context.asset.symbol, quote=quote_currency
    ))

    transaction_df = extract_transactions(perf)
    if not transaction_df.empty:
        buy_df = transaction_df[transaction_df['amount'] > 0]
        sell_df = transaction_df[transaction_df['amount'] < 0]
        ax2.scatter(
            buy_df.index.to_pydatetime(),
            perf.loc[buy_df.index, 'price'],
            marker='^',
            s=100,
            c='green',
            label=''
        )
        ax2.scatter(
            sell_df.index.to_pydatetime(),
            perf.loc[sell_df.index, 'price'],
            marker='v',
            s=100,
            c='red',
            label=''
        )

    ax4 = plt.subplot(613, sharex=ax1)
    perf.loc[:, 'cash'].plot(
        ax=ax4, label='Quote Currency ({})'.format(quote_currency)
    )
    ax4.set_ylabel('Cash ({})'.format(quote_currency))

    perf['algorithm'] = perf.loc[:, 'algorithm_period_return']

    ax5 = plt.subplot(614, sharex=ax1)
    perf.loc[:, ['algorithm', 'price_change']].plot(ax=ax5)
    ax5.set_ylabel('Percent Change')

    plt.legend(loc=3)

    # Show the plot.
    plt.gcf().set_size_inches(18, 8)
    plt.show()
    pass
Ejemplo n.º 4
0
def analyze(context, results):
    # Save results in CSV file
    filename = os.path.splitext(os.path.basename('talib_simple'))[0]
    results.to_csv(filename + '.csv')

    log.info('the daily stats:\n{}'.format(get_pretty_stats(results)))
    chart(context, context.prices, context.analysis, results)
    pass
Ejemplo n.º 5
0
def analyze(context, results):
    # Save results in CSV file
    filename = os.path.splitext(os.path.basename('talib_simple'))[0]
    results.to_csv(filename + '.csv')

    log.info('the daily stats:\n{}'.format(get_pretty_stats(results)))
    chart(context, context.prices, context.analysis, results)
    pass
Ejemplo n.º 6
0
def analyze(context, perf):
    stats = get_pretty_stats(perf)
    print('the algo stats:\n{}'.format(stats))
    # Get the base_currency that was passed as a parameter to the simulation
    exchange = list(context.exchanges.values())[0]
    quote_currency = exchange.quote_currency.upper()

    # First chart: Plot portfolio value using base_currency
    ax1 = plt.subplot(411)
    perf.loc[:, ['portfolio_value']].plot(ax=ax1)
    ax1.legend_.remove()
    ax1.set_ylabel('Portfolio Value\n({})'.format(quote_currency))
    start, end = ax1.get_ylim()
    ax1.yaxis.set_ticks(np.arange(start, end, (end - start) / 5))

    ax2 = plt.subplot(412, sharex=ax1)
    perf.loc[:, ['price']].plot(ax=ax2, label='Price')
    ax2.legend_.remove()
    ax2.set_ylabel('{asset}\n({base})'.format(asset=context.asset.symbol,
                                              base=quote_currency))
    start, end = ax2.get_ylim()
    ax2.yaxis.set_ticks(np.arange(0, end, (end - start) / 5))

    transaction_df = extract_transactions(perf)
    if not transaction_df.empty:
        ax2.scatter(transaction_df.index.to_pydatetime(),
                    perf.loc[transaction_df.index, 'price'],
                    marker='x',
                    s=150,
                    c='black',
                    label='')

    # Plot our cash
    ax3 = plt.subplot(413, sharex=ax1)
    perf.cash.plot(ax=ax3)
    ax3.set_ylabel('Cash\n({})'.format(quote_currency))
    start, end = ax3.get_ylim()
    ax3.yaxis.set_ticks(np.arange(0, end, end / 5))

    # Plot % change in look back period
    percent_change_data = perf.loc[:, ['percent_change']]
    pos_percent_change = percent_change_data.copy()
    neg_percent_change = percent_change_data.copy()

    pos_percent_change[pos_percent_change <= 0] = np.nan
    neg_percent_change[neg_percent_change > 0] = np.nan

    ax4 = plt.subplot(414, sharex=ax1)
    pos_percent_change.loc[:, ['percent_change']].plot(ax=ax4, color='g')

    neg_percent_change.loc[:, ['percent_change']].plot(ax=ax4, color='r')

    ax4.set_ylabel('% change 28 d\n({})'.format(quote_currency))
    start, end = ax4.get_ylim()
    ax4.yaxis.set_ticks(np.arange(start, end, end / 5))
    ax4.legend_.remove()

    plt.show()
Ejemplo n.º 7
0
def analyze(context, perf):
    log.info('the daily stats:\n{}'.format(get_pretty_stats(perf)))

    ax1 = plt.subplot(211)
    perf.portfolio_value.plot(ax=ax1)
    ax1.set_ylabel('portfolio value')
    ax2 = plt.subplot(212, sharex=ax1)
    perf.ending_cash.plot(ax=ax2)
    ax2.set_ylabel('ending_cash')
    plt.show()
Ejemplo n.º 8
0
    def analyze(context, perf):
        set_print_settings()

        transaction_df = extract_transactions(perf)
        print('the transactions:\n{}'.format(transaction_df))

        orders_df = extract_orders(perf)
        print('the orders:\n{}'.format(orders_df))

        stats = get_pretty_stats(perf, show_tail=False, num_rows=5)
        print('the stats:\n{}'.format(stats))
        pass
Ejemplo n.º 9
0
    def analyze(context, perf):
        set_print_settings()

        transaction_df = extract_transactions(perf)
        print('the transactions:\n{}'.format(transaction_df))

        orders_df = extract_orders(perf)
        print('the orders:\n{}'.format(orders_df))

        stats = get_pretty_stats(perf, show_tail=False, num_rows=5)
        print('the stats:\n{}'.format(stats))
        pass
Ejemplo n.º 10
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
Ejemplo n.º 11
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)

        # creating and saving the pnl_stats into the local
        # directory
        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,
                )
            ))

        # Since live mode does not use daily frequency,
        # there is no need to save the output of this method.
        self.prepare_period_stats(
            start_dt=today,
            end_dt=data.current_dt
        )

        return recorded_cols
Ejemplo n.º 12
0
def analyze(context, stats):
    log.info('the daily stats:\n{}'.format(get_pretty_stats(stats)))

    exchange = list(context.exchanges.values())[0]
    quote_currency = exchange.quote_currency.upper()
Ejemplo n.º 13
0
def analyze(context, perf):
    stats = get_pretty_stats(perf)
    print(stats)
    pass
Ejemplo n.º 14
0
def analyze(context, stats):
    log.info('the daily stats:\n{}'.format(get_pretty_stats(stats)))
    pass
Ejemplo n.º 15
0
def analyze(context, perf):
    exchange = list(context.exchanges.values())[0]
    quote_currency = exchange.quote_currency.upper()

    #  print(perf)

    #  1st graph
    ax1 = plt.subplot(411)
    perf.loc[:, ['portfolio_value']].plot(ax=ax1)
    ax1.legend_.remove()
    ax1.set_title("Portfolio Value ({})".format(quote_currency), rotation=0)
    start, end = ax1.get_ylim()

    ax1.yaxis.set_ticks(np.arange(start, end, (end - start) / 5))

    # Second graph
    ax2 = plt.subplot(412, sharex=ax1)
    perf.loc[:, ['poloniex_price']].plot(ax=ax2, label='Price')
    ax2.legend_.remove()
    ax2.set_title('Price ({asset} / {quote})'.format(
        asset=context.asset.symbol, quote=quote_currency),
                  rotation=0)
    start, end = ax2.get_ylim()
    #  ax2.yaxis.set_ticks(np.arange(floor(start), ceil(end), 300))
    ax2.yaxis.set_ticks(np.arange(start, end, (start - end) / 5))
    ax2.set_title('Poloniex Price', rotation=0)

    transaction_df = extract_transactions(perf)
    if not transaction_df.empty:
        buy_df = transaction_df[transaction_df['amount'] > 0]
        sell_df = transaction_df[transaction_df['amount'] < 0]
        ax2.scatter(buy_df.index.to_pydatetime(),
                    perf.loc[buy_df.index, 'poloniex_price'],
                    marker='^',
                    s=50,
                    c='green',
                    label='')
        ax2.scatter(sell_df.index.to_pydatetime(),
                    perf.loc[sell_df.index, 'poloniex_price'],
                    marker='v',
                    s=50,
                    c='red',
                    label='')

    ax3 = plt.subplot(413, sharex=ax1)
    perf.loc[:, ['binance_price']].plot(ax=ax3, label='Price')
    ax3.legend_.remove()
    ax3.set_title('Price ({asset} / {quote})'.format(
        asset=context.asset.symbol, quote=quote_currency),
                  rotation=0)
    start, end = ax3.get_ylim()
    #  ax2.yaxis.set_ticks(np.arange(floor(start), ceil(end), 300))
    ax3.yaxis.set_ticks(np.arange(start, end, (start - end) / 5))
    ax3.set_title('Binance Price', rotation=0)

    transaction_df = extract_transactions(perf)
    if not transaction_df.empty:
        buy_df = transaction_df[transaction_df['amount'] > 0]
        sell_df = transaction_df[transaction_df['amount'] < 0]
        ax3.scatter(buy_df.index.to_pydatetime(),
                    perf.loc[buy_df.index, 'binance_price'],
                    marker='^',
                    s=50,
                    c='green',
                    label='')
        ax3.scatter(sell_df.index.to_pydatetime(),
                    perf.loc[sell_df.index, 'binance_price'],
                    marker='v',
                    s=50,
                    c='red',
                    label='')

    # Third graph (cash)
    #  ax3 = plt.subplot(513, sharex=ax1)
    #  perf.cash.plot(ax=ax3)
    #  ax3.set_title('Cash ({})'.format(quote_currency), rotation=0)

    ax4 = plt.subplot(414, sharex=ax1)
    perf.max_drawdown.plot(ax=ax4, kind='area', color='coral', alpha=0.7)
    ax4.set_title('Max drawdown', rotation=0)
    ax4.set_ylim(-1.0, 0)

    #  perf[[
    #  'treasury',
    #  'algorithm',
    #  'benchmark',
    #  ]] = perf[[
    #  'treasury_period_return',
    #  'algorithm_period_return',
    #  'benchmark_period_return',
    #  ]]
    #
    #  ax5 = plt.subplot(515, sharex=ax1)
    #  perf[[
    #  'treasury',
    #  'algorithm',
    #  'benchmark',
    #  ]].plot(ax=ax5)
    #  ax5.set_ylabel('Percent Change')

    plt.savefig("arbitrage.png")

    #  fig = plt.figure()
    #  ax1 = plt.subplot(311)
    #  perf.portfolio_value.plot(ax=ax1)
    #
    #  ax2 = plt.subplot(312, sharex=ax1)
    #  perf.benchmark_period_return.plot(ax=ax2)

    #  print(perf.columns)

    #  ax3 = plt.subplot(313, sharex=ax1)
    #  perf.alpha.plot(ax=ax3)

    plt.show()

    print("Starting Cash: $", perf.starting_cash.iloc[0])
    print("Ending portfolio value: $", perf.portfolio_value.iloc[-1])
    print("Cash: $", perf.cash.iloc[-1])
    print("Ending cash: $", perf.ending_cash.iloc[-1])
    print("Max Drawdown: ", perf.max_drawdown.min() * 100, "%")
    print("Algorithm Period Return: ",
          perf.algorithm_period_return.iloc[-1] * 100, "%")
    print("Pnl $", perf.pnl.sum())

    print(get_pretty_stats(perf))
Ejemplo n.º 16
0
def analyze(context=None, perf=None):
    stats = get_pretty_stats(perf)
    print('the algo stats:\n{}'.format(stats))
    pass
def analyze(context, stats):
    log.info('the daily stats:\n{}'.format(get_pretty_stats(stats)))
    pass
Ejemplo n.º 18
0
    def _process_data(self, context, data):
        """Called at each algo iteration

        Calculates indicators, processes signals, and
        records market and external data

        Arguments:
            context {pandas.Dataframe} -- Catalyst context object
            data {pandas.Datframe} -- Catalyst data object
        """
        # catalyst dumps pickle file after handle_data called
        # so this call uploads the state of
        # the previously compelted iteration

        self.state.i += 1
        self.log.info(f"Processing algo iteration - {self.state.i}")

        if not self.is_backtest and self.state.i > 1:
            outputs.upload_state_to_storage(self)

        else:
            self.log.debug("Skipping stats upload until catalyst writes to file")

        end = arrow.get(self.state.END)
        time_left = end.humanize(only_distance=True)
        self.log.debug(f"Stopping strategy in {time_left}")

        # the following called methods return:
        # True if the iteration should continued
        # False if the algo should not continue

        if not self._set_current_fields(context, data):
            return

        # To check to apply stop-loss, take-profit or keep position
        self.check_open_positions(context)

        # set date first for logging purposes
        self.current_date = get_datetime()

        if not self.fetch_history(context, data):
            return

        #  Filter minute frequency
        self._check_minute_freq(context, data)

        if self.in_job:

            job = get_current_job()
            job.meta["date"] = str(self.current_date)
            job.save_meta()

        for i in context.blotter.open_orders:
            msg = "Canceling unfilled open order {}".format(i)
            self.log.info(msg)
            self.notify(msg)
            cancel_order(i)

        if not self.fetch_history(context, data):
            return

        self._filter_fetched_history(context, data)

        # ## enqueue ml models as soon as data filtered
        if self._ml_models:
            self._enqueue_ml_calcs(context, data)

        else:
            for dataset, manager in self._datasets.items():
                manager.calculate(context)
                manager.record_data(context)

        for i in self._market_indicators:
            try:
                i.calculate(self.state.prices)
                i.record()
            except Exception as e:
                self.log.error(e)
                self.log.error("Error calculating {}, skipping...".format(i.name))

        for i in self._ml_models:
            i.record()

        self._extra_handle(context, data)
        self._count_signals(context, data)

        if context.frame_stats:
            pretty_output = stats_utils.get_pretty_stats(context.frame_stats)
            self.log.debug(pretty_output)
            if not self.is_backtest:
                outputs.save_stats_to_storage(self)

        self.state.dump_to_context(context)