Python交易逻辑 [英] Python trading logic

查看:146
本文介绍了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:我只使用Pandas,numpy,matplotlib和Quandl. 代码:

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.

在您的脚本中将这几行代码放在"df.to_excel(download_source)"之前,它应该可以工作.

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

这篇关于Python交易逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