Python trading logic

2024/11/15 14:03:06

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 quandldownload_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)
Answer
    previous_close = df['Close'].shift(1).valueslast_close = df['Close'].valuesbband_low = df['bblower'].valuesbband_up = df['bbUp'].valuescond_buy1 = previous_close < bband_lowcond_buy2 = last_close > bband_lowdf['BUY'] = np.where((cond_buy1 & cond_buy2), True, False)cond_sell1 = previous_close > bband_upcond_sell2 = last_close < bband_updf['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.

https://en.xdnf.cn/q/120449.html

Related Q&A

Convert PDF to Excel [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Return the furthermost outlier in kmeans clustering? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Highest number of consecutively repeating values in a list

Lets say I have this list:List= [1,1,1,0,0,1,1,1,1,1]How do I display the highest number of repeating 1s in a row? I want to return 5.

Python Maze Generation

I am trying to make a python maze generator but I keep getting an IndexError: list index out of range. Any ideas? Im kinda new to this stuff so I was using the code from rosetta code on maze generatio…

Why does Pythons `any` function not return True or False? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Write a list of lists into csv in Python [duplicate]

This question already has answers here:Writing a Python list of lists to a csv file(12 answers)Closed 5 years ago.I have a list of lists of pure data like thisa=[[1,2,3],[4,5,6],[7,8,9]]How can I write…

Upper limit of points pyplot

Just being a curious George here. But Im handling 279 million data points (x,y) and am wondering if pyplot can scatter such a number?Thanks.

how to delete the u and before the of database table display by python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

python list permutations [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate:How to generate all permutations of a list in Python I am given a list [1,2,3] and the task is to create all the possible …

Repeating Characters in the Middle of a String

Here is the problem I am trying to solve but having trouble solving:Define a function called repeat_middle which receives as parameter one string (with at least one character), and it should return a n…