Пример #1
0
 def testPriceForBuyTrade(self):
     prediction_key = Prediction(contract_one=0.00,
                                 contract_two=0.00,
                                 liquidity=100,
                                 statement="Test",
                                 end_time=datetime.datetime.now()).put()
     user_key = Profile().put()
     trade = Trade(prediction_id=prediction_key,
                   user_id=user_key,
                   direction='BUY',
                   contract='CONTRACT_ONE',
                   quantity=10.00)
     priceBuy = get_price_for_trade(prediction_key.get(), trade)
     self.assertEqual(5.124947951362557, priceBuy)
     prediction_key = Prediction(contract_one=10.00,
                                 contract_two=0.00,
                                 liquidity=100,
                                 statement="Test",
                                 end_time=datetime.datetime.now()).put()
     trade = Trade(prediction_id=prediction_key,
                   user_id=user_key,
                   direction='SELL',
                   contract='CONTRACT_ONE',
                   quantity=10.00)
     priceSale = get_price_for_trade(prediction_key.get(), trade)
     self.assertEqual(-5.124947951362557, priceSale)
Пример #2
0
def fill_prediction(provider, confidences=None, error=None):
    if error:
        return Prediction(provider=provider, error=error)
    predominant_emotion = get_predominant_emotion(confidences)
    return Prediction(
        provider=provider,
        confidences=confidences,
        predominant_emotion=predominant_emotion
    )
Пример #3
0
def set_predictions(user, predictions):
    for prediction in predictions:
        try:
            db_prediction = Prediction.query.filter_by(
                user_id=user.id,
                matchday=prediction["matchday"],
                home_team=prediction["home_team"],
                away_team=prediction["away_team"]).first()

            if db_prediction is None:
                db_prediction = Prediction(matchday=prediction["matchday"],
                                           home_team=prediction["home_team"],
                                           home_score=prediction["home_score"],
                                           away_team=prediction["away_team"],
                                           away_score=prediction["away_score"])
                db.session.add(db_prediction)
                user.predictions.append(db_prediction)
            else:
                db_prediction.home_score = prediction["home_score"]
                db_prediction.away_score = prediction["away_score"]
        except:
            db.session.rollback()
            raise
    db.session.commit()
    return True
Пример #4
0
    def add_predictions(self, user, predictions):
        for prediction in predictions:
            try:
                db_prediction = Prediction.query.filter_by(
                    user_id=user.id,
                    matchday=prediction['matchday'],
                    home_team=prediction['home_team'],
                    away_team=prediction['away_team']).first()

                if db_prediction is None:
                    db_prediction = Prediction(
                        matchday=prediction['matchday'],
                        home_team=prediction['home_team'],
                        home_score=prediction['home_score'],
                        away_team=prediction['away_team'],
                        away_score=prediction['away_score'])
                    db.session.add(db_prediction)
                    user.predictions.append(db_prediction)
                else:
                    db_prediction.home_score = prediction['home_score']
                    db_prediction.away_score = prediction['away_score']
            except Exception:
                db.session.rollback()
                raise
        db.session.commit()
        return True
def db_insert(prediction, length, human):
    if not os.path.exists('predictions.db'):
        db.create_all()

    p = Prediction(prediction=prediction, length=length, human=human)
    db.session.add(p)

    db.session.commit()
Пример #6
0
 def testBuyTradeLikelihood(self):
     prediction_key = Prediction(contract_one=0.00,
                                 contract_two=0.00,
                                 liquidity=100,
                                 statement="Test",
                                 end_time=datetime.datetime.now()).put()
     user_key = Profile(balance=100).put()
     profile = user_key.get()
     prediction = prediction_key.get()
     probability = 90
     trade_likeliness = calculate_trade_from_likelihood(
         probability, prediction, profile)
     self.assertEqual(29.789603833999628, trade_likeliness.quantity)
