这是一个用于下载每日股票数据和计算布林带指标的简单代码,但我无法做的是建立一个生成买入和卖出信号的逻辑。有人可以帮我这个。
我想要的是系统检查之前的收盘价是否低于布林带低位且最后收盘价应高于布林带低位。如果是,则系统应将其显示为买入,反之亦然。
PS:我只使用Pandas,numpy,matplotlib和Quandl。 代码:
import quandl
download_source = (r'F:\Trading\download.xlsx')
df = quandl.get('NSE/RELIANCE', api_key = '*Quandl Api key*')
sma20 = df['Close'].rolling(window=20, min_periods=20 - 1).mean()
std = df['Close'].rolling(window=20, min_periods=20 - 1).std()
df['bbMid'] = sma20
df['bbUp'] = (sma20 + (std * 2))
df['bblower'] = (sma20 - (std * 2))
df.to_excel(download_source)
答案 0 :(得分:0)
previous_close = df['Close'].shift(1).values
last_close = df['Close'].values
bband_low = df['bblower'].values
bband_up = df['bbUp'].values
cond_buy1 = previous_close < bband_low
cond_buy2 = last_close > bband_low
df['BUY'] = np.where((cond_buy1 & cond_buy2), True, False)
cond_sell1 = previous_close > bband_up
cond_sell2 = last_close < bband_up
df['SELL'] = np.where((cond_sell1 & cond_sell2), True, False)
我认为这就是你要找的东西。
在&#34; df.to_excel(download_source)&#34;之前,将这几行代码放在脚本中。它应该工作。