def _get_position(self, symbol): if symbol not in self._position_dict: contract = ContractData.get_contract(symbol) position = PositionFactory(contract.contract_type) position.symbol = symbol position.strategy_id = self._strategy_id position.asset_base = contract.asset_base position.asset_quote = contract.asset_quote self._position_dict[symbol] = position return self._position_dict[symbol]
def check_risk(self, strategy: StrategyTemplate): strategy.update_portfolio() for position in strategy.portfolio: symbol = position.symbol pnl_ratio = position.unrealized_pnl_rate() contract = ContractData.get_contract(symbol) if (abs(position.amount) > contract.min_quantity) and ( pnl_ratio < -abs(self.stop_loss)): logger.info( f'Stop loss triggered by {self} with pnl rate {pnl_ratio}\n{position.pretty_string()}' ) strategy.close_position(position.symbol)
def execute(self, actor: _Actor): balance_df = super(CloseOpenPositionCommand, self).execute(actor) for asset in balance_df.index: position = balance_df.loc[asset] symbol = position['symbol'] if symbol: price = position['price'] contract = ContractData.get_contract(symbol) if position['total_amount'] > contract.min_quantity and \ position['notional'] > contract.min_notional: self.render(f'handling position {asset}...') if position['frozen_amount'] > 0: pass order = OrderData() order.symbol = symbol order.direction = EnumOrderDirection.SELL order.price = price order.volume = position['total_amount'] order.strategy_id = 'manual' actor.send_order(order)
def contract(self): return ContractData.get_contract(self.symbol)
def available_quote_amount(self, symbol): quote_symbol = ContractData.get_contract(symbol).asset_quote return self._available_balance(quote_symbol)
def available_base_amount(self, symbol): base_symbol = ContractData.get_contract(symbol).asset_base return self._available_balance(base_symbol)