def new_buy_and_hold_port(self, qty=1., open_px='close', open_dt=None, close_px='close', close_dt=None, ret_calc=None): """ :param qty: float :param open_px: one of {string, float}, opening trade price. If string define open, high, low, close as source. :param open_dt: opening trade date :param close_px: one of {string, float}, closing trade price. If string define open, high, low, close :param close_dt: closing trade date :param ret_calc: :return: """ from tia.analysis.model.trd import TradeBlotter from tia.analysis.model.port import SingleAssetPortfolio getpx = lambda how, dt: how if not isinstance(how, basestring) else self.pxs.frame[how].asof(dt) open_dt = open_dt or self.pxs.frame.index[0] open_px = getpx(open_px, open_dt) close_dt = close_dt or self.pxs.frame.index[-1] close_px = getpx(close_px, close_dt) pricer = self.truncate(open_dt, close_dt) blotter = TradeBlotter() blotter.ts = open_dt blotter.open(qty, open_px) blotter.ts = close_dt blotter.close(close_px) trds = blotter.trades return SingleAssetPortfolio(pricer, trds, ret_calc=ret_calc)
def buy_and_hold(self, qty=1., start_dt=None, end_dt=None, start_px=None, end_px=None): """Construct a portfolio which opens a position with size qty at start (or first data in pricer) and continues to the specified end date. It uses the end of day market prices defined by the pricer (or prices supplied) :param qty: :param start: datetime :param end: datetime :param which: which price series to use for inital trade px :param ret_cacls: portfolio return calculator :return: SingleAssetPortfolio """ from tia.analysis.model.trd import TradeBlotter eod = self.pricer.get_eod_frame().close start_dt = start_dt and pd.to_datetime(start_dt) or eod.index[0] start_px = start_px or eod.asof(start_dt) end_dt = end_dt and pd.to_datetime(end_dt) or eod.index[-1] end_px = end_px or eod.asof(end_dt) pricer = self.pricer.trunace(start_dt, end_dt) blotter = TradeBlotter() blotter.ts = start_dt blotter.open(qty, start_px) blotter.ts = end_dt blotter.close(end_px) trds = blotter.trades return SingleAssetPortfolio(pricer, trds, ret_calc=self.ret_calc)
def close_to_close(self, pxs): """ :param pxs: :return: """ if not isinstance(pxs, pd.Series): raise ValueError('pxs expected to be Series') blotter = TradeBlotter() signal = self.signal.dropna() diff = signal.diff() diff.iloc[0] = 0 changes = signal[diff != 0] lsig = 0 qtyfct = self._qty_fct(pxs.index) for ts, sig in changes.items(): blotter.ts = ts if sig != lsig: px = pxs.get(ts, None) if px is None: raise Exception( 'insufficient price data: no data found at %s' % ts) if lsig != 0: # close open trd blotter.close(px=px) if sig != 0: size = abs(qtyfct(ts)) qty = sig > 0 and size or -size blotter.open(qty, px) lsig = sig return blotter.trades
def open_to_close(self, open_pxs, close_pxs): blotter = TradeBlotter() signal = self.signal.dropna() diff = signal.diff() diff.iloc[0] = 0 changes = signal[diff != 0] lsig = 0 nopen = len(open_pxs) qtyfct = self._qty_fct(open_pxs.index) for ts, sig in changes.items(): blotter.ts = ts if sig != lsig: if lsig != 0: # close open trd with today's closing price px = close_pxs.get(ts, None) if px is None: raise Exception( 'insufficient close price data: no data found at %s' % ts) blotter.close(px) if sig != 0: idx = open_pxs.index.get_loc(ts) if (idx + 1) != nopen: ts_plus1 = open_pxs.index[idx + 1] px = open_pxs.iloc[idx + 1] size = abs(qtyfct(ts)) qty = sig > 0 and size or -size blotter.ts = ts_plus1 blotter.open(qty, px) else: pass lsig = sig return blotter.trades
def open_to_close(self, open_pxs, close_pxs): blotter = TradeBlotter() signal = self.signal.dropna() diff = signal.diff() diff.iloc[0] = 0 changes = signal[diff != 0] lsig = 0 nopen = len(open_pxs) qtyfct = self._qty_fct(open_pxs.index) for ts, sig in changes.iteritems(): blotter.ts = ts if sig != lsig: if lsig != 0: # close open trd with today's closing price px = close_pxs.get(ts, None) if px is None: raise Exception('insufficient close price data: no data found at %s' % ts) blotter.close(px) if sig != 0: idx = open_pxs.index.get_loc(ts) if (idx + 1) != nopen: ts_plus1 = open_pxs.index[idx + 1] px = open_pxs.iloc[idx + 1] size = abs(qtyfct(ts)) qty = sig > 0 and size or -size blotter.ts = ts_plus1 blotter.open(qty, px) else: pass lsig = sig return blotter.trades
def close_to_close(self, pxs): """ :param pxs: :return: """ if not isinstance(pxs, pd.Series): raise ValueError('pxs expected to be Series') blotter = TradeBlotter() signal = self.signal.dropna() diff = signal.diff() diff.iloc[0] = 0 changes = signal[diff != 0] lsig = 0 qtyfct = self._qty_fct(pxs.index) for ts, sig in changes.iteritems(): blotter.ts = ts if sig != lsig: px = pxs.get(ts, None) if px is None: raise Exception('insufficient price data: no data found at %s' % ts) if lsig != 0: # close open trd blotter.close(px=px) if sig != 0: size = abs(qtyfct(ts)) qty = sig > 0 and size or -size blotter.open(qty, px) lsig = sig return blotter.trades
def new_buy_and_hold_port(self, qty=1., open_px='close', open_dt=None, close_px='close', close_dt=None, ret_calc=None): """ :param qty: float :param open_px: one of {string, float}, opening trade price. If string define open, high, low, close as source. :param open_dt: opening trade date :param close_px: one of {string, float}, closing trade price. If string define open, high, low, close :param close_dt: closing trade date :param ret_calc: :return: """ from tia.analysis.model.trd import TradeBlotter from tia.analysis.model.port import SingleAssetPortfolio getpx = lambda how, dt: how if not isinstance(how, str) else self.pxs.frame[how].asof(dt) open_dt = open_dt or self.pxs.frame.index[0] open_px = getpx(open_px, open_dt) close_dt = close_dt or self.pxs.frame.index[-1] close_px = getpx(close_px, close_dt) pricer = self.truncate(open_dt, close_dt) blotter = TradeBlotter() blotter.ts = open_dt blotter.open(qty, open_px) blotter.ts = close_dt blotter.close(close_px) trds = blotter.trades return SingleAssetPortfolio(pricer, trds, ret_calc=ret_calc)
def buy_and_hold(ins, qty=1., start=None, end=None, which='close', rets_calc=None): """Construct a portfolio which opens a position with size qty at start (or first instrument price date) and continues to the specified end date. :param ins: Instrument :param qty: :param start: datetime :param end: datetime :param which: which price series to use for inital trade px :param ret_cacls: portfolio return calculator :return: SingleAssetPortfolio """ from tia.analysis.model.trd import TradeBlotter start = start and pd.to_datetime(start) or ins.pxs.frame.index[0] end = end and pd.to_datetime(end) or ins.pxs.frame.index[-1] if start != ins.pxs.frame.index[0] or end != ins.pxs.frame.index[-1]: ins = ins.truncate(start, end) start_px = ins.pxs.frame[which].iloc[0] end_px = ins.pxs.frame.close.iloc[-1] blotter = TradeBlotter() blotter.ts = ins.pxs.frame.index[0] blotter.open(qty, start_px) blotter.ts = ins.pxs.frame.index[-1] blotter.close(end_px) trds = blotter.trades return SingleAssetPortfolio(ins, trds, rets_calc=rets_calc)