Пример #7
0
def test():
    # If this file is executed directly, the below examples will be run and tested:
    visualizer = MatPlotLibVisualizer()
    last_run = now = datetime.now()
    minute = timedelta(minutes=1)

    test_predictions = [
        Prediction('AAPL', now, now + minute, 100, 101),
        Prediction('TSLA', now, now + minute, 300, 296),
        Prediction('GME', now, now + minute, 30, 31),
        Prediction('AAPL', now + minute, now + minute * 2, 103, 105),
        Prediction('TSLA', now + minute, now + minute * 2, 290, 296),
        Prediction('GME', now + minute, now + minute * 2, 33, 35),
    ]

    last_predictions = test_predictions
    minutes_passed = 5

    while True:
        new_predictions = [
            Prediction('AAPL', now + minute * minutes_passed,
                       now + minute * (minutes_passed + 1),
                       100 + randint(0, 10), 100 + randint(0, 10)),
            Prediction('TSLA', now + minute * minutes_passed,
                       now + minute * (minutes_passed + 1),
                       280 + randint(0, 20), 280 + randint(0, 20)),
            Prediction('GME', now + minute * minutes_passed,
                       now + minute * (minutes_passed + 1), 30 + randint(0, 5),
                       30 + randint(0, 5))
        ]

        visualizer.update_predictions_plot(last_predictions + new_predictions)
        visualizer.update_traders_plot(now + minute * minutes_passed, [
            SafeTrader("Trader 1", randint(500, 1000)),
            SafeTrader("Trader 2", randint(500, 1000))
        ], {})

        last_predictions = new_predictions
        minutes_passed += 1
        sleep(1)

        print("Last run difference: ", datetime.now() - last_run)
        last_run = datetime.now()
Пример #8
0
def NewPrediction():
    try:
        prediction = Prediction(liquidity=float(request.form['liquidity']),
                                info=request.form['info'],
                                statement=request.form['statement'],
                                end_time=datetime.datetime.strptime(
                                    request.form['endtime'], "%Y-%m-%d"),
                                org=request.form['org'])
        prediction_id = prediction.put()
        flash('You created a new prediction!')
        return redirect('/predictions/' + prediction_id.urlsafe())
    except:
        flash('error')
        return redirect('/predictions/create')
def kafkaconsumer():
    consumer = KafkaConsumer(bootstrap_servers=BOOTSTRAP_SERVERS)

    tp = TopicPartition(TOPIC_NAME, 0)
    # register to the topic
    consumer.assign([tp])

    # obtain the last offset value
    consumer.seek_to_end(tp)
    lastOffset = consumer.position(tp)
    consumer.seek_to_beginning(tp)

    for message in consumer:
        # print(message, "msg", message.offset, lastOffset)
        consumer_message = message
        message = json.loads(message.value)

        if message.get("type") == "photos":
            model_data = [
                Photo(name=data.get("name"), data=data.get("data"))
                for data in message.get("data")
            ]
        elif message.get("type") == "matches":
            model_data = [
                Match(name=data.get("name"), data=data.get("data"))
                for data in message.get("data")
            ]
        elif message.get("type") == "prediction":
            model_data = [
                Prediction(
                    photo=data.get("photo"),
                    match=data.get("match"),
                    status=data.get("status"),
                    score=data.get("score"),
                )
                for data in message.get("data")
            ]

        try:
            db.session.add_all(model_data)
            db.session.commit()
        except IntegrityError:
            db.session.rollback()

        if consumer_message.offset == lastOffset - 1:
            break

    consumer.close()
Пример #10
0
    def predict_next_price(self, current_data_point: DataPoint) -> Prediction:
        inputs, labels = next(
            iter(
                self.make_dataset(
                    np.concatenate(
                        (self.last_batch,
                         [self.data_point_to_array(current_data_point)])))))
        predictions = self.model(inputs).numpy()
        last_batch = predictions[len(predictions) - 1]
        prediction = last_batch[len(last_batch) - 1][0]

        return Prediction(
            self.stock, current_data_point.timestamp,
            current_data_point.timestamp + self.data_interval,
            current_data_point.close_price,
            prediction * self.std[LABEL_INDEX] + self.mean[LABEL_INDEX])
Пример #11
0
def predict_view(request):
    if request.method == "GET":
        end_time = timezone.now()
        start_time = end_time + relativedelta(hours=-1)
        predicitons = Prediction.objects.filter(date__range=(start_time,
                                                             end_time))
        if len(predicitons) == 0:
            new_prediction = Prediction(current_power=40000)
            new_prediction.save()
        all_predictions = Prediction.objects.all()
        return HttpResponse(
            json.dumps({
                'total':
                float(
                    reduce(lambda x, y: x.current_power + y.current_power,
                           all_predictions).current_power) /
                len(all_predictions)
            }))
