Пример #1
0
 def setUp(self):
     self.user = UserFactory()
     self.order_1 = OrderFactory()
     self.order_2 = OrderFactory()
     self.order_3 = OrderFactory()
     self.order_4 = OrderFactory()
     self.asset = AssetFactory(symbol="TEST")
     self.strategy = StrategyFactory(asset=self.asset)
Пример #2
0
    def test_update_bars(self, mock_api):
        """Latest bars for list of symbols is fetched and saved."""
        mock_api().get_bars.return_value = self.bars

        tesla = AssetFactory(symbol="TSLA")
        microsoft = AssetFactory(symbol="AAPL")
        symbols = [tesla.symbol, microsoft.symbol]
        bars = update_bars(symbols=symbols, timeframe="1D", limit=2)

        # Bars are fetched
        self.assertEqual(len(bars), 2)
        self.assertTrue(isinstance(bars["TSLA"][0], AlpacaBar))
        self.assertTrue(isinstance(bars["AAPL"][0], AlpacaBar))

        # Bars are saved to db correctly
        self.assertEqual(Bar.objects.filter(asset=tesla).count(), 2)
        self.assertEqual(Bar.objects.filter(asset=microsoft).count(), 2)
Пример #3
0
    def test_create_invalid_strategy_order(self):
        """An order asset_id and strategy asset must be the same."""
        invalid_asset = AssetFactory()

        with self.assertRaisesRegex(
                ValidationError,
                "Strategy ``asset`` and order ``asset_id`` must be the same",
        ):
            OrderFactory(strategy=self.strategy, asset_id=invalid_asset)
Пример #4
0
 def setUp(self):
     self.admin = AdminFactory()
     self.user = UserFactory()
     self.asset = AssetFactory(symbol="AAPL")
     self.strategy = StrategyFactory(asset=self.asset)
     self.client.credentials(HTTP_AUTHORIZATION="Token " + self.admin.auth_token.key)
     self.order_1 = OrderFactory(
         user=self.user, strategy=self.strategy, asset_id=self.asset
     )
     self.order_2 = OrderFactory(
         user=self.user, strategy=self.strategy, asset_id=self.asset
     )
     self.order_3 = OrderFactory()
     self.order_4 = OrderFactory()
Пример #5
0
 def setUp(self):
     self.tsla = AssetFactory(symbol="TSLA")
     self.user_1 = UserFactory()
     self.user_2 = UserFactory()
     self.user_3 = UserFactory()
     self.strategy_1 = StrategyFactory(asset=self.tsla,
                                       user=self.user_1,
                                       trade_value=1000)
     self.strategy_2 = StrategyFactory(asset=self.tsla,
                                       user=self.user_2,
                                       trade_value=1000)
     self.inactive_strategy = StrategyFactory(
         user=self.user_3,
         start_date=timezone.now() - timedelta(days=7),
         end_date=timezone.now() - timedelta(days=5),
     )
Пример #6
0
 def setUp(self):
     self.admin = AdminFactory()
     self.client.credentials(HTTP_AUTHORIZATION="Token " +
                             self.admin.auth_token.key)
     self.asset = AssetFactory(symbol="AAPL")
     self.bar_1 = BarFactory(asset=self.asset, t=100000)
     self.bar_2 = BarFactory(asset=self.asset, t=150000)
     self.bar_3 = BarFactory(asset=self.asset, t=200000)
     self.data = {
         "asset": self.asset.symbol,
         "t": 2100000000,
         "o": 100.01,
         "h": 110.05,
         "l": 99.05,
         "c": 105.01,
         "v": 200000,
     }
Пример #7
0
class VideoFactory(factory.django.DjangoModelFactory):
    """Default Video factory."""
    class Meta(object):
        """VideoFactory Meta class."""

        model = Video

    class Params(object):
        """VideoFactory Params class.

        user is the shared user owning the asset and the session.
        """

        user = factory.SubFactory(UserFactory)

    session = factory.LazyAttribute(lambda o: SessionFactory(owner=o.user))
    asset = factory.LazyAttribute(lambda o: AssetFactory(owner=o.user))
    start_date = make_aware(datetime.datetime(2018, 3, 7, 11, 37))
    index = 0
Пример #8
0
 def setUp(self):
     self.admin = AdminFactory()
     self.client.credentials(HTTP_AUTHORIZATION="Token " +
                             self.admin.auth_token.key)
     self.exchange = ExchangeFactory()
     self.asset_class = AssetClassFactory()
     self.asset = AssetFactory(name="Asset",
                               exchange=self.exchange,
                               asset_class=self.asset_class)
     self.data = {
         "id": str(uuid.uuid4()),
         "name": "Asset Name",
         "asset_class": self.asset_class.name,
         "easy_to_borrow": True,
         "exchange": self.exchange.name,
         "marginable": True,
         "shortable": True,
         "status": Asset.ACTIVE,
         "symbol": "SYMBOL",
         "tradable": True,
     }
Пример #9
0
 def setUp(self):
     self.user = UserFactory()
     self.strategy = StrategyFactory()
     self.asset_1 = AssetFactory(symbol="AAPL")
     self.asset_2 = AssetFactory(symbol="TLSA")
     self.asset_3 = AssetFactory(symbol="MFST")
     self.uuid_1 = str(uuid.uuid4())
     self.uuid_2 = str(uuid.uuid4())
     self.uuid_3 = str(uuid.uuid4())
     self.uuid_4 = str(uuid.uuid4())
     self.uuid_5 = str(uuid.uuid4())
     self.uuid_6 = str(uuid.uuid4())
     self.order_data = [
         AlpacaOrder(
             {
                 "status": "filled",
                 "asset_id": self.asset_1.pk,
                 "symbol": self.asset_1.symbol,
                 "asset_class": self.asset_1.asset_class.name,
                 "qty": 10.01,
                 "filled_qty": 10.01,
                 "side": "buy",
                 "type": "market",
                 "time_in_force": "day",
                 "client_order_id": self.uuid_1,
                 "id": self.uuid_2,
                 "created_at": str(timezone.now()),
             }
         ),
         AlpacaOrder(
             {
                 "status": "filled",
                 "asset_id": self.asset_2.pk,
                 "symbol": self.asset_2.symbol,
                 "asset_class": self.asset_1.asset_class.name,
                 "qty": 101.05,
                 "filled_qty": 10.01,
                 "side": "buy",
                 "type": "market",
                 "time_in_force": "day",
                 "client_order_id": self.uuid_3,
                 "id": self.uuid_4,
                 "created_at": str(timezone.now()),
             }
         ),
         AlpacaOrder(
             {
                 "status": "filled",
                 "asset_id": self.asset_3.pk,
                 "symbol": self.asset_3.symbol,
                 "asset_class": self.asset_1.asset_class.name,
                 "qty": 105.08,
                 "filled_qty": 10.01,
                 "side": "buy",
                 "type": "market",
                 "time_in_force": "day",
                 "client_order_id": self.uuid_5,
                 "id": self.uuid_6,
                 "created_at": str(timezone.now()),
             }
         ),
     ]
Пример #10
0
 def setUp(self):
     self.user = UserFactory()
     self.asset = AssetFactory(symbol="AAPL")
     self.strategy = StrategyFactory(asset=self.asset)
     self.order = OrderFactory(strategy=self.strategy, asset_id=self.asset)
Пример #11
0
 def setUp(self):
     self.user = UserFactory()
     self.asset = AssetFactory()