Exemplo n.º 1
0
 def setUp(self):
     call_command(flush.Command(), interactive=False)
Exemplo n.º 2
0
 def tearDown(self):
     call_command(flush.Command(), interactive=False)
Exemplo n.º 3
0
def parse_nasdaq(request):
    """Функция для парсинга nasdaq и наполнения БД."""

    prices_data = None
    insider_trades_data = None
    result = {'message': 'Parsing is finished!'}

    tickers_file = TICKERS_FILE
    with open(tickers_file) as f:
        content = f.readlines()
    companies = [x.strip().lower() for x in content]
    try:
        prices_data = parse_prices(companies, 10)
    except TypeError as e:
        error = f'prices parsing failed: {e}'
        result.update(message=error)
    try:
        insider_trades_data = parse_insider_trades(companies, 10)
    except TypeError as e:
        error = f'prices parsing failed: {e}'
        result.update(message=error)

    if prices_data:
        # truncate all tables
        cmd = flush.Command()
        call_command(cmd, verbosity=0, interactive=False)
        for company, rows in prices_data.items():
            company_obj = Company.objects.create(name=company)
            for row in rows:
                StockPrice.objects.create(company=company_obj,
                                          date=date_parse(row[0]),
                                          open=row[1],
                                          high=row[2],
                                          low=row[3],
                                          close=row[4],
                                          volume=int(row[5].replace(',', '')))
            # let's create insider trades for this company
            if insider_trades_data:
                for row in insider_trades_data[company]:
                    owner_obj, owner_created = Owner.objects.get_or_create(
                        name=row[0])
                    if owner_created:
                        Relation.objects.create(position=row[1],
                                                owner=owner_obj,
                                                company=company_obj)
                    try:
                        last_price = Decimal(row[6])
                    except DecimalException:
                        last_price = None

                    Trade.objects.create(
                        company=company_obj,
                        insider=owner_obj,
                        last_date=date_parse(row[2]),
                        transaction_type=row[3],
                        owner_type=row[4],
                        shares_traded=int(row[5].replace(',', '')),
                        last_price=last_price,
                        shares_held=int(row[7].replace(',', '')))

    return JsonResponse(result)