Example #1
0
async def test_get_bankruptcy_price_with_short(
        future_trader_simulator_with_default_linear):
    config, exchange_manager_inst, trader_inst, default_contract = future_trader_simulator_with_default_linear

    position_inst = personal_data.LinearPosition(trader_inst, default_contract)
    position_inst.update_from_raw({
        enums.ExchangeConstantsPositionColumns.SYMBOL.value:
        DEFAULT_FUTURE_SYMBOL
    })
    position_inst.entry_price = constants.ONE_HUNDRED
    await position_inst.update(update_size=-constants.ONE_HUNDRED,
                               mark_price=constants.ONE_HUNDRED)
    assert position_inst.get_bankruptcy_price() == decimal.Decimal("200")
    assert position_inst.get_bankruptcy_price(
        with_mark_price=True) == decimal.Decimal("100")
    default_contract.set_current_leverage(constants.ONE_HUNDRED)
    assert position_inst.get_bankruptcy_price() == decimal.Decimal('101.00')
    assert position_inst.get_bankruptcy_price(
        with_mark_price=True) == decimal.Decimal('100')
    exchange_manager_inst.exchange_personal_data.portfolio_manager.portfolio.get_currency_portfolio(
        "USDT").wallet_balance = decimal.Decimal(
            10000)  # TO prevent portfolio negative error
    await position_inst.update(update_size=constants.ONE_HUNDRED,
                               mark_price=decimal.Decimal(2) *
                               constants.ONE_HUNDRED)
    assert position_inst.get_bankruptcy_price() == constants.ZERO
    assert position_inst.get_bankruptcy_price(
        with_mark_price=True) == constants.ZERO
Example #2
0
async def test_get_bankruptcy_price_with_long(
        future_trader_simulator_with_default_linear):
    config, exchange_manager_inst, trader_inst, default_contract = future_trader_simulator_with_default_linear

    position_inst = personal_data.LinearPosition(trader_inst, default_contract)
    position_inst.update_from_raw({
        enums.ExchangeConstantsPositionColumns.SYMBOL.value:
        DEFAULT_FUTURE_SYMBOL
    })
    position_inst.entry_price = constants.ONE_HUNDRED
    await position_inst.update(update_size=constants.ONE_HUNDRED,
                               mark_price=constants.ONE_HUNDRED)
    assert position_inst.get_bankruptcy_price() == constants.ZERO
    assert position_inst.get_bankruptcy_price(
        with_mark_price=True) == constants.ONE_HUNDRED
    default_contract.set_current_leverage(constants.ONE_HUNDRED)
    assert position_inst.get_bankruptcy_price() == decimal.Decimal("99.00")
    assert position_inst.get_bankruptcy_price(
        with_mark_price=True) == decimal.Decimal("100")
    await position_inst.update(update_size=constants.ONE_HUNDRED,
                               mark_price=decimal.Decimal(2) *
                               constants.ONE_HUNDRED)
    assert position_inst.get_bankruptcy_price() == decimal.Decimal("99.00")
    assert position_inst.get_bankruptcy_price(
        with_mark_price=True) == decimal.Decimal("200")
Example #3
0
async def test_get_fee_to_open(future_trader_simulator_with_default_linear):
    config, exchange_manager_inst, trader_inst, default_contract = future_trader_simulator_with_default_linear
    position_inst = personal_data.LinearPosition(trader_inst, default_contract)

    await position_inst.update(update_size=constants.ZERO,
                               mark_price=constants.ZERO)
    assert position_inst.get_fee_to_open() == constants.ZERO
    await position_inst.update(update_size=constants.ONE_HUNDRED,
                               mark_price=constants.ONE_HUNDRED)
    assert position_inst.get_fee_to_open() == decimal.Decimal("10.000")
Example #4
0
async def test_update_pnl_with_short_linear_position(
        future_trader_simulator_with_default_linear):
    config, exchange_manager_inst, trader_inst, default_contract = future_trader_simulator_with_default_linear

    position_inst = personal_data.LinearPosition(trader_inst, default_contract)
    position_inst.entry_price = constants.ONE_HUNDRED
    await position_inst.update(update_size=-constants.ONE_HUNDRED,
                               mark_price=constants.ONE_HUNDRED)
    position_inst.update_pnl()
    assert position_inst.unrealised_pnl == constants.ZERO
    await position_inst.update(update_size=-constants.ONE_HUNDRED,
                               mark_price=constants.ONE_HUNDRED /
                               decimal.Decimal(10))
    position_inst.update_pnl()
    assert position_inst.unrealised_pnl == decimal.Decimal("18000")
