Created
August 31, 2023 16:36
-
-
Save 18182324/b4415702cd3006c2bf7ab7a211ea6f09 to your computer and use it in GitHub Desktop.
Analyzing Trade Entry and Exit Positions with Regression Analysis with Quandl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import necessary libraries | |
import quandl | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# Set your Quandl API key (you need to sign up on Quandl's website to get your API key) | |
quandl.ApiConfig.api_key = 'YOUR_API_KEY' | |
# Define the dataset codes for Google and Yahoo | |
google_dataset = 'WIKI/GOOGL' # Replace with the correct dataset code for Google | |
yahoo_dataset = 'WIKI/YHOO' # Replace with the correct dataset code for Yahoo | |
# Define the date range for the past 350 weeks | |
end_date = '2023-08-31' # Replace with the desired end date | |
# Fetch the price data for Google and Yahoo | |
google_data = quandl.get(google_dataset, end_date=end_date) | |
yahoo_data = quandl.get(yahoo_dataset, end_date=end_date) | |
# Calculate the returns for Google and Yahoo | |
google_returns = google_data['Adj. Close'].pct_change().dropna() | |
yahoo_returns = yahoo_data['Adj. Close'].pct_change().dropna() | |
# Calculate the regression channel for Google and Yahoo | |
google_x = np.arange(len(google_returns)) | |
yahoo_x = np.arange(len(yahoo_returns)) | |
google_slope, google_intercept = np.polyfit(google_x, google_returns, 1) | |
yahoo_slope, yahoo_intercept = np.polyfit(yahoo_x, yahoo_returns, 1) | |
google_regression = google_intercept + google_slope * google_x | |
yahoo_regression = yahoo_intercept + yahoo_slope * yahoo_x | |
# Calculate the confidence intervals for the regression lines | |
google_residuals = google_returns - google_regression | |
yahoo_residuals = yahoo_returns - yahoo_regression | |
google_std = np.std(google_residuals) | |
yahoo_std = np.std(yahoo_residuals) | |
google_confidence_interval = 2 * google_std | |
yahoo_confidence_interval = 2 * yahoo_std | |
# Plot the regression channel for Google | |
plt.figure(figsize=(12, 6)) | |
plt.plot(google_x, google_returns, label='Google Returns', alpha=0.5) | |
plt.plot(google_x, google_regression, label='Google Regression', color='green') | |
plt.fill_between(google_x, google_regression - google_confidence_interval, google_regression + google_confidence_interval, color='green', alpha=0.2) | |
plt.title('Regression Channel: Google') | |
plt.xlabel('Weeks') | |
plt.ylabel('Returns') | |
plt.legend() | |
plt.grid(True) | |
# Plot the regression channel for Yahoo | |
plt.figure(figsize=(12, 6)) | |
plt.plot(yahoo_x, yahoo_returns, label='Yahoo Returns', alpha=0.5) | |
plt.plot(yahoo_x, yahoo_regression, label='Yahoo Regression', color='blue') | |
plt.fill_between(yahoo_x, yahoo_regression - yahoo_confidence_interval, yahoo_regression + yahoo_confidence_interval, color='blue', alpha=0.2) | |
plt.title('Regression Channel: Yahoo') | |
plt.xlabel('Weeks') | |
plt.ylabel('Returns') | |
plt.legend() | |
plt.grid(True) | |
# Show the plots | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment