data is dataframe having columns Close, ph, slope_ph and upper
<code># Initialize variables
data['upos'] = 0
length = 14
# Calculate breakout conditions
for i in range(len(data)):
# Calculate upward breakout condition
if data.loc[i, 'ph'] and i + length < len(data): # if ph and not exceeding index limit
data.loc[i + length, 'upos'] = 0
elif i > 0:
if data.loc[i, 'Close'] > data.loc[i, 'upper'] - (data.loc[i, 'slope_ph'] * length):
data.loc[i, 'upos'] = 1
else:
data.loc[i, 'upos'] = data.loc[i - 1, 'upos']
</code>
<code># Initialize variables
data['upos'] = 0
length = 14
# Calculate breakout conditions
for i in range(len(data)):
# Calculate upward breakout condition
if data.loc[i, 'ph'] and i + length < len(data): # if ph and not exceeding index limit
data.loc[i + length, 'upos'] = 0
elif i > 0:
if data.loc[i, 'Close'] > data.loc[i, 'upper'] - (data.loc[i, 'slope_ph'] * length):
data.loc[i, 'upos'] = 1
else:
data.loc[i, 'upos'] = data.loc[i - 1, 'upos']
</code>
# Initialize variables
data['upos'] = 0
length = 14
# Calculate breakout conditions
for i in range(len(data)):
# Calculate upward breakout condition
if data.loc[i, 'ph'] and i + length < len(data): # if ph and not exceeding index limit
data.loc[i + length, 'upos'] = 0
elif i > 0:
if data.loc[i, 'Close'] > data.loc[i, 'upper'] - (data.loc[i, 'slope_ph'] * length):
data.loc[i, 'upos'] = 1
else:
data.loc[i, 'upos'] = data.loc[i - 1, 'upos']
print(data)
I am trying to get results based on below conditions :
- When ph = True, reset the value of 14th row of upos to 0
- Else check if Close > upper – slope_ph * length — if yes set value of upos to 1
- if 2nd point is not true, retain old value of upos
Tried to update the code multiple time, but each time code keeps updating same row of upos to 0 where ph = True
Data_Image
New contributor
AJAY GULIA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.