Skip to content

Instantly share code, notes, and snippets.

@Kouhei-Takagi
Last active April 6, 2024 13:23
Show Gist options
  • Save Kouhei-Takagi/7fc42a218c895705daedbf0dd2a855cc to your computer and use it in GitHub Desktop.
Save Kouhei-Takagi/7fc42a218c895705daedbf0dd2a855cc to your computer and use it in GitHub Desktop.
Nasdaq vs Commodity Features(Corration 0.868)
import yfinance as yf
import pandas as pd
class Data:
def __init__(self, symbol, start_date, end_date):
self.symbol = symbol
self.start_date = start_date
self.end_date = end_date
def get_data(self):
data = yf.download(self.symbol, start=self.start_date, end=self.end_date)
return data
# カカオのデータの取得
cacao_instance = Data("CC=F", "1990-01-01", "2024-03-31")
cacao_data = cacao_instance.get_data()
# アルミニウムのデータの取得
aluminum_instance = Data("ALI=F", "1990-01-01", "2024-03-31")
aluminum_data = aluminum_instance.get_data()
# 銅のデータの取得
copper_instance = Data("HG=F", "1990-01-01", "2024-03-31")
copper_data = copper_instance.get_data()
# 銀のデータの取得
silver_instance = Data("SI=F", "1990-01-01", "2024-03-31")
silver_data = silver_instance.get_data()
# 金のデータの取得
gold_instance = Data("GC=F", "1990-01-01", "2024-03-31")
gold_data = gold_instance.get_data()
# 砂糖のデータの取得
sugar_instance = Data("SB=F", "1990-01-01", "2024-03-31")
sugar_data = sugar_instance.get_data()
# コーヒーのデータの取得
coffee_instance = Data("KC=F", "1990-01-01", "2024-03-31")
coffee_data = coffee_instance.get_data()
# ナスダックのデータの取得
nasdaq_instance = Data("^IXIC", "1990-01-01", "2024-03-31")
nasdaq_data = nasdaq_instance.get_data()
# 終値の列のみを抽出してデータフレームに結合
merged_data = pd.concat([
cacao_data["Close"].rename("Cacao_Close"),
aluminum_data["Close"].rename("Aluminum_Close"),
copper_data["Close"].rename("Copper_Close"),
silver_data["Close"].rename("Silver_Close"),
gold_data["Close"].rename("Gold_Close"),
sugar_data["Close"].rename("Sugar_Close"),
coffee_data["Close"].rename("Coffee_Close"),
nasdaq_data["Close"].rename("Nasdaq_Close")
], axis=1).dropna()
# 列名を変数として定義
cacao_close_col = "Cacao_Close"
aluminum_close_col = "Aluminum_Close"
copper_close_col = "Copper_Close"
silver_close_col = "Silver_Close"
gold_close_col = "Gold_Close"
sugar_close_col = "Sugar_Close"
coffee_close_col = "Coffee_Close"
nasdaq_close_col = "Nasdaq_Close"
# 終値の列のみを抽出してデータフレームに結合
merged_data = pd.concat([
cacao_data["Close"].rename(cacao_close_col),
aluminum_data["Close"].rename(aluminum_close_col),
copper_data["Close"].rename(copper_close_col),
silver_data["Close"].rename(silver_close_col),
gold_data["Close"].rename(gold_close_col),
sugar_data["Close"].rename(sugar_close_col),
coffee_data["Close"].rename(coffee_close_col),
nasdaq_data["Close"].rename(nasdaq_close_col)
], axis=1).dropna()
#print(merged_data.tail())
mixed = merged_data['Coffee_Close'] ** -0.45 * \
merged_data['Cacao_Close'] ** 0.105 * \
(merged_data['Aluminum_Close'] + \
merged_data['Copper_Close'] + \
merged_data['Silver_Close'] + \
merged_data['Gold_Close'] ** 3.35) - \
merged_data['Sugar_Close']
testCorrelation = mixed.corr(merged_data["Nasdaq_Close"])
print(testCorrelation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment