Skip to content

Commit

Permalink
[ScriptedLib] fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDSM committed Dec 4, 2024
1 parent 0b211e2 commit 79471fd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 102 deletions.
31 changes: 9 additions & 22 deletions Meta/Keywords/scripting_library/orders/position_size/amount.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,15 @@ async def get_amount(
unknown_portfolio_on_creation=False,
target_price=None
):
try:
amount_value = await script_keywords.get_amount_from_input_amount(
context=context,
input_amount=input_amount,
side=side,
reduce_only=reduce_only,
is_stop_order=is_stop_order,
use_total_holding=use_total_holding,
target_price=target_price
)
except NotImplementedError:
amount_type, amount_value = script_keywords.parse_quantity(input_amount)
if amount_type is script_keywords.QuantityType.POSITION_PERCENT: # todo handle existing open short position
amount_value = \
exchange_private_data.open_position_size(context, side,
amount_type=commons_constants.PORTFOLIO_AVAILABLE) \
* amount_value / 100
else:
raise trading_errors.InvalidArgumentError("make sure to use a supported syntax for amount")
return await script_keywords.adapt_amount_to_holdings(context, amount_value, side,
use_total_holding, reduce_only, is_stop_order,
target_price=target_price)
amount_value = await script_keywords.get_amount_from_input_amount(
context=context,
input_amount=input_amount,
side=side,
reduce_only=reduce_only,
is_stop_order=is_stop_order,
use_total_holding=use_total_holding,
target_price=target_price
)
if unknown_portfolio_on_creation:
# no way to check if the amount is valid when creating order
_, amount_value = script_keywords.parse_quantity(input_amount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import octobot_trading.personal_data.orders.order_util as order_util
import octobot_trading.api as api
import octobot_trading.errors as errors
import octobot_trading.enums as trading_enums
import octobot_trading.constants as trading_constants
import tentacles.Meta.Keywords.scripting_library as scripting_library

Expand Down Expand Up @@ -102,6 +103,13 @@ async def test_orders_with_invalid_values(mock_context, skip_if_octobot_trading_
@pytest.mark.parametrize("backtesting_config", ["USDT"], indirect=["backtesting_config"])
async def test_orders_amount_then_position_sequence(mock_context):
initial_usdt_holdings, btc_price = await _usdt_trading_context(mock_context)
mock_context.exchange_manager.is_future = True
api.load_pair_contract(
mock_context.exchange_manager,
api.create_default_future_contract(
mock_context.symbol, decimal.Decimal(1), trading_enums.FutureContractType.LINEAR_PERPETUAL
).to_dict()
)

if os.getenv('CYTHON_IGNORE'):
return
Expand Down Expand Up @@ -193,7 +201,7 @@ async def test_concurrent_orders(mock_context):

# create 3 sell orders (at price = 500 + 10 = 510)
# that would end up selling more than what we have if not executed sequentially
# 1st order is 80% of position, second is 80% of the remaining 20% and so on
# 1st order is 80% of available btc, second is 80% of the remaining 20% and so on

orders = []
async def create_order(amount):
Expand All @@ -207,13 +215,13 @@ async def create_order(amount):
)
await asyncio.gather(
*(
create_order("80%p")
create_order("80%a")
for _ in range(3)
)
)

initial_btc_holdings = btc_val
btc_val = initial_btc_holdings * decimal.Decimal("0.2") ** 3 # 0.16
btc_val = initial_btc_holdings * (decimal.Decimal("0.2") ** 3)
usdt_val = usdt_val + (initial_btc_holdings - btc_val) * (btc_price + 10) # 50118.40
await _fill_and_check(mock_context, btc_val, usdt_val, orders, orders_count=3)

Expand Down

This file was deleted.

0 comments on commit 79471fd

Please sign in to comment.