This document defines two functions - one to calculate support and resistance levels from historical price data, and another to output "Buy", "Sell", or "Hold" signals based on whether the latest price has broken above resistance or below support. It applies these functions to 1-year daily Apple stock price data from Yahoo Finance to generate and print buy/sell signals for the last few days.
This document defines two functions - one to calculate support and resistance levels from historical price data, and another to output "Buy", "Sell", or "Hold" signals based on whether the latest price has broken above resistance or below support. It applies these functions to 1-year daily Apple stock price data from Yahoo Finance to generate and print buy/sell signals for the last few days.
This document defines two functions - one to calculate support and resistance levels from historical price data, and another to output "Buy", "Sell", or "Hold" signals based on whether the latest price has broken above resistance or below support. It applies these functions to 1-year daily Apple stock price data from Yahoo Finance to generate and print buy/sell signals for the last few days.
# Define function to calculate support and resistance levels
def calc_support_resistance(data): # Calculate support level support = min(data['Low'][:-1]) # Calculate resistance level resistance = max(data['High'][:-1]) return support, resistance
# Define function to buy or sell based on support and resistance levels
def buy_sell_support_resistance(data): # Calculate support and resistance levels support, resistance = calc_support_resistance(data) # Get latest price latest_price = data['Close'][-1] # Buy if price breaks resistance level if latest_price > resistance: return "Buy" # Sell if price breaks support level elif latest_price < support: return "Sell" # Hold if price is between support and resistance levels else: return "Hold"
# Set ticker symbol and time frame
symbol = 'AAPL' time_frame = '1d'
# Get data from Yahoo Finance
data = yf.download(symbol, period='1y', interval=time_frame)
# Apply buy_sell_support_resistance function to data