pyfolio icon indicating copy to clipboard operation
pyfolio copied to clipboard

Port logic from standalone to catch cases when no live trading data is available

Open justinlent opened this issue 11 years ago • 9 comments

here is one example algo id: 557e96eb8ba119ea30000409

IndexError: index out of bounds

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-2-e2266cc245fd> in <module>()
      3 # B algo:  54349eefcd2e3f7f57000025
      4 #a, b, c = internals.get_and_analyze_algo('54349eefcd2e3f7f57000025', contest='MEGARUN_1');
----> 5 a, b, c = internals.get_and_analyze_algo('557e96eb8ba119ea30000409', backtest_min_years=5, cone_std=1.0);

/Users/jlent/github_projects/quantrisk/quantrisk/internals.pyc in get_and_analyze_algo(algo_id, contest, cone_std, plot_risk_factors, algo_live_date, include_positions, backtest_min_years, backtest_max_years)
    283 
    284     tears.create_full_tear_sheet(df_rets, df_pos=df_pos, df_txn=df_txn_daily, gross_lev=gross_lev, fetcher_urls=fetcher_urls,
--> 285                                  algo_create_date=algo_create_date, cone_std=cone_std)
    286 
    287     return df_rets, df_pos, df_txn_daily

/Users/jlent/github_projects/quantrisk/quantrisk/tears.pyc in create_full_tear_sheet(df_rets, df_pos, df_txn, gross_lev, fetcher_urls, algo_create_date, backtest_days_pct, cone_std)
    111                            backtest_days_pct=0.5, cone_std=1.0):
    112 
--> 113     create_returns_tear_sheet(df_rets, algo_create_date=algo_create_date, backtest_days_pct=backtest_days_pct, cone_std=cone_std)
    114 
    115     if df_pos is not None:

/Users/jlent/github_projects/quantrisk/quantrisk/tears.pyc in create_returns_tear_sheet(df_rets, algo_create_date, backtest_days_pct, cone_std)
     36     print '\n'
     37 
---> 38     plotting.show_perf_stats(df_rets, algo_create_date, benchmark_rets)
     39 
     40     plotting.plot_rolling_returns(

/Users/jlent/github_projects/quantrisk/quantrisk/plotting.pyc in show_perf_stats(df_rets, algo_create_date, benchmark_rets)
    299 
    300     diff_pct = timeseries.out_of_sample_vs_in_sample_returns_kde(timeseries.cum_returns(df_rets_backtest , 1.0), 
--> 301                                                              timeseries.cum_returns(df_rets_live, 1.0) )
    302 
    303     consistency_pct = int( 100*(1.0 - diff_pct) )

/Users/jlent/github_projects/quantrisk/quantrisk/timeseries.pyc in cum_returns(df_rets, starting_value)
     64     # Note that we can't add that ourselves as we don't know which dt
     65     # to use.
---> 66     if pd.isnull(df_rets.iloc[0]):
     67         df_rets.iloc[0] = 0.
     68 

/Users/jlent/anaconda/lib/python2.7/site-packages/pandas/core/indexing.pyc in __getitem__(self, key)
   1215             return self._getitem_tuple(key)
   1216         else:
-> 1217             return self._getitem_axis(key, axis=0)
   1218 
   1219     def _getitem_axis(self, key, axis=0):

/Users/jlent/anaconda/lib/python2.7/site-packages/pandas/core/indexing.pyc in _getitem_axis(self, key, axis)
   1506                 self._is_valid_integer(key, axis)
   1507 
-> 1508             return self._get_loc(key, axis=axis)
   1509 
   1510     def _convert_to_indexer(self, obj, axis=0, is_setter=False):

/Users/jlent/anaconda/lib/python2.7/site-packages/pandas/core/indexing.pyc in _get_loc(self, key, axis)
     90 
     91     def _get_loc(self, key, axis=0):
---> 92         return self.obj._ixs(key, axis=axis)
     93 
     94     def _slice(self, obj, axis=0, kind=None):

/Users/jlent/anaconda/lib/python2.7/site-packages/pandas/core/series.pyc in _ixs(self, i, axis)
    486             values = self.values
    487             if isinstance(values, np.ndarray):
--> 488                 return _index.get_value_at(values, i)
    489             else:
    490                 return values[i]

pandas/index.pyx in pandas.index.get_value_at (pandas/index.c:2358)()

pandas/src/util.pxd in util.get_value_at (pandas/index.c:15287)()

IndexError: index out of bounds

justinlent avatar Jun 29 '15 16:06 justinlent

Yes, the reason is that data_live is empty because the algo_created_date < last_returns_dt. I don't know why that would be the case though.

twiecki avatar Jun 29 '15 18:06 twiecki

ah good idea. yeah, this is quite commonly the case when the test harness backtest that was run does not include the algo_created_date in the backtest dates (this will be the case with very recent contest entries)

justinlent avatar Jun 29 '15 18:06 justinlent

Still odd that it would work in standalone but not quantrisk?

twiecki avatar Jun 29 '15 18:06 twiecki

in standalone, i added in logic to catch this case and then work around it. i should port over this code to the new nb to be robust to this condition possibly

justinlent avatar Jun 29 '15 18:06 justinlent

Great!

twiecki avatar Jun 29 '15 18:06 twiecki

@twiecki do you think we need this for the Public Beta? I believe it is only necessary logic when using one of our Q backtests.

Actually, I guess from the perspective of the bigger picture, I wonder if we need to just start maintaining 2 tearsheets from the beginning/now. e.g. tearsheet_public and tearsheet_internal, where tearsheet_public will the be tearsheet code that we release in the public opensource repo, and tearsheet_internal ends up eventually being in our internal (private) branch of the repo (or simply a whole new Quantopian private repo). Maybe you have some thoughts about organizing this as well?

justinlent avatar Jun 29 '15 21:06 justinlent

@justinlent Agreed. I removed the milestone. However, I think we really need to get rid of the multiple versions floating around as soon as we can or we'll continuously have to port NBs and will be out of sync. I actually thought that's what https://github.com/quantopian/quantrisk/commit/8b8c0fcd69d34fb4009b844bcde3b697828aa724 was doing?

twiecki avatar Jun 30 '15 07:06 twiecki

yep,i definitely should have caught this edge case in that commit.

justinlent avatar Jun 30 '15 17:06 justinlent

@justinlent I moved your change over to here now: https://github.com/quantopian/quantrisk/commit/d4eff107e784eab6c30f34bc1bcbe4b6f06a4659

twiecki avatar Jul 02 '15 13:07 twiecki