#Unexpected Plot Points
1 messages · Page 1 of 1 (latest)
import matplotlib.pyplot as plt
# Fetch historical data for AAPL from Yahoo Finance
aapl = yf.Ticker("AAPL")
historical_data = aapl.history(period="1y")
# Calculate the 3-day moving average
historical_data['3-Day MA'] = historical_data['Close'].rolling(window=3).mean()
# Identify buy signals
buy_signals = (
(historical_data['Close'] > historical_data['Close'].shift(-1)) &
(historical_data['3-Day MA'] >= historical_data['Close']) &
(historical_data['3-Day MA'] > historical_data['3-Day MA'].shift(-1))
)
# Print buy signals for debugging
print("Buy Signals:")
print(historical_data.loc[buy_signals])
# Plotting
plt.figure(figsize=(10, 6))
plt.plot(historical_data.index, historical_data['Close'], label='AAPL Close Price')
plt.plot(historical_data.index, historical_data['3-Day MA'], label='3-Day Moving Average')
# Plot buy signals
plt.scatter(
historical_data.index[buy_signals],
historical_data['Close'][buy_signals],
marker='^',
color='g',
label='Buy Signal'
)
plt.title('AAPL Close Price and 3-Day Moving Average with Buy Signals')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.grid(True)
plt.show()
Tried updating the way I do the graph and checked what you had just sent here, both seem to be further off when ran vs the original
import matplotlib.pyplot as plt
# Fetch historical data for AAPL from Yahoo Finance
aapl = yf.Ticker("AAPL")
historical_data = aapl.history(period="1y")
# Calculate the 3-day moving average
historical_data['3-Day MA'] = historical_data['Close'].rolling(window=3).mean()
# Identify buy signals
buy_signals = (
(historical_data['Close'] > historical_data['Close'].shift(-1)) &
(historical_data['3-Day MA'] >= historical_data['Close']) &
(historical_data['3-Day MA'] > historical_data['3-Day MA'].shift(-1))
)
# Print buy signals and associated data for debugging
print("Buy Signals:")
print(historical_data.loc[buy_signals])
# Plotting
fig, ax1 = plt.subplots(figsize=(10, 6))
# Plot AAPL Close Price
ax1.plot(historical_data.index, historical_data['Close'], label='AAPL Close Price', color='blue')
ax1.set_xlabel('Date')
ax1.set_ylabel('Close Price', color='blue')
ax1.tick_params('y', colors='blue')
# Create a secondary y-axis for the 3-Day Moving Average
ax2 = ax1.twinx()
ax2.plot(historical_data.index, historical_data['3-Day MA'], label='3-Day Moving Average', color='green')
ax2.set_ylabel('3-Day Moving Average', color='green')
ax2.tick_params('y', colors='green')
# Highlight buy signals
ax1.scatter(
historical_data.index[buy_signals],
historical_data['Close'][buy_signals],
marker='^',
color='red',
label='Buy Signal'
)
plt.title('AAPL Close Price and 3-Day Moving Average with Buy Signals')
plt.legend()
plt.grid(True)
plt.show()
Could it be an issue with the timeframe not being detailed enough?
I feel like even if that was the case it should still be graphing appropriately
That would just shorten the range, not make it more detailed
then
import matplotlib.pyplot as plt
# Fetch historical data for AAPL from Yahoo Finance with a shorter period and more detailed interval
aapl = yf.Ticker("AAPL")
historical_data = aapl.history(period="3mo", interval="1d")
# Calculate the 3-day moving average
historical_data['3-Day MA'] = historical_data['Close'].rolling(window=3).mean()
# Identify buy signals
buy_signals = (
(historical_data['Close'] > historical_data['Close'].shift(-1)) &
(historical_data['3-Day MA'] >= historical_data['Close']) &
(historical_data['3-Day MA'] > historical_data['3-Day MA'].shift(-1))
)
# Print buy signals and associated data for debugging
print("Buy Signals:")
print(historical_data.loc[buy_signals])
# Plotting
fig, ax1 = plt.subplots(figsize=(10, 6))
# Plot AAPL Close Price
ax1.plot(historical_data.index, historical_data['Close'], label='AAPL Close Price', color='blue')
ax1.set_xlabel('Date')
ax1.set_ylabel('Close Price', color='blue')
ax1.tick_params('y', colors='blue')
# Create a secondary y-axis for the 3-Day Moving Average
ax2 = ax1.twinx()
ax2.plot(historical_data.index, historical_data['3-Day MA'], label='3-Day Moving Average', color='green')
ax2.set_ylabel('3-Day Moving Average', color='green')
ax2.tick_params('y', colors='green')
# Highlight buy signals
ax1.scatter(
historical_data.index[buy_signals],
historical_data['Close'][buy_signals],
marker='^',
color='red',
label='Buy Signal'
)
plt.title('AAPL Close Price and 3-Day Moving Average with Buy Signals')
plt.legend()
plt.grid(True)
plt.show()
Still incorrect - Tried moving the interval to 1 hour and increasing the window on the MA accordingly, the issue 100% lies within the graphing or formula somehow
yes i saw
Printed out the buy signal as text, its the formula
import matplotlib.pyplot as plt
# Fetch historical data for AAPL from Yahoo Finance with a shorter period and more detailed interval
aapl = yf.Ticker("AAPL")
historical_data = aapl.history(period="3mo", interval="1h")
# Calculate the 3-day moving average
historical_data['3-Day MA'] = historical_data['Close'].rolling(window=12).mean() # Adjusted window for 1-hour interval
# Identify buy signals
buy_signals = (
(historical_data['Close'] > historical_data['Close'].shift(-1)) &
(historical_data['3-Day MA'] >= historical_data['Close']) &
(historical_data['3-Day MA'] > historical_data['3-Day MA'].shift(-1))
)
# Print buy signals and associated data for debugging
print("Buy Signals:")
print(historical_data.loc[buy_signals])
# Plotting
fig, ax1 = plt.subplots(figsize=(10, 6))
# Plot AAPL Close Price
ax1.plot(historical_data.index, historical_data['Close'], label='AAPL Close Price', color='blue')
ax1.set_xlabel('Date')
ax1.set_ylabel('Close Price', color='blue')
ax1.tick_params('y', colors='blue')
# Create a secondary y-axis for the 3-Day Moving Average
ax2 = ax1.twinx()
ax2.plot(historical_data.index, historical_data['3-Day MA'], label='3-Day Moving Average', color='green')
ax2.set_ylabel('3-Day Moving Average', color='green')
ax2.tick_params('y', colors='green')
# Highlight buy signals with larger markers
ax1.scatter(
historical_data.index[buy_signals],
historical_data['Close'][buy_signals],
marker='^',
color='red',
label='Buy Signal',
s=100 # Increase marker size
)
plt.title('AAPL Close Price and 3-Day Moving Average with Buy Signals')
plt.legend()
plt.grid(True)
plt.show()
here
Your MA rolling window change is wrong (should be 72), printed the buy signal, and made the marker size larger... no change to the buy formula itself which has been stated to be the issue somehow
I'm just going to close this as there's no progress being made here
ash wait
check now
import matplotlib.pyplot as plt
# Fetch historical data for AAPL from Yahoo Finance with a shorter period and more detailed interval
aapl = yf.Ticker("AAPL")
historical_data = aapl.history(period="3mo", interval="1h")
# Calculate the 3-day moving average
historical_data['3-Day MA'] = historical_data['Close'].rolling(window=72).mean() # Adjusted window for 1-hour interval
# Identify buy signals
buy_signals = (
(historical_data['Close'] > historical_data['Close'].shift(-1)) &
(historical_data['Close'] > historical_data['3-Day MA'].shift(-1))
)
# Print buy signals and associated data for debugging
print("Buy Signals:")
print(historical_data.loc[buy_signals])
# Plotting
fig, ax1 = plt.subplots(figsize=(10, 6))
# Plot AAPL Close Price
ax1.plot(historical_data.index, historical_data['Close'], label='AAPL Close Price', color='blue')
ax1.set_xlabel('Date')
ax1.set_ylabel('Close Price', color='blue')
ax1.tick_params('y', colors='blue')
# Create a secondary y-axis for the 3-Day Moving Average
ax2 = ax1.twinx()
ax2.plot(historical_data.index, historical_data['3-Day MA'], label='3-Day Moving Average', color='green')
ax2.set_ylabel('3-Day Moving Average', color='green')
ax2.tick_params('y', colors='green')
# Highlight buy signals with larger markers
ax1.scatter(
historical_data.index[buy_signals],
historical_data['Close'][buy_signals],
marker='^',
color='red',
label='Buy Signal',
s=100 # Increase marker size
)
plt.title('AAPL Close Price and 3-Day Moving Average with Buy Signals')
plt.legend()
plt.grid(True)
plt.show()
You somehow made it even worse
then why are you trying to help
sorry if i didnt fix