twopirllc / pandas-ta

Technical Analysis Indicators - Pandas TA is an easy to use Python 3 Pandas Extension with 150+ Indicators
https://twopirllc.github.io/pandas-ta/
MIT License
5.21k stars 1.02k forks source link

Boolean Operations on Multiple (X)Signals #587

Open aress31 opened 2 years ago

aress31 commented 2 years ago

I would like to be able to apply multiple cross signals based on boolean algorithmic. In the example below, it appears that only the last applied signals, meaning the last call of benchmarkdf.ta.xsignals sets the values for [TS_Trends, TS_Trades, TS_Entries, TS_Exit]. Exposing a method that would allow basic boolean operation such as or and and on different signals to granularly configure how to set the [TS_Trends, TS_Trades, TS_Entries, TS_Exit] entries would be amazing.

Alternatively, if you have a work around, please share. 😁

benchmarkdf = ta.df.ta.ticker("AAPL", timed=True)

benchmarkdf.ta.cci(append=True)
benchmarkdf.ta.percent_return(cumulative=True, append=True)
benchmarkdf.ta.rsi(append=True)

benchmarkdf.ta.xsignals(
    benchmarkdf['RSI_14'], 20, 80, asbool=True, above=True, append=True)
benchmarkdf.ta.xsignals(
    benchmarkdf['CCI_14_0.015'], -150, 150, asbool=True, above=True, append=True)
benchmarkdf.ta.xsignals(
    benchmarkdf['CUMPCTRET_1'], -0.02, 0.02, asbool=True, above=True, append=True)

cond = benchmarkdf['TS_Exits']
x = benchmarkdf[cond]

print(x)
print(x.shape)
twopirllc commented 1 year ago

@aress31,

Here is a, non-tested, modified example with your code. Tweek as necessary.

import pandas as pd
import pandas_ta as ta

df = pd.DataFrame()
benchmarkdf = df.ta.ticker("AAPL", timed=True)

benchmarkdf.ta.cci(append=True)
benchmarkdf.ta.percent_return(cumulative=True, append=True)
benchmarkdf.ta.rsi(append=True)

# Your example above was just overwriting: [TS_Trends, TS_Trades, TS_Entries, TS_Exit]
# Prefix them as well as capture their individual DataFrames
rsi_signals = benchmarkdf.ta.xsignals(benchmarkdf['RSI_14'], 20, 80, asbool=True, above=True, prefix="RSI", append=True)
cci_signals = benchmarkdf.ta.xsignals(benchmarkdf['CCI_14_0.015'], -150, 150, asbool=True, above=True, prefix="CCI", append=True)
cpr_signals = benchmarkdf.ta.xsignals(benchmarkdf['CUMPCTRET_1'], -0.02, 0.02, asbool=True, above=True, prefix="CPR", append=True)

# Some example signals. Unchecked code, so
or_signal = (rsi_signals["TS_Trends"] | cci_signals["TS_Trends"] | cpr_signals["TS_Trends"])
and_signal = (rsi_signals["TS_Trends"] | cci_signals["TS_Trends"] | cpr_signals["TS_Trends"])
complex_signal = ((rsi_signals["TS_Trends"] | cci_signals["TS_Trends"]) & cpr_signals["TS_Trends"])

# Feed combined signal of choice back into ta.tsignals() to get final: [TS_Trends, TS_Trades, TS_Entries, TS_Exit]
combined_signals = benchmarkdf.ta.tsignals(and_signal, asbool=True, append=True)

print(combined_signals)
print(combined_signals.shape)

# ...

https://github.com/twopirllc/pandas-ta/blob/development/examples/TA_Analysis.ipynb

Exposing a method that would allow basic boolean operation such as or and and on different signals to granularly configure how to set the [TS_Trends, TS_Trades, TS_Entries, TS_Exit] entries would be amazing.

Certainly. It's possibly not terribly complex to implement. Though I am curious about future non-trivial cases that could be requested by users. 🤔 Would you like to give it a go? 😎

KJ