Пример #12
0
def process_prediction(prediction_data):
    # logging.warning("===========PROCESS PRED METHOD CALLED=============")
    cur = get_cur()
    predictions = []
    if prediction_data == None:
        # logging.warning("===========PREDICTION DATA IS NONE=============")
        pass
    else:
        if type(prediction_data == list):
            try:
                for prediction in prediction_data:
                    # print("type of prediction list...", type(prediction))
                    # logging.warning("===========ADDING TO PREDICTION LIST THE FOLLOWING=============")
                    logging.warning(prediction)
                    predictions.append(Prediction(prediction))
            except:
                pass
    # logging.warning("===========TIME TO MAKE SOME SQLLLLS=============")
    for prediction in predictions:

        predicted_departure_dt = datetime.strptime(
            prediction.predicted_departure, "%Y-%m-%dT%H:%M:%S")
        prediction_datetime_dt = datetime.strptime(
            prediction.prediction_datetime, "%Y-%m-%dT%H:%M:%S")
        delta = predicted_departure_dt - prediction_datetime_dt
        print("here is the time delta....  ", delta)
        if (delta.seconds > 600):
            print("Delta too much..... ", delta.seconds > 600)
            continue

        sql_string = """
        INSERT INTO predictions VALUES ('{stop_id}', '{trip_id}', '{vehicle_id}', '{route_name}', '{predicted_delay}',
                                        '{predicted_departure}', '{prediction_datetime}');

        """.format(stop_id=prediction.stop_id,
                   trip_id=prediction.trip_id,
                   vehicle_id=prediction.vehicle_id,
                   route_name=prediction.route_name,
                   predicted_delay=prediction.predicted_delay,
                   predicted_departure=prediction.predicted_departure,
                   prediction_datetime=prediction.prediction_datetime)
        logging.warning("=========SEEE SQL STRING===========")
        logging.warning(sql_string)
        cur.execute(sql_string)
Пример #13
0
    def process_prediction(self, prediction_data):

        session = Session()

        predictions = []
        if prediction_data == None:
            pass
        else:
            if type(prediction_data == list):
                try:
                    for prediction in prediction_data:
                        # # print("type of prediction list...", type(prediction))
                        predictions.append(Prediction(prediction))
                except:
                    pass

        for prediction in predictions:
            predicted_departure_dt = datetime.strptime(
                prediction.predicted_departure, "%Y-%m-%dT%H:%M:%S")
            prediction_datetime_dt = datetime.strptime(
                prediction.prediction_datetime, "%Y-%m-%dT%H:%M:%S")
            delta = predicted_departure_dt - prediction_datetime_dt
            if (delta.seconds > 600):
                # logging.warning("=====BAD TIMING DATA=====")
                continue

        # logging.warning("===Made it to has predictions====")
        # logging.warning("===I has this many predictions====")
        # logging.warning(len(predictions))
        sql_predictions = []

        for prediction in predictions:
            sql_predictions.append(
                sql_Predictions(
                    stop_id=prediction.stop_id,
                    trip_id=prediction.trip_id,
                    vehicle_id=prediction.vehicle_id,
                    route_name=prediction.route_name,
                    predicted_delay=prediction.predicted_delay,
                    predicted_departure=prediction.predicted_departure,
                    prediction_datetime=prediction.prediction_datetime))

        session.add_all(sql_predictions)
        session.commit()
