Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: ufunc 'isnan' not supported for the input types #104

Closed
alitekdemir opened this issue Jan 18, 2022 · 9 comments
Closed

TypeError: ufunc 'isnan' not supported for the input types #104

alitekdemir opened this issue Jan 18, 2022 · 9 comments

Comments

@alitekdemir
Copy link

alitekdemir commented Jan 18, 2022

Description

When I try the RSI code on the Google Colab Jupyter, I get the following error.

Error on Colab

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
INFO: "use_yfinance" not specified in preferences, defaulting to: "False"
No cached data found for ETH-USDT from: 1642515960 to 1642524144 at a resolution of 3600 seconds.

Initializing...

Backtesting...
Progress: [#---------] 14.87% Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/backtest_controller.py", line 706, in run
    'state_object'])
  File "<ipython-input-3-1733380c8989>", line 10, in price_event
    state.interface.market_order(symbol, side='buy', size=buy)
  File "/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/paper_trade_interface.py", line 363, in market_order
    raise InvalidOrder(f"Size is too small. Minimum is: {min_size}. You requested {size}.")
blankly.utils.exceptions.InvalidOrder: Size is too small. Minimum is: 0.0001. You requested 0.
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/backtest_controller.py", line 706, in run
    'state_object'])
  File "<ipython-input-3-1733380c8989>", line 10, in price_event
    state.interface.market_order(symbol, side='buy', size=buy)
  File "/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/paper_trade_interface.py", line 363, in market_order
    raise InvalidOrder(f"Size is too small. Minimum is: {min_size}. You requested {size}.")
blankly.utils.exceptions.InvalidOrder: Size is too small. Minimum is: 0.0001. You requested 0.
Traceback (most recent call last):
...
/usr/local/lib/python3.7/dist-packages/blankly/metrics/portfolio.py:55: RuntimeWarning: invalid value encountered in double_scalars
  return mean / std
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-1733380c8989> in <module>()
     30   # strategy.start()
     31   # Or backtest using this
---> 32   results = strategy.backtest(to='1y', initial_values={'USDT': 10000})
     33   print(results)

1 frames
/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/backtest_controller.py in run(self)
    908         # Lastly remove Nan values in the metrics
    909         for i in metrics_indicators:
--> 910             if np.isnan(metrics_indicators[i]):
    911                 metrics_indicators[i] = None
    912 

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

chrome_0J9966GLO4

When I try the RSI code on the Windows desktop, I get the following error.

Error on Windows Desktop

c:\python38\lib\site-packages\blankly\metrics\portfolio.py:60: RuntimeWarning: invalid value encountered in double_scalars
  return return_series.mean() * n / abs(max_drawdown(return_series))

pyzo_dTWgsPQ6Cc

@EmersonDove
Copy link
Member

EmersonDove commented Jan 18, 2022

I believe this should be addressed in a recent update. Try running pip install blankly --upgrade and tell me how it goes.

@alitekdemir
Copy link
Author

Google Colab Output

Initializing...

Backtesting...
Progress: [######----] 60.61% Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/backtest_controller.py", line 769, in run
    'state_object'])
  File "<ipython-input-11-aad4e5fddf26>", line 10, in price_event
    state.interface.market_order(symbol, side='buy', size=buy)
  File "/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/paper_trade_interface.py", line 366, in market_order
    raise InvalidOrder(f"Size is too small. Minimum is: {min_size}. You requested {size}.")
blankly.utils.exceptions.InvalidOrder: Size is too small. Minimum is: 0.0001. You requested 0.0.
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/backtest_controller.py", line 769, in run
    'state_object'])
  File "<ipython-input-11-aad4e5fddf26>", line 10, in price_event
    state.interface.market_order(symbol, side='buy', size=buy)
  File "/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/paper_trade_interface.py", line 366, in market_order
    raise InvalidOrder(f"Size is too small. Minimum is: {min_size}. You requested {size}.")
blankly.utils.exceptions.InvalidOrder: Size is too small. Minimum is: 0.0001. You requested 0.0.
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/backtest_controller.py", line 769, in run
    'state_object'])
  File "<ipython-input-11-aad4e5fddf26>", line 10, in price_event
    state.interface.market_order(symbol, side='buy', size=buy)
  File "/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/paper_trade_interface.py", line 366, in market_order
    raise InvalidOrder(f"Size is too small. Minimum is: {min_size}. You requested {size}.")
blankly.utils.exceptions.InvalidOrder: Size is too small. Minimum is: 0.0001. You requested 0.0.
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/backtest_controller.py", line 769, in run
    'state_object'])
  File "<ipython-input-11-aad4e5fddf26>", line 10, in price_event
    state.interface.market_order(symbol, side='buy', size=buy)
  File "/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/paper_trade_interface.py", line 366, in market_order
    raise InvalidOrder(f"Size is too small. Minimum is: {min_size}. You requested {size}.")
