Start and end index that clips the price/value of a time series the most

    Assumes that the integrated maximum includes the peak (instantaneous maximum).

    Arguments:
      ts (TimeSeries): Time series to attempt to clip to as low a max value as possible
      capacity (float): Total "funds" or "energy" available for clipping (integrated area under time series)

    Returns:
      2-tuple: Timestamp of the start and end of the period of the maximum clipped integrated increase

def clipping_params(ts, capacity=100):
    """Start and end index that clips the price/value of a time series the most

    Assumes that the integrated maximum includes the peak (instantaneous maximum).

    Arguments:
      ts (TimeSeries): Time series to attempt to clip to as low a max value as possible
      capacity (float): Total "funds" or "energy" available for clipping (integrated area under time series)

    Returns:
      2-tuple: Timestamp of the start and end of the period of the maximum clipped integrated increase
    """
    ts_sorted = ts.order(ascending=False)
    i, t0, t1, integral = 1, None, None, 0
    while integral <= capacity and i+1 < len(ts):
        i += 1
        t0_within_capacity = t0
        t1_within_capacity = t1
        t0 = min(ts_sorted.index[:i])
        t1 = max(ts_sorted.index[:i])
        integral = integrated_change(ts[t0:t1])
        print i, t0, ts[t0], t1, ts[t1], integral
    if t0_within_capacity and t1_within_capacity:
        return t0_within_capacity, t1_within_capacity