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

Creating a Custom Indicator: The Big 4 #355

Closed
twopirllc opened this issue Jul 26, 2021 · 0 comments
Closed

Creating a Custom Indicator: The Big 4 #355

twopirllc opened this issue Jul 26, 2021 · 0 comments

Comments

@twopirllc
Copy link
Owner

In short, there are basically four steps to fully add an indicator to Pandas TA. For concrete examples, I recommend looking at @rengel8's RSX and @whubsch's MCG indicators. Essentially their indicator submission is some combination of steps outlined below.

Creating a Custom Indicator: The Big 4

This is a step by step guide to creating a Custom Indicator to provide both Standard and Pandas TA DataFrame Extensionable Indicators. So lets create a "hypothetical indicator" named ni(close, length=10, **kwargs) that belongs in the Trend Category. It's implementation is up to you, however at a minimum it should only rely on Pandas's dependencies (typically Pandas or NumPy calculations).

Step One: Adding the Indicator to it's Category

For ni(close, length=None, **kwargs) to be evaluated the Standard way, we need to do two things:

  1. Add the line from .ni import ni to pandas_ta/trend/__init__.py
  2. Create the file: pandas_ta/trend/ni.py.
    1. Copy a similar indicator as a template for the ni indicator specification and then modify it.

Now it should be callable the Standard way like a TA Lib indicator:

import pandas as pd
import pandas_ta as ta

df = # ohlcv data
nidf = ta.ni(df["close"]) #  = ta.ni(df["close"], 10)

Step Two: Adding the Indicator to the Pandas TA DataFrame

The next two steps are required to link ni(close, length=10, **kwargs) with Pandas TA DataFrame. This allows you to call it from the ta extension or from the ta.strategy() extension method.

  1. Add "ni" to the Category dict under the "trend" key in pandas_ta/__init__.py
  2. Add the ni method to pandas_ta/core.py.
    1. Copy a similar working core DataFrame method and update the required names.
@pd.api.extensions.register_dataframe_accessor("ta")
class AnalysisIndicators(BasePandasObject):

    # ...
    # Trend
    # ...

    def ni(self, length=None, offset=None, **kwargs):
        close = self._get_column(kwargs.pop("close", "close"))
        result = ni(close=close, length=length, offset=offset, **kwargs)
        return self._post_process(result, **kwargs)

That should be it!

Now you can run ni(close, length=10, **kwargs) either the Standard way above or as a DataFrame Extension:

import pandas as pd
import pandas_ta as ta

df = # ohlcv data
df.ta.ni(append=True)

Or via a Custom Strategy:

import pandas as pd
import pandas_ta as ta

df = # ohlcv data
# (1) Create the Strategy
MyStrategy = ta.Strategy(
    name="NI Trend",
    ta=[
        {"kind": "ohlc4"},
        {"kind": "ni", "length": 12},
    ]
)

# (2) Run the Strategy
df.ta.strategy(MyStrategy, **kwargs)

TLDR

Add and edit the file pandas_ta/trend/ni.py. Then modify the three files: pandas_ta/trend/__init__.py, pandas_ta/__init__.py, and pandas_ta/core.py


Hope this helps! Please let me know (good/bad)! Future contributions are greatly appreciated! 😎

Kind Regards,
KJ

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

1 participant