def reset_trading(): enable_trading_data = server_get('enable_trading', default_data={'activated': False}).data server_set('enable_trading', { 'activated': False }) trading_source: ICryptoCurrencySource = dependency_dispatcher.request_implementation(ICryptoCurrencySource) storage: ILocalStorage = dependency_dispatcher.request_implementation(ILocalStorage) now = pytz.utc.localize(datetime.utcnow()) trading_cryptocurrencies = trading_source.get_trading_cryptocurrencies() trading_source.start_conversions() for currency in trading_cryptocurrencies: amount = trading_source.get_amount_owned(currency) if round(amount) == 0.0: continue prices = trading_source.get_last_month_prices(currency) qs = PricesQueryset(prices) if len(qs.filter_by_last(timedelta(days=30), now=now)) == 0: continue packages = storage.get_cryptocurrency_packages(currency) amount = two_decimals_floor(amount) target = trading_source.get_stable_cryptocurrency() trading_source.convert(currency, amount, target) for package in packages: storage.delete_package(package) trading_source.finish_conversions() server_set('enable_trading', { 'activated': enable_trading_data.get('activated') })
def user_get(user_pk, key: str, default_data=None) -> UserConfiguration: storage: AbstractConfigurationStorage = dependency_dispatcher.request_implementation(AbstractConfigurationStorage) value = storage.user_get(user_pk, key) if value is None and default_data is not None: storage.user_set(user_pk, key, default_data) return storage.user_get(user_pk, key) return value
def server_set(key: str, data: dict): if type(key) != str: raise ValueError(f'Invalid key provided. Must be str') if type(data) != dict: raise ValueError(f'Data must be a dict object') storage: AbstractConfigurationStorage = dependency_dispatcher.request_implementation(AbstractConfigurationStorage) storage.server_set(key, data)
def _do_migrate(apps, schema_editor): trading_source: ICryptoCurrencySource = dependency_dispatcher.request_implementation( ICryptoCurrencySource) for currency in trading_source.get_trading_cryptocurrencies(): prices = trading_source.get_last_month_prices(currency) print(f'Migrating {currency}') for p in prices: DCryptocurrencyPrice.objects.create( symbol=p.symbol, instant=p.instant, sell_price=p.sell_price, buy_price=p.buy_price, )
def all_last_month_prices_view(request): if not request.user.is_authenticated: return Response(status=401) trading_source: ICryptoCurrencySource = dependency_dispatcher.request_implementation( ICryptoCurrencySource) all_prices = {} days = int(request.GET.get('days', 30)) for currency in trading_source.get_trading_cryptocurrencies(): prices = DCryptocurrencyPrice.objects.filter( symbol=currency.symbol, instant__gte=timezone.now() - timedelta(days=days)).order_by('instant').all() all_prices[currency.symbol] = [{ 'i': p.instant.timestamp(), 's': p.sell_price, 'b': p.buy_price, } for p in prices] return Response(all_prices)