import pandas as pd
import numpy as np
import yfinance as yf # You can use other APIs for real-time data
# Define the cryptocurrency (e.g., Bitcoin) and time frame
crypto_symbol = "BTC-USD"
start_date = "2022-01-01"
end_date = "2023-01-01"
# Fetch historical data
crypto_data = yf.download(crypto_symbol, start=start_date, end=end_date)
# Calculate moving averages
short_window = 50
long_window = 200
crypto_data['SMA50'] = crypto_data['Close'].rolling(window=short_window).mean()
crypto_data['SMA200'] = crypto_data['Close'].rolling(window=long_window).mean()
# Create signals
crypto_data['Signal'] = 0
crypto_data['Signal'][short_window:] = np.where(
crypto_data['SMA50'][short_window:] > crypto_data['SMA200'][short_window:], 1, 0
)
# Generate buy/sell signals
crypto_data['Buy_Signal'] = np.where(
crypto_data['Signal'] > crypto_data['Signal'].shift(1), 1, 0
)
crypto_data['Sell_Signal'] = np.where(
crypto_data['Signal'] < crypto_data['Signal'].shift(1), -1, 0
)
# Print the DataFrame with buy and sell signals
print(crypto_data[['Close', 'SMA50', 'SMA200', 'Signal', 'Buy_Signal', 'Sell_Signal']])
import numpy as np
import yfinance as yf # You can use other APIs for real-time data
# Define the cryptocurrency (e.g., Bitcoin) and time frame
crypto_symbol = "BTC-USD"
start_date = "2022-01-01"
end_date = "2023-01-01"
# Fetch historical data
crypto_data = yf.download(crypto_symbol, start=start_date, end=end_date)
# Calculate moving averages
short_window = 50
long_window = 200
crypto_data['SMA50'] = crypto_data['Close'].rolling(window=short_window).mean()
crypto_data['SMA200'] = crypto_data['Close'].rolling(window=long_window).mean()
# Create signals
crypto_data['Signal'] = 0
crypto_data['Signal'][short_window:] = np.where(
crypto_data['SMA50'][short_window:] > crypto_data['SMA200'][short_window:], 1, 0
)
# Generate buy/sell signals
crypto_data['Buy_Signal'] = np.where(
crypto_data['Signal'] > crypto_data['Signal'].shift(1), 1, 0
)
crypto_data['Sell_Signal'] = np.where(
crypto_data['Signal'] < crypto_data['Signal'].shift(1), -1, 0
)
# Print the DataFrame with buy and sell signals
print(crypto_data[['Close', 'SMA50', 'SMA200', 'Signal', 'Buy_Signal', 'Sell_Signal']])
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
