facioquo / stock-indicators-python

Stock Indicators for Python. Maintained by @LeeDongGeon1996
https://python.StockIndicators.dev
Apache License 2.0
205 stars 34 forks source link

ZigZag indicator possible look ahed bias error #363

Closed luigif2000 closed 3 months ago

luigif2000 commented 3 months ago

Dear, my relative ZigZag feauters created as foolow:

def calculate_zigzag_features(df):

Converti il DataFrame in una lista di istanze Quote utilizzando l'indice datetime

quotes_list = [
    Quote(d, o, h, l, c, v)
    for d, o, h, l, c, v
    in zip(df.index, df['open'], df['high'], df['low'], df['close'], df['volume'])
]

# Calcola l'indicatore ZigZag per ogni finestra di interesse e aggiungi le relative features al DataFrame
for window in [3,5, 10]:
    # Calcola l'indicatore ZigZag
    zigzag = indicators.get_zig_zag(quotes_list, EndType.CLOSE, window)

    # Crea un DataFrame a partire dai risultati dell'indicatore Zig Zag
    zigzag_df = pd.DataFrame([(z.date, z.zig_zag) for z in zigzag], columns=['Date', f'ZigZagValue_{window}'])
    zigzag_df['Date'] = pd.to_datetime(zigzag_df['Date'])
    zigzag_df.set_index('Date', inplace=True)

    # Unisci i DataFrames
    df = df.join(zigzag_df, how='left')

    # Assicurati che i valori ZigZag siano in formato float
    df[f'ZigZagValue_{window}'] = df[f'ZigZagValue_{window}'].apply(lambda x: float(x) if x is not None else None)

    # Calcola la direzione del Zig Zag e convertila in valori interi
    direction_map = {'Up': 1, 'Down': -1, 'Flat': 0}
    df[f'ZigZagDirection_{window}'] = df[f'ZigZagValue_{window}'].fillna(0).diff().apply(lambda x: 'Up' if x > 0 else ('Down' if x < 0 else 'Flat')).map(direction_map)

    # Calcola l'indicatore Zig Zag relativo
    df[f'ZigZagRelative_{window}'] = ((df[f'ZigZagValue_{window}'].fillna(0) - df['close']) / df['close']) * 100

return df

return a very high importance from my classificator model searcher!

Could be a look ahed bias problem in the indicator!?!?

Thanks in advance and waiting Your kindly reply.

luigi

DaveSkender commented 3 months ago

There is a known amount of historical value repainting with the Zig Zag indicator. This repainting is the root cause of a look-ahead bias. This happens in several indicators in the library. It's not a bug, it's just how the indicator was written by the original author -- I don't invent these things.

To help users identify indicators that repaint or have convergence related initialization errors we've put warnings in our documentation, like this one for Zig Zag:

image
DaveSkender commented 3 months ago

I’d call this one more of a price overlay than an actual indicator of future performance, and wouldn’t recommend it for any predictive analytics.

luigif2000 commented 3 months ago

Dear Dave, thanks a lot for reply, that's very kind of You. Yes repaint=lookahed bias; it's not good proprieties of financial indicator.....when used in machine learning prediction model discover it's a big problem; must avoid.

I will study more about; it's no easy.

Please let us know, when you have time, no urgent, how to collect Your most important indicators, NO repaint, in order to use to create features <- this is the most important use in AI era.

thanks in any case, sorry for my bad english and wish You the best!.

luigi

DaveSkender commented 3 months ago

I don't have a pre-determined list of the most important indicators since I've not done a full analysis of all possibilities. Some of my personal favorites recently are ATR Trailing Stop, Elder Ray Index, Schaff Trend Cycle. I'd started a discussion on the topic:

My personal philosophy to developing a trading strategy is much more broad than just finding that one perfect indicator, but rather it follows this this logic:

  1. Do not rely on one indicator for a trade signal. Consider that you may have one triggering signal event, but that it is also confirmed with one or more other indicators that depict favorable conditions.
  2. Use backtesting on multiple securities to confirm your hypothesis, but understand that changing market conditions may make backtesting an invalid test of your strategy looking forward. Be careful of curve-fitting and reverse-engineering where your strategy works perfectly for a single security, but fails everywhere else.
  3. The industry standard settings for indicators may not be the best settings and that many or most indicators are terrible. Don't believe everything you read in blogs and be cautious around charting sites that share unverified user-defined indicators.
  4. Strategies often do not transfer across asset classes. For example, a strategy that works with equities might not work with currencies.
  5. Finding your own strategy is a journey of personal preferences, homework, testing, and continuous improvement.

It should be noted, that my approach to developing a strategy is constantly evolving and is still a work in progress and probably will be for a very long time.

Originally posted by @DaveSkender in https://github.com/DaveSkender/Stock.Indicators/discussions/277#discussioncomment-287215

luigif2000 commented 3 months ago

Thanks a lot Dear Dave, You are a good guy.....i wish you the very best.

Luigi

Il Gio 4 Apr 2024, 16:36 Dave Skender @.***> ha scritto:

I don't have a pre-determined list of the most important indicators since I've not done a full analysis of all possibilities. Some of my personal favorites recently are ATR Trailing Stop, Elder Ray Index, Schaff Trend Cycle. I'd started a discussion on the topic:

My personal philosophy to developing a trading strategy is much more broad than just finding that one perfect indicator, but rather it follows this this logic:

  1. Do not rely on one indicator for a trade signal. Consider that you may have one triggering signal event, but that it is also confirmed with one or more other indicators that depict favorable conditions.
  2. Use backtesting on multiple securities to confirm your hypothesis, but understand that changing market conditions may make backtesting an invalid test of your strategy looking forward. Be careful of curve-fitting and reverse-engineering where your strategy works perfectly for a single security, but fails everywhere else.
  3. The industry standard settings for indicators may not be the best settings and that many or most indicators are terrible. Don't believe everything you read in blogs and be cautious around charting sites that share unverified user-defined indicators.
  4. Strategies often do not transfer across asset classes. For example, a strategy that works with equities might not work with currencies.
  5. Finding your own strategy is a journey of personal preferences, homework, testing, and continuous improvement.

It should be noted, that my approach to developing a strategy is constantly evolving and is still a work in progress and probably will be for a very long time.

Originally posted by @DaveSkender https://github.com/DaveSkender in DaveSkender/Stock.Indicators#277 (reply in thread) https://github.com/DaveSkender/Stock.Indicators/discussions/277#discussioncomment-287215

— Reply to this email directly, view it on GitHub https://github.com/DaveSkender/Stock.Indicators.Python/issues/363#issuecomment-2037390973, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABNWP4LBNBMRQE6KDY3CFMLY3VQN3AVCNFSM6AAAAABFVLDAHSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMZXGM4TAOJXGM . You are receiving this because you authored the thread.Message ID: @.***>