This is a simple code that resolve your problem/exemple:
l = [ 1, 2, 6, -3, -2, -5, 6, 7, 1, -1, -3]
for i in range(0, len(l)-1):
p = l[i] * l[i+1]
if p > 0:
print('ok')
elif l[i+1] < 0:
print('sell')
else:
print('buy')
And if you want a new column in your dataframe, this is an example for you:
d = {'col1': range(11), 'col2': [ 1, 2, 6, -3, -2, -5, 6, 7, 1, -1, -3]}
df = pd.DataFrame(data=d)
df['shift_col'] = df.col2.shift(-1)
df
col1 col2 shift_col
0 0 1 2.0
1 1 2 6.0
2 2 6 -3.0
3 3 -3 -2.0
4 4 -2 -5.0
5 5 -5 6.0
6 6 6 7.0
7 7 7 1.0
8 8 1 -1.0
9 9 -1 -3.0
10 10 -3 NaN
and this is a our simple function in the previous example:
def compare(line):
p = line.col2 * line.shift_col
if p > 0:
return 'ok'
elif line.shift_col < 0:
return 'sell'
else:
return 'buy'
df['order'] = df.apply(compare, axis=1)
Finaly, this will be the result:
col1 col2 shift_col order
0 0 1 2.0 ok
1 1 2 6.0 ok
2 2 6 -3.0 sell
3 3 -3 -2.0 ok
4 4 -2 -5.0 ok
5 5 -5 6.0 buy
6 6 6 7.0 ok
7 7 7 1.0 ok
8 8 1 -1.0 sell
9 9 -1 -3.0 ok
10 10 -3 NaN buy
7
solved Detect sign changes in an array [closed]