alvarobartt / trendet

:chart_with_upwards_trend: Python package for trend detection on stock time series data :chart_with_downwards_trend:
https://trendet.readthedocs.io/
MIT License
461 stars 80 forks source link

The algorithm does not find trends in the final intervals #11

Open jackure opened 4 years ago

jackure commented 4 years ago

What makes an algorithm like this attractive is being able to identify the current trend of the market. However, in all tests I've done, the algorithm ignores the final intervals. Below I show you the result of the algorithm with smoothed data, to further accentuate the phenomenon that I mentioned.

image

alvarobartt commented 4 years ago

Hi @jackure, thank you for reporting this!

I already need to perform some tests over this so as to identify the problem and find the best possible solution! Please, could you send me a CSV file containing the data that produced that concrete error? Or just the trendet function call that you made?

Thank you! Remember to star the repo if you found it useful and follow me at GitHub so as to get notified of all the updates!

mattstone commented 4 years ago

Just adding to this.. something does look a bit broken with the trend identification algorithm.

This is the test data I've used.

test = investpy.get_stock_historical_data(stock='AAPL', country='united states', from_date='01/04/2020', to_date='20/07/2020')

df = trendet.identify_df_trends(df=test, column='Close')

This is the result.

Screen Shot 2020-07-26 at 10 15 56 am

Also, the demonstration breaks if there is no downtrend (as in the above example). Quick fix highlighted in bold.

with plt.style.context('seaborn-paper'): plt.figure(figsize=(20, 10))

ax = sns.lineplot(x=df['Date'], y=df['Close'])

**if 'Up Trend' in df.columns:**
  labels = df['Up Trend'].dropna().unique().tolist()

  for label in labels:
      sns.lineplot(x=df[df['Up Trend'] == label]['Date'],
                   y=df[df['Up Trend'] == label]['Close'],
                   color='green')

      ax.axvspan(df[df['Up Trend'] == label]['Date'].iloc[0],
                 df[df['Up Trend'] == label]['Date'].iloc[-1],
                 alpha=0.2,
                 color='green')

**if 'Down Trend' in df.columns:**
  labels = df['Down Trend'].dropna().unique().tolist()

  for label in labels:
      sns.lineplot(x=df[df['Down Trend'] == label]['Date'],
                   y=df[df['Down Trend'] == label]['Close'],
                   color='red')

      ax.axvspan(df[df['Down Trend'] == label]['Date'].iloc[0],
                 df[df['Down Trend'] == label]['Date'].iloc[-1],
                 alpha=0.2,
                 color='red')

plt.show()
sunilhariharan commented 3 years ago

Hi…i have fixed this….can i post an image of my code /the modified code ?

yuvalfoox commented 3 years ago

@sunilhariharan Can you please share your code?

AliAlhajji commented 3 years ago

I noticed the same issue too. When I change the to_date parameter to be in the middle of an area that was predicted as a trend, it does not identify it as a trend anymore.

Example:

image

And when I changed to_date to be in 30/06/2021, the uptrend was not identified: image

You can try it with this stock:

Stock: 8240 Country: Saudi Arabia From: "01/02/2021" To (1): "08/08/2021" To (2): "30/06/2021" Window: 5

sunilhariharan commented 3 years ago

The bug is in the for loop…This algo will never be able to find the last upward and the last downward local trend. In the for loop,when the limit is being compared to the value, what is happening is for the last updard n downward trend iteration, the algorithm is not able to get a value greater than the limit (in the elif part)…so it is not appending that trend

AliAlhajji commented 3 years ago

The bug is in the for loop…This algo will never be able to find the last upward and the last downward local trend. In the for loop,when the limit is being compared to the value, what is happening is for the last updard n downward trend iteration, the algorithm is not able to get a value greater than the limit (in the elif part)…so it is not appending that trend

Were you able to fix it? Can you share the code if you did?

Thanks.

sunilhariharan commented 3 years ago

WhatsApp Image 2021-08-11 at 12 01 21 AM (6)

sunilhariharan commented 3 years ago

WhatsApp Image 2021-08-11 at 12 01 21 AM WhatsApp Image 2021-08-11 at 12 01 21 AM (1) WhatsApp Image 2021-08-11 at 12 01 21 AM (2) WhatsApp Image 2021-08-11 at 12 01 21 AM (3) WhatsApp Image 2021-08-11 at 12 01 21 AM (4) WhatsApp Image 2021-08-11 at 12 01 21 AM (5)

AliAlhajji commented 3 years ago

Thansk @sunilhariharan ! If the code is working as expected, would you like to share it as a forked repo or a pull request? It's hard to follow line by line here :)

AliAlhajji commented 3 years ago

Or at least copy and paste the code in here so that we can follow your logic easily.

@sunilhariharan

yuvalfoox commented 3 years ago

@sunilhariharan Thanks for sharing! Can you please share it as a repo or copy the full text as comment with example data? I got a bit lost there :| Thanks!

jokaorgua commented 2 years ago

@sunilhariharan could you share the code in text please?

jokaorgua commented 2 years ago

@alvarobartt I've made a pull request. please check it and if everything is ok - add to master

AliAlhajji commented 2 years ago

@alvarobartt I've made a pull request. please check it and if everything is ok - add to master

I tried your code. I don't think it's doing better than what we have now. I've back-tested it on several stocks, none of which showed uptrend towards the end even if there was one.

jokaorgua commented 2 years ago

@alvarobartt I've made a pull request. please check it and if everything is ok - add to master

I tried your code. I don't think it's doing better than what we have now. I've back-tested it on several stocks, none of which showed uptrend towards the end even if there was one.

Could you provide example data so I can backtest it too ?

sunilhariharan commented 2 years ago

@AliAlhajji did you try my code?