|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from core.models import AssetGroup, AssetRule, User
|
|
|
|
from core.trading import assetfilter
|
|
|
|
|
|
|
|
|
|
|
|
class AssetfilterTestCase(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.user = User.objects.create_user(
|
|
|
|
username="testuser",
|
|
|
|
email="test@example.com",
|
|
|
|
)
|
|
|
|
self.group = AssetGroup.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
name="Group1",
|
|
|
|
description="Test group",
|
|
|
|
when_no_data=6,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_get_allowed_prohibited(self):
|
|
|
|
"""
|
|
|
|
Test that the asset filter works on negative aggregations.
|
|
|
|
"""
|
|
|
|
# We have negative news about EUR
|
|
|
|
self.asset_rule = AssetRule.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
group=self.group,
|
|
|
|
asset="EUR",
|
|
|
|
status=3,
|
|
|
|
)
|
|
|
|
self.asset_rule.save()
|
|
|
|
|
|
|
|
# This means that:
|
|
|
|
# * base == EUR: long is not allowed, short is allowed
|
|
|
|
# * quote == EUR: long is allowed, short is not allowed
|
|
|
|
# We are betting on it going down, so we short base, and long quote.
|
|
|
|
|
|
|
|
# Test that short on base of EUR is allowed
|
|
|
|
self.assertTrue(assetfilter.get_allowed(self.group, "EUR", "USD", "short"))
|
|
|
|
|
|
|
|
# Test that long on quote of EUR is allowed
|
|
|
|
self.assertTrue(assetfilter.get_allowed(self.group, "USD", "EUR", "long"))
|
|
|
|
|
|
|
|
# Test that long on base of EUR is not allowed
|
|
|
|
self.assertFalse(assetfilter.get_allowed(self.group, "EUR", "USD", "long"))
|
|
|
|
|
|
|
|
# Test that short on quote of EUR is not allowed
|
|
|
|
self.assertFalse(assetfilter.get_allowed(self.group, "USD", "EUR", "short"))
|
|
|
|
|
|
|
|
def test_get_allowed_permitted(self):
|
|
|
|
"""
|
|
|
|
Test that the asset filter works on positive aggregations.
|
|
|
|
"""
|
|
|
|
# We have positive news about EUR
|
|
|
|
self.asset_rule = AssetRule.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
group=self.group,
|
|
|
|
asset="EUR",
|
|
|
|
status=2,
|
|
|
|
)
|
|
|
|
self.asset_rule.save()
|
|
|
|
|
|
|
|
# This means that:
|
|
|
|
# * base == EUR: long is allowed, short is not allowed
|
|
|
|
# * quote == EUR: long is not allowed, short is allowed
|
|
|
|
# We are betting on it going up, so we long base, and short quote.
|
|
|
|
|
|
|
|
# Test that long on base of EUR is allowed
|
|
|
|
self.assertTrue(assetfilter.get_allowed(self.group, "EUR", "USD", "long"))
|
|
|
|
|
|
|
|
# Test that short on quote of EUR is allowed
|
|
|
|
self.assertTrue(assetfilter.get_allowed(self.group, "USD", "EUR", "short"))
|
|
|
|
|
|
|
|
# Test that short on base of EUR is not allowed
|
|
|
|
self.assertFalse(assetfilter.get_allowed(self.group, "EUR", "USD", "short"))
|
|
|
|
|
|
|
|
# Test that long on quote of EUR is not allowed
|
|
|
|
self.assertFalse(assetfilter.get_allowed(self.group, "USD", "EUR", "long"))
|
|
|
|
|
|
|
|
def test_get_allowed_no_data(self):
|
|
|
|
self.asset_rule = AssetRule.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
group=self.group,
|
|
|
|
asset="EUR",
|
|
|
|
status=0,
|
|
|
|
)
|
|
|
|
self.asset_rule.save()
|
|
|
|
|
|
|
|
self.assertTrue(assetfilter.get_allowed(self.group, "EUR", "USD", "long"))
|
|
|
|
|
|
|
|
def test_get_allowed_no_data_prohibited(self):
|
|
|
|
self.group.when_no_data = 7
|
|
|
|
self.group.save()
|
|
|
|
self.asset_rule = AssetRule.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
group=self.group,
|
|
|
|
asset="EUR",
|
|
|
|
status=0,
|
|
|
|
)
|
|
|
|
self.asset_rule.save()
|
|
|
|
self.assertFalse(assetfilter.get_allowed(self.group, "EUR", "USD", "long"))
|
|
|
|
|
|
|
|
def test_get_allowed_no_match(self):
|
|
|
|
self.asset_rule = AssetRule.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
group=self.group,
|
|
|
|
asset="EUR",
|
|
|
|
status=1,
|
|
|
|
)
|
|
|
|
self.asset_rule.save()
|
|
|
|
|
|
|
|
self.assertTrue(assetfilter.get_allowed(self.group, "EUR", "USD", "long"))
|
|
|
|
|
|
|
|
def test_get_allowed_no_aggregation(self):
|
|
|
|
self.asset_rule = AssetRule.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
group=self.group,
|
|
|
|
asset="EUR",
|
|
|
|
status=4,
|
|
|
|
)
|
|
|
|
self.asset_rule.save()
|
|
|
|
|
|
|
|
self.assertTrue(assetfilter.get_allowed(self.group, "EUR", "USD", "long"))
|
|
|
|
|
|
|
|
def test_get_allowed_not_in_bounds(self):
|
|
|
|
self.asset_rule = AssetRule.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
group=self.group,
|
|
|
|
asset="EUR",
|
|
|
|
status=5,
|
|
|
|
)
|
|
|
|
self.asset_rule.save()
|
|
|
|
|
|
|
|
self.assertTrue(assetfilter.get_allowed(self.group, "EUR", "USD", "long"))
|
|
|
|
|
|
|
|
def test_get_allowed_always_allow(self):
|
|
|
|
self.asset_rule = AssetRule.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
group=self.group,
|
|
|
|
asset="EUR",
|
|
|
|
status=6,
|
|
|
|
)
|
|
|
|
self.asset_rule.save()
|
|
|
|
|
|
|
|
self.assertTrue(assetfilter.get_allowed(self.group, "EUR", "USD", "long"))
|
|
|
|
|
|
|
|
def test_get_allowed_always_deny(self):
|
|
|
|
self.asset_rule = AssetRule.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
group=self.group,
|
|
|
|
asset="EUR",
|
|
|
|
status=7,
|
|
|
|
)
|
|
|
|
self.asset_rule.save()
|
|
|
|
|
|
|
|
self.assertFalse(assetfilter.get_allowed(self.group, "EUR", "USD", "long"))
|
|
|
|
|
|
|
|
def test_check_asset_aggregation(self):
|
|
|
|
"""
|
|
|
|
Test that the asset aggregation works.
|
|
|
|
"""
|
|
|
|
# Test within lower bound
|
|
|
|
self.assertEqual(
|
|
|
|
assetfilter.check_asset_aggregation(
|
|
|
|
1.0, trigger_above=None, trigger_below=2.0
|
|
|
|
),
|
|
|
|
3, # Prohibited
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test within upper bound
|
|
|
|
self.assertEqual(
|
|
|
|
assetfilter.check_asset_aggregation(
|
|
|
|
1.0, trigger_above=0.0, trigger_below=None
|
|
|
|
),
|
|
|
|
2, # Permitted
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test within bounds
|
|
|
|
self.assertEqual(
|
|
|
|
assetfilter.check_asset_aggregation(
|
|
|
|
1.0, trigger_above=0.0, trigger_below=2.0
|
|
|
|
),
|
|
|
|
3, # Prohibited
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test outside lower bound
|
|
|
|
self.assertEqual(
|
|
|
|
assetfilter.check_asset_aggregation(
|
|
|
|
1.0, trigger_above=None, trigger_below=0.0
|
|
|
|
),
|
|
|
|
5, # Not in bounds
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test outside upper bound
|
|
|
|
self.assertEqual(
|
|
|
|
assetfilter.check_asset_aggregation(
|
|
|
|
1.0, trigger_above=2.0, trigger_below=None
|
|
|
|
),
|
|
|
|
5, # Not in bounds
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test no bounds, just to be sure
|
|
|
|
self.assertEqual(
|
|
|
|
assetfilter.check_asset_aggregation(
|
|
|
|
1.0, trigger_above=None, trigger_below=None
|
|
|
|
),
|
|
|
|
4, # No aggregation
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test both bounds, but inverted
|
|
|
|
self.assertEqual(
|
|
|
|
assetfilter.check_asset_aggregation(
|
|
|
|
1.0, trigger_above=2.0, trigger_below=0.0
|
|
|
|
),
|
|
|
|
5, # Not in bounds
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test within negative and positive bounds
|
|
|
|
self.assertEqual(
|
|
|
|
assetfilter.check_asset_aggregation(
|
|
|
|
-1.0, trigger_above=-2.0, trigger_below=0.0
|
|
|
|
),
|
|
|
|
3, # Prohibited
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_update_status_from_mappings_when_no_data(self):
|
|
|
|
self.group.when_no_data = 2 # Bullish
|
|
|
|
self.group.save()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
assetfilter.update_status_from_mappings(0, self.group),
|
|
|
|
2, # Bullish
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_update_status_from_mappings_when_no_match(self):
|
|
|
|
self.group.when_no_match = 2
|
|
|
|
self.group.save()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
assetfilter.update_status_from_mappings(1, self.group),
|
|
|
|
2, # Bullish
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_update_status_from_mappings_when_no_aggregation(self):
|
|
|
|
self.group.when_no_aggregation = 2
|
|
|
|
self.group.save()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
assetfilter.update_status_from_mappings(4, self.group),
|
|
|
|
2, # Bullish
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_update_status_from_mappings_when_not_in_bounds(self):
|
|
|
|
self.group.when_not_in_bounds = 2
|
|
|
|
self.group.save()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
assetfilter.update_status_from_mappings(5, self.group),
|
|
|
|
2, # Bullish
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_update_status_from_mappings_when_bullish(self):
|
|
|
|
self.group.when_bullish = 3 # Bearish
|
|
|
|
self.group.save()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
assetfilter.update_status_from_mappings(2, self.group),
|
|
|
|
3, # Bearish
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_update_status_from_mappings_when_bearish(self):
|
|
|
|
self.group.when_bearish = 2 # Bullish
|
|
|
|
self.group.save()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
assetfilter.update_status_from_mappings(3, self.group),
|
|
|
|
2, # Bullish
|
|
|
|
)
|