It's a bit tricky since you cannot mark a point as pivot until you find the next potential pivot (ie if you are in an upward trend, you can't say it's done until you find a low sufficiently low).
This code does the trick - I've put your data in the tmpData.txt file for convenience, and get the desired result. Please check
def get_pivots():
data = pd.DataFrame.from_csv('tmpData.txt')
data['swings'] = np.nan
pivot = data.irow(0).open
last_pivot_id = 0
up_down = 0
diff = .3
for i in range(0, len(data)):
row = data.irow(i)
# We don't have a trend yet
if up_down == 0:
if row.low < pivot - diff:
data.ix[i, 'swings'] = row.low - pivot
pivot, last_pivot_id = row.low, i
up_down = -1
elif row.high > pivot + diff:
data.ix[i, 'swings'] = row.high - pivot
pivot, last_pivot_id = row.high, i
up_down = 1
# Current trend is up
elif up_down == 1:
# If got higher than last pivot, update the swing
if row.high > pivot:
# Remove the last pivot, as it wasn't a real one
data.ix[i, 'swings'] = data.ix[last_pivot_id, 'swings'] + (row.high - data.ix[last_pivot_id, 'high'])
data.ix[last_pivot_id, 'swings'] = np.nan
pivot, last_pivot_id = row.high, i
elif row.low < pivot - diff:
data.ix[i, 'swings'] = row.low - pivot
pivot, last_pivot_id = row.low, i
# Change the trend indicator
up_down = -1
# Current trend is down
elif up_down == -1:
# If got lower than last pivot, update the swing
if row.low < pivot:
# Remove the last pivot, as it wasn't a real one
data.ix[i, 'swings'] = data.ix[last_pivot_id, 'swings'] + (row.low - data.ix[last_pivot_id, 'low'])
data.ix[last_pivot_id, 'swings'] = np.nan
pivot, last_pivot_id = row.low, i
elif row.high > pivot - diff:
data.ix[i, 'swings'] = row.high - pivot
pivot, last_pivot_id = row.high, i
# Change the trend indicator
up_down = 1
print data
Output:
date close high low open volume swings
2014-05-09 13:30:00 187.56 187.73 187.54 187.70 1922600 NaN
2014-05-09 13:31:00 187.49 187.56 187.42 187.55 534400 NaN
2014-05-09 13:32:00 187.42 187.51 187.35 187.49 224800 -0.35
2014-05-09 13:33:00 187.55 187.58 187.39 187.40 303700 NaN
2014-05-09 13:34:00 187.67 187.67 187.53 187.56 438200 NaN
2014-05-09 13:35:00 187.60 187.71 187.56 187.68 296400 0.36
2014-05-09 13:36:00 187.41 187.67 187.38 187.60 329900 NaN
2014-05-09 13:37:00 187.31 187.44 187.28 187.40 404000 NaN
2014-05-09 13:38:00 187.26 187.37 187.26 187.30 912800 NaN
2014-05-09 13:39:00 187.22 187.28 187.12 187.25 607700 -0.59