Example #5
0
async def test_update_initial_margin(
        future_trader_simulator_with_default_linear):
    config, exchange_manager_inst, trader_inst, default_contract = future_trader_simulator_with_default_linear
    position_inst = personal_data.LinearPosition(trader_inst, default_contract)

    if not os.getenv('CYTHON_IGNORE'):
        await position_inst.update(update_size=constants.ZERO,
                                   mark_price=constants.ZERO)
        position_inst._update_initial_margin()
        assert position_inst.initial_margin == constants.ZERO
        await position_inst.update(update_size=constants.ONE_HUNDRED,
                                   mark_price=constants.ONE_HUNDRED)
        position_inst._update_initial_margin()
        assert position_inst.initial_margin == decimal.Decimal("10000")
        default_contract.set_current_leverage(constants.ONE_HUNDRED)
        position_inst._update_initial_margin()
        assert position_inst.initial_margin == decimal.Decimal("100")
Example #6
0
async def test_update_average_entry_price_increased_short(
        future_trader_simulator_with_default_linear):
    config, exchange_manager_inst, trader_inst, default_contract = future_trader_simulator_with_default_linear
    position_inst = personal_data.LinearPosition(trader_inst, default_contract)

    await position_inst.update(update_size=-decimal.Decimal(10),
                               mark_price=decimal.Decimal(10))
    position_inst.entry_price = decimal.Decimal(10)
    position_inst.update_average_entry_price(-decimal.Decimal(10),
                                             decimal.Decimal(5))
    assert position_inst.entry_price == decimal.Decimal(7.5)
    position_inst.update_average_entry_price(-decimal.Decimal(100),
                                             decimal.Decimal(2))
    assert position_inst.entry_price == decimal.Decimal(2.5)
    position_inst.update_average_entry_price(-decimal.Decimal(2),
                                             decimal.Decimal(0.1))
    assert position_inst.entry_price == decimal.Decimal(
        "2.100000000000000000925185854")
Example #7
0
async def test_get_size_from_margin(
        future_trader_simulator_with_default_linear):
    config, exchange_manager_inst, trader_inst, default_contract = future_trader_simulator_with_default_linear
    position_inst = personal_data.LinearPosition(trader_inst, default_contract)

    await position_inst.update(update_size=constants.ONE_HUNDRED,
                               mark_price=constants.ONE_HUNDRED)
    assert position_inst.get_size_from_margin(
        constants.ONE) == decimal.Decimal('0.01')
    default_contract.set_current_leverage(constants.ONE_HUNDRED)
    assert position_inst.get_size_from_margin(constants.ONE) == constants.ONE
    default_contract.set_current_leverage(constants.ONE)
    assert position_inst.get_size_from_margin(
        decimal.Decimal('0.01')) == decimal.Decimal('0.0001')
    assert position_inst.get_size_from_margin(
        decimal.Decimal('0.1')) == decimal.Decimal('0.001')
    assert position_inst.get_size_from_margin(
        decimal.Decimal('1')) == decimal.Decimal('0.01')
Example #8
0
async def test_update_average_entry_price_decreased_long(
        future_trader_simulator_with_default_linear):
    config, exchange_manager_inst, trader_inst, default_contract = future_trader_simulator_with_default_linear
    position_inst = personal_data.LinearPosition(trader_inst, default_contract)

    await position_inst.update(update_size=decimal.Decimal(100),
                               mark_price=decimal.Decimal(10))
    position_inst.entry_price = decimal.Decimal(10)
    position_inst.update_average_entry_price(-decimal.Decimal(10),
                                             decimal.Decimal(20))
    assert position_inst.entry_price == decimal.Decimal(
        "8.888888888888888888888888889")
    position_inst.update_average_entry_price(-decimal.Decimal(50),
                                             decimal.Decimal(1.5))
    assert position_inst.entry_price == decimal.Decimal(
        "16.27777777777777777777777778")
    position_inst.update_average_entry_price(-decimal.Decimal(2),
                                             decimal.Decimal(7000))
    assert position_inst.entry_price == constants.ZERO
Example #9
0
async def test_update_average_entry_price_increased_long(
        future_trader_simulator_with_default_linear):
    config, exchange_manager_inst, trader_inst, default_contract = future_trader_simulator_with_default_linear
    position_inst = personal_data.LinearPosition(trader_inst, default_contract)

    await position_inst.update(update_size=decimal.Decimal(10),
                               mark_price=decimal.Decimal(10))
    position_inst.entry_price = decimal.Decimal(10)
    position_inst.update_average_entry_price(decimal.Decimal(10),
                                             decimal.Decimal(20))
    assert position_inst.entry_price == decimal.Decimal(15)
    position_inst.update_average_entry_price(decimal.Decimal(100),
                                             decimal.Decimal(20))
    assert position_inst.entry_price == decimal.Decimal(
        "19.54545454545454545454545455")
    position_inst.update_average_entry_price(decimal.Decimal(2),
                                             decimal.Decimal(500))
    assert position_inst.entry_price == decimal.Decimal(
        "99.62121212121212121212121217")