Пример #14
0
def process_prediction(prediction_data):

    cur = get_cur()
    predictions = []
    if prediction_data == None:
        pass
    else:
        if type(prediction_data == list):
            try:
                for prediction in prediction_data:
                    # print("type of prediction list...", type(prediction))
                    predictions.append(Prediction(prediction))
            except:
                pass

    bulk_template = "INSERT INTO predictions (stop_id, trip_id, vehicle_id, route_name, predicted_delay, predicted_departure, prediction_datetime ) VALUES"
    print("OG bs len", len(bulk_template))

    print("HOW MANYT PREDS???", len(predictions))
    for prediction in predictions:
        predicted_departure_dt = datetime.strptime(
            prediction.predicted_departure, "%Y-%m-%dT%H:%M:%S")
        prediction_datetime_dt = datetime.strptime(
            prediction.prediction_datetime, "%Y-%m-%dT%H:%M:%S")
        delta = predicted_departure_dt - prediction_datetime_dt
        print("here is the time delta....  ", delta)
        if (delta.seconds > 600):
            print("Delta too much..... ", delta.seconds > 600)
            continue

        sql_string = """
        INSERT INTO predictions VALUES ('{stop_id}', '{trip_id}', '{vehicle_id}', '{route_name}', '{predicted_delay}',
                                        '{predicted_departure}', '{prediction_datetime}');
        """.format(stop_id=prediction.stop_id,
                   trip_id=prediction.trip_id,
                   vehicle_id=prediction.vehicle_id,
                   route_name=prediction.route_name,
                   predicted_delay=prediction.predicted_delay,
                   predicted_departure=prediction.predicted_departure,
                   prediction_datetime=prediction.prediction_datetime)
        print(sql_string)
        cur.execute(sql_string)
        print("prediction inserted??")
def index():
    raw_hurricane_data_from_sql = pd.read_sql_table('hurricane', con=engine)
    hurricane_data_from_sql = raw_hurricane_data_from_sql[[
        'identifier', 'name', 'num_pts', 'datetime', 'status', 'latitude',
        'longitude'
    ]]

    if (hurricane_data_from_sql.empty
            and request.args.get('data_url') == None):
        return redirect('/update')
    elif hurricane_data_from_sql.empty:
        hurricane_data_url = request.args.get('data_url')
        hurricane_data = clean(hurricane_data_url)
        update_database(hurricane_data)
        return "<h1>Data loaded</h1>"
    elif request.method == 'POST':

        #validate and get location
        location = geolocator.geocode(request.form['location'])

        if not location:
            flash(
                'Invalid Address - enter coordinates, address with city or just city',
                'error')
            return render_template('index.html')

        # validate and get radius
        if not request.form['radius'].isnumeric():
            flash('please enter a number for radius', 'error')
            return render_template('index.html')
        radius = (float(request.form['radius']) / 69)

        impact = float(request.form['impact'])

        my_prediction = Prediction(hurricane_data_from_sql, location, radius,
                                   impact)

        return render_template('/analysis.html', prediction=my_prediction)
    else:
        return render_template('/index.html')
Пример #16
0
def fixture_stats():
    """
    Calculate the predicted home and away goals for a fixture
    and store those values in the database.
    """
    fixtures = db.session.query(Fixture).filter_by(completed=False)
    for fixture in fixtures:
        league_stats = db.session.query(League_Stats).filter_by(
            league_id=fixture.league_id).one()

        home_stats = db.session.query(Team_Stats).filter_by(
            team_id=fixture.home_team_id).one()

        away_stats = db.session.query(Team_Stats).filter_by(
            team_id=fixture.away_team_id).one()

        home_goals = (home_stats.home_attack_strength) * (
            away_stats.away_defense_strength) * (league_stats.avg_home_goals)

        away_goals = (away_stats.away_attack_strength) * (
            home_stats.home_defense_strength) * (league_stats.avg_away_goals)

        prediction = db.session.query(Prediction).filter_by(
            fixture_id=fixture.id).first()

        if not prediction:
            prediction = Prediction(fixture)

        prediction.home_goals = home_goals
        prediction.away_goals = away_goals

        prediction = _fixture_probabilities(fixture, prediction)

        odds = db.session.query(Odds).filter_by(fixture_id=fixture.id).first()

        if not odds:
            odds = Odds(fixture)

    db.session.commit()
Пример #17
0
 def testScoringCase(self):
     prediction_key = Prediction(contract_one=0.00,
                                 contract_two=0.00,
                                 liquidity=100,
                                 resolved=False,
                                 outcome='CONTRACT_ONE',
                                 statement='Test',
                                 end_time=datetime.datetime.now()).put()
     user_key = Profile(balance=100,
                        user_ledger=[
                            LedgerRecords(
                                prediction_id=prediction_key.urlsafe(),
                                contract_one=10.00,
                                contract_two=0.00)
                        ]).put()
     trade_key = Trade(prediction_id=prediction_key,
                       user_id=user_key,
                       direction='BUY',
                       contract='CONTRACT_ONE',
                       quantity=10).put()
     user = user_key.get()
     audit = scoring()
     self.assertEqual(10, audit[0]['earned'])
