#Unexpected Plot Points

1 messages · Page 1 of 1 (latest)

red shard
#

Your logic for identifying buy signals seems correct, but the issue might be related to plotting or the condition for buy signals

#
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()
knotty wadi
#

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

red shard
# knotty wadi Tried updating the way I do the graph and checked what you had just sent here, b...
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()
knotty wadi
#

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

knotty wadi
#

That would just shorten the range, not make it more detailed

red shard
#
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()
knotty wadi
#

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

knotty wadi
#

Printed out the buy signal as text, its the formula

red shard
# knotty wadi 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

knotty wadi
#

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

red shard
#
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()
knotty wadi
#

You somehow made it even worse

red shard
#

what is the error

#

then

#

you says

knotty wadi
#

Please just stop

red shard
#

what

#

am bad at python

knotty wadi
#

then why are you trying to help

red shard
#

sorry if i didnt fix