HDFC BANK LTD

Vidu

82
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Function to check for price and volume breakouts
def breakout_scanner(ticker, price_breakout_pct=1.05, volume_mult=1.5, lookback=20):
# Download historical data (6 months of daily data)
df = yf.download(ticker, period="6mo", interval="1d")

# Calculate moving averages for volume
df['Avg_Volume'] = df['Volume'].rolling(window=lookback).mean()

# Get the recent high and low
recent_high = df['High'].max()
recent_low = df['Low'].min()

# Last row data (current day)
last_row = df.iloc[-1]

# Check for Price Breakout (above recent high or below recent low)
price_breakout_up = last_row['Close'] >= recent_high * price_breakout_pct
price_breakout_down = last_row['Close'] <= recent_low * (2 - price_breakout_pct) # A breakout below recent low

# Check for Volume Breakout (above average volume threshold)
volume_breakout = last_row['Volume'] >= df['Avg_Volume'].iloc[-1] * volume_mult

# Print scanner results
if price_breakout_up:
print(f"{ticker}: Price Breakout Up! (Price above {recent_high * price_breakout_pct})")
if price_breakout_down:
print(f"{ticker}: Price Breakout Down! (Price below {recent_low * (2 - price_breakout_pct)})")

if volume_breakout:
print(f"{ticker}: Volume Breakout! (Volume above {df['Avg_Volume'].iloc[-1] * volume_mult})")

# Optional: Plot stock chart for visual inspection
df[['Close', 'Avg_Volume']].plot(figsize=(10,6), subplots=True)
plt.suptitle(f'{ticker} - Price and Volume Breakout')
plt.show()

# List of stocks to scan
stocks = ["AAPL", "GOOG", "MSFT", "AMZN"]

for stock in stocks:
breakout_scanner(stock)

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.