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.
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
from core.trading.market import to_currency
|
||||
|
||||
|
||||
def check_max_loss(risk_model, initial_balance, account_balance):
|
||||
"""
|
||||
Check that the account balance is within the max loss limit.
|
||||
@@ -10,23 +7,26 @@ def check_max_loss(risk_model, initial_balance, account_balance):
|
||||
return account_balance > max_loss
|
||||
|
||||
|
||||
def check_max_risk(risk_model, account_trades):
|
||||
def check_max_risk(risk_model, account_balance_usd, account_trades):
|
||||
"""
|
||||
Check that all of the trades in the account are within the max risk limit.
|
||||
"""
|
||||
max_risk_percent = risk_model.max_risk_percent
|
||||
# Calculate the max risk of the account in USD
|
||||
max_risk_usd = account_balance_usd * (max_risk_percent / 100)
|
||||
total_risk = 0
|
||||
for trade in account_trades:
|
||||
max_tmp = []
|
||||
# Need to calculate the max risk in base account currency
|
||||
# Percentages relate to the price movement, without accounting the size of the trade
|
||||
if "stop_loss_percent" in trade:
|
||||
max_tmp.append(trade["stop_loss_percent"])
|
||||
if "trailing_stop_loss_percent" in trade:
|
||||
max_tmp.append(trade["trailing_stop_loss_percent"])
|
||||
# Percentages relate to the price movement, without accounting the
|
||||
# size of the trade
|
||||
if "stop_loss_usd" in trade:
|
||||
max_tmp.append(trade["stop_loss_usd"])
|
||||
if "trailing_stop_loss_usd" in trade:
|
||||
max_tmp.append(trade["trailing_stop_loss_usd"])
|
||||
total_risk += max(max_tmp)
|
||||
print("Total risk: ", total_risk)
|
||||
return total_risk < max_risk_percent
|
||||
allowed = total_risk < max_risk_usd
|
||||
return allowed
|
||||
|
||||
|
||||
def check_max_open_trades(risk_model, account_trades):
|
||||
|
||||
Reference in New Issue
Block a user