Implement trading time limits

This commit is contained in:
2022-11-25 19:28:21 +00:00
parent 69a2b269ad
commit 4973582bdf
11 changed files with 257 additions and 25 deletions

View File

@@ -1,3 +1,4 @@
from datetime import datetime
from decimal import Decimal as D
from core.exchanges import GenericAPIError
@@ -7,15 +8,6 @@ from core.util import logs
log = logs.get_logger(__name__)
# def to_usd(account, amount, from_currency):
# if account.exchange == "alpaca":
# separator = "/"
# elif account.exchange == "oanda":
# separator = "_"
# symbol = f"{from_currency.upper()}{separator}{to_currency.upper()}"
# prices = account.client.get_currencies([symbol])
def get_pair(account, base, quote, invert=False):
"""
Get the pair for the given account and currencies.
@@ -273,6 +265,17 @@ def execute_strategy(callback, strategy):
:param strategy: Strategy object
"""
# Check if we can trade now!
now_utc = datetime.utcnow()
trading_times = strategy.trading_times.all()
if not trading_times:
log.error("No trading times set for strategy")
return
matches = [x.within_range(now_utc) for x in trading_times]
if not any(matches):
log.debug("Not within trading time range")
return
# Get the account's balance in the native account currency
cash_balance = strategy.account.client.get_balance()
log.debug(f"Cash balance: {cash_balance}")