blankly.utils.exceptions.InvalidOrder: Size is too small. Minimum is: 0.0001. You requested 0.0.
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/backtest_controller.py", line 769, in run
    'state_object'])
  File "<ipython-input-11-aad4e5fddf26>", line 10, in price_event
    state.interface.market_order(symbol, side='buy', size=buy)
  File "/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/paper_trade_interface.py", line 366, in market_order
    raise InvalidOrder(f"Size is too small. Minimum is: {min_size}. You requested {size}.")
blankly.utils.exceptions.InvalidOrder: Size is too small. Minimum is: 0.0001. You requested 0.0.

Historical Dataframes: 
Account History: 
     USDT ETH          time  Account Value (USDT)
0    1000   0  1.641946e+09                1000.0
1    1000   0  1.641946e+09                1000.0
2    1000   0  1.641949e+09                1000.0
3    1000   0  1.641953e+09                1000.0
4    1000   0  1.641956e+09                1000.0
..    ...  ..           ...                   ...
163  1000   0  1.642529e+09                1000.0
164  1000   0  1.642532e+09                1000.0
165  1000   0  1.642536e+09                1000.0
166  1000   0  1.642540e+09                1000.0
167  1000   0  1.642543e+09                1000.0

[168 rows x 4 columns]
Account Returns: 
         time value
0  1641945600  None
1  1642032000     0
2  1642118400     0
3  1642204800     0
4  1642291200     0
5  1642377600     0
6  1642464000     0
Resampled Account Value: 
           time   value
0  1.641946e+09  1000.0
1  1.642032e+09  1000.0
2  1.642118e+09  1000.0
3  1.642205e+09  1000.0
4  1.642291e+09  1000.0
5  1.642378e+09  1000.0
6  1.642464e+09  1000.0
Blankly Metrics: 
Compound Annual Growth Rate (%):   0.0%
Cumulative Returns (%):            0.0%
Max Drawdown (%):                  0.0%
Variance (%):                      0.0%
Sortino Ratio:                     None
Sharpe Ratio:                      None
Calmar Ratio:                      failed: float division by zero
Volatility:                        0.0
Value-at-Risk:                     None
Conditional Value-at-Risk:         0.0
Risk Free Return Rate:             0.0
Resampled Time:                    86400.0

/usr/local/lib/python3.7/dist-packages/blankly/metrics/portfolio.py:55: RuntimeWarning: invalid value encountered in double_scalars
  return mean / std

Windows Output

Traceback (most recent call last):
  File "c:\python38\lib\site-packages\blankly\exchanges\interfaces\paper_trade\backtest_controller.py", line 704, in run
    no_trade = []
  File "J:\Blankly2022\BlanklyRSI.py", line 10, in price_event
    state.interface.market_order(symbol, side='buy', size=buy)
  File "c:\python38\lib\site-packages\blankly\exchanges\interfaces\paper_trade\paper_trade_interface.py", line 363, in market_order
    max_size = market_limits['market_order']["base_max_size"]
blankly.utils.exceptions.InvalidOrder: Size is too small. Minimum is: 0.0001. You requested 0.
c:\python38\lib\site-packages\blankly\metrics\portfolio.py:55: RuntimeWarning: invalid value encountered in double_scalars
  return mean / std
c:\python38\lib\site-packages\blankly\metrics\portfolio.py:60: RuntimeWarning: invalid value encountered in double_scalars
  return return_series.mean() * n / abs(max_drawdown(return_series))

Historical Dataframes: 
Account History: 
      USDT ETH          time  Account Value (USDT)
0     1000   0  1.611014e+09                1000.0
1     1000   0  1.611014e+09                1000.0
2     1000   0  1.611018e+09                1000.0
3     1000   0  1.611022e+09                1000.0
4     1000   0  1.611025e+09                1000.0
...    ...  ..           ...                   ...
8755  1000   0  1.642529e+09                1000.0
8756  1000   0  1.642532e+09                1000.0
8757  1000   0  1.642536e+09                1000.0
8758  1000   0  1.642540e+09                1000.0
8759  1000   0  1.642543e+09                1000.0

[8760 rows x 4 columns]
Account Returns: 
             time  value
0    1.611014e+09    NaN
1    1.611101e+09    0.0
2    1.611187e+09    0.0
3    1.611274e+09    0.0
4    1.611360e+09    0.0
..            ...    ...
360  1.642118e+09    0.0
361  1.642205e+09    0.0
362  1.642291e+09    0.0
363  1.642378e+09    0.0
364  1.642464e+09    0.0

[365 rows x 2 columns]
Resampled Account Value: 
             time   value
0    1.611014e+09  1000.0
1    1.611101e+09  1000.0
2    1.611187e+09  1000.0
3    1.611274e+09  1000.0
4    1.611360e+09  1000.0
..            ...     ...
360  1.642118e+09  1000.0
361  1.642205e+09  1000.0
362  1.642291e+09  1000.0
363  1.642378e+09  1000.0
364  1.642464e+09  1000.0

