Skip to content

Commit

Permalink
Fix bug in decayosc calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvan2006 committed Sep 11, 2024
1 parent dfa24ff commit 265f384
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions backend/indicators/management/commands/fetchindicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,24 +256,30 @@ def calculate_decayosc():

t0_datetime = datetime(2009, 1, 3)
df['Days'] = (df.index - t0_datetime).days

# Clean data
df = df.replace([np.inf, -np.inf], np.nan).dropna() # Remove inf values
df = df[(df['price'] > 0) & (df['Days'] > 0)] # Remove days and prices less than 0

X = np.log(df['Days'].values).reshape(-1, 1)
y = np.log(df['price'].values)
X = np.log(df['Days']).values.reshape(-1, 1)
y = np.log(df['price']).values

# Power law fit
X_with_const = add_constant(X)
results_power_law = OLS(y, X_with_const).fit()
model_power_law = OLS(y, X_with_const)
results_power_law = model_power_law.fit()

# Quadratic fit
X_log_squared = np.vander(X.ravel(), 3)
X_log_squared_with_const = add_constant(X_log_squared)
results_quadratic = QuantReg(y, X_log_squared_with_const).fit(q=0.999)
model_quadratic = QuantReg(y, X_log_squared_with_const)
results_quadratic = model_quadratic.fit(q=0.999)

# Calculate predictions
y_pred_power_law = results_power_law.predict(X_with_const)
y_pred_power_law = results_power_law.predict(X_with_const) - 0.95
y_pred_quadratic = results_quadratic.predict(X_log_squared_with_const)

df['Oscillator'] = (y - y_pred_quadratic) / (y_pred_quadratic - y_pred_power_law)
df['Oscillator'] = (y - y_pred_power_law) / (y_pred_quadratic - y_pred_power_law)

# Save to database
category, _ = Category.objects.get_or_create(name='Technical')
Expand All @@ -289,6 +295,8 @@ def calculate_decayosc():
}
)

print(df)

for date, row in df.iterrows():
if pd.notna(row['Oscillator']):
IndicatorValue.objects.update_or_create(
Expand Down

0 comments on commit 265f384

Please sign in to comment.