Пример #18
0
def index():

    if request.method == 'POST':
        if request.form['action'] == 'enter':
            confidence = request.form['confidence']
            currency =  request.form['currency']
            target_price =  request.form['target_price']
            date_until =  request.form['date_until']
            confidence = request.form['confidence']
            date_until_formated = datetime.datetime.strptime(date_until, '%Y-%m-%d').date()
            currency_info = cryptocompare.get_price(currency, currency='USD')
            starting_price = currency_info[currency]['USD']
            price_difference = float(target_price) - starting_price

            if not currency and target_price and date_until:
                flash('please fill everything', 'danger')
            else:
                post = Prediction(currency=currency, date_to_target=date_until_formated, target_price=target_price, starting_price=starting_price, confidence=confidence,price_difference=price_difference)
                db.session.add(post)
                db.session.commit()
                return redirect(url_for('feed'))
            return redirect(url_for('feed'))
    return render_template("index.html")
Пример #19
0
def make_prediction(timestamp, stop_data):
    base = stop_data['MonitoredVehicleJourney']
    base_prediction = base['MonitoredCall']

    predict_dict = {
        'response_time': date_parser(timestamp),
        'recorded_time': date_parser(stop_data['RecordedAtTime']),
        'line_ref': base['LineRef'],
        'direction_ref': base['DirectionRef'],
        'stop_point_ref': base_prediction['StopPointRef'],
        'scheduled_arrival_time':
        date_parser(base_prediction['AimedArrivalTime'])
    }
    try:
        predict_dict['expected_arrival_time'] = date_parser(
            base_prediction['ExpectedArrivalTime'])
    except TypeError:
        log.error(TypeError)
        predict_dict['expected_arrival_time'] = None
    except ValueError:
        predict_dict['expected_arrival_time'] = None

    return Prediction(**predict_dict)
Пример #20
0
def process_prediction(prediction_data):

    logging.warning("=======ARE YOU EVEN WORKING??=====")
    cur = get_cur()
    predictions = []
    if prediction_data == None:
        pass
    else:
        if type(prediction_data == list):
            try:
                for prediction in prediction_data:
                    # # print("type of prediction list...", type(prediction))
                    predictions.append(Prediction(prediction))
            except:
                pass

    for prediction in predictions:
        predicted_departure_dt = datetime.strptime(
            prediction.predicted_departure, "%Y-%m-%dT%H:%M:%S")
        prediction_datetime_dt = datetime.strptime(
            prediction.prediction_datetime, "%Y-%m-%dT%H:%M:%S")
        delta = predicted_departure_dt - prediction_datetime_dt
        if (delta.seconds > 600):
            continue

        sql_string = """
        INSERT INTO predictions VALUES ('{stop_id}', '{trip_id}', '{vehicle_id}', '{route_name}', '{predicted_delay}',
                                        '{predicted_departure}', '{prediction_datetime}');
        """.format(stop_id=prediction.stop_id,
                   trip_id=prediction.trip_id,
                   vehicle_id=prediction.vehicle_id,
                   route_name=prediction.route_name,
                   predicted_delay=prediction.predicted_delay,
                   predicted_departure=prediction.predicted_departure,
                   prediction_datetime=prediction.prediction_datetime)
        cur.execute(sql_string)
Пример #21
0
def _setup_initial_prediction(user, prediction, competition):
    this_year = datetime.datetime(settings.CURRENT_SEASON, 1, 1)
    if not Prediction.objects.filter(year=this_year,
                                     user=user,
                                     competition=competition)\
                             .count():        
        prediction_obj = Prediction(year=this_year,
                                    user=user,
                                    name=user.email,
                                    competition=competition)
        prediction_obj.save()

        for t_id in prediction:
            prediction_obj.teams.add(Team.objects.get(pk=t_id))
        prediction_obj.save()
        prediction_obj.calculateScore()
        prediction_obj.calculateGoalDiff()
        prediction_obj.calculatePosition()
        meta_competition = Competition.objects.get(
            pk=settings.CURRENT_META_COMPETITION_ID)
        runningscore = RunningScore.objects.create(
            name="Running score",
            user=user,
            competition=meta_competition)