比特币公式
Title: Bitcoin Indicator Formula Code
```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def moving_average(data, window):
return data.rolling(window=window).mean()
def exponential_moving_average(data, window):
return data.ewm(span=window, adjust=False).mean()
def relative_strength_index(data, window):
delta = data.diff()
gain = (delta.where(delta > 0, 0)).rolling(window=window).mean()
loss = (delta.where(delta < 0, 0)).rolling(window=window).mean()
rs = gain / loss
return 100 (100 / (1 rs))
def stochastic_oscillator(data, window):
low_min = data.rolling(window=window).min()
high_max = data.rolling(window=window).max()
return ((data low_min) / (high_max low_min)) * 100
def macd(data, short_window, long_window, signal_window):
short_ema = exponential_moving_average(data, short_window)
long_ema = exponential_moving_average(data, long_window)
macd_line = short_ema long_ema
signal_line = exponential_moving_average(macd_line, signal_window)
return macd_line, signal_line
Example usage
if __name__ == "__main__":
Load Bitcoin price data (replace with your data)
btc_data = pd.read_csv('bitcoin_price.csv')
Calculate indicators
btc_data['MA_50'] = moving_average(btc_data['Close'], 50)
btc_data['EMA_200'] = exponential_moving_average(btc_data['Close'], 200)
btc_data['RSI_14'] = relative_strength_index(btc_data['Close'], 14)
btc_data['%K'] = stochastic_oscillator(btc_data['Close'], 14)
btc_data['%D'] = moving_average(btc_data['%K'], 3)
btc_data['MACD'], btc_data['Signal_line'] = macd(btc_data['Close'], 12, 26, 9)
Plotting
plt.figure(figsize=(14, 7))
plt.plot(btc_data['Close'], label='Bitcoin Price', color='blue')
plt.plot(btc_data['MA_50'], label='50Day Moving Average', color='red')
plt.plot(btc_data['EMA_200'], label='200Day Exponential Moving Average', color='green')
plt.legend(loc='upper left')
plt.title('Bitcoin Price with Moving Averages')
plt.xlabel('Date')
plt.ylabel('Price (USD)')
plt.show()
```
This code provides functions to calculate commonly used technical indicators for Bitcoin:
1.
Moving Average (MA)
: Both Simple Moving Average (SMA) and Exponential Moving Average (EMA) are provided.2.
Relative Strength Index (RSI)
: This measures the magnitude of recent price changes to evaluate overbought or oversold conditions in the price of a stock or other asset.3.
Stochastic Oscillator
: It compares the current closing price with the range of prices over a certain period to indicate overbought or oversold conditions.4.
Moving Average Convergence Divergence (MACD)
: This is a trendfollowing momentum indicator that shows the relationship between two moving averages of a security’s price.You can replace `'bitcoin_price.csv'` with your Bitcoin price data file. After calculating the indicators, you can use them for analysis or visualization. This code also includes an example plot of Bitcoin price with Moving Averages.