import pandas as pd
import matplotlib.pyplot as plt

# Load data
# For this example, we'll use a CSV file with 'Date' and 'Close' columns.
# Adjust the file path as needed.
data = pd.read_csv('path/to/your/data.csv')

# Convert 'Date' column to datetime
data['Date'] = pd.to_datetime(data['Date'])

# Set 'Date' column as the index
data.set_index('Date', inplace=True)

# Define the short-term and long-term moving averages
short_window = 50
long_window = 200

# Calculate the short-term and long-term moving averages
data['Short_MA'] = data['Close'].rolling(window=short_window, min_periods=1).mean()
data['Long_MA'] = data['Close'].rolling(window=long_window, min_periods=1).mean()

# Create signals
data['Signal'] = 0
data['Signal'][short_window:] = np.where(data['Short_MA'][short_window:] > data['Long_MA'][short_window:], 1, 0)

# Generate trading orders
data['Position'] = data['Signal'].diff()

# Plotting the results
plt.figure(figsize=(14, 7))

# Plot the closing price and moving averages
plt.plot(data['Close'], label='Close Price', alpha=0.5)
plt.plot(data['Short_MA'], label=f'Short {short_window}-Day MA', alpha=0.75)
plt.plot(data['Long_MA'], label=f'Long {long_window}-Day MA', alpha=0.75)

# Plot buy signals
plt.plot(data[data['Position'] == 1].index, data['Short_MA'][data['Position'] == 1], '^', markersize=10, color='g', lw=0, label='Buy Signal')

# Plot sell signals
plt.plot(data[data['Position'] == -1].index, data['Short_MA'][data['Position'] == -1], 'v', markersize=10, color='r', lw=0, label='Sell Signal')

# Add labels and legend
plt.title('Moving Average Crossover Strategy')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.show()
Chart Patterns

Penafian