def as_dict(self) -> Dict: return { "type": self.type, "from": format_date(self.from_date), "to": format_date(self.to_date), "holdings": [holding._asdict() for holding in self.holdings], "pctChange": self.pct_change }
def as_dict(self) -> Dict: return { "statement": AccountStatement.from_account(self.result.account).as_dict(), "returns": self.result.returns, "maxDrawdown": self.result.max_drawdown, "sharpeRatio": self.result.sharpe_ratio, "startDate": format_date(self.result.start_date), "endDate": format_date(self.result.end_date) }
def calc(self, historic_prices: FundHistoricPrices) -> FundIndicator: try: drawdown_series = to_drawdown_series(historic_prices) date = drawdown_series.idxmin() return FundIndicator(drawdown_series[date], metadata={"date": format_date(date)}) except: return FundIndicator(np.nan)
def _reduce_returns(historic_prices_series: FundHistoricPrices, offset: pd.DateOffset, agg_func: Callable[[FundHistoricPrices], datetime]) -> FundIndicator: try: shifted_series = drop_duplicate_index( historic_prices_series.shift(freq=offset)) \ .reindex(index=historic_prices_series.index, method="ffill") returns_series = (historic_prices_series - shifted_series) / shifted_series date = agg_func(returns_series) return FundIndicator(returns_series[date], metadata={"date": format_date(date)}) except: return FundIndicator(np.nan)
def calc(self, historic_prices: FundHistoricPrices) -> FundIndicator: try: peaks = historic_prices.expanding().max() peak_diffs = peaks.diff() recovery_dates = peak_diffs.index[peak_diffs.gt(0)] # add begin and last before comparison recovery_dates = recovery_dates \ .insert(0, historic_prices.first_valid_index()) \ .append(DatetimeIndex([historic_prices.last_valid_index()])) recovery_times = recovery_dates.to_series().diff() date, mdt = recovery_times.idxmax(), recovery_times.max().days return FundIndicator(mdt, metadata={"date": format_date(date)}) except: return FundIndicator(np.nan)
def on_post_predict(self, req: falcon.Request, resp: falcon.Response): params = SimulateRoutes.PredictParam.from_dict(req.media) predict_date = BDAY.rollback(params.date) if params.date else None simulator = self._build_simulator(params.simulate_param) result = simulator.predict(predict_date) resp.media = { "date": format_date(result.date), "funds": [ { "isin": fund.isin, "sedol": fund.sedol, "name": fund.name } for fund in result.funds ] }
def test_format_date(): d = datetime(2019, 1, 1, tzinfo=timezone.utc) assert format_date(d) == "2019-01-01T00:00:00Z"
def as_dict(self) -> Dict: return { "date": format_date(self.date), "price": self.price }