[365 rows x 2 columns]
Blankly Metrics: 
Compound Annual Growth Rate (%):   0.0%
Cumulative Returns (%):            0.0%
Max Drawdown (%):                  0.0%
Variance (%):                      0.0%
Sortino Ratio:                     None
Sharpe Ratio:                      None
Calmar Ratio:                      None
Volatility:                        0.0
Value-at-Risk:                     0.0
Conditional Value-at-Risk:         0.0
Risk Free Return Rate:             0.0
Resampled Time:                    86400.0

@EmersonDove
Copy link
Member

Looks like it worked great. I think the size might be casted to an int so none of the orders are going through. Try using the rsi.py script in the examples folder of the repo.

@alitekdemir
Copy link
Author

I try rsi.py but it is not worked.

Traceback (most recent call last):
  File "c:\python38\lib\site-packages\blankly\exchanges\interfaces\paper_trade\backtest_controller.py", line 704, in run
    no_trade = []
  File "J:\Blankly2022\BlanklyRSI.py", line 12, in price_event
    state.interface.market_order(symbol, side='buy', size=buy)
  File "c:\python38\lib\site-packages\blankly\exchanges\interfaces\paper_trade\paper_trade_interface.py", line 363, in market_order
    max_size = market_limits['market_order']["base_max_size"]
blankly.utils.exceptions.InvalidOrder: Size is too small. Minimum is: 0.0001. You requested 0.0.
c:\python38\lib\site-packages\blankly\metrics\portfolio.py:55: RuntimeWarning: invalid value encountered in double_scalars
  return mean / std
c:\python38\lib\site-packages\blankly\metrics\portfolio.py:60: RuntimeWarning: invalid value encountered in double_scalars
  return return_series.mean() * n / abs(max_drawdown(return_series))

Historical Dataframes: 
Account History: 
       USDT ETH          time  Account Value (USDT)
0    100000   0  1.641946e+09              100000.0
1    100000   0  1.641946e+09              100000.0
2    100000   0  1.641949e+09              100000.0
3    100000   0  1.641953e+09              100000.0
4    100000   0  1.641956e+09              100000.0
..      ...  ..           ...                   ...

chrome_Eq1aZqPH9V

and i try on colab
chrome_Ff5j2VoBmL

@EmersonDove
Copy link
Member

I believe the blankly code is working correctly, it is mainly due to issues with order sizing. Try out this version:

import blankly


def price_event(price, symbol, state: blankly.StrategyState):
    """ This function will give an updated price every 15 seconds from our definition below """
    state.variables['history'].append(price)
    rsi = blankly.indicators.rsi(state.variables['history'])
    if rsi[-1] < 30 and not state.variables['owns_position']:
        # Dollar cost average buy
        buy = blankly.trunc(state.interface.cash/price, 2)
        state.interface.market_order(symbol, side='buy', size=buy)
        state.variables['owns_position'] = True
    elif rsi[-1] > 70 and state.variables['owns_position']:
        # Dollar cost average sell
        curr_value = state.interface.account[state.base_asset].available
        state.interface.market_order(symbol, side='sell', size=curr_value)
        state.variables['owns_position'] = False


def init(symbol, state: blankly.StrategyState):
    # Download price data to give context to the algo
    state.variables['history'] = state.interface.history(symbol, to=150, return_as='deque',
                                                         resolution=state.resolution)['close']
    state.variables['owns_position'] = False


if __name__ == "__main__":
    # Authenticate coinbase pro strategy
    exchange = blankly.CoinbasePro()

    # Use our strategy helper on coinbase pro
    strategy = blankly.Strategy(exchange)

    # Run the price event function every time we check for a new price - by default that is 15 seconds
    strategy.add_price_event(price_event, symbol='BTC-USDT', resolution='1h', init=init)

    # Start the strategy. This will begin each of the price event ticks
    # strategy.start()
    # Or backtest using this
    results = strategy.backtest(to='1y', initial_values={'USDT': 10000})
    print(results)

@alitekdemir
Copy link
Author

I guess i'm on a day when i'm unlucky and incompetent.

pyzo_SaNJwZG1zb
!

@EmersonDove
Copy link
Member

This is extremely interesting to me - I'm testing the code locally with the same 1h for 1 week resolution & I'm getting a result. I think that to make it work you

  • Must be using live keys
  • Must delete the price_caches folder beforehand

Thanks for sticking through this process. We're trying to identify and reduce the pains with onboarding new users and we are learning a ton from this and will give debugging prints when users hit this error in the future.

@alitekdemir
Copy link
Author

I found the error. I hope you don't get mad at me.😟 error messages could be very instructive. The cash setting is BUSD in the setting file, but the result failed because I tried USDT in the code I tested. I noticed it in the VM where I installed Linux Mint.
I want to check results with busd and usdt pair and be able to do additional tests with btc pair. For this, I think that there should be no obligation to constantly interfere with the settings file.
Thank you very much for your hard work and helpfulness. 👏

@EmersonDove
Copy link
Member

Awesome I'm glad you got it working. These symbol and quote errors can be extremely easy to miss and will require more work on our part to get everything working. Just let me know if you have any more issues and I'm glad to help out. We learn a lot every time we help someone get up and running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants