Code Monkey home page Code Monkey logo

Comments (12)

polakowo avatar polakowo commented on August 11, 2024 1

Great, I'll include a fix in the next release then.

Portfolio stats and any plot function can only work with one column of data. Even if you have a DataFrame with one column, you need to make it Series first, since vectorbt treats DataFrames as plural. The good news is: most classes in vectorbt support pandas-like indexing, so you can use prt['price'].trades.plot() or prt.trades['price'].plot(). In the next release each method will also have a column argument to simplify column selection.

A small hint: If you have got only one column of pricing data, a better decision is to make it a Series from the beginning. Also make sure to use descriptive names for columns, since columns in vectorbt mean different backtest configurations.

from vectorbt.

polakowo avatar polakowo commented on August 11, 2024 1

I’m going to release the fix to signals in 2 days. Regarding the heatmap, you probably need to install plotly extensions. I don’t know if they can be installed for spyder though.

from vectorbt.

polakowo avatar polakowo commented on August 11, 2024

I can't reproduce your issue. You can pass size type manually size_type=vbt.portfolio.enums.SizeType.Shares.

from vectorbt.

zecariocatrader avatar zecariocatrader commented on August 11, 2024

Hi, @polakowo
I have the same issue, and this error is too cryptic, so I have no idea what it means. The docs on SizeType and Portfolio.from_signals didn't help in this case.

I've also tried executing with size_type=vbt.portfolio.enums.SizeType.Shares, no change.

I was trying to write a concise version to post here as an reproducible example, but the example worked. Then I tried to extend the example to my whole DF and it failed again. So I manually "binary searched" my DF up to the point it fails. I have no idea why it fails, but you can reproduce it with this code:

dates_list = ['2017-08-17 04:00:00', '2017-08-17 05:00:00', '2017-08-17 06:00:00', '2017-08-17 07:00:00', '2017-08-17 08:00:00', '2017-08-17 09:00:00', '2017-08-17 10:00:00', '2017-08-17 11:00:00', '2017-08-17 12:00:00', '2017-08-17 13:00:00', '2017-08-17 14:00:00', '2017-08-17 15:00:00', '2017-08-17 16:00:00', '2017-08-17 17:00:00', '2017-08-17 18:00:00', '2017-08-17 19:00:00', '2017-08-17 20:00:00', '2017-08-17 21:00:00', '2017-08-17 22:00:00', '2017-08-17 23:00:00', '2017-08-18 00:00:00', '2017-08-18 01:00:00', '2017-08-18 02:00:00', '2017-08-18 03:00:00', '2017-08-18 04:00:00', '2017-08-18 05:00:00', '2017-08-18 06:00:00', '2017-08-18 07:00:00', '2017-08-18 08:00:00', '2017-08-18 09:00:00', '2017-08-18 10:00:00', '2017-08-18 11:00:00', '2017-08-18 12:00:00', '2017-08-18 13:00:00', '2017-08-18 14:00:00', '2017-08-18 15:00:00', '2017-08-18 16:00:00', '2017-08-18 17:00:00', '2017-08-18 18:00:00', '2017-08-18 19:00:00']
price_list = [4308.83, 4315.32, 4324.35, 4349.99, 4360.69, 4444.0, 4460.0, 4427.3, 4411.0, 4459.0, 4470.82, 4352.34, 4354.18, 4289.24, 4256.97, 4325.23, 4346.74, 4333.55, 4336.8, 4285.08, 4286.53, 4243.59, 4267.59, 4292.39, 4287.92, 4313.56, 4279.46, 4300.25, 4282.73, 4304.15, 4356.31, 4340.31, 4331.71, 4293.09, 4259.4, 4236.89, 4250.34, 4193.35, 4117.41, 4136.28]
d = {
    'date': dates_list,
    'price': price_list
}

df = pd.DataFrame(d)

# If I don't change the index I get another error "Failed in nopython mode pipeline..." 
df.loc[:,'date'] = pd.to_datetime(df.date)
df.set_index('date', inplace=True)

rsi_test = vbt.RSI.run(df)
entries = rsi_test.rsi_below(30)
exits = rsi_test.rsi_above(70)

prt = vbt.Portfolio.from_signals(df, entries, exits)

The interesting thing is, if I reduce one record from the dataframe the code above works. Like this:

d = {
    'date': dates_list[:39],
    'price': price_list[:39]
}
df = pd.DataFrame(d)
[...]
prt = vbt.Portfolio.from_signals(df, entries, exits)

But wait, there is more! πŸ˜…
If I change the my RSI settings to include two windows, then I need to slice the dataframe even further for it to work.
While this would work:

