def __default_loader(id): normalized_id = __normalize_id(id) ticker,subject = normalized_id.split('::') fname = '{}/TIME_SERIES/{}/{}'.format(os.getenv('HOME'), ticker, subject) entries = [] with open(fname) as f: for line in f: try: pair = line.split() entries.append((core.to_jdate(pair[0]),float(pair[1]))) except: pass return obs_series.obs_to_series(entries, name=normalized_id)
def cross(*, slower=None, faster=None, upfactor=1.0, downfactor=1.0, dates=None): '''cross generates a signal series, i.e. series of [1,-1,None], set to 1 where the faster series moves above the slower series and -1 where it moves below. Changing the upfactor from its default 1.0 changes the border for crossing. For instance, upfactor=1.02 means that the faster series must exceed the lower series by 2% before generating the buy value of 1. Likewise, setting downfactor to .98 would require passing 2% below the faster series to generate a sell signal of -1. As usual, dates can be supplied, but default to the current_dates() value.''' if (not slower) or (not faster): raise Exception('slower,faster are required keyword arguments') dates = dates or core.current_dates() sv = series_dates_values(slower, dates) fv = series_dates_values(faster, dates) obs = [] prev_sig = None for ndx, dt in enumerate(dates): s_val = sv[ndx] f_val = fv[ndx] if not (core.is_valid_num(s_val) and core.is_valid_num(f_val)): continue if f_val > s_val * upfactor: sig = 1 elif f_val < s_val * downfactor: sig = -1 else: sig = None if sig and (sig != prev_sig): obs.append((dt, sig)) prev_sig = sig return obs_to_series(obs)
def series_map(proc, *seriesz, missing_data_permitted=False, dts=None): '''Analogous to Python's standard map operation, map the arity-compatible function :code:`proc` over one or more series returning a single value on each date.''' def missing_nums(nums): return len(nums) != sum((1 for n in nums if core.is_valid_num(n))) converted = [convert(s) for s in seriesz] dts = dts or dates.current_dates() sfs = [s.f for s in converted] obs = [] for dt in dts: nums = [f(dt) for f in sfs] if (not missing_data_permitted) and missing_nums(nums): continue val = proc(*nums) if core.is_valid_num(val): obs.append((dt, val)) return obs_to_series(obs)
def test_cross(self): CROSS_EMA_30_SERIES = obs_to_series(CROSS_EMA_30_OBS) self.verify_two_series( cross(slower=ema(self.TEST_SERIES, 30), faster=self.TEST_SERIES), CROSS_EMA_30_SERIES)
def setUp(self): self.TEST_SERIES = obs_to_series(TEST_SERIES_OBS) core.current_dates(core.dates(self.TEST_SERIES))