Beispiel #1
0
def save_pv_histories_to_redis(ph_object, pipeline=None):
    if ph_object.source is not BINANCE or ph_object.counter_currency not in [
            BTC, USDT
    ]:
        return pipeline or [0]

    using_local_pipeline = (not pipeline)

    if using_local_pipeline:
        pipeline = database.pipeline()  # transaction=False

    ticker = f'{ph_object.transaction_currency}_{ph_object.get_counter_currency_display()}'
    exchange = str(ph_object.get_source_display())
    unix_timestamp = int(ph_object.timestamp.timestamp())

    # SAVE VALUES IN REDIS USING PriceVolumeHistoryStorage OBJECT
    # CREATE OBJECT FOR STORAGE
    pv_storage = PriceVolumeHistoryStorage(ticker=ticker,
                                           exchange=exchange,
                                           timestamp=unix_timestamp)

    publish_close_price = timestamp_is_near_5min(unix_timestamp)

    if ph_object.volume and ph_object.volume > 0:
        pv_storage.index = "close_volume"
        pv_storage.value = ph_object.volume
        pipeline = pv_storage.save(publish=publish_close_price,
                                   pipeline=pipeline)

    if ph_object.open_p and ph_object.open_p > 0:
        pv_storage.index = "open_price"
        pv_storage.value = ph_object.open_p
        pipeline = pv_storage.save(publish=False, pipeline=pipeline)

    if ph_object.high and ph_object.high > 0:
        pv_storage.index = "high_price"
        pv_storage.value = ph_object.high
        pipeline = pv_storage.save(publish=False, pipeline=pipeline)

    if ph_object.low and ph_object.low > 0:
        pv_storage.index = "low_price"
        pv_storage.value = ph_object.low
        pipeline = pv_storage.save(publish=False, pipeline=pipeline)

    # always run 'close_price' index last
    # why? when it saves, it triggers price storage to resample
    # after resampling history indexes are deleted
    # so all others should be available for resampling before being deleted

    if ph_object.close and ph_object.close > 0:
        pv_storage.index = "close_price"
        pv_storage.value = ph_object.close
        pipeline = pv_storage.save(publish=True, pipeline=pipeline)

    if using_local_pipeline:
        return pipeline.execute()
    else:
        return pipeline


### END PULL OF PRICE HISTORY RECORDS ###
Beispiel #2
0
    def put(self, request, ticker, format=None):
        """
        This should receive a resampled price
        for the upcoming or nearly past 5min period
        where timestamp is divisible by 300s (5 min)
        and represents a resampled data point for
        :return:
        """

        ticker = ticker or request.data.get('ticker')
        exchange = request.data.get('exchange')
        timestamp = request.data.get('timestamp')

        # SAVE VALUES IN REDIS USING PriceVolumeHistoryStorage OBJECT
        pipeline = database.pipeline()  # transaction=False

        # CREATE OBJECT FOR STORAGE
        data_history = PriceVolumeHistoryStorage(ticker=ticker,
                                                 exchange=exchange,
                                                 timestamp=timestamp)
        data_history_objects = {}

        try:

            if "_ETH" in ticker:
                return Response(
                    {
                        'success':
                        f'not assessing ETH base tickers, 0 db entries created'
                    },
                    status=status.HTTP_202_ACCEPTED)

            if "_BTC" in ticker:

                close_volume = int(float(request.data["close_volume"]))
                close_price = int(float(request.data["close_price"]))

                if close_volume and close_price and (
                        close_volume *
                        close_price) < 50:  # less than 50 BTC volume
                    return Response(
                        {
                            'success':
                            f'volume {close_volume} x {close_price} is low, 0 db entries created'
                        },
                        status=status.HTTP_202_ACCEPTED)

            for index in default_price_indexes + default_volume_indexes:
                if not request.data.get(index):
                    continue

                index_value = int(float(request.data[index]))

                if index in default_price_indexes:
                    index_value = index_value * (10**8)

                if index_value > 0:
                    data_history.index = index
                    data_history.value = int(index_value)

                    # ensure the object stays separate in memory
                    # (because saving is pipelined)
                    data_history_objects[index] = data_history

                    # add the saving of this object to the pipeline
                    pipeline = data_history_objects[index].save(
                        publish=True, pipeline=pipeline)

            database_response = pipeline.execute()

            return Response(
                {
                    'success':
                    f'{sum(database_response)} '
                    f'db entries created and TA subscribers received'
                },
                status=status.HTTP_201_CREATED)

        except Exception as e:
            logger.error(str(e))
            return Response({'error': str(e)},
                            status=status.HTTP_501_NOT_IMPLEMENTED)