def get_all_active_upbit_accounts(self,
                                      alias: Optional[str] = None
                                      ) -> List[Account]:
        db: Session
        accounts: List[AccountEntity]

        with session_scope(self.session) as db:
            start_of_day = datetime.now().replace(hour=0,
                                                  minute=0,
                                                  second=0,
                                                  microsecond=0)

            filters = [
                AccountEntity.enabled,
                AccountEntity.expired_at >= start_of_day,
                AccountEntity.vendor == "upbit"
            ]

            if alias:
                filters.append(AccountEntity.alias == alias)

            query = db.query(AccountEntity) \
                .filter(*filters)

            accounts = query.all()

        return [
            AccountEntityAdapter(self.__decrypt(account))
            for account in accounts
        ]
Beispiel #2
0
    def get_open_orders(self, account_id: int, count: int) -> List[Order]:
        db: Session

        with session_scope(self.session) as db:
            open_orders = db.query(OrderEntity)\
                .filter(OrderEntity.account_id == account_id, OrderEntity.status == "wait")\
                .limit(count)\
                .all()

        return [OrderEntityAdapter(order) for order in open_orders]
    def get_active_account(self) -> Optional[Account]:
        db: Session
        account: AccountEntity

        with session_scope(self.session) as db:
            account = db.query(AccountEntity) \
                .filter_by(enabled=True) \
                .first()

        return AccountEntityAdapter(
            self.__decrypt(account)) if account is not None else None
Beispiel #4
0
    def get_candles(self, ticker: str, count: int) -> List[Candle]:
        db: Session
        candles: List[CandleEntity]

        with session_scope(self.session) as db:
            candles = db \
                .query(CandleEntity) \
                .filter_by(ticker=ticker) \
                .order_by(CandleEntity.closed_at.desc()) \
                .limit(count) \
                .all()

        return [CandleEntityAdapter(candle) for candle in candles]
Beispiel #5
0
 def add_candles(self, candles: List[Candle]):
     with session_scope(self.session) as db:
         values = [
             dict(
                 ticker=candle.get_ticker(),
                 closed_at=candle.get_closed_at(),
                 open=candle.get_open_price(),
                 high=candle.get_high_price(),
                 low=candle.get_low_price(),
                 close=candle.get_close_price(),
                 volume=candle.get_volume()
             ) for candle in candles
         ]
         db.execute(candle_table.insert(on_duplicate_key_update={"id": "candle.id"}), values)
Beispiel #6
0
    def finish_order(self, id: str, order: Order) -> None:
        db: Session

        with session_scope(self.session) as db:
            db.query(OrderEntity)\
                .filter(OrderEntity.id == id)\
                .update(
                    dict(
                        avg_price=order.get_avg_price(),
                        volume=order.get_volume(),
                        amount=order.get_amount(),
                        raw_data=order.get_raw_data(),
                        status="filled"
                    )
                )
Beispiel #7
0
    def get_last_order(self, ticker: str, strategy: str,
                       account_id: int) -> Optional[Order]:
        db: Session

        with session_scope(self.session) as db:
            max_created_at = (db.query(func.max(
                OrderEntity.created_at)).filter_by(
                    ticker=ticker, strategy=strategy,
                    account_id=account_id).subquery())
            order_entity = db.query(OrderEntity) \
                .filter_by(created_at=max_created_at, ticker=ticker, strategy=strategy,
                           account_id=account_id) \
                .first()

        return OrderEntityAdapter(order_entity) if order_entity else None
Beispiel #8
0
    def add_order(self, order: Order, meta: OrderMeta) -> None:
        db: Session

        with session_scope(self.session) as db:
            order = OrderEntity(id=order.get_id(),
                                exchange=order.get_exchange(),
                                type=order.get_order_type(),
                                ticker=order.get_ticker(),
                                strategy=meta.strategy,
                                avg_price=order.get_avg_price(),
                                volume=order.get_volume(),
                                amount=order.get_amount(),
                                raw_data=order.get_raw_data(),
                                account_id=meta.account_id,
                                status="wait")
            db.add(order)
    def add_account(self, vendor: str, access_key: str, secret_key: str,
                    expired_at: datetime, alias: str):
        db: Session
        account: AccountEntity

        data_key, plain_key = self.kms.create_data_key()
        key = plain_key.decode("utf-8")

        access_key_enc = aes.encrypt(plaintext=access_key, key=key)
        secret_key_enc = aes.encrypt(plaintext=secret_key, key=key)

        with session_scope(self.session) as db:
            account = AccountEntity(vendor=vendor,
                                    enabled=True,
                                    access_key=access_key_enc,
                                    secret_key=secret_key_enc,
                                    expired_at=expired_at,
                                    alias=alias,
                                    data_key=data_key)
            db.add(account)
Beispiel #10
0
    def update_credential(self, id: int, access_key: str, secret_key: str,
                          expired_at: datetime):
        db: Session

        data_key, plain_key = self.kms.create_data_key()
        key = plain_key.decode("utf-8")

        access_key_enc = aes.encrypt(plaintext=access_key, key=key)
        secret_key_enc = aes.encrypt(plaintext=secret_key, key=key)

        with session_scope(self.session) as db:
            db.query(AccountEntity)\
                .filter(AccountEntity.id == id)\
                .update(
                    dict(
                        access_key=access_key_enc,
                        secret_key=secret_key_enc,
                        expired_at=expired_at,
                        data_key=data_key
                    )
                )