d = {
    'date': dates_list[:21],
    'price': price_list[:21]
}
df = pd.DataFrame(d)
rsi_test = vbt.RSI.run(df, window=[12,14])
[...]
prt = vbt.Portfolio.from_signals(df, entries, exits)

If I use dates_list[:22], 'price': price_list[:22] it will fail again.

--

Just an addon, if I don't change the index, it raises this exception:

Failed in nopython mode pipeline (step: nopython frontend) non-precise type array(pyobject, 2d, F) 
During: typing of argument at \venv\lib\site-packages\vectorbt\indicators\nb.py (90)

from vectorbt.

polakowo avatar polakowo commented on August 11, 2024

@zecariocatrader thanks for trying to debug this thing, it's not always easy with numba. Strangely, but all of your examples work for me. I'm running them in a Colab notebook using vectorbt 0.13.7, pandas 1.0.5, numpy 1.18.5, and python 3.6.5. Can you run your examples there? It might be a platform-specific issue.

from vectorbt.

Jaclong avatar Jaclong commented on August 11, 2024

Same as @zecariocatrader reduce one row of dataframe the example is work. and the Colab notebook is works. I tried two platform Spyer3 and Jupyter got the same error of raise ValueError("Only SizeType.Shares and SizeType.Cash are supported").

my environment is:
python 3.8.5
pandas 1.1.1
numpy 1.19.1
vectorbt 0.13.7

from vectorbt.

polakowo avatar polakowo commented on August 11, 2024

@Jaclong I created the same environment as yours and still see no errors (tried on MacOS and Ubuntu). If you're running on Windows that might be the reason why it behaves differently. But I might have an idea where it comes from and will release the potential fix with the release 0.14. If you have time, you can clone this repository, change this line to if size_type != SizeType.Shares and size_type != SizeType.Cash: and run your example in the root directory of the cloned repository to see if the error still occurs.

from vectorbt.

zecariocatrader avatar zecariocatrader commented on August 11, 2024

I'm running on Windows 10, python 3.7.2, vectorbt==0.13.7, pandas==1.1.1 and numpy==1.19.1

Even changing this line as you suggested didn't solved the problem. On Colab my example works indeed but raises some warnings:

/usr/local/lib/python3.6/dist-packages/vectorbt/base/accessors.py:411: RuntimeWarning:

invalid value encountered in less

/usr/local/lib/python3.6/dist-packages/vectorbt/base/accessors.py:411: RuntimeWarning:

invalid value encountered in greater

Not sure if those are expected.

from vectorbt.

polakowo avatar polakowo commented on August 11, 2024

Those warnings are expected, they are coming from RSI. Can you print size_type variable at the beginning of simulate_from_signals_nb and in signals_order_func_nb before the error and also print the whole stack trace?

Edit: size_type instead of size

from vectorbt.

zecariocatrader avatar zecariocatrader commented on August 11, 2024

I tested today again, and it seems that this: if size_type != SizeType.Shares and size_type != SizeType.Cash: solved the problem. I guess I didin't restarted jupyter's kernell the last time. I also printed the size_type value and it's 0. If you still want the tb:

ValueError                                Traceback (most recent call last)
<ipython-input-10-2568753ec3d8> in <module>
      3     entries,
      4     exits,
----> 5     freq='1h'
      6     )

\venv\lib\site-packages\vectorbt\portfolio\base.py in from_signals(cls, main_price, entries, exits, size, size_type, entry_price, exit_price, init_capital, fees, fixed_fees, slippage, accumulate, accumulate_exit_mode, conflict_mode, broadcast_kwargs, freq, **kwargs)
    369             accumulate_exit_mode,
    370             conflict_mode,
--> 371             is_2d=main_price.ndim == 2
    372         )
    373 

\venv\lib\site-packages\vectorbt\portfolio\nb.py in signals_order_func_nb()
    602     #print(size_type, type(size_type))
    603     #if size_type != SizeType.Shares and size_type != SizeType.Cash:
--> 604         raise ValueError("Only SizeType.Shares and SizeType.Cash are supported")
    605     if is_entry and not is_exit:
    606         # Open or increase the position

ValueError: Only SizeType.Shares and SizeType.Cash are supported

PS.: I couldn't find anywhere in the docs, and want to avoid creating another issue to something is probably silly. If I run prt.trades.plot() in the example above I get TypeError: You must select a column first. My question is how do I select a column, and which column are we talkin about?

from vectorbt.

Jaclong avatar Jaclong commented on August 11, 2024

@polakowo changed this line to if size_type != SizeType.Shares and size_type != SizeType.Cash: and it works!
But somehow the heatmap of example can't show on Spyder3 nut it could show on jupyter.
Thaks for helping

from vectorbt.

polakowo avatar polakowo commented on August 11, 2024

Fixed in #46

from vectorbt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.