def test_can_calculate_free_equity_when_zero(self): # Arrange event = AccountStateEvent( AccountId.py_from_string('FXCM-123456-SIMULATED'), Currency.AUD, Money(20000, Currency.AUD), Money(100000, Currency.AUD), Money(0, Currency.AUD), Money(0, Currency.AUD), Money(20000, Currency.AUD), Decimal(0), ValidString('N'), GUID(uuid.uuid4()), UNIX_EPOCH) # Act account = Account(event) # Assert self.assertEqual(AccountId.py_from_string('FXCM-123456-SIMULATED'), account.id) self.assertEqual(Brokerage('FXCM'), account.broker) self.assertEqual(AccountNumber('123456'), account.account_number) self.assertEqual(Currency.AUD, account.currency) self.assertEqual(Money(0, Currency.AUD), account.free_equity) self.assertEqual(Money(100000, Currency.AUD), account.cash_start_day) self.assertEqual(Money(0, Currency.AUD), account.cash_activity_day) self.assertEqual(Money(0, Currency.AUD), account.margin_used_liquidation) self.assertEqual(Money(20000, Currency.AUD), account.margin_used_maintenance) self.assertEqual(Decimal(0), account.margin_ratio) self.assertEqual('N', account.margin_call_status.value) self.assertEqual(UNIX_EPOCH, account.last_updated)
def test_initialized_with_no_value_returns_valid_decimal(self): # Arrange # Act result = Decimal() # Assert self.assertEqual(0, result) self.assertEqual(0, result.precision) self.assertEqual(decimal.Decimal('0'), result.as_decimal()) self.assertEqual(0, result.as_double())
def test_decimal_multiplication(self): # Arrange # Act result0 = Decimal(1.00001, 5) * 2.0 result1 = Decimal(1.00001, 5) * Decimal(1.5000, 5) # Assert self.assertEqual(float, type(result0)) self.assertEqual(float, type(result1)) self.assertEqual(2.00002, result0) self.assertEqual(1.500015, result1)
def test_decimal_division(self): # Arrange # Act result0 = Decimal(1.00001, 5) / 2.0 result1 = Decimal(1.00001, 5) / Decimal(0.5000, 5) # Assert self.assertEqual(float, type(result0)) self.assertEqual(float, type(result1)) self.assertEqual(0.500005, result0) self.assertEqual(2.00002, result1) self.assertEqual(result0, Decimal(1.00001, 5) / Decimal(2.0, 1))
def test_initialized_with_many_scientific_notation_returns_zero(self): # Arrange # Act result1 = Decimal(0E-30) result2 = Decimal(-0E-33) # Assert self.assertEqual(0.0, result1.as_double()) self.assertEqual(0.0, result2.as_double()) self.assertEqual(decimal.Decimal('0'), result1.as_decimal()) self.assertEqual(decimal.Decimal('0'), result2.as_decimal())
def test_decimal_addition(self): # Arrange # Act result0 = Decimal(1.00001, 5) + 0.00001 result1 = Decimal(1.00001, 5) + Decimal(0.00001, 5) result3 = Decimal(1.00001, 5).add_decimal(Decimal(0.00001, 5)) result4 = Decimal(1.00001, 5).add_decimal(Decimal(0.5, 1)) # Assert self.assertEqual(float, type(result0)) self.assertEqual(float, type(result1)) self.assertEqual(Decimal, type(result3)) self.assertEqual(Decimal, type(result4)) self.assertEqual(1.0000200000000001, result0) self.assertEqual(1.0000200000000001, result1) self.assertEqual(Decimal(1.00002, 5), result3) self.assertEqual(Decimal(1.50001, 5), result4)
def test_decimal_subtraction(self): # Arrange # Act result0 = Decimal(1.00001, 5) - 0.00001 result1 = Decimal(1.00001, 5) - Decimal(0.00001, 5) result3 = Decimal(1.00001, 5).subtract_decimal(Decimal(0.00001, 5)) result4 = Decimal(1.00001, 5).subtract_decimal(Decimal(0.5, 1)) # Assert self.assertEqual(float, type(result0)) self.assertEqual(float, type(result1)) self.assertEqual(Decimal, type(result3)) self.assertEqual(Decimal, type(result4)) self.assertEqual(1.0, result0) self.assertEqual(1.0, result1) self.assertEqual(result0, result1) self.assertEqual(Decimal(1.00000, 5), result3) self.assertEqual(Decimal(0.50001, 5), result4)
def test_default_fx_with_3_dp_returns_expected_instrument(self): # Arrange loader = InstrumentLoader() # Act instrument = loader.default_fx_ccy(Symbol('USDJPY', Venue('DUKASCOPY'))) # Assert self.assertEqual(Symbol('USDJPY', Venue('DUKASCOPY')), instrument.symbol) self.assertEqual('USD/JPY', instrument.broker_symbol) self.assertEqual(3, instrument.price_precision) self.assertEqual(Decimal(0.001, 3), instrument.tick_size) self.assertEqual(392, instrument.quote_currency)
def test_can_add_account(self): # Arrange event = AccountStateEvent( AccountId.py_from_string('SIMULATED-123456-SIMULATED'), Currency.USD, Money(1000000, Currency.USD), Money(1000000, Currency.USD), Money(0, Currency.USD), Money(0, Currency.USD), Money(0, Currency.USD), Decimal(0), ValidString('N'), GUID(uuid.uuid4()), UNIX_EPOCH) account = Account(event) # Act self.database.add_account(account) # Assert self.assertTrue(True) # Did not raise exception
def test_can_apply_order_filled_event_to_buy_limit_order(self): # Arrange order = self.order_factory.limit(AUDUSD_FXCM, OrderSide.BUY, Quantity(100000), Price(1.00000, 5)) event = OrderFilled(self.account_id, order.id, ExecutionId('SOME_EXEC_ID_1'), PositionIdBroker('SOME_EXEC_TICKET_1'), order.symbol, order.side, order.quantity, Price(1.00001, 5), Currency.USD, UNIX_EPOCH, GUID(uuid.uuid4()), UNIX_EPOCH) # Act order.apply(event) # Assert self.assertEqual(OrderState.FILLED, order.state) self.assertEqual(Quantity(100000), order.filled_quantity) self.assertEqual(Price(1.00000, 5), order.price) self.assertEqual(Price(1.00001, 5), order.average_price) self.assertEqual(Decimal(0.00001, 5), order.slippage) self.assertTrue(order.is_completed) self.assertEqual(UNIX_EPOCH, order.filled_timestamp)
def test_initialized_with_valid_inputs(self): # Arrange # Act result0 = Decimal(1.0, 1) result1 = Decimal(1.0, 2) result2 = Decimal(-1.001, 3) result3 = Decimal(1.0005, 3) result4 = Decimal(100) # Assert self.assertEqual(decimal.Decimal('1'), result0.as_decimal()) self.assertEqual(decimal.Decimal('1.00'), result1.as_decimal()) self.assertEqual(decimal.Decimal('-1.001'), result2.as_decimal()) self.assertEqual(decimal.Decimal('1.001'), result3.as_decimal()) # Rounds up self.assertEqual(100, result4) self.assertEqual(1, result0.as_double()) self.assertEqual(1, result0.as_double()) self.assertEqual(-1.001, result2.as_double()) self.assertEqual(1.001, result3.as_double()) self.assertEqual('1.0', result0.to_string()) self.assertEqual('1.00', result1.to_string()) self.assertEqual('-1.001', result2.to_string()) self.assertEqual('1.001', result3.to_string())
def make_decimal(): Decimal(1.23456, 5)
# limitations under the License. # ------------------------------------------------------------------------------------------------- import decimal import unittest from nautilus_trader.core.decimal import Decimal from nautilus_trader.model.objects import Price from tests.test_kit.performance import PerformanceHarness _PRECISION_5_CONTEXT = decimal.Context(prec=5) _BUILTIN_DECIMAL1 = decimal.Decimal('1.00000') _BUILTIN_DECIMAL2 = decimal.Decimal('1.00001') _DECIMAL1 = Decimal(1.00000, 5) _DECIMAL2 = Decimal(1.00001, 5) class DecimalTesting: @staticmethod def make_builtin_decimal(): decimal.Decimal('1.23456') @staticmethod def make_decimal(): Decimal(1.23456, 5) @staticmethod def float_comparisons(): # x1 = 1.0 > 2.0