Skip to content

Commit

Permalink
Fix flake8 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
MDUYN committed Dec 16, 2024
1 parent 021be50 commit a7c50fd
Show file tree
Hide file tree
Showing 16 changed files with 450 additions and 307 deletions.
1 change: 0 additions & 1 deletion investing_algorithm_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
CCXTTickerMarketDataSource, CSVOHLCVMarketDataSource, \
CSVTickerMarketDataSource
from .create_app import create_app
from investing_algorithm_framework.indicators import *

__all__ = [
"Algorithm",
Expand Down
64 changes: 38 additions & 26 deletions investing_algorithm_framework/app/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,16 @@ def _validate_name(self, name):
None
"""
if not isinstance(name, str):
raise OperationalException("The name of the algorithm must be a string")

raise OperationalException(
"The name of the algorithm must be a string"
)

pattern = re.compile(r"^[a-zA-Z0-9]*$")
if not pattern.match(name):
raise OperationalException("The name of the algorithm can only contain letters and numbers")
raise OperationalException(
"The name of the algorithm can only contain" +
" letters and numbers"
)

def initialize_services(
self,
Expand Down Expand Up @@ -256,19 +261,19 @@ def create_order(
return self.order_service.create(
order_data, execute=execute, validate=validate, sync=sync
)

def has_balance(self, symbol, amount, market=None):
"""
Function to check if the portfolio has enough balance to
create an order. This function will return True if the
portfolio has enough balance to create an order, False
portfolio has enough balance to create an order, False
otherwise.
Parameters:
symbol: The symbol of the asset
amount: The amount of the asset
market: The market of the asset
Returns:
Boolean: True if the portfolio has enough balance
"""
Expand Down Expand Up @@ -309,20 +314,26 @@ def create_limit_order(
price: The price of the asset
order_side: The side of the order
amount (optional): The amount of the asset to trade
amount_trading_symbol (optional): The amount of the trading symbol to trade
percentage (optional): The percentage of the portfolio to allocate to the
amount_trading_symbol (optional): The amount of the
trading symbol to trade
percentage (optional): The percentage of the portfolio
to allocate to the
order
percentage_of_portfolio (optional): The percentage of the portfolio to
allocate to the order
percentage_of_position (optional): The percentage of the position to
allocate to the order. (Only supported for SELL orders)
percentage_of_portfolio (optional): The percentage
of the portfolio to allocate to the order
percentage_of_position (optional): The percentage
of the position to allocate to
the order. (Only supported for SELL orders)
precision (optional): The precision of the amount
market (optional): The market to trade the asset
execute (optional): Default True. If set to True, the order will be executed
validate (optional): Default True. If set to True, the order will be validated
sync (optional): Default True. If set to True, the created order will be synced with the
execute (optional): Default True. If set to True,
the order will be executed
validate (optional): Default True. If set to
True, the order will be validated
sync (optional): Default True. If set to True,
the created order will be synced with the
portfolio of the algorithm
Returns:
Order: Instance of the order created
"""
Expand Down Expand Up @@ -369,10 +380,10 @@ def create_limit_order(
raise OperationalException(
"The amount parameter is required to create a limit order." +
"Either the amount, amount_trading_symbol, percentage, " +
"percentage_of_portfolio or percentage_of_position parameter " +
"must be specified."
"percentage_of_portfolio or percentage_of_position "
"parameter must be specified."
)

order_data = {
"target_symbol": target_symbol,
"price": price,
Expand Down Expand Up @@ -417,7 +428,7 @@ def create_market_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
Returns:
Order: Instance of the order created
"""
Expand Down Expand Up @@ -454,7 +465,7 @@ def get_portfolio(self, market=None) -> Portfolio:
Parameters:
market: The market of the portfolio
Returns:
Portfolio: The portfolio of the algorithm
"""
Expand Down Expand Up @@ -492,7 +503,7 @@ def get_total_size(self):
"""
Returns the total size of the portfolio.
The total size of the portfolio is the unallocated balance and the
The total size of the portfolio is the unallocated balance and the
allocated balance of the portfolio.
Returns:
Expand Down Expand Up @@ -597,7 +608,7 @@ def get_positions(
amount_lt: The amount of the asset must be less than this
amount_lte: The amount of the asset must be less than or equal
to this
Returns:
List[Position]: A list of positions that match the query parameters
"""
Expand Down Expand Up @@ -1167,7 +1178,7 @@ def get_closed_trades(self) -> List[Trade]:
"""
Function to get all closed trades. This function will return all
closed trades of the algorithm.
Returns:
List[Trade]: A list of closed trades
"""
Expand Down Expand Up @@ -1234,8 +1245,9 @@ def has_trading_symbol_position_available(
the position must be greater than the net_size of the
portfolio.
:param amount_gt: The amount of the position must be greater than
this amount.
Parameters:
amount_gt: The amount of the position must be greater than this
amount.
:param amount_gte: The amount of the position must be greater than
or equal to this amount.
:param percentage_of_portfolio: The amount of the position must be
Expand Down
40 changes: 22 additions & 18 deletions investing_algorithm_framework/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def _create_backtest_database_if_not_exists(self):
should be called before running a backtest for an algorithm.
It creates the database if it does not exist.
Args:
Parameters:
None
Returns
Expand Down Expand Up @@ -728,9 +728,10 @@ def run_backtest(
Algorithm)
backtest_date_range: The date range to run the backtest for
(instance of BacktestDateRange)
pending_order_check_interval: str - pending_order_check_interval: The interval at which to check
pending orders (e.g. 1h, 1d, 1w)
output_directory: str - The directory to write the backtest report to
pending_order_check_interval: str - pending_order_check_interval:
The interval at which to check pending orders (e.g. 1h, 1d, 1w)
output_directory: str - The directory to
write the backtest report to
Returns:
Instance of BacktestReport
Expand Down Expand Up @@ -776,7 +777,7 @@ def run_backtests(
date_ranges: List[BacktestDateRange] = None,
pending_order_check_interval=None,
output_directory=None,
checkpoint = False
checkpoint=False
) -> List[BacktestReport]:
"""
Run a backtest for a set algorithm. This method should be called when
Expand All @@ -788,15 +789,17 @@ def run_backtests(
backtests for
pending_order_check_interval: str - The interval at which to check
pending orders
output_directory: str - The directory to write the backtest report to.
checkpoint: bool - Whether to checkpoint the backtest, If True, then it
will be checked if for a given algorithm name and date range,
a backtest report already exists. If it does, then the backtest will
not be run again. This is useful when running backtests
for a large number of algorithms and date ranges where some of the
backtests may fail and you want to re-run only the failed backtests.
Returns
output_directory: str - The directory to write the backtest
report to.
checkpoint: bool - Whether to checkpoint the backtest,
If True, then it will be checked if for a given algorithm name
and date range, a backtest report already exists. If it does,
then the backtest will not be run again. This is useful
when running backtests for a large number of algorithms
and date ranges where some of the backtests may fail
and you want to re-run only the failed backtests.
Returns
List of BacktestReport intances
"""
logger.info("Initializing backtests")
Expand All @@ -822,17 +825,18 @@ def run_backtests(
if checkpoint:
backtest_service = self.container.backtest_service()
report = backtest_service.get_report(
algorithm_name=algorithm.name,
backtest_date_range=date_range,
algorithm_name=algorithm.name,
backtest_date_range=date_range,
directory=output_directory
)

if report is not None:

print(
f"{COLOR_YELLOW}Backtest already exists "
f"for algorithm {algorithm.name} date "
f"range:{COLOR_RESET} {COLOR_GREEN}{date_range.name} "
f"range:{COLOR_RESET} {COLOR_GREEN} "
f"{date_range.name} "
f"{date_range.start_date} - "
f"{date_range.end_date}"
)
Expand Down
Loading

0 comments on commit a7c50fd

Please sign in to comment.