Python交易逻辑

2024-05-08 01:56:14 发布

您现在位置:Python中文网/ 问答频道 /正文

这里有一个简单的代码来下载每日股票数据和计算布林格带指标,但我不能做的是建立一个逻辑,以产生一个买入和卖出信号。有人能帮我吗。在

我想要的是系统检查前收盘价是否低于布林格波段低点,而上次收盘价是否应高于布林格波段低点。如果是,则系统应显示为买入,反之亦然。在

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)

Tags: key代码sourcedfclosedownload系统波段
1条回答
网友
1楼 · 发布于 2024-05-08 01:56:14
    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)

我想这就是你要找的。在

把这几行代码放在你的脚本之前“数据框到excel(下载源代码)它应该可以工作。在

相关问题 更多 >