Fix circular import

master
Mark Veidemanis 1 year ago
parent 70d1fdbbd3
commit f4ef280f80
Signed by: m
GPG Key ID: 5ACFCEED46C0904F

@ -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.

@ -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.

Loading…
Cancel
Save