def get_param(self, param_name: ParamName) -> Param:
     with transaction_context() as session:
         entry = session.query(ParamDb).filter_by(
             name=param_name.value).first()
         if entry is None:
             logger.error(f'{param_name} not in db')
             raise Exception(f'{param_name} not in db')
         param = Param(name=param_name, value=entry.value)
     return param
 def add_param(self, name: str, value):
     param_name = ParamName(name)
     if param_name is ParamName.OTHER:
         logger.error(f'{name} not in ParamName enum')
         raise Exception(
             f'{name} not in ParamName enum. Accepted value : ' +
             ';'.join([i for i in ParamName]))
     with transaction_context() as session:
         entry = ParamDb(name=param_name, value=value)
         session.add(entry)
 def _log(self):
     while True:
         msg = self.queue_log.get()
         if msg['level'] == 'info':
             logger.info(msg['message'])
         elif msg['level'] == 'error':
             logger.error(msg['message'])
         else:
             logger.debug(msg['message'])
         if msg.get('tid'):
             tid = msg['tid']
             self.tasks[tid].ttl = msg['ttl']
             self.tasks[tid].result = msg['result']
Example #4
0
 def evaluate(self,
              ptf: Portfolio,
              previous_positions: List[Position],
              previous_money: float = None):
     if previous_money:
         ptf.money = previous_money
     ptf.positions = deepcopy(previous_positions)
     ptf.compute_liquid_value()
     ptf.close_position()
     side: Side = self.strategy_service.evaluate(ptf.as_of_date)
     try:
         ptf.make_operation(side, 1, self.transaction_fee, self.take_profit,
                            self.stop_loss)
     except (PortfolioException, NotEnoughMoneyException) as e:
         logger.error(e)
Example #5
0
async def backtest_strategy(params: StrategySchemaIn):
    start_date = params.start_date
    end_date = params.end_date
    transaction_fee = params.transaction_fee
    initial_capital = params.initial_capital
    stock = params.stock
    take_profit = params.take_profit if params.take_profit != 0 else None
    stop_loss = params.stop_loss if params.stop_loss != 0 else None
    strategy = params.strategy
    ptf_type = params.ptf_type
    strat_params = params.params
    try:
        data_connector = data_connector_factory(stock)
        data_service = DataService(data_connector, start_date, end_date)
        strategy_service = strategy_service_factory(strategy, data_service,
                                                    *strat_params)
        portfolio_service = PortfolioService(transaction_fee,
                                             strategy_service,
                                             asset_values=data_service.df,
                                             initial_capital=initial_capital,
                                             ptf_type=ptf_type,
                                             take_profit=take_profit,
                                             stop_loss=stop_loss)
        service = MainService(data_service, portfolio_service,
                              strategy_service)
        service.evaluate_all()
        fig = service.get_reporting()
        file_name = uuid.uuid4().hex + '.png'
        file_path = os.path.join(app_config.GRAPH_FOLDER, file_name)
        fig.savefig(file_path)
        ret = service.serialize
        ret['base_100_file_name'] = file_name
    except NoDataFoundException as e:
        logger.error('NoDataFoundException :  ' + str(e))
        raise HTTPException(400, str(e))
    except Exception as e:
        logger.error('Exception : ' + str(e))
        raise HTTPException(400, str(e))
    print(ret)
    return ret