You are on page 1of 1

# Import necessary libraries

import pandas as pd
import yfinance as yf

# 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


data['Buy/Sell'] = data.apply(buy_sell_support_resistance, axis=1)

# Print the last few rows of the data, including the buy/sell signals
print(data.tail())

You might also like