1

I am using np.where function to calculate supertrend (an indicator in stock market). For this I need to do a simple calculation which np.where is giving wrong results most of the items. I have done the same calculations in excel too but not sure why np.where is giving wrong calculation in calculating Upper_Band column

Here is the excel calculation which I think is correct.

enter image description here

Here is the excel generated after running python code

enter image description here

from S.NO 6 onwards calculation has been different this is the actual python code

import numpy as np
data["High-Low"] = data["High"]  - data["Low"]
data["Close-low"] = abs(data["Close"].shift(1) - data["Low"])
data["Close-High"] = abs(data["Close"].shift(1) - data["High"])
data["True_Range"] = data[["High-Low", "Close-low", "Close-High"]].max(axis=1)
data["ATR"] = data["True_Range"].rolling(window=10).mean()
data["Basic_Upper_Band"] = (data["High"] + data["Low"])/2 + (data["ATR"]*2)
data["Basic_Lower_Band"] = (data["High"] + data["Low"])/2 - (data["ATR"]*2)
data["Upper_Band"] = 0
data["Lower_Band"] = 0


PreviousUB = data["Upper_Band"].shift(1)
BasicUB = data["Basic_Upper_Band"]
PreviousClose = data["Close"].shift(1)

data["Upper_Band"] = np.where((PreviousUB > BasicUB) | (PreviousUB < PreviousClose),BasicUB,PreviousUB)

Here is the output in python format too

enter image description here

main calculation in the code is data["Upper_Bank"]. I am using the same formula in excel and through np.where but it is giving me different results.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • Can you share a small sample of data in python format? – Jano Feb 28 '20 at 07:07
  • Added, please check – tanmay agrawal Feb 28 '20 at 07:15
  • What I mean is the code that would generate the dataframe with the required data. See for example https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples Check too https://stackoverflow.com/help/minimal-reproducible-example to see how to better formulate the questions. Your problem is likely very easy to solve, but it's missing a way to reproduce it, what the expected result is and what the current result is, so it would take too much time to lay the ground to verify the solution. – Jano Feb 28 '20 at 07:28

0 Answers0