def detail(request, coin_id): """ This is the view corresponding to the cryptocurrency trading page, and details of the analysis the platform provides. I.e, Price predictions, chart analysis results, and sentiment analysis :param request: Django request :type request: request :param coin_id: UID of coin in DB :type coin_id: int :return: redirect to dashboard detail page """ coin = shortcuts.get_object_or_404(CoinPrices, pk=coin_id) coin_id_tag = coin.ticker.lower() if int(coin_id) >= 4: return shortcuts.redirect('/dashboard/auth-keys') item = get_trading_data(coin_id_tag) json_data_charts = json.loads( json.dumps(item, indent=4, cls=DecimalEncoder)) trend_data = _get_support_res_from_json(json_data_charts) price = CryptoApi.get_prices(coin.ticker, 'usd')['last'] if price > trend_data[0]: CoinPrices.objects.filter(pk=coin_id).update(trend="Bullish") if price < trend_data[1]: CoinPrices.objects.filter(pk=coin_id).update(trend="Bearish") all_coins = CoinPrices.objects.all() wallet = CryptoApi.get_balance_eq() buy = BuyForm(request.POST, prefix='buy') sell = SellForm(request.POST, prefix='sell') dynamodb = boto3.resource('dynamodb', region_name='us-east-2') dynamo_table = dynamodb.Table('wallet') _make_buy(buy, coin, dynamo_table) _make_sell(sell, coin, dynamo_table) pos_neg_next_day = coin.current_price - coin.predictions return shortcuts.render( request, 'dashboard/detail.html', { 'coin': coin, 'all_coins': all_coins, 'wallet': wallet, 'b_form': BuyForm(prefix='buy'), 's_form': SellForm(prefix='sell'), 'pos_neg_next_day': pos_neg_next_day, 'resi': trend_data[0], 'support': trend_data[1], 'resi2': trend_data[2], 'support2': trend_data[3] })
def _make_buy_fx(buy, coin): if buy.is_valid(): trade_amount = buy.cleaned_data['post'] buy = BuyForm() ForexApi.buy_order(str(coin.ticker + "_USD"), trade_amount, coin.ticker) return trade_amount
def forex(request, coin_id): """ This is the view corresponding to the FX trading page, and details of the analysis the platform provides. I.e, Price predictions, chart analysis results, and sentiment analysis :param request: request from Django :type request: request :param coin_id: uid of coin in DB :type coin_id: int :return redirect to forex trading page """ coin = shortcuts.get_object_or_404(CoinPrices, pk=coin_id) all_coins = CoinPrices.objects.all() trade_amount = 0.0 buy = BuyForm(request.POST, prefix='buy') sell = SellForm(request.POST, prefix='sell') if buy.is_valid(): trade_amount = _make_buy_fx(buy, coin) if sell.is_valid(): trade_amount = _make_sell_fx(sell, coin) fx_positions = dict({s for s in ForexApi.get_pos()['positions']}) instrument = fx_positions['instrument'], units = fx_positions['long']['units'] \ if 'instrument' in fx_positions else None wallet = CryptoApi.get_balance_eq() time.sleep(5) return shortcuts.render( request, 'dashboard/forex.html', { 'coin': coin, 'all_coins': all_coins, 'wallet': wallet, 'b_form': BuyForm(prefix='buy'), 's_form': SellForm(prefix='sell'), 'trade_amount': trade_amount, 'instrument': instrument, 'units': units })
def forex(request, coin_id): """ This is the view corresponding to the FX trading page, and details of the analysis the platform provides. I.e, Price predictions, chart analysis results, and sentiment analysis """ coin = shortcuts.get_object_or_404(CoinPrices, pk=coin_id) all_coins = CoinPrices.objects.all() wallet = CryptoApi.get_balance_eq() text = 0.0 buy = BuyForm(request.POST, prefix='buy') sell = SellForm(request.POST, prefix='sell') if buy.is_valid(): text = buy.cleaned_data['post'] buy = BuyForm() ForexApi.buy_order(str(coin.ticker + "_USD"), text, coin.ticker) wallet = CryptoApi.get_balance_eq() time.sleep(5) if sell.is_valid(): text = sell.cleaned_data['post'] sell = SellForm() ForexApi.sell_order(str(coin.ticker + "_USD")) wallet = CryptoApi.get_balance_eq() time.sleep(5) positions = ForexApi.get_pos()['positions'] dict = {} for s in positions: dict = s instrument = " " units = " " if 'instrument' in dict: instrument = dict['instrument'] units = dict['long']['units'] return shortcuts.render( request, 'dashboard/forex.html', { 'coin': coin, 'all_coins': all_coins, 'wallet': wallet, 'b_form': BuyForm(prefix='buy'), 's_form': SellForm(prefix='sell'), 'trade_amount': text, 'instrument': instrument, 'units': units })
def detail(request, coin_id): """ This is the view corresponding to the cryptocurrency trading page, and details of the analysis the platform provides. I.e, Price predictions, chart analysis results, and sentiment analysis """ global coin_support2, coin_resistance_2, all_coins, coin_resistance coin_resistance = "" coin_support = "" coin_resistance_2 = "" coin_support2 = "" dynamodb = boto3.resource('dynamodb', region_name='us-east-2') dynamoTables = dynamodb.Table('crypto_predictions') coin = shortcuts.get_object_or_404(CoinPrices, pk=coin_id) all_coins = CoinPrices.objects.all() coin_id_tag = coin.ticker.lower() if int(coin_id) < 4: try: response = dynamoTables.get_item(Key={'coin_id': coin_id_tag}) except ClientError as e: print(e.response['Error']['Message']) else: item = response['Item'] result = (json.dumps(item, indent=4, cls=DecimalEncoder)) json_data_charts = json.loads(result) coin_resistance = str(json_data_charts['resi']) coin_support = str(json_data_charts['support']) coin_resistance_2 = str(json_data_charts['resi2']) coin_support2 = str(json_data_charts['support2']) price = CryptoApi.get_prices(coin.ticker, 'usd')['last'] if price > coin_resistance: CoinPrices.objects.filter(pk=coin_id).update(trend="Bullish") if price < coin_support: CoinPrices.objects.filter(pk=coin_id).update(trend="Bearish") all_coins = CoinPrices.objects.all() wallet = CryptoApi.get_balance_eq() buy = BuyForm(request.POST, prefix='buy') sell = SellForm(request.POST, prefix='sell') if buy.is_valid(): text = buy.cleaned_data['post'] buy = BuyForm() CryptoApi.buy_crypto(text, coin.ticker) time.sleep(5) wallet = CryptoApi.get_balance_eq() for key, val in wallet.items(): dynamoTable.put_item(Item={'coin_id': key, 'amount': val}) if sell.is_valid(): text = sell.cleaned_data['post'] sell = SellForm() CryptoApi.sell_crypto(text, coin.ticker) time.sleep(5) wallet = CryptoApi.get_balance_eq() for key, val in wallet.items(): dynamoTable.put_item(Item={'coin_id': key, 'amount': val}) pos_neg_next_day = coin.current_price - coin.predictions return shortcuts.render( request, 'dashboard/detail.html', { 'coin': coin, 'all_coins': all_coins, 'wallet': wallet, 'b_form': BuyForm(prefix='buy'), 's_form': SellForm(prefix='sell'), 'pos_neg_next_day': pos_neg_next_day, 'resi': coin_resistance, 'support': coin_support, 'resi2': coin_resistance_2, 'support2': coin_support2 })
def test_forms(self): """ testing forms """ form = BuyForm(prefix='buy') self.assertTrue(not form.is_valid())
def _make_buy(buy, coin, dynamo_table): if buy.is_valid(): text = buy.cleaned_data['post'] buy = BuyForm() CryptoApi.buy_crypto(text, coin.ticker) _update_balance_table(dynamo_table)