def to_json(src: Any) -> str: if not src: return '' elif isinstance(src, list): return json.dumps(ModelUtils.get_dicts(src)) else: return json.dumps(ModelUtils.get_dict(src))
def set_readonly_props(self) -> None: if not self._transactions: return self._start_capital = self._capital.get( ModelUtils.get_first_key(self._capital)) self._end_capital = self._capital.get( ModelUtils.get_last_key(self._capital)) self._hold_length_days_stats = StatUtils.get_descriptive_stats( [t.hold_length_days for t in self._transactions]) self._change_in_capital_stats = StatUtils.get_descriptive_stats( [t.change_in_capital for t in self._transactions]) self._has_profit_stats = StatUtils.get_descriptive_stats( [NumberUtils.to_int(t.has_profit) for t in self._transactions]) self._pct_return = NumberUtils.get_change(self._end_capital, self._start_capital) self._best_transactions = [ t for t in sorted(self._transactions, key=lambda x: x.change_in_capital, reverse=True) if t.has_profit ][:20] self._worst_transactions = [ t for t in sorted(self._transactions, key=lambda x: x.change_in_capital) if not t.has_profit ][:20] symbol_grouped: Dict = {} for t in self._transactions: if not t.symbol_master.symbol in symbol_grouped: symbol_grouped[t.symbol_master.symbol]: Dict = { 'symbol_master': t.symbol_master, 'change_in_capital': 0, 'no_of_transactions': 0 } symbol_grouped[t.symbol_master. symbol]['change_in_capital'] += t.change_in_capital symbol_grouped[t.symbol_master.symbol]['no_of_transactions'] += 1 symbol_grouped_list: List[BackTestResultItemPerSymbol] = [] for k, v in symbol_grouped.items(): item: BackTestResultItemPerSymbol = BackTestResultItemPerSymbol() item.symbol_master = symbol_grouped[k]['symbol_master'] item.change_in_capital = NumberUtils.round( symbol_grouped[k]['change_in_capital']) item.no_of_transactions = symbol_grouped[k]['no_of_transactions'] symbol_grouped_list.append(item) self._best_symbols = [ i for i in sorted(symbol_grouped_list, key=lambda x: x.change_in_capital, reverse=True) if i.change_in_capital > 0 ][:20] self._worst_symbols = [ i for i in sorted(symbol_grouped_list, key=lambda x: x.change_in_capital) if i.change_in_capital < 0 ][:20]
def strategy_request(self) -> SmaCrossoverStrategyRequest: return ModelUtils.get_obj(SmaCrossoverStrategyRequest(), self._strategy_request)
def strategy_request(self) -> DoubleTopsStrategyRequest: return ModelUtils.get_obj(DoubleTopsStrategyRequest(), self._strategy_request)
def get_suggestions() -> str: req: TradeSuggestionsRequest = ModelUtils.get_obj( TradeSuggestionsRequest(), request.get_json()) return StringUtils.to_json( ResponseModel(__trade_service.get_suggestions(req)))
def get_trade_orders() -> str: req: GetTradeOrdersRequest = ModelUtils.get_obj(GetTradeOrdersRequest(), request.get_json()) return StringUtils.to_json( ResponseModel(__trade_service.get_trade_orders(req)))
def get_sample_prices_for_charts() -> str: req: CR = ModelUtils.get_obj(CR(), request.get_json()) return StringUtils.to_json( ResponseModel(__stock_service.get_sample_prices_for_charts(req)))
def update_symbol() -> str: req: SymbolMaster = ModelUtils.get_obj(SymbolMaster(), request.get_json()) return StringUtils.to_json( ResponseModel(__stock_service.update_symbol(req)))
def strategy_request(self) -> DemoStrategyRequest: return ModelUtils.get_obj(DemoStrategyRequest(), self._strategy_request)
def run() -> str: req: BackTestRunRequest = ModelUtils.get_obj(BackTestRunRequest(), request.get_json()) return StringUtils.to_json(ResponseModel(__back_test_service.run(req)))
def strategy_request(self) -> InvertedHeadAndShouldersRequest: return ModelUtils.get_obj(InvertedHeadAndShouldersRequest(), self._strategy_request)
def strategy_request(self) -> DoubleBollingerBandsStrategyRequest: return ModelUtils.get_obj(DoubleBollingerBandsStrategyRequest(), self._strategy_request)