From a675b2bd68259184392cfae7f14657c97be3326f Mon Sep 17 00:00:00 2001 From: Mark Spratt <72772558+Hopelezz@users.noreply.github.com> Date: Tue, 11 Jun 2024 12:23:45 -0500 Subject: [PATCH 1/3] Add Average Trend data This now allows you to set the depth of the historical data rather than infinite which could cause memory issues for long periods. --- trends.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/trends.py b/trends.py index 47f0d2c..764d93e 100644 --- a/trends.py +++ b/trends.py @@ -16,15 +16,23 @@ def check_price_trends(): if current_price is not None: price_change = ((current_price - previous_price) / previous_price) if previous_price else 0 price_trends.append(price_change) + + # Keep only the last 10 trends + if len(price_trends) > 10: + price_trends.pop(0) + details['previous_price'] = current_price details['price_trends'] = price_trends details['current_price'] = current_price details['usd_value'] = current_price * balance + # Averaging the last 5 trends + avg_trend = np.mean(price_trends[-5:]) + # Update trend status based on price trends - if price_change > 0: + if avg_trend > 0: trend_status = 'upward' - elif price_change < 0: + elif avg_trend < 0: trend_status = 'downward' else: trend_status = 'stable' From d681a2b4a4f43ce76d4e903eed5272ed696fb732 Mon Sep 17 00:00:00 2001 From: Mark Spratt <72772558+Hopelezz@users.noreply.github.com> Date: Tue, 11 Jun 2024 12:24:21 -0500 Subject: [PATCH 2/3] Add Numpy Forgot to add numpy --- trends.py | 1 + 1 file changed, 1 insertion(+) diff --git a/trends.py b/trends.py index 764d93e..185f9ad 100644 --- a/trends.py +++ b/trends.py @@ -1,5 +1,6 @@ from coinbase import get_current_price from config import load_coins_settings, update_coins_settings +import numpy as np coins_settings = load_coins_settings() From 872489b3edec10762e12a90a711190b865a115b9 Mon Sep 17 00:00:00 2001 From: Mark Spratt <72772558+Hopelezz@users.noreply.github.com> Date: Tue, 11 Jun 2024 12:50:53 -0500 Subject: [PATCH 3/3] Update trends.py with SMA alternative Simple Moving Average: smooth out price data by creating a constantly updated average price (Close prices over "n" period)/n = SMA * The SMA helps to identify trends by filtering out the noise from random price fluctuations. * It provides a clearer view of the asset's price direction over time. --- trends.py | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/trends.py b/trends.py index 185f9ad..55e8b42 100644 --- a/trends.py +++ b/trends.py @@ -4,42 +4,54 @@ coins_settings = load_coins_settings() - +def calculate_sma(prices, window): + if len(prices) < window: + return None # Not enough data to calculate SMA + return np.mean(prices[-window:]) + def check_price_trends(): + short_term_window = 10 + long_term_window = 30 + for coin, details in coins_settings.items(): if details.get('enabled', False): previous_price = details.get('previous_price') current_price = get_current_price(f"{coin}-USD") balance = details.get('balance', 0) current_cost_usd = details.get('current_cost_usd', -1) - price_trends = details.get('price_trends', []) if current_price is not None: - price_change = ((current_price - previous_price) / previous_price) if previous_price else 0 - price_trends.append(price_change) - - # Keep only the last 10 trends - if len(price_trends) > 10: - price_trends.pop(0) - + details['prices'].append(current_price) + if previous_price is not None: + price_change = (current_price - previous_price) / previous_price + else: + price_change = 0 + + details['price_trends'].append(price_change) + if len(details['price_trends']) > short_term_window: + details['price_trends'].pop(0) + details['previous_price'] = current_price - details['price_trends'] = price_trends details['current_price'] = current_price details['usd_value'] = current_price * balance - # Averaging the last 5 trends - avg_trend = np.mean(price_trends[-5:]) - - # Update trend status based on price trends - if avg_trend > 0: - trend_status = 'upward' - elif avg_trend < 0: - trend_status = 'downward' + # Calculate SMA + details['sma_short_term'] = calculate_sma(details['prices'], short_term_window) + details['sma_long_term'] = calculate_sma(details['prices'], long_term_window) + + # Determine trend status using SMA + if details['sma_short_term'] and details['sma_long_term']: + if details['sma_short_term'] > details['sma_long_term']: + trend_status = 'upward' + elif details['sma_short_term'] < details['sma_long_term']: + trend_status = 'downward' + else: + trend_status = 'stable' else: trend_status = 'stable' details['trend_status'] = trend_status - + update_coins_settings(coins_settings) else: print(f"Could not retrieve current price for {coin}. Skipping...")