0% found this document useful (0 votes)
12 views

سسس

Uploaded by

ads.bkcy1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

سسس

Uploaded by

ads.bkcy1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import yfinance as yf

import pandas as pd
import numpy as np
import os
from multiprocessing import Pool, cpu_count
from tqdm import tqdm
from google.colab import drive

# Mount Google Drive


drive.mount('/content/drive')

# Function to download stock data from Yahoo Finance and save it locally
def download_stock_data(tickers, save_path):
if not os.path.exists(save_path):
os.makedirs(save_path)

for ticker in tickers:


file_path = os.path.join(save_path, f"{ticker}.csv")
if not os.path.exists(file_path):
print(f"Downloading data for {ticker}...")
try:
stock_data = yf.download(ticker, period="max", interval="1d") #
Daily data
stock_data.to_csv(file_path)
except Exception as e:
print(f"Failed to download data for {ticker}: {e}")
else:
print(f"Data for {ticker} already exists. Skipping download.")

# Function to load stock data from saved files


def load_stock_data(save_path):
stock_data = {}
for file in os.listdir(save_path):
if file.endswith(".csv"):
ticker = file.replace(".csv", "")
stock_data[ticker] = pd.read_csv(os.path.join(save_path, file))
return stock_data

# Function to find the most similar periods


def find_most_similar_period_efficient(args):
series1, series2, ticker = args
similarities = []

for i in range(0, len(series2) - len(series1) + 1):


window = series2[i:i + len(series1)]
correlation = np.corrcoef(series1, window)[0, 1]
if correlation > 0.8: # You can adjust the threshold as needed
similarities.append((i, correlation))

return ticker, similarities

# Function to compare a given chart with all loaded stock data


def compare_chart_with_stocks(chart_data, stock_data):
results = []
chart_close = chart_data['Close'].values
if len(chart_close) == 0 or np.std(chart_close) == 0:
return results

chart_close_normalized = (chart_close - np.mean(chart_close)) /


np.std(chart_close)

tasks = []
for ticker, data in stock_data.items():
stock_close = data['Close'].values
if len(stock_close) == 0 or np.std(stock_close) == 0:
continue
stock_close_normalized = (stock_close - np.mean(stock_close)) /
np.std(stock_close)
tasks.append((chart_close_normalized, stock_close_normalized, ticker))

with Pool(cpu_count()) as pool:


for ticker, similarities in
tqdm(pool.imap_unordered(find_most_similar_period_efficient, tasks),
total=len(tasks)):
if len(similarities) > 1: # Check if there are multiple similarities
# Sort similarities by their indices
similarities.sort()
# Keep track of non-overlapping periods
non_overlapping = []
for i, (idx, sim) in enumerate(similarities):
if all(abs(idx - prev_idx) >= len(chart_close) for prev_idx, _
in non_overlapping):
non_overlapping.append((idx, sim))

if len(non_overlapping) > 1:
for idx1, sim1 in non_overlapping:
chart_start_date = chart_data['Date'].iloc[0]
chart_end_date = chart_data['Date'].iloc[-1]
stock_start_date = stock_data[ticker]['Date'].iloc[idx1]
stock_end_date = stock_data[ticker]['Date'].iloc[min(idx1 +
len(chart_close), len(stock_data[ticker]) - 1)]

result = {
"ticker": ticker,
"similarity": sim1,
"chart_period": (chart_start_date, chart_end_date),
"stock_period": (stock_start_date, stock_end_date)
}
results.append(result)
print(f"Ticker: {ticker}")
print(f"Similarity: {sim1}")
print(f"Chart Period: {chart_start_date} to
{chart_end_date}")
print(f"Stock Period: {stock_start_date} to
{stock_end_date}")
print("\n")

return results

# Path to the file containing the stock tickers on Google Drive


tickers_file_path = "/content/drive/MyDrive/all_tickers.txt"
# Path to save the downloaded stock data on Google Drive
save_path = "/content/drive/MyDrive/MM"
# Path to the chart file to be compared on Google Drive
chart_file_path = "/content/drive/MyDrive/DOGE-USD.csv"

# Read the tickers from the file


with open(tickers_file_path, "r") as file:
tickers = [line.strip() for line in file]

# Download the stock data


download_stock_data(tickers, save_path)

# Load the stock data


stock_data = load_stock_data(save_path)

# Load the chart to be compared


chart_data = pd.read_csv(chart_file_path)

# Ensure the 'Date' column is in datetime format


chart_data['Date'] = pd.to_datetime(chart_data['Date'])

# Compare the chart with all loaded stock data


results = compare_chart_with_stocks(chart_data, stock_data)

You might also like