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

Problem with strategy (all) #466

Closed
hn2 opened this issue Jan 20, 2022 · 26 comments
Closed

Problem with strategy (all) #466

hn2 opened this issue Jan 20, 2022 · 26 comments
Assignees
Labels
info Informational question Further information is requested wontfix This will not be worked on

Comments

@hn2
Copy link

hn2 commented Jan 20, 2022

Which version are you running? The lastest version is on Github. Pip is for major releases.

import pandas_ta as ta
print(ta.version)

0.3.14b0

Do you have TA Lib also installed in your environment?

$ pip list

Yes
TA-Lib 0.4.17

Upgrade.

$ pip install -U git+https://github.com/twopirllc/pandas-ta

Describe the bug
When trying to add all indicators (ta.AllStrategy) to my dataframe I get:

Traceback (most recent call last):
  File "C:\Users\hanna\Anaconda3\lib\multiprocessing\pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "C:\Users\hanna\Anaconda3\lib\multiprocessing\pool.py", line 44, in mapstar
    return list(map(*args))
  File "C:\Users\hanna\Anaconda3\lib\site-packages\pandas_ta\core.py", line 467, in _mp_worker
    return getattr(self, method)(*args, **kwargs)
  File "C:\Users\hanna\Anaconda3\lib\site-packages\pandas_ta\core.py", line 874, in cdl_pattern
    result = cdl_pattern(open_=open_, high=high, low=low, close=close, name=name, offset=offset, **kwargs)
  File "C:\Users\hanna\Anaconda3\lib\site-packages\pandas_ta\candles\cdl_pattern.py", line 64, in cdl_pattern
    pattern_result = Series(pattern_func(open_, high, low, close, **kwargs) / 100 * scalar)
  File "_abstract.pxi", line 352, in talib._ta_lib.Function.__call__
  File "_abstract.pxi", line 383, in talib._ta_lib.Function.__call_function
  File "C:\Users\hanna\Anaconda3\lib\site-packages\talib\__init__.py", line 24, in wrapper
    return func(*args, **kwargs)

TypeError: Argument 'open' has incorrect type (expected numpy.ndarray, got NoneType)

As you can see it is probably due to cdl_pattern error.

When I switch to ta.CommonStrategy I get:

