Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions investing_algorithm_framework/app/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,57 @@ def __init__(
self.trade_take_profit_service: TradeTakeProfitService = \
trade_take_profit_service

def _validate_target_symbol(self, target_symbol, market=None):
"""
Validate the target_symbol for order creation:
1. Prevents orders where target_symbol equals trading_symbol
(e.g. EUR/EUR).
2. Checks that a data source exists for the
target_symbol/trading_symbol combination (e.g. BTC/EUR).

Args:
target_symbol: The symbol of the asset to trade
market: The market to check against

Raises:
OperationalException: If validation fails
"""
portfolio = self.portfolio_service.find({"market": market})
trading_symbol = portfolio.trading_symbol

# Check target_symbol != trading_symbol
if target_symbol.upper() == trading_symbol.upper():
raise OperationalException(
f"target_symbol '{target_symbol}' is the same as "
f"the trading_symbol '{trading_symbol}'. "
f"This would result in a "
f"'{trading_symbol}/{trading_symbol}' "
f"order which is not valid. "
f"To skip this check, set validate_symbol=False "
f"or omit the parameter."
)

# Check that a data source is registered for this pair
expected_symbol = f"{target_symbol}/{trading_symbol}".upper()
known_symbols = set()

if self.data_provider_service.data_provider_index is not None:
for data_source, _ in \
self.data_provider_service \
.data_provider_index.get_all():
if data_source.symbol is not None:
known_symbols.add(data_source.symbol.upper())

if expected_symbol not in known_symbols:
sorted_symbols = sorted(known_symbols)
raise OperationalException(
f"No data source registered for '{expected_symbol}'. "
f"A data source is required to track price history. "
f"Registered data source symbols: {sorted_symbols}. "
f"To skip this check, set validate_symbol=False "
f"or omit the parameter."
)

@property
def config(self):
"""
Expand Down Expand Up @@ -78,7 +129,8 @@ def create_order(
market=None,
execute=True,
validate=True,
sync=True
sync=True,
validate_symbol=False
) -> Order:
"""
Function to create an order. This function will create an order
Expand All @@ -96,10 +148,15 @@ def create_order(
validate: If set to True, the order will be validated
sync: If set to True, the created order will be synced
with the portfolio of the algorithm.
validate_symbol: Default False. If set to True,
validates that target_symbol is not the trading_symbol.

Returns:
The order created
"""
if validate_symbol:
self._validate_target_symbol(target_symbol, market=market)

portfolio = self.portfolio_service.find({"market": market})
order_data = {
"target_symbol": target_symbol,
Expand Down Expand Up @@ -162,7 +219,8 @@ def create_limit_order(
execute=True,
validate=True,
sync=True,
metadata=None
metadata=None,
validate_symbol=False
) -> Order:
"""
Function to create a limit order. This function will create a limit
Expand Down Expand Up @@ -193,10 +251,15 @@ def create_limit_order(
sync (optional): Default True. If set to True,
the created order will be synced with the
portfolio of the algorithm
validate_symbol (optional): Default False. If set to True,
validates that target_symbol is not the trading_symbol.

Returns:
Order: Instance of the order created
"""
if validate_symbol:
self._validate_target_symbol(target_symbol, market=market)

portfolio = self.portfolio_service.find({"market": market})

if percentage_of_portfolio is not None:
Expand Down
6 changes: 4 additions & 2 deletions investing_algorithm_framework/app/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,8 @@ def create_limit_order(
execute=True,
validate=True,
sync=True,
metadata=None
metadata=None,
validate_symbol=False
) -> Order:
"""
Function to create a limit order. This function will create
Expand Down Expand Up @@ -986,7 +987,8 @@ def create_limit_order(
execute=execute,
validate=validate,
sync=sync,
metadata=metadata
metadata=metadata,
validate_symbol=validate_symbol
)

def close_position(
Expand Down
Loading
Loading