def test_ema_3(self): AAA_SERIES = obs_to_series(AAA_SERIES_OBS) BBB_SERIES = obs_to_series(BBB_SERIES_OBS) core.set_dates(AAA_SERIES) allocations = [ Allocation(core.first_date(), [Portion(AAA_SERIES, 1234), Portion(BBB_SERIES, 1234)]) ] self.verify_two_series( mul(allocation_equity_line(allocations), 2), add(calibrate(AAA_SERIES), calibrate(BBB_SERIES)))
def reversals(s, down_factor=1.0, up_factor=1.0, dates=None): '''When a series ascends above up-factor multiplied by a preceding local minimum, a buy (1) signal is produced. Upon descending below the product of a local maximum and down-factor, a sell (-1) signal is produced. ''' dates = dates or core.current_dates() fd, ld = core.first_date(dates), core.last_date(dates) outv = [None for dt in range(fd, ld + 1)] min_ob = max_ob = first_ob(s, dates) sf = s.f state = None for dt in dates: val = sf(dt) if not core.is_valid_num(val): continue if val > max_ob[1]: max_ob = (dt, val) if val < min_ob[1]: min_ob = (dt, val) if (1 != state) and (val > min_ob[1] * up_factor): max_ob = min_ob = (dt, val) outv[dt - fd] = 1 state = 1 elif (-1 != state) and (val < max_ob[1] * down_factor): max_ob = min_ob = (dt, val) outv[dt - fd] = -1 state = -1 return core.vector_series(outv, fd, name="reversals({})".format(abbreviate(s)))
def calibrate(s, init=100, date=None): '''To calibrate a series is to make it value-compatible with another. For example, a spider graph picks a point at which multiple time series are the same value. The default date is the beginning of the dateset and the default value of the series there is 100, making any value in the series equivalent to the percentage of the beginning.''' date = core.to_date(date) or core.first_date() f = s.f val = f(date) if not core.is_valid_num(val): raise Exception("no observation at {}".format(date)) ratio = init / val def fetcher(dt): val = f(dt) if core.is_valid_num(val): val = val * ratio return val return core.series(fetcher, "calibrate({})".format(abbreviate(s)))
def test_22(self): cal = calibrate(self.TEST_SERIES, init=22) ratio = 22 / self.TEST_SERIES.f(core.first_date()) self.verify_two_series(cal, mul(self.TEST_SERIES, ratio))
def test_100(self): cal = calibrate(self.TEST_SERIES) ratio = 100 / self.TEST_SERIES.f(core.first_date()) self.verify_two_series(cal, mul(self.TEST_SERIES, ratio))