async def test_inserting_new_transaction(pg_engine): async with pg_engine.acquire() as conn: date = get_midnight(datetime.now()) new_transaction = NewTransaction( amount=100, rate_buy=27.8, rate_sale=27.7, date=date, ) r = await insert_new_transaction(conn, new_transaction) assert r
async def get_daily_bids(conn, *, bid_type: BidType = None, statuses: Iterable[BidStatus] = None): datetime_today = datetime.now() datetime_tomorrow = datetime_today + timedelta(days=1) midnight_today = get_midnight(datetime_today) midnight_tomorrow = get_midnight(datetime_tomorrow) query = bid.select().where( sa.and_( bid.c.created > midnight_today, bid.c.created <= midnight_tomorrow, )) if bid_type is not None: query = query.where(bid.c.bid_type == bid_type.value) if statuses is not None: status_values = get_statuses(*statuses) query = query.where(bid.c.status.in_(status_values)) result = await conn.execute(query) return await result.fetchall()
async def test_updating_existing_transaction(pg_engine): date = get_midnight(datetime.now()) async with pg_engine.acquire() as conn: new_transaction = NewTransaction( amount=100, rate_buy=27.8, rate_sale=27.7, date=date, ) r = await insert_new_transaction(conn, new_transaction) t_id = r[0] query = transaction.update() \ .where(transaction.c.id == t_id) \ .values(amount=150) r = await conn.execute(query) assert r.rowcount == 1
async def trade(self, daily_data): date = daily_data['date'] # our perspective rate_sale = daily_data['buy'] rate_buy = daily_data['sale'] bank = 'privatbank' self.logger.info('Trading on %.2f/%.2f', rate_sale, rate_buy) await notify(f'{DOLLAR_ICON} {rate_sale:.2f}/{rate_buy:.2f}') transactions = await self.hanging() self.logger.info('Handling hanging transactions') for t in transactions: open_date = get_midnight(t.date_opened) if (open_date < date and rate_sale > t.rate_buy # todo: we can add coefficient here ): amount = await self.sale_transaction(t, rate_sale) await notify(format_message('sale', amount, rate_sale, bank)) # buy some amount of currency t = NewTransaction( rate_buy=rate_buy, rate_sale=rate_sale, amount=self.daily_amount, date=date, ) # if debt < 0: # raise ValueError( # 'Cannot buy {:.2f}$. Available: {:.2f}UAH'.format(self.daily_amount, self.amount)) self.amount -= t.price await notify(format_message('buy', self.daily_amount, rate_buy, bank)) await self.add_transaction(t) self.logger.info('Amount in the end of the day: %.2f', self.amount) potential = await self.get_potential(rate_sale) self.logger.info('Potential is %.2f', potential) # todo: self.notifier.notify_telegram(message) await self.notify(f'Current potential is {potential}')
async def daily(self): # fetch rate from cache now = datetime.now() key = get_date_cache_key(now) data = await self.cache.get(key) # bank's perspective sale = None buy = None # todo: iterate banks, iterate currencies for item in data: if item['ccy'] == 'USD': sale = float(item['sale']) buy = float(item['buy']) break daily_data = { 'date': get_midnight(now), 'sale': sale, 'buy': buy, } await self.trade(daily_data=daily_data)
from datetime import datetime import sqlalchemy as sa from utils import get_logger, get_midnight from . import metadata logger = get_logger(__name__) default_date = lambda: get_midnight(datetime.now()) rate = sa.Table( 'rate', metadata, sa.Column('id', sa.Integer, nullable=False), sa.Column('date', sa.DateTime, nullable=False, default=default_date), sa.Column('bank', sa.String, nullable=False), sa.Column('currency', sa.String, nullable=False), sa.Column( 'rate_buy', sa.Numeric(asdecimal=False), nullable=False, ), sa.Column('rate_sale', sa.Numeric(asdecimal=False), nullable=False), sa.PrimaryKeyConstraint('id', name='rate_id_pkey'), ) async def insert_new_rate( conn, *,
def _get_starting_day(days_passed=DAYS_IN_MONTH): now = datetime.now() starting_point = now - timedelta(days=days_passed) starting_day = get_midnight(starting_point) return starting_day