Index(['open', 'high', 'low', 'close', 'volume', 'SMA_10', 'SMA_20', 'SMA_50',
'SMA_200', 'VOL_SMA_20'],

I get 6 indicators(sma) added to my dataframe.
Is this the expected behavior? Only 6 common indicators?

@hn2 hn2 added the bug Something isn't working label Jan 20, 2022
@twopirllc twopirllc removed their assignment Jan 20, 2022
@twopirllc twopirllc added info Informational question Further information is requested wontfix This will not be worked on and removed bug Something isn't working labels Jan 20, 2022
@twopirllc
Copy link
Owner

Hello @hn2,

When I switch to ta.CommonStrategy I get:

Index(['open', 'high', 'low', 'close', 'volume', 'SMA_10', 'SMA_20', 'SMA_50',
'SMA_200', 'VOL_SMA_20'],

I get 6 indicators(sma) added to my dataframe.
Is this the expected behavior? Only 6 common indicators?

Yes. This is the correct behavior for the Common Strategy (as defined below). These are the indicators used the be financial news media (CNBC, Bloomberg, et al...) You are welcome to submit more Builtin Strategies if you want to share with the community.

# All Default Strategy
AllStrategy = Strategy(
name="All",
description="All the indicators with their default settings. Pandas TA default.",
ta=None,
)
# Default (Example) Strategy.
CommonStrategy = Strategy(
name="Common Price and Volume SMAs",
description="Common Price SMAs: 10, 20, 50, 200 and Volume SMA: 20.",
ta=[
{"kind": "sma", "length": 10},
{"kind": "sma", "length": 20},
{"kind": "sma", "length": 50},
{"kind": "sma", "length": 200},
{"kind": "sma", "close": "volume", "length": 20, "prefix": "VOL"}
]
)


Could you please share reproducible code that was requested in the bug report?

According to the Stack Trace, it is not a Pandas TA problem. The problem has to do with your "open" data type: TypeError: Argument 'open' has incorrect type (expected numpy.ndarray, got NoneType). For some reason, your "open" data type is not like your "close" and "volume" data types (since those worked fine with the Common Strategy).

Kind Regards,
KJ

@hn2
Copy link
Author

hn2 commented Jan 20, 2022

I believe that it has to do with the fact the pandas-ta candlestick patterns are build on top of ta-lib, and ta-lib is probably expecting a different data type. Could that be the reason for the error?

@hn2
Copy link
Author

hn2 commented Jan 20, 2022

I am still not sure how to resolve it though. I tried converting to nparray but it didn't work.

@hn2
Copy link
Author

hn2 commented Jan 20, 2022

class technical_features:
    def __init__(self,
                 security,
                 open_name,
                 high_name,
                 low_name,
                 close_name,
                 volume_name):

        open_price = security[open_name].values
        high_price = security[high_name].values
        low_price = security[low_name].values
        close_price = security[close_name].values

        security['open'] = np.array(open_price)
        security['high'] = np.array(high_price)
        security['low'] = np.array(low_price)
        security['close'] = np.array(close_price)

        print('Type')
        print(type(security['open']))

        
    def get_indicators_all(self):
       
        security.ta.strategy('all')

        
        print(security.columns)
        print(security.ta.categories)
        print(security)

        return security

@hn2
Copy link
Author

hn2 commented Jan 20, 2022

The type is this:
Type
<class 'pandas.core.series.Series'>
for open and for close.

@twopirllc
Copy link
Owner

I believe that it has to do with the fact the pandas-ta candlestick patterns are build on top of ta-lib, and ta-lib is probably expecting a different data type. Could that be the reason for the error?

No. It's a Type Error as the stack trace says. Again, it is not a Pandas TA error.

Thanks for sharing "some" code this time. Please share your input data or how you read your data in? Also please provide a small csv sample (around 300 rows should do).

What type is security? A Pandas DataFrame or a dict? You are not clear on that in your code since there are no type hints. When calling: security.ta.strategy('all'), security needs to be a DataFrame.

Also, you shouldn't have to use either of these:

        open_price = security[open_name].values  # This converts to numpy values
        security['open'] = np.array(open_price) # This line is redundant

Do you have yfinance installed? (pip install yfinance) If so, you should be able to do the following:

import pandas as pd
import pandas_ta as ta

security = pd.DataFrame()
security = security.ta.ticker("aapl", period="1y")
security.ta.strategy(timed=True)

print(security)
print(security.columns)

Screen Shot 2022-01-20 at 8 36 43 AM


Would be interested in your output with the code I provided.

Kind Regards,
KJ

@hn2
Copy link
Author

hn2 commented Jan 20, 2022

You code did not run for me. Same error:
TypeError: Argument 'open' has incorrect type (expected numpy.ndarray, got NoneType)
I have an older pandas version (I need it to work with quantconnect).
pandas 0.25.3
numpy 1.19.5
TA-Lib 0.4.17

@twopirllc
Copy link
Owner

Screenshot?

@hn2
Copy link
Author

hn2 commented Jan 20, 2022

image

@hn2
Copy link
Author

hn2 commented Jan 20, 2022

image

@twopirllc
Copy link
Owner

Is there anything in security?

import pandas as pd
import pandas_ta as ta

security = pd.DataFrame()
security = security.ta.ticker("aapl", period="1y")
print(security)
print(security.dtypes)

@hn2
Copy link
Author

hn2 commented Jan 20, 2022

I closed visual code and reopened it. Restarted the kernal. I don't believe that there is anything in security.
I also tried upgrading pandas to 1.1.5.
Same problem.

@twopirllc
Copy link
Owner

twopirllc commented Jan 20, 2022

pip install yfinance

@twopirllc
Copy link
Owner

Also upgrade Pandas TA.

$ pip install -U git+https://github.com/twopirllc/pandas-ta

Has other bug fixes and new features. I am changing strategy() to study() but both will work.

@twopirllc
Copy link
Owner

Your local environment could be corrupted... it happens sometimes.

@hn2
Copy link
Author

hn2 commented Jan 20, 2022

That wouldn't help.
I believe that the problem has to do with TA-Lib.
Are you sure that you are getting all 61 TA-Lib candlestick patterns?
My guess is that you are not.

@twopirllc
Copy link
Owner

twopirllc commented Jan 20, 2022

I am sorry solutions I have presented have not solved your issue.

I believe that the problem has to do with TA-Lib.

I agree. I would recreate the local environment, usually solves most of my problems. Also time permitting, I am trying to implement numpy versions of TA Lib and hopefully the candles so TA Lib is optional.

Are you sure that you are getting all 61 TA-Lib candlestick patterns?

Yes, I am sure. In fact, 62.

Screen Shot 2022-01-20 at 10 21 38 AM

Screen Shot 2022-01-20 at 10 20 05 AM


KJ

@hn2
Copy link
Author

hn2 commented Jan 20, 2022

What version of TA-Lib are you using?

@twopirllc
Copy link
Owner

Screen Shot 2022-01-20 at 10 32 31 AM

@hn2
Copy link
Author

hn2 commented Jan 20, 2022

Ok. I have an earlier version. I will try to install version 0.4.21 (right now it is giving me an error).
I wish that you could implement everything including candle patterns in pure python without TA-Lib which is a pain to install.

@twopirllc
Copy link
Owner

I wish that you could implement everything including candle patterns in pure python without TA-Lib which is a pain to install.

I know. That's partly the reason this package exists.

@hn2
Copy link
Author

hn2 commented Jan 21, 2022

I upgraded my ta-lib. I now get this error:

in get_indicators_all(self)
81 # self.security.ta.strategy(ta.CommonStrategy)
82
---> 83 security.ta.strategy('all')
84
85 # # Use verbose if you want to make sure it is running.

~\Anaconda3\lib\site-packages\pandas_ta\core.py in strategy(self, *args, **kwargs)
790
791 # Apply prefixes/suffixes and appends indicator results to the DataFrame
--> 792 [self._post_process(r, **kwargs) for r in results]
793
794 if verbose:

~\Anaconda3\lib\site-packages\pandas_ta\core.py in (.0)
790
791 # Apply prefixes/suffixes and appends indicator results to the DataFrame
--> 792 [self._post_process(r, **kwargs) for r in results]
793
794 if verbose:

~\Anaconda3\lib\site-packages\tqdm\std.py in iter(self)
1178
1179 try:
-> 1180 for obj in iterable:
1181 yield obj
1182 # Update and possibly print the progressbar.

~\Anaconda3\lib\multiprocessing\pool.py in (.0)
318 result._set_length
319 ))
--> 320 return (item for chunk in result for item in chunk)
321
322 def imap_unordered(self, func, iterable, chunksize=1):

~\Anaconda3\lib\multiprocessing\pool.py in next(self, timeout)
733 if success:
734 return value
--> 735 raise value
736
737 next = next # XXX

Exception: inputs are all NaN

This is my data:

open high low close volume
0 1.151420 1.151915 1.150835 1.151040 0.0
1 1.151040 1.155060 1.145575 1.146480 0.0
2 1.146480 1.149190 1.143950 1.147080 0.0
3 1.147080 1.147700 1.137870 1.139860 0.0
4 1.139860 1.143285 1.135630 1.137515 0.0
.. ... ... ... ... ...
995 1.131090 1.133190 1.128465 1.129500 0.0
996 1.129515 1.136490 1.129005 1.136155 0.0
997 1.135980 1.136255 1.134955 1.135365 0.0
998 1.135385 1.135825 1.128505 1.133005 0.0
999 1.132985 1.137550 1.131305 1.136600 0.0

@hn2
Copy link
Author

hn2 commented Jan 21, 2022

I guess that the dataframe needs datetimeindex?
Is there a workaround?

@twopirllc
Copy link
Owner

@hn2,

I guess that the dataframe needs datetimeindex?

No. The only indicator that requires it is: vwap.

How come you use time invariant data?

Is there a workaround?
Yes. You can exclude a list of indicators you don't want from strategy.

import pandas as pd
import pandas_ta as ta

security = pd.DataFrame()
security = security.ta.ticker("aapl", period="1y")

vwap_ = ["vwap"]  # 
security.ta.strategy(exclude=vwap_, timed=True)

print(security)
print(security.columns)

# help(security.ta.strategy)  # Documentation on the strategy method

Also, since you are using Windows and strategy (which uses multiprocessing), you might get a freeze_support error when creating a strategy Pinned Issue #181


Here are others ways to get help. I recommend reading Quick Start, Help and Issues.

Screen Shot 2022-01-21 at 9 43 06 AM

KJ

@hn2
Copy link
Author

hn2 commented Jan 21, 2022

Thank you

@hn2
Copy link
Author

hn2 commented Jan 21, 2022

Is there a way to easily exclude the entire volume group of indicators?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
info Informational question Further information is requested wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants