polakowo / vectorbt

Find your trading edge, using the fastest engine for backtesting, algorithmic trading, and research.
https://vectorbt.dev
Other
4.22k stars 609 forks source link

A guide how to setup vectorbt for stocks would be so great #342

Closed wojoeraf closed 2 years ago

wojoeraf commented 2 years ago

Hi there, great work here.

I'm trying to setup vectorbt for the use with stocks. I found some hints e.g. about freq and year_freq settings but I'm not getting it to work well. (Also on which classes to set those settings).

When plotting indicators either there are gaps like this... image

...or if I use dropna() on the data it looks like this (which feels wrong) image

A short guide on how to setup everything for the use of stocks in different timeframes (1 minute and higher) would help so much.

Thanks a lot!

Rafael

polakowo commented 2 years ago

@wojoeraf vectorbt doesn't care much if you trade stocks, crypto, or anything else. What it needs is an array of data, even without datetime-like index. But having a proper index with a frequency will let you compute annualization metrics. To trade stocks use size_granularity=1, which will disable fractional size. You can set freq in vbt.settings['array_wrapper']['freq'] and year_freq in vbt.settings['returns']['year_freq'].

wojoeraf commented 2 years ago

@polakowo thanks, but where do I set size_granularity=1? It is neither a parameter of the vbt.MA.run() method nor of the plot() method.

polakowo commented 2 years ago

@wojoeraf size granularity belongs neither to indicators nor plotting, it's an argument of Portfolio.from_signals and comparable simulation methods to instruct that any order size should be rounded to the next integer.

wojoeraf commented 2 years ago

@polakowo Thanks so far for your help. Can I be sure then, when I set vbt.settings['array_wrapper']['freq'] according to my data (e.g. 1h) and the size_granularity=1 for the Portfolio.from_signals, that indicators are calculated and handled correctly (because the plots of the indicators still show the gaps in between)?

wojoeraf commented 2 years ago

@polakowo I have a complete example adapted from the usage page: image

I can't imagine, that the result is a correct one.

polakowo commented 2 years ago

@wojoeraf fees of 0.1 is 10%. Furthermore you should remove gaps in data completely, there is no point in having nans.

wojoeraf commented 2 years ago

@polakowo about fees, ok, thats my mistake ;) About that nan's, yes, I tried that after taking the screenshot (with drpona()), somehow even my index contains clean data without nans or gaps, the plotting always contains gaps.

polakowo commented 2 years ago

@wojoeraf this isn't an issue, plotting will always show gaps.

wojoeraf commented 2 years ago

@polakowo Ah ok, I see. Unfortunately then plotting e.g. for a moving average makes no sense since no MA is even visible, if the consecutive bars are less then the MA's length. Thanks for your help and quick responses

polakowo commented 2 years ago

@wojoeraf MA should be computed from the series that contains no gaps. Plotting it shouldn't be a problem, just pass connectgaps=True to whatever trace should connect gaps (look at trace_kwargs argument in the documentation of the particular method and pass trace_kwargs=dict(connectgaps=True)).

wojoeraf commented 2 years ago

@polakowo Thanks for your consistent input. I'm kinda new to the python and Data Science world so I need to build up a lot of knowledge. The connectgaps is not a valid parameter, but I found a solution on the plotly pages: image This also works for indicators.

wojoeraf commented 2 years ago

I have another question: When running

fast_ma, slow_ma = vbt.MA.run_combs(price, window=windows, r=2, short_names=['fast', 'slow'])

I have to give a column value when trying to plot e.g. the fast_ma: fast_ma.plot(0). What does the columns represent in that case?

polakowo commented 2 years ago

@wojoeraf it's the same columns as in fast_ma.ma. If columns have multiple levels, you can provide each column as a tuple.