加载数据集
import yfinance as yf
import pandas as pd
import pandas_ta as ta
def get_data(symbol: str):
data = yf.download(tickers=symbol, period='300d', interval='1d')
data.reset_index(inplace=True, drop=True)
return data
# Get the data
data = get_data('BTC-USD')
- inplace=True 表示直接在原本数据集上进行修改,
-
drop=True
:当重置索引时,原来的索引可以被添加为新的列,或者被完全丢弃。drop=True
意味着在重置索引时,原来的索引会被丢弃,不会保留为新的列。如果设置为False
,原索引会成为新的一列数据。
计算布林带和均线指标
# Calculate Bollinger Bands using pandas_ta
data.ta.bbands(length=10, std=1.5, append=True)
# Add the upper and lower bands to the DataFrame
data['Upper Band'] = data['BBU_10_1.5']
data['Lower Band'] = data['BBL_10_1.5']
def calculate_sma(data, length: int):
return ta.sma(data['Close'], length)
# Calculate the moving average
data['SMA'] = calculate_sma(data, 20)
data.dropna(inplace=True)
根据均线确定趋势
def check_candles(data, backcandles, ma_column):
categories = [0 for _ in range(backcandles)]
for i in range(backcandles, len(data)):
if all(data['Close'][i-backcandles:i] > data[ma_column][i-backcandles:i]):
categories.append(2) # Uptrend
elif all(data['Close'][i-backcandles:i] < data[ma_column][i-backcandles:i]):
categories.append(1) # Downtrend
else:
categories.append(0) # No trend
return categories
# Apply the function to the DataFrame
data['Trend'] = check_candles(data, 7, 'SMA')
确定买点
# Check conditions and assign entry values
data['entry'] = 0
# Condition for entry category 2 (buy entry)
buy_entry_condition = (data['Trend'] == 2) & ((data['Open'] < data['Lower Band']) & (data['Close'] > data['Lower Band']))
data.loc[buy_entry_condition, 'entry'] = 2
# Condition for entry category 1 (sell entry)
sell_entry_condition = (data['Trend'] == 1) & ((data['Open'] > data['Upper Band']) & (data['Close'] < data['Upper Band']))
data.loc[sell_entry_condition, 'entry'] = 1
根据RSI和布林带确定买点
def add_rsi_column(data):
# Calculate RSI with a period of 14
data['RSI'] = ta.rsi(data['Close'])
return data
data = add_rsi_column(data)
def rsi_signal(data):
data['RSI Signal'] = 0 # Initialize the signal column with 0
# Set the signal category to 2 when the price is below the lower Bollinger Band and RSI is below 30
data.loc[(data['Close'] < data['Lower Band']) & (data['RSI'] < 55), 'RSI Signal'] = 2
# Set the signal category to 1 when the price is above the upper Bollinger Band and RSI is above 70
data.loc[(data['Close'] > data['Upper Band']) & (data['RSI'] > 45), 'RSI Signal'] = 1
return data
计算RSI信号,根据RSI信号确定买点
data['entry'] = 0
# Condition for entry category 2 (buy entry)
buy_entry_condition = (data['Trend'] == 2) & (data['RSI Signal'] == 2) & (data['Low'] < data['Lower Band'])
data.loc[buy_entry_condition, 'entry'] = 2
# Condition for entry category 1 (sell entry)
sell_entry_condition = (data['Trend'] == 1) & (data['RSI Signal'] == 1) & (data['High'] > data['Upper Band'])
data.loc[sell_entry_condition, 'entry'] = 1
根据布林带和反转蜡烛确定买点
def identify_shooting_star(data):
# Create a new column for shooting star
data['shooting_star'] = data.apply(lambda row: 2 if (
( (min(row['Open'], row['Close']) - row['Low']) > (1.5 * abs(row['Close'] - row['Open']))) and
(row['High'] - max(row['Close'], row['Open'])) < (0.8 * abs(row['Close'] - row['Open'])) and
(abs(row['Open'] - row['Close']) > row['Open'] * 0.01)
) else 1 if (
(row['High'] - max(row['Open'], row['Close'])) > (1.5 * abs(row['Open'] - row['Close'])) and
(min(row['Close'], row['Open']) - row['Low']) < (0.8 * abs(row['Open'] - row['Close'])) and
(abs(row['Open'] - row['Close']) > row['Open'] * 0.01)
) else 0, axis=1)
return data
data = identify_shooting_star(data)