async def _create_initial_arbitrage_order(self, arbitrage_container): current_symbol_holding, current_market_holding, market_quantity, price, symbol_market = \ await trading_personal_data.get_pre_order_data(self.exchange_manager, symbol=self.trading_mode.symbol, timeout=trading_constants.ORDER_DATA_FETCHING_TIMEOUT) created_orders = [] order_type = trading_enums.TraderOrderType.BUY_LIMIT if arbitrage_container.state is trading_enums.EvaluatorStates.LONG \ else trading_enums.TraderOrderType.SELL_LIMIT quantity = self._get_quantity_from_holdings(current_symbol_holding, market_quantity, arbitrage_container.state) if order_type is trading_enums.TraderOrderType.SELL_LIMIT: quantity = trading_personal_data.add_dusts_to_quantity_if_necessary( quantity, price, symbol_market, current_symbol_holding) for order_quantity, order_price in trading_personal_data.check_and_adapt_order_details_if_necessary( quantity, arbitrage_container.own_exchange_price, symbol_market): current_order = trading_personal_data.create_order_instance( trader=self.exchange_manager.trader, order_type=order_type, symbol=self.trading_mode.symbol, current_price=arbitrage_container.own_exchange_price, quantity=order_quantity, price=order_price) created_order = await self.exchange_manager.trader.create_order( current_order) created_orders.append(created_order) arbitrage_container.initial_limit_order_id = created_order.order_id self.open_arbitrages.append(arbitrage_container) # only create one order per arbitrage return created_orders
async def create_sell_orders(self, symbol, timeout, sell_orders_count, quantity, sell_weight, sell_base): current_order = None try: current_symbol_holding, current_market_holding, market_quantity, price, symbol_market = \ await trading_personal_data.get_pre_order_data(self.exchange_manager, symbol=symbol, timeout=timeout) created_orders = [] sell_max_quantity = min(current_symbol_holding, quantity) to_create_orders = self._generate_sell_orders(sell_orders_count, sell_max_quantity, sell_weight, sell_base, symbol_market) for order_quantity, order_price in to_create_orders: current_order = trading_personal_data.create_order_instance(trader=self.exchange_manager.trader, order_type=trading_enums.TraderOrderType.SELL_LIMIT, symbol=symbol, current_price=sell_base, quantity=order_quantity, price=order_price) created_order = await self.exchange_manager.trader.create_order(current_order) created_orders.append(created_order) if created_orders: return created_orders raise trading_errors.MissingMinimalExchangeTradeVolume() except (trading_errors.MissingFunds, trading_errors.MissingMinimalExchangeTradeVolume): raise except Exception as e: self.logger.error(f"Failed to create order : {e} ({e.__class__.__name__}). Order: " f"{current_order.get_string_info() if current_order else None}") self.logger.exception(e, False) return []
async def create_buy_order(self, symbol, timeout, volume_weight, price_weight): current_order = None try: current_symbol_holding, current_market_holding, market_quantity, price, symbol_market = \ await trading_personal_data.get_pre_order_data(self.exchange_manager, symbol=symbol, timeout=timeout) base, _ = symbol_util.split_symbol(symbol) created_orders = [] quantity = await self._get_buy_quantity_from_weight(volume_weight, market_quantity, base) limit_price = trading_personal_data.adapt_price(symbol_market, self.get_limit_price(price)) for order_quantity, order_price in trading_personal_data.check_and_adapt_order_details_if_necessary( quantity, limit_price, symbol_market): current_order = trading_personal_data.create_order_instance(trader=self.exchange_manager.trader, order_type=trading_enums.TraderOrderType.BUY_LIMIT, symbol=symbol, current_price=price, quantity=order_quantity, price=order_price) created_order = await self.exchange_manager.trader.create_order(current_order) created_orders.append(created_order) self._register_buy_order(created_order.order_id, price_weight) if created_orders: return created_orders raise trading_errors.MissingMinimalExchangeTradeVolume() except (trading_errors.MissingFunds, trading_errors.MissingMinimalExchangeTradeVolume): raise except Exception as e: self.logger.error(f"Failed to create order : {e}. Order: " f"{current_order.get_string_info() if current_order else None}") self.logger.exception(e, False) return []
async def _create_order(self, symbol, order_type, current_price, quantity, limit_price): current_order = trading_personal_data.create_order_instance(trader=self.exchange_manager_instance.trader, order_type=order_type, symbol=symbol, current_price=current_price, quantity=quantity, price=limit_price) await self.exchange_manager_instance.trader.create_order(current_order)
async def _create_secondary_arbitrage_order(self, arbitrage_container, quantity): created_orders = [] current_symbol_holding, current_market_holding, market_quantity, price, symbol_market = \ await trading_personal_data.get_pre_order_data(self.exchange_manager, symbol=self.trading_mode.symbol, timeout=trading_constants.ORDER_DATA_FETCHING_TIMEOUT) now_selling = arbitrage_container.state is trading_enums.EvaluatorStates.LONG if now_selling: quantity = trading_personal_data.add_dusts_to_quantity_if_necessary( quantity, price, symbol_market, current_symbol_holding) for order_quantity, order_price in trading_personal_data.check_and_adapt_order_details_if_necessary( quantity, arbitrage_container.target_price, symbol_market): current_order = trading_personal_data.create_order_instance( trader=self.exchange_manager.trader, order_type=trading_enums.TraderOrderType.SELL_LIMIT if now_selling else trading_enums.TraderOrderType.BUY_LIMIT, symbol=self.trading_mode.symbol, current_price=arbitrage_container.own_exchange_price, quantity=order_quantity, price=order_price) created_order = await self.exchange_manager.trader.create_order( current_order) created_orders.append(created_order) arbitrage_container.secondary_limit_order_id = created_order.order_id stop_price = self._get_stop_loss_price( symbol_market, arbitrage_container.own_exchange_price, now_selling) current_order = trading_personal_data.create_order_instance( trader=self.exchange_manager.trader, order_type=trading_enums.TraderOrderType.STOP_LOSS, symbol=self.trading_mode.symbol, current_price=arbitrage_container.own_exchange_price, quantity=order_quantity, price=stop_price, linked_to=created_order, side=trading_enums.TradeOrderSide.SELL if now_selling else trading_enums.TradeOrderSide.BUY) await self.exchange_manager.trader.create_order(current_order) arbitrage_container.secondary_stop_order_id = current_order.order_id return created_orders
async def create_new_orders(self, symbol, final_note, state, **kwargs): current_order = None try: base, market = symbol_util.split_symbol(symbol) if market != self.exchange_manager.exchange_personal_data.portfolio_manager.reference_market: self.logger.warning(f"Ignored DCA order creation on {symbol} : it's not a reference market pair.") return [] current_symbol_holding, current_market_holding, market_quantity, price, symbol_market = \ await trading_personal_data.get_pre_order_data(self.exchange_manager, symbol=symbol, timeout=trading_constants.ORDER_DATA_FETCHING_TIMEOUT) created_orders = [] quantity = self.order_quantity_of_ref_market / price limit_price = trading_personal_data.adapt_price(symbol_market, price * (1 - self.ORDER_PRICE_DISTANCE)) for order_quantity, order_price in trading_personal_data.check_and_adapt_order_details_if_necessary( quantity, limit_price, symbol_market): current_order = trading_personal_data.create_order_instance(trader=self.exchange_manager.trader, order_type=trading_enums.TraderOrderType.BUY_LIMIT, symbol=symbol, current_price=price, quantity=order_quantity, price=order_price) created_order = await self.exchange_manager.trader.create_order(current_order) created_orders.append(created_order) if created_orders: return created_orders raise trading_errors.MissingMinimalExchangeTradeVolume() except (trading_errors.MissingFunds, trading_errors.MissingMinimalExchangeTradeVolume): raise except Exception as e: self.logger.error(f"Failed to create order : {e}. Order: " f"{current_order if current_order else None}") self.logger.exception(e, False) return []
async def create_new_orders(self, symbol, final_note, state, **kwargs): current_order = None timeout = kwargs.pop("timeout", trading_constants.ORDER_DATA_FETCHING_TIMEOUT) try: current_symbol_holding, current_market_holding, market_quantity, price, symbol_market = \ await trading_personal_data.get_pre_order_data(self.exchange_manager, symbol=symbol, timeout=timeout) quote, _ = symbol_util.split_symbol(symbol) created_orders = [] if state == trading_enums.EvaluatorStates.VERY_SHORT.value and not self.DISABLE_SELL_ORDERS: quantity = self._get_market_quantity_from_risk( final_note, current_symbol_holding, quote, True) quantity = trading_personal_data.add_dusts_to_quantity_if_necessary( quantity, price, symbol_market, current_symbol_holding) for order_quantity, order_price in self._check_and_adapt_order_details_if_necessary( quantity, price, symbol_market): current_order = trading_personal_data.create_order_instance( trader=self.trader, order_type=trading_enums.TraderOrderType.SELL_MARKET, symbol=symbol, current_price=float(order_price), quantity=float(order_quantity), price=float(order_price)) await self.trader.create_order(current_order) created_orders.append(current_order) elif state == trading_enums.EvaluatorStates.SHORT.value and not self.DISABLE_SELL_ORDERS: quantity = await self._get_sell_limit_quantity_from_risk( final_note, current_symbol_holding, quote) quantity = trading_personal_data.add_dusts_to_quantity_if_necessary( quantity, price, symbol_market, current_symbol_holding) limit_price = trading_personal_data.adapt_price( symbol_market, price * self._get_limit_price_from_risk(final_note)) for order_quantity, order_price in self._check_and_adapt_order_details_if_necessary( quantity, limit_price, symbol_market): current_order = trading_personal_data.create_order_instance( trader=self.trader, order_type=trading_enums.TraderOrderType.SELL_LIMIT, symbol=symbol, current_price=price, quantity=float(order_quantity), price=float(order_price)) updated_limit = await self.trader.create_order( current_order) created_orders.append(updated_limit) if self.USE_STOP_ORDERS: stop_price = trading_personal_data.adapt_price( symbol_market, price * self._get_stop_price_from_risk()) current_order = trading_personal_data.create_order_instance( trader=self.trader, order_type=trading_enums.TraderOrderType.STOP_LOSS, symbol=symbol, current_price=price, quantity=float(order_quantity), price=stop_price, linked_to=updated_limit) await self.trader.create_order(current_order) elif state == trading_enums.EvaluatorStates.NEUTRAL.value: return [] # TODO : stop loss elif state == trading_enums.EvaluatorStates.LONG.value and not self.DISABLE_BUY_ORDERS: quantity = self._get_buy_limit_quantity_from_risk( final_note, market_quantity, quote) quantity = quantity * await self._get_quantity_ratio(quote) limit_price = trading_personal_data.adapt_price( symbol_market, price * self._get_limit_price_from_risk(final_note)) for order_quantity, order_price in self._check_and_adapt_order_details_if_necessary( quantity, limit_price, symbol_market): current_order = trading_personal_data.create_order_instance( trader=self.trader, order_type=trading_enums.TraderOrderType.BUY_LIMIT, symbol=symbol, current_price=price, quantity=float(order_quantity), price=float(order_price)) await self.trader.create_order(current_order) created_orders.append(current_order) elif state == trading_enums.EvaluatorStates.VERY_LONG.value and not self.DISABLE_BUY_ORDERS: quantity = self._get_market_quantity_from_risk( final_note, market_quantity, quote) quantity = quantity * await self._get_quantity_ratio(quote) for order_quantity, order_price in self._check_and_adapt_order_details_if_necessary( quantity, price, symbol_market): current_order = trading_personal_data.create_order_instance( trader=self.trader, order_type=trading_enums.TraderOrderType.BUY_MARKET, symbol=symbol, current_price=float(order_price), quantity=float(order_quantity), price=float(order_price)) await self.trader.create_order(current_order) created_orders.append(current_order) if created_orders: return created_orders raise trading_errors.MissingMinimalExchangeTradeVolume() except (trading_errors.MissingFunds, trading_errors.MissingMinimalExchangeTradeVolume): raise except asyncio.TimeoutError as e: self.logger.error( f"Impossible to create order for {symbol} on {self.exchange_manager.exchange_name}: {e} " f"and is necessary to compute the order details.") return [] except Exception as e: self.logger.exception(e, True, f"Failed to create order : {e}.") return []