def output_results(self): # Closes off the Backteste.csv file so it can be read via Pandas # without the problems self.backtest_file.close() in_filename = 'backtest.csv' out_filename = 'equity.csv' in_file = os.path.join(OUTPUT_RESULT_DIR, in_filename) out_file = os.path.join(OUTPUT_RESULT_DIR, out_filename) # Create equity curve dataframe df = pd.read_csv(in_file, index_col=0) df.dropna(inplace=True) df["Total"] = df.sum( axis=1) # what value are we summing from backtest.csv? df["Returns"] = df["Total"].pct_change() df["Equity"] = (1.0 + df["Returns"]).cumprod() # Create drawdown statistics drawdown, max_dd, dd_duration = create_drawdowns( df["Equity"]) # input needs to be pnl df['Drawdown'] = drawdown df.to_csv(out_file, index=True) print("Simulation compelte and results exported to %s" % out_filename)
def output_summary_stats(self): """ Creates a list of summary statistics for the portfolio. """ total_return = self.equity_curve['equity_curve'][-1] returns = self.equity_curve['returns'] pnl = self.equity_curve['equity_curve'] sharpe_ratio = create_sharpe_ratio(returns, periods=252 * 60 * 6.5) #import time #time0 = time.time() max_dd, dd_duration = create_drawdowns(pnl) #time1 = time.time() #print('timing create drawdown: {}'.format(time1 - time0)) stats = dict() stats['Total Return'] = (total_return - 1.0) stats['Annualized Return'] = ( total_return ** (365 / len(self.equity_curve.index))) - 1 stats['Length of Series'] = len(self.equity_curve.index) stats['Sharpe Ratio'] = sharpe_ratio stats['Max Drawdown'] = max_dd stats['Drawdown Duration'] = floor(dd_duration) self.equity_curve.to_csv('equity.csv') return stats
def test_create_drowdown1(self): """ Test1: non random serie with 3 inflection points """ rows = 100 step = 1 values = [100] inflection1 = 0.15 inflection2 = 0.50 inflection3 = 0.90 for each_row in range(rows - 1): if each_row < (rows * inflection1): values.append(values[-1] - step) elif (each_row >= rows) * inflection1 and \ (each_row < rows) * inflection2: values.append(values[-1] + step) elif (each_row >= rows * inflection2) and \ (each_row < rows) * inflection3: values.append(values[-1] - step) elif each_row >= rows * inflection3: values.append(values[-1] + step) pnl = pd.Series(values, index=np.arange(rows)) result = performance.create_drawdowns(pnl) expected = (40.0, 49.0) self.assertEqual(result, expected, 'Drawdown error calculation')
def output_summary_stats(self): """ Crea un elenco di statistiche di riepilogo per il portafoglio come lo Sharpe Ratio e le informazioni sul drowdown. """ total_return = self.equity_curve['equity_curve'][-1] returns = self.equity_curve['returns'] pnl = self.equity_curve['equity_curve'] sharpe_ratio = create_sharpe_ratio(returns, periods=252 * 6.5 * 60) drawdown, max_dd, dd_duration = create_drawdowns(pnl) self.equity_curve['drawdown'] = drawdown stats = [("Total Return", "%0.2f%%" % \ ((total_return - 1.0) * 100.0)), ("Sharpe Ratio", "%0.2f" % sharpe_ratio), ("Max Drawdown", "%0.2f%%" % (max_dd * 100.0)), ("Drawdown Duration", "%d" % dd_duration)] self.equity_curve.to_csv('equity.csv') return stats
def output_results(self): # Closes off the Backtest.csv file so it can be # read via Pandas without problems self.backtest_file.close() in_filename = "backtest.csv" out_filename = "equity.csv" in_file = os.path.join(OUTPUT_RESULTS_DIR, in_filename) out_file = os.path.join(OUTPUT_RESULTS_DIR, out_filename) # Create equity curve dataframe df = pd.read_csv(in_file, index_col=0) df.dropna(inplace=True) df["Total"] = df.sum(axis=1) df["Returns"] = df["Total"].pct_change() df["Equity"] = (1.0 + df["Returns"]).cumprod() # Create drawdown statistics drawdown, max_dd, dd_duration = create_drawdowns(df["Equity"]) df["Drawdown"] = drawdown df.to_csv(out_file, index=True) print("Simulation complete and results exported to %s" % out_filename)
with performance calculation, that avoids having to re-run the full backtest. In this case it simply works off the "backtest.csv" file that is produced from a backtest.py run. """ import os import pandas as pd from performance.performance import create_drawdowns from settings import OUTPUT_RESULTS_DIR if __name__ == "__main__": in_filename = "backtest.csv" out_filename = "equity.csv" in_file = os.path.join(OUTPUT_RESULTS_DIR, in_filename) out_file = os.path.join(OUTPUT_RESULTS_DIR, out_filename) # Create equity curve dataframe df = pd.read_csv(in_file, index_col=0) df.dropna(inplace=True) df["Total"] = df.sum(axis=1) df["Returns"] = df["Total"].pct_change() df["Equity"] = (1.0 + df["Returns"]).cumprod() # Create drawdown statistics drawdown, max_dd, dd_duration = create_drawdowns(df["Equity"]) df["Drawdown"] = drawdown df.to_csv(out_file, index=True)