(22 June, 2023 - 08:33 PM)Shenjin Wrote: Show MoreDownload anything you want from udemy
GIVE LIKE AND +REP
Big Data Engineering with Hadoop and Spark
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from skimage.transform import resize
import yfinance as yf
# Define the trading symbol
symbol = "AAPL"
# Get the historical data from yfinance
data = yf.download(symbol, start="2022-01-01", end="2023-01-01")
# Calculate the Bollinger Bands
upper_band = np.std(data["Close"]) * 2 + data["Close"].mean()
lower_band = np.std(data["Close"]) * 2 - data["Close"].mean()
# Create a trading signal
signal = np.where(data["Close"] > upper_band, 1, 0)
# Upscale the data
data_upscaled = resize(data, (2 * data.shape[0], 2 * data.shape[1]))
# Plot the Bollinger Bands and the trading signal
plt.plot(data_upscaled["Close"], label="Close")
plt.plot(upper_band, label="Upper Band")
plt.plot(lower_band, label="Lower Band")
plt.plot(signal, label="Trading Signal")
plt.legend()
plt.show()
# Reduce the margin of error
number_of_stds = 1.5
upper_band = np.std(data["Close"]) * number_of_stds + data["Close"].mean()
lower_band = np.std(data["Close"]) * number_of_stds - data["Close"].mean()
# Plot the Bollinger Bands and the trading signal
plt.plot(data_upscaled["Close"], label="Close")
plt.plot(upper_band, label="Upper Band")
plt.plot(lower_band, label="Lower Band")
plt.plot(signal, label="Trading Signal")
plt.legend()
plt.show()