From f4ef280f805f65ee454fce1bbf1c2741a798f11f Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Wed, 11 Jan 2023 21:12:43 +0000 Subject: [PATCH] Fix circular import --- core/exchanges/convert.py | 63 ++++++++++++++++++++++++++++++++++++++- core/trading/market.py | 62 ++------------------------------------ 2 files changed, 64 insertions(+), 61 deletions(-) diff --git a/core/exchanges/convert.py b/core/exchanges/convert.py index 31bf0f2..d956671 100644 --- a/core/exchanges/convert.py +++ b/core/exchanges/convert.py @@ -1,13 +1,74 @@ from decimal import Decimal as D +from core.exchanges import GenericAPIError from core.models import Account -from core.trading.market import direction_to_side, get_price, side_to_direction +from core.util import logs +log = logs.get_logger(__name__) # Separate module to prevent circular import from # models -> exchanges -> common -> models # Since we need Account here to look up missing prices +def get_price(account, direction, symbol): + """ + Get the price for a given symbol. + :param account: Account object + :param direction: direction of the trade + :param symbol: symbol + :return: price of bid for buys, price of ask for sells + """ + if direction == "buy": + price_index = "bids" + elif direction == "sell": + price_index = "asks" + try: + prices = account.client.get_currencies([symbol]) + except GenericAPIError as e: + log.error(f"Error getting currencies: {e}") + return None + price = D(prices["prices"][0][price_index][0]["price"]) + return price + + +def side_to_direction(side, flip_direction=False): + """ + Convert a side to a direction. + :param side: Side, e.g. long, short + :param flip_direction: Flip the direction + :return: Direction, e.g. buy, sell + """ + if side == "long": + if flip_direction: + return "sell" + return "buy" + elif side == "short": + if flip_direction: + return "buy" + return "sell" + else: + return False + + +def direction_to_side(direction, flip_side=False): + """ + Convert a direction to a side. + :param direction: Direction, e.g. buy, sell + :param flip_side: Flip the side + :return: Side, e.g. long, short + """ + if direction == "buy": + if flip_side: + return "short" + return "long" + elif direction == "sell": + if flip_side: + return "long" + return "short" + else: + return False + + def tp_price_to_percent(tp_price, side, current_price, current_units, unrealised_pl): """ Determine the percent change of the TP price from the initial price. diff --git a/core/trading/market.py b/core/trading/market.py index ad816cd..ba75a1d 100644 --- a/core/trading/market.py +++ b/core/trading/market.py @@ -1,7 +1,8 @@ from datetime import datetime from decimal import Decimal as D -from core.exchanges import GenericAPIError, common +from core.exchanges import common +from core.exchanges.convert import get_price, side_to_direction from core.lib.notify import sendmsg from core.models import Account, Strategy, Trade from core.trading.crossfilter import crossfilter @@ -11,44 +12,6 @@ from core.util import logs log = logs.get_logger(__name__) -def side_to_direction(side, flip_direction=False): - """ - Convert a side to a direction. - :param side: Side, e.g. long, short - :param flip_direction: Flip the direction - :return: Direction, e.g. buy, sell - """ - if side == "long": - if flip_direction: - return "sell" - return "buy" - elif side == "short": - if flip_direction: - return "buy" - return "sell" - else: - return False - - -def direction_to_side(direction, flip_side=False): - """ - Convert a direction to a side. - :param direction: Direction, e.g. buy, sell - :param flip_side: Flip the side - :return: Side, e.g. long, short - """ - if direction == "buy": - if flip_side: - return "short" - return "long" - elif direction == "sell": - if flip_side: - return "long" - return "short" - else: - return False - - def convert_trades_to_usd(account, trades): """ Convert a list of trades to USD. Input will also be mutated. @@ -91,27 +54,6 @@ def get_base_quote(exchange, symbol): return (base, quote) -def get_price(account, direction, symbol): - """ - Get the price for a given symbol. - :param account: Account object - :param direction: direction of the trade - :param symbol: symbol - :return: price of bid for buys, price of ask for sells - """ - if direction == "buy": - price_index = "bids" - elif direction == "sell": - price_index = "asks" - try: - prices = account.client.get_currencies([symbol]) - except GenericAPIError as e: - log.error(f"Error getting currencies: {e}") - return None - price = D(prices["prices"][0][price_index][0]["price"]) - return price - - def get_trade_size_in_base(direction, account, strategy, cash_balance, base): """ Get the trade size in the base currency.