def test_drawdown_details(): drawdown = ffn.to_drawdown_series(df['MSFT']) drawdown_details = ffn.drawdown_details(drawdown) assert (drawdown_details.loc[drawdown_details.index[1], 'Length'] == 18) num_years = 30 num_months = num_years * 12 np.random.seed(0) returns = np.random.normal(loc=0.06 / 12, scale=0.20 / np.sqrt(12), size=num_months) returns = pd.Series(np.cumprod(1 + returns)) drawdown = ffn.to_drawdown_series(returns) drawdown_details = ffn.drawdown_details(drawdown, index_type=drawdown.index)
def load_and_report(): # fetching the current time as name name = names_of_fig() # load Pricing test data all_contracts, p_sorted = load_data() # List contracts print(f"Showing all_contracts..........\n{all_contracts.tail(10)}\n") print(f"Showing p_sorted......\n{p_sorted.tail(10)}\n") # output from potential_pair ret, list_sect = potential_pairs(all_contracts, p_sorted) print(f"showing list_sect.......\n{list_sect}\n") print(f"showing ret........\n{ret.tail(10)}\n") # show the results of in sample testing ret.iloc[0] = 1 ret.index = all_contracts.index plt.figure(figsize=(15, 7)) plt.xlabel('Trade Date') plt.grid(True) plt.plot(ret) plt.legend(list(ret.columns)) plt.show() plt.savefig(os.path.join("charts/sample test", "sample test " + name)) # calculate the performance perf = ret.calc_stats() perf.display() perf.to_csv(sep=',', path="train_perfer.csv") # plot the maxinum drawndown of each pair ffn_ret = ffn.to_drawdown_series(ret) plt.figure(figsize=(15, 7)) plt.grid(True) plt.plot(ffn_ret) plt.legend(list(ffn_ret.columns)) plt.show() plt.savefig(os.path.join("charts/ffn drawdown", "ffn max drawdown " + name)) # In sample back testing of portfolio port = ret.mean(axis=1) plt.figure(figsize=(15, 7)) plt.grid(True) plt.plot(port) plt.show() #plt.legend(list(port.columns)) plt.savefig(os.path.join("charts/testing of portfolio", "portfolio test " + name)) perf = port.calc_stats() print(f"\n\nPrinting perf stats.......\n{perf.stats}\n") # In sample back testing of portfolio maxinum drawndown ffn_port = ffn.to_drawdown_series(port) plt.figure(figsize=(15, 7)) plt.grid(True) plt.plot(ffn_port) #plt.legend(list(ffn_port.columns)) plt.savefig(os.path.join("charts/back testing of portfolio maxinum drawndown", "maxinum drawndown " + name)) #################### ##sample back testing###### ##################### test_ret, testing_data = sample_backtest(list_sect) print(f"\n\n\nShowing sample back testing- testing data tail.......\n\n{testing_data.tail(3)}\n") test_ret.iloc[0] = 1 print(f"\n\nShowing sample backtesting test_ret tail.......\n\n{test_ret.tail(3)}") print(f"\n\nshowing test_ret shape......\n\n{test_ret.shape})") print(f"\n\n\nshowing test_ret index........\n\n{test_ret.index}") # plotting test_ret sample back testing plt.plot(test_ret) plt.legend(list(test_ret.columns)) plt.savefig(os.path.join("charts/sample Backtesting/test_ret", "test_ret " + name)) # Out sample back testing of portfolio port = test_ret.mean(axis=1) plt.figure(figsize=(15, 7)) plt.grid(True) plt.plot(port) # plt.legend(list(port.columns)) plt.savefig(os.path.join("charts/sample Backtesting/portfolio", "backtesting portfolio " + name)) perf = port.calc_stats() print(perf.stats) ffn_backtest_sample = ffn.to_drawdown_series(port) plt.figure(figsize=(15, 7)) plt.grid(True) plt.plot(ffn_backtest_sample) # plt.legend(list(ffn_backtest_sample.columns)) plt.savefig(os.path.join("charts/sample Backtesting/ffn_drawdown_port", "drwadown_port " + name)) return None
# Apply Financial Functions for Python (FFN) to calculate statistics of algorithm equity_curve_df['Equity'] = equity_curve_df perf = equity_curve_df['Equity'].calc_stats() # plot equity curve perf.plot() plt.show() # show overall metrics perf.display() # display monthly returns perf.display_monthly_returns() # plotting visual representation of strategy drawdown series: ffn.to_drawdown_series(equity_curve_df['Equity']).plot(figsize=(15, 7), grid=True) plt.show() # plot histogram of returns perf.plot_histogram() plt.show() # extract lookback returns perf.display_lookback_returns() # ************************************************************************************************************************************************************************************************* # PRINT COLUMN OF SIGNALS FOR THE STRATEGY print("CURRENT SIGNAL FOR ALGORITHM: 1: BUY, 0: HOLD, -1:SELL") print(pred2.iloc[:, 2]) # **************************************************************************************************************************************************************************************************
# In[39]: #equity curve pairwise_stats.plot() # In[40]: #some more metrics pairwise_stats.display() # In[41]: #draw down curve for pair in pairs: plt.figure() ffn.to_drawdown_series(returns_df[pair[0] + " " + pair[1]]).plot( figsize=(15, 7), grid=True, legend=True) # In[42]: pairwise_stats_df = pairwise_stats.stats # In[43]: #stores the df of stats pairwise_stats_df.head() # In[44]: def portfolio_return_analysis(df): # df: returnsdf of pairs