Exemplo n.º 1
0
def getLatestMarketData(domain: str, reload: str = "0", sync: str = "0"):
    # Build indicator if not exist
    mayGetLatestStockData(domain, reload, sync)
    mayBuildStockIndicatorInBackground(domain, TCandleType.DAY_1, reload, sync)

    dlog.d("getting data from cache")
    return {
        'latest': dredis.getPickle("market_data_{}".format(domain)),
        'indicator':
        dredis.getPickle("indicator_data_{}_{}".format(domain, '1d'))
    }
Exemplo n.º 2
0
        def wrapper(*args, **kwargs):
            print(args)
            print(kwargs)
            func_name = func.__name__

            cache_key_loading = "{}_loading".format(cache_key)
            cache_key_ts = "{}_ts".format(cache_key)
            # Check if cache exist.
            if kwargs.get('ignore_cache') != True:
                cache = dredis.getPickle(cache_key, None)
                if cache:
                    return cache
            # Check global lock
            if dredis.get(cache_key_loading) == "1":
                raise Exception(
                    "{} is locked by smart cache".format(func_name))
            # Lock
            dredis.set(cache_key_loading, "1")
            # We need to use try catch to avoid unlock
            res = None
            try:
                # Execute
                res = func(*args, **kwargs)
                dredis.setPickle(cache_key, res)
                dredis.set(cache_key_ts, time.time())
            except Exception as e:
                dlog.ex(e, "exception happened while executing:{}".format(func_name))
                pass
            # Unlock
            dredis.set(cache_key_loading, "0")
            return res
Exemplo n.º 3
0
def getIndicatorHistory(domain) -> dict:
    indicator_history_key = "indicator_history_{}".format(domain)
    # TODO : CHECK TS for reload
    data = dredis.getPickle(indicator_history_key)
    if not data:
        raise Exception(
            "Indicator History is not yet avlaible for domain:{}".format(domain))
    return data
Exemplo n.º 4
0
def indicator():
    "status of the app"
    candle_type: str = get_param_or_default(request, "candle_type", "1d")
    domain: str = get_param_or_default(request, "domain", "IN")
    sync: str = get_param_or_default(request, "sync", "0")
    reload: str = get_param_or_default(request, "reload", "0")
    show_result: str = get_param_or_default(request, "result", "0")
    rkey = "indicator_history_{}".format(domain)
    result = dredis.getPickle(rkey)
    has_data = dredis.hasKey(rkey)

    # reload
    if reload == "1":
        if sync == "1":
            tasks.taskBuildIndicator(domain, candle_type)
            result = dredis.getPickle(rkey)
            return buildSuccess(
                "Got indicator",
                result if show_result == "1" else 'result is hidden')
        else:
            task_id = tasks.taskBuildIndicator.delay(domain, candle_type)
            if has_data:
                return buildSuccess(
                    "Indicator is not yet ready",
                    "Scheduled task id: /result/{}".format(task_id.id),
                )
            else:
                return buildError(
                    "Indicator is not yet ready",
                    "Scheduled task id: /result/{}".format(task_id.id),
                )

    # No data
    if result is None:
        # submit task
        task_id = tasks.taskBuildIndicator.delay(domain, candle_type)
        return buildError("Indicator is not yet ready",
                          "Scheduled task id: /result/{}".format(task_id.id))

    # Reload on time
    # mayUpdateStateData(domain, candle_type)

    return buildSuccess("Got indicator",
                        result if show_result == "1" else 'result is hidden')
Exemplo n.º 5
0
def mayBuildStockIndicatorInBackground(domain: str, candle_type: TCandleType,
                                       reload: str, sync: str):
    # reload
    if reload == "1":
        if sync == "1":
            tasks.taskBuildIndicator(domain, candle_type=candle_type.value)
        else:
            tasks.taskBuildIndicator.delay(domain, candle_type.value)
        dlog.d("taskBuildIndicator: task submitted")
        return

    if dredis.getPickle("indicator_data_{}_{}".format(domain, '1d')) is None:
        # task submitted
        tasks.taskBuildIndicator.delay(domain, candle_type.value)
        dlog.d("task submitted")
    else:
        dlog.d("Data is already there")
Exemplo n.º 6
0
def get_summary():
    return dredis.getPickle("summary_result")
Exemplo n.º 7
0
def redis():
    key: str = get_param_or_default(request, "key", "")
    result = dredis.getPickle(key, {})
    return buildSuccess("Saved redis data", result)
Exemplo n.º 8
0
def downloadAndBuildIndicator(domain, candle_type: TCandleType):
    # Optimization
    if not shouldBuildIndicator(domain, candle_type):
        dlog.d("Ignore rebuilding shouldBuildIndicator")
        return

    # Locking
    lockkey = "downloadAndBuildindicator_{}_{}".format(domain,
                                                       candle_type.value)
    if dredis.get(lockkey) == "1":
        dlog.d("downloadAndBuildIndicator locked for key {}".format(lockkey))
        raise Exception("downloadAndBuildIndicator is progress")
    dredis.set(lockkey, "1")

    try:
        dlog.d("downloadAndBuildIndicator start")

        dlog.d("downloadAndBuildIndicator download start")
        ret_value, download_data = ddownload.download(domain,
                                                      interval=candle_type)
        if ret_value is False:
            dlog.d("Download fails")
            return {
                "status": "error",
                "msg": "something goes wrong",
                "out": None
            }

        dlog.d("downloadAndBuildIndicator building start")
        processed_df = dindicator.buildTechnicalIndicators(
            download_data, domain)

        # DONOT STOARE AS FILEdlog.d("downloadAndBuildIndicator: saving to storage start")
        #path_to_store = dstorage.get_default_path_for_candle(candle_type)
        #dstorage.store_data_to_disk(processed_df, path_to_store)

        dlog.d("downloadAndBuildIndicator: building indicator history map")
        # Building Indicator map for O(1) looks up.
        # This will be a 4d map
        # map[REL][1d][-1][close]...
        last15SlotIndicator = getLastNIndicatorInJson(domain, processed_df)
        indicator_history_key = "indicator_history_{}".format(domain)
        olddata = dredis.getPickle(indicator_history_key)
        if not olddata:
            olddata = {}
        for key in last15SlotIndicator.keys():
            if key not in olddata:
                olddata[key] = {}
            olddata[key][candle_type.value] = last15SlotIndicator.get(key)
        dredis.setPickle(indicator_history_key, olddata)
        dlog.d(
            "downloadAndBuildIndicator: saved indicator history to {}".format(
                indicator_history_key))

        dlog.d("downloadAndBuildIndicator: saving to redis start")
        dredis.setPickle(
            "indicator_data_{}_{}".format(domain, candle_type.value), {
                'data': getLatestDataInJson(domain, processed_df),
                'timestamp': getCurTimeStr()
            })

        # update market data
        if candle_type == TCandleType.DAY_1:
            saveMarketDataFormDayDF(domain, download_data)

        # Set TimeStamp key
        dredis.set(
            "indicator_timestamp_{}_{}".format(domain, candle_type.value),
            getCurTimeStr())

        # unlock
        dredis.set(lockkey, "0")

        dlog.d("downloadAndBuildIndicator ends")
        return {
            "status": "success",
            "msg": "Completed snapshot pipeline",
            "out": None
        }
    except Exception as e:
        dredis.set(lockkey, "0")
        dlog.d("downloadAndBuildIndicator Exception happened")
        danalytics.reportException(e, "Exception in downloadAndBuildIndicator")
        dlog.ex(e)
        raise e
    finally:
        dredis.set(lockkey, "0")
        pass