Example #10
0
async def test_update_average_entry_price_decreased_short(
        future_trader_simulator_with_default_linear):
    config, exchange_manager_inst, trader_inst, default_contract = future_trader_simulator_with_default_linear
    position_inst = personal_data.LinearPosition(trader_inst, default_contract)

    await position_inst.update(update_size=-decimal.Decimal(100),
                               mark_price=decimal.Decimal(10))
    position_inst.entry_price = decimal.Decimal(10)
    position_inst.update_average_entry_price(decimal.Decimal(10),
                                             decimal.Decimal(20))
    assert position_inst.entry_price == decimal.Decimal(
        "8.888888888888888888888888889")
    position_inst.update_average_entry_price(decimal.Decimal(100),
                                             decimal.Decimal('35.678'))
    assert position_inst.entry_price == decimal.Decimal(
        "2678.911111111111111111111111")
    position_inst.update_average_entry_price(
        decimal.Decimal(2), decimal.Decimal("0.0000000025428"))
    assert position_inst.entry_price == decimal.Decimal(
        "2733.582766439857403174603174")
Example #11
0
async def test_update_isolated_liquidation_price_with_short(
        future_trader_simulator_with_default_linear):
    config, exchange_manager_inst, trader_inst, default_contract = future_trader_simulator_with_default_linear
    exchange_manager_inst.exchange_symbols_data.get_exchange_symbol_data(
        DEFAULT_FUTURE_SYMBOL).funding_manager.funding_rate = decimal.Decimal(
            DEFAULT_FUTURE_FUNDING_RATE)

    position_inst = personal_data.LinearPosition(trader_inst, default_contract)
    position_inst.symbol = DEFAULT_FUTURE_SYMBOL
    position_inst.entry_price = constants.ONE_HUNDRED
    await position_inst.update(update_size=-constants.ONE_HUNDRED,
                               mark_price=constants.ONE_HUNDRED)
    position_inst.update_isolated_liquidation_price()
    assert position_inst.liquidation_price == decimal.Decimal("199.0")
    default_contract.set_current_leverage(constants.ONE_HUNDRED)
    await position_inst.update(update_size=-constants.ONE_HUNDRED,
                               mark_price=constants.ONE_HUNDRED /
                               decimal.Decimal(10))
    position_inst.update_isolated_liquidation_price()
    assert position_inst.liquidation_price == decimal.Decimal("100")
Example #12
0
async def test_calculate_maintenance_margin(
        future_trader_simulator_with_default_linear):
    config, exchange_manager_inst, trader_inst, default_contract = future_trader_simulator_with_default_linear
    position_inst = personal_data.LinearPosition(trader_inst, default_contract)

    position_inst.symbol = DEFAULT_FUTURE_SYMBOL
    await position_inst.update(update_size=constants.ZERO,
                               mark_price=constants.ZERO)
    assert position_inst.calculate_maintenance_margin() == constants.ZERO
    await position_inst.update(update_size=constants.ONE_HUNDRED,
                               mark_price=constants.ONE_HUNDRED)
    assert position_inst.calculate_maintenance_margin(
    ) == constants.ONE_HUNDRED
    exchange_manager_inst.exchange_symbols_data.get_exchange_symbol_data(
        DEFAULT_FUTURE_SYMBOL).funding_manager.funding_rate = decimal.Decimal(
            DEFAULT_FUTURE_FUNDING_RATE)
    await position_inst.update(update_size=constants.ONE_HUNDRED,
                               mark_price=constants.ONE_HUNDRED)
    assert position_inst.calculate_maintenance_margin() == decimal.Decimal(
        '200')
Example #13
0
async def test_update_pnl_with_loss_with_long_linear_position(
        future_trader_simulator_with_default_linear):
    config, exchange_manager_inst, trader_inst, default_contract = future_trader_simulator_with_default_linear

    position_inst = personal_data.LinearPosition(trader_inst, default_contract)
    position_inst.update_from_raw({
        enums.ExchangeConstantsPositionColumns.SYMBOL.value:
        DEFAULT_FUTURE_SYMBOL
    })
    position_inst.entry_price = constants.ONE_HUNDRED
    await position_inst.update(update_size=constants.ONE_HUNDRED,
                               mark_price=constants.ONE_HUNDRED)
    position_inst.update_pnl()
    assert position_inst.unrealised_pnl == constants.ZERO
    exchange_manager_inst.exchange_personal_data.portfolio_manager.portfolio.get_currency_portfolio(
        "USDT").wallet_balance = decimal.Decimal(
            100000)  # TO prevent portfolio negative error
    await position_inst.update(update_size=constants.ONE_HUNDRED,
                               mark_price=constants.ONE_HUNDRED /
                               decimal.Decimal(2.535485))
    position_inst.update_pnl()
    assert position_inst.unrealised_pnl == decimal.Decimal(
        "-12111.96280001656484319345490")