Forked from quantra-go-algo/moving_average_trading_signal.py
Created
March 29, 2023 21:47
-
-
Save lamass/ea72a97e884f20a7c9d10d953f1e7222 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Take long positions | |
data['long_positions'] = np.where(data['short_mavg'] > data['long_mavg'], 1, 0) | |
# Take short positions | |
data['short_positions'] = np.where(data['short_mavg'] < data['long_mavg'], -1, 0) | |
data['positions'] = data['long_positions'] + data['short_positions'] | |
# Plot close price and moving averages | |
plot_data = data[-3000:] | |
plt.figure(figsize=(10, 7)) | |
plt.title('Long and Short Signal', fontsize=14) | |
plt.xlabel('Date') | |
plt.ylabel('Price') | |
plt.plot(plot_data['Close'], label='Close') | |
plt.plot(plot_data['short_mavg'], label='50-Day Moving Average') | |
plt.plot(plot_data['long_mavg'], label='200-day Moving Average') | |
plt.plot(plot_data[(plot_data['long_positions'] == 1) & | |
(plot_data['long_positions'].shift(1) == 0)]['short_mavg'], | |
'^', ms=15, label='Buy Signal', color='green') | |
plt.plot(plot_data[(plot_data['short_positions'] == -1) & | |
(plot_data['short_positions'].shift(1) == 0)]['short_mavg'], | |
'^', ms=15, label='Sell Signal', color='red') | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment