Check the max risk relative to the account balance
This commit is contained in:
@@ -10,6 +10,35 @@ from core.util import logs
|
||||
log = logs.get_logger(__name__)
|
||||
|
||||
|
||||
def side_to_direction(side):
|
||||
"""
|
||||
Convert a side to a direction.
|
||||
"""
|
||||
if side == "long":
|
||||
return "buy"
|
||||
elif side == "short":
|
||||
return "sell"
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def convert_trades_to_usd(account, trades):
|
||||
"""
|
||||
Convert a list of trades to USD.
|
||||
"""
|
||||
for trade in trades:
|
||||
amount = trade["amount"]
|
||||
symbol = trade["symbol"]
|
||||
side = trade["side"]
|
||||
direction = side_to_direction(side)
|
||||
base, quote = get_base_quote(account.exchange, symbol)
|
||||
print("BASE", base)
|
||||
print("QUOTE", quote)
|
||||
print("AMOUNT", amount)
|
||||
amount_usd = to_currency(direction, account, amount, quote, "USD")
|
||||
print("AMOUNT USD", amount_usd)
|
||||
|
||||
|
||||
def get_pair(account, base, quote, invert=False):
|
||||
"""
|
||||
Get the pair for the given account and currencies.
|
||||
@@ -36,6 +65,21 @@ def get_pair(account, base, quote, invert=False):
|
||||
return symbol
|
||||
|
||||
|
||||
def get_base_quote(exchange, symbol):
|
||||
"""
|
||||
Get the base and quote currencies from a symbol.
|
||||
:param exchange: Exchange name
|
||||
:param symbol: Symbol
|
||||
:return: Tuple of base and quote currencies
|
||||
"""
|
||||
if exchange == "alpaca":
|
||||
separator = "/"
|
||||
elif exchange == "oanda":
|
||||
separator = "_"
|
||||
base, quote = symbol.split(separator)
|
||||
return (base, quote)
|
||||
|
||||
|
||||
def to_currency(direction, account, amount, from_currency, to_currency):
|
||||
"""
|
||||
Convert an amount from one currency to another.
|
||||
|
||||
Reference in New Issue
Block a user