Esempio n. 1
0
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
        })
Esempio n. 2
0
def _make_sell(sell, coin, dynamo_table):
    if sell.is_valid():
        text = sell.cleaned_data['post']
        sell = SellForm()
        CryptoApi.sell_crypto(text, coin.ticker)
        _update_balance_table(dynamo_table)