vectorbt icon indicating copy to clipboard operation
vectorbt copied to clipboard

Returns_Accessor

Open stevennevins opened this issue 5 years ago • 8 comments

Is there a generic way to create a new method for these performance metrics, kind of how indicators has an indicator factory? Would be nice to be able to add these performance metrics and a generic way to implement new ones and add to the portfolio class.

I've only started implementing the examples so I might not have dug deep enough to find how to do this yet

stevennevins avatar Jul 30 '20 13:07 stevennevins

The way performance metrics based on returns are implemented is: you write a Numba-compiled function (or any other function) that takes a two-dimensional array and returns a one-dimensional array with values per column. Then, it gets wrapped into a pandas object and finally returned. There is no need for any factory since the process cannot be further automated.

You can easily subclass the Portfolio and define your custom cached properties there. I was thinking about doing many crazy things with Portfolio, but the way it is right now is the most generic. For example, below is an example how attach a skew method:

from vectorbt import Portfolio
from vectorbt.utils.decorators import cached_property

class MyPortfolio(Portfolio):
    @cached_property
    def returns_skew(self):
        return self.returns.skew()

This will behave the same as if it was defined in the original class.

polakowo avatar Jul 30 '20 14:07 polakowo

I'm trying to make a similar property but from trades. Basically I want to apply a log((trade.pnl/100)+1) function to each trade and then a reduce each columnsum()/len()

Based on the code of the WalkForwardOptimization, im running simulate_all_params(in_df, params_range). So in Portfolio.from_signals(close, , close.shape = (1080, 405) as there are many windows and combination of params.

portfolio.returns has the same shape as close. But trades does not. How can I wrap it so that I can calculate the log return over the return of each trade in each window?

emiliobasualdo avatar Apr 09 '21 15:04 emiliobasualdo

Trade pnl is a MappedArray, so if you want to bring it to the same shape as close, use portfolio.trades.pnl.to_matrix and do your operations.

polakowo avatar Apr 09 '21 15:04 polakowo

Perfect, works like a charm. Now, I see more returns values per window than trades.pnl values per window. So mind my question but, what is the difference between returns and trades.pnl in vbt?

emiliobasualdo avatar Apr 09 '21 16:04 emiliobasualdo

returns are generated from portfolio value that is available at each bar (it's your close * current stake + current cash), while trade returns are generated only at the bar where the trade has been closed.

polakowo avatar Apr 09 '21 16:04 polakowo

Thanks!

emiliobasualdo avatar Apr 09 '21 18:04 emiliobasualdo

Hi @polakowo . Is there a way to add custom @cached_property to the Trades class and then use the Portfolio_fom_signals method?

emiliobasualdo avatar May 09 '21 16:05 emiliobasualdo

Hi @emiliobasualdo, you need to subclass Trades, add your method, then subclass Portfolio, and overwrite trades property to call your new Trades class (see this line)

polakowo avatar May 09 '21 16:05 polakowo