python交易,Python交易逻辑

Here's a simple code for downloading daily stock data and computing Bollinger band indicator, but what I am not able to do is set up a logic for generating a buy and sell signal. Can someone help me with that.

What i want is for the system to check if previous close price is less than Bollinger Band low and last close price should be above the Bollinger Band low. if yes then the system should show it as a buy and vice versa.

PS: I am only using Pandas, numpy, matplotlib and Quandl.

Code:

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)

解决方案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)

I think this is what you are looking for.

Put these few lines of codes in your script before "df.to_excel(download_source)" and it should work.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值