Telegram Group & Telegram Channel
یک مثال ساده برای آشنایی با ماژول Alphalens

در مثال این پست خروجی‌های Alphalens کمک می کند تا عملکرد فاکتور تفاوت میانگین متحرک کوتاه‌مدت و بلندمدت را از جنبه‌های مختلف ارزیابی کنیم.
قبل از اجرای برنامه مطمئن شوید تمامی ماژول های نصب شده اند یا آنها را از این طریق تصب کنید:

pip install yfinance alphalens-reloaded pandas numpy matplotlib

سورس برنامه
#code by @python4finance
import yfinance as yf
import pandas as pd
import numpy as np
from alphalens.utils import get_clean_factor_and_forward_returns
from alphalens.tears import create_full_tear_sheet
import warnings
warnings.filterwarnings("ignore")

# ---------------------------------------------
# 1. دریافت داده‌های تاریخی از Yahoo Finance
# ---------------------------------------------
tickers = ["AAPL", "MSFT", "GOOG", "AMZN", "META"] # مثال: ۵ سهم بزرگ فناوری
start_date = "2020-01-01"
end_date = "2023-01-01"

# دریافت داده‌های قیمتی
data = yf.download(tickers, start=start_date, end=end_date)
prices = data["Adj Close"] # استفاده از قیمت تعدیل‌شده

# تبدیل ایندکس به DateTimeIndex و تنظیم فرکانس
prices = prices.asfreq('B').ffill() # تبدیل به فرکانس روزانه و پر کردن مقادیر خالی

# ---------------------------------------------
# 2. محاسبه فاکتور (مثال: Moving Average Crossover)
# ---------------------------------------------
def calculate_factor(prices, short_window=10, long_window=50):
"""
محاسبه فاکتور تفاوت میانگین متحرک کوتاه‌مدت و بلندمدت
"""
short_ma = prices.rolling(window=short_window).mean()
long_ma = prices.rolling(window=long_window).mean()
factor = short_ma - long_ma
return factor

factor = calculate_factor(prices)

# ---------------------------------------------
# 3. آماده‌سازی داده‌ها برای Alphalens
# ---------------------------------------------
# تبدیل فاکتور به فرمت MultiIndex (Date, Asset)
factor = factor.stack().reset_index()
factor.columns = ['date', 'asset', 'factor']
factor = factor.set_index(['date', 'asset'])['factor']

# اطمینان از هماهنگی ایندکس قیمت و فاکتور
common_index = prices.index.intersection(factor.index.get_level_values(0).unique())
prices = prices.loc[common_index]
factor = factor.loc[common_index]

# ---------------------------------------------
# 4. تحلیل عملکرد فاکتور با Alphalens
# ---------------------------------------------
# محاسبه بازده‌های آینده و پاک‌سازی داده‌ها
factor_data = get_clean_factor_and_forward_returns(
factor,
prices,
quantiles=5, # تقسیم داده به ۵ کوانتایل
periods=(1, 5, 10) # بازه‌های بازدهی (1 روز، 5 روز، 10 روز)
)

# ایجاد گزارش کامل
create_full_tear_sheet(factor_data)


#پایتون_مالی
#معاملات_الگوریتمی
#بک_تست

#Algorithmic_Trading
#Back_Test
#Alphalens

پایتون برای مالی


🆔 www.tg-me.com/us/Python4Finance/com.python4finance
🆔 ble.ir/us/Python4Finance/com.python4finance



tg-me.com/python4finance/1087
Create:
Last Update:

یک مثال ساده برای آشنایی با ماژول Alphalens

در مثال این پست خروجی‌های Alphalens کمک می کند تا عملکرد فاکتور تفاوت میانگین متحرک کوتاه‌مدت و بلندمدت را از جنبه‌های مختلف ارزیابی کنیم.
قبل از اجرای برنامه مطمئن شوید تمامی ماژول های نصب شده اند یا آنها را از این طریق تصب کنید:

pip install yfinance alphalens-reloaded pandas numpy matplotlib

سورس برنامه
#code by @python4finance
import yfinance as yf
import pandas as pd
import numpy as np
from alphalens.utils import get_clean_factor_and_forward_returns
from alphalens.tears import create_full_tear_sheet
import warnings
warnings.filterwarnings("ignore")

# ---------------------------------------------
# 1. دریافت داده‌های تاریخی از Yahoo Finance
# ---------------------------------------------
tickers = ["AAPL", "MSFT", "GOOG", "AMZN", "META"] # مثال: ۵ سهم بزرگ فناوری
start_date = "2020-01-01"
end_date = "2023-01-01"

# دریافت داده‌های قیمتی
data = yf.download(tickers, start=start_date, end=end_date)
prices = data["Adj Close"] # استفاده از قیمت تعدیل‌شده

# تبدیل ایندکس به DateTimeIndex و تنظیم فرکانس
prices = prices.asfreq('B').ffill() # تبدیل به فرکانس روزانه و پر کردن مقادیر خالی

# ---------------------------------------------
# 2. محاسبه فاکتور (مثال: Moving Average Crossover)
# ---------------------------------------------
def calculate_factor(prices, short_window=10, long_window=50):
"""
محاسبه فاکتور تفاوت میانگین متحرک کوتاه‌مدت و بلندمدت
"""
short_ma = prices.rolling(window=short_window).mean()
long_ma = prices.rolling(window=long_window).mean()
factor = short_ma - long_ma
return factor

factor = calculate_factor(prices)

# ---------------------------------------------
# 3. آماده‌سازی داده‌ها برای Alphalens
# ---------------------------------------------
# تبدیل فاکتور به فرمت MultiIndex (Date, Asset)
factor = factor.stack().reset_index()
factor.columns = ['date', 'asset', 'factor']
factor = factor.set_index(['date', 'asset'])['factor']

# اطمینان از هماهنگی ایندکس قیمت و فاکتور
common_index = prices.index.intersection(factor.index.get_level_values(0).unique())
prices = prices.loc[common_index]
factor = factor.loc[common_index]

# ---------------------------------------------
# 4. تحلیل عملکرد فاکتور با Alphalens
# ---------------------------------------------
# محاسبه بازده‌های آینده و پاک‌سازی داده‌ها
factor_data = get_clean_factor_and_forward_returns(
factor,
prices,
quantiles=5, # تقسیم داده به ۵ کوانتایل
periods=(1, 5, 10) # بازه‌های بازدهی (1 روز، 5 روز، 10 روز)
)

# ایجاد گزارش کامل
create_full_tear_sheet(factor_data)


#پایتون_مالی
#معاملات_الگوریتمی
#بک_تست

#Algorithmic_Trading
#Back_Test
#Alphalens

پایتون برای مالی


🆔 www.tg-me.com/us/Python4Finance/com.python4finance
🆔 ble.ir/us/Python4Finance/com.python4finance

BY Python4Finance


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/python4finance/1087

View MORE
Open in Telegram


Python4Finance Telegram | DID YOU KNOW?

Date: |

Telegram announces Search Filters

With the help of the Search Filters option, users can now filter search results by type. They can do that by using the new tabs: Media, Links, Files and others. Searches can be done based on the particular time period like by typing in the date or even “Yesterday”. If users type in the name of a person, group, channel or bot, an extra filter will be applied to the searches.

The lead from Wall Street offers little clarity as the major averages opened lower on Friday and then bounced back and forth across the unchanged line, finally finishing mixed and little changed.The Dow added 33.18 points or 0.10 percent to finish at 34,798.00, while the NASDAQ eased 4.54 points or 0.03 percent to close at 15,047.70 and the S&P 500 rose 6.50 points or 0.15 percent to end at 4,455.48. For the week, the Dow rose 0.6 percent, the NASDAQ added 0.1 percent and the S&P gained 0.5 percent.The lackluster performance on Wall Street came on uncertainty about the outlook for the markets following recent volatility.

Python4Finance from us


Telegram Python4Finance
FROM USA