示例#1
0
def batchProcessRtnToNormalizedPrice(sourceFile, targetFile):
    data = loadJson(sourceFile)
    priceData = [1]
    dataLabel = "data"

    for rtn in data[dataLabel]:
        priceData.append(priceData[-1] * (1 + rtn))

    data[dataLabel] = priceData

    with open(targetFile, "w") as file:
        json.dump(data, file, indent=4)
示例#2
0
def processBatchFinalRtnAndDumpToJson(fileNameWithoutDotJson):

    tmp = dict()
    datas = loadJson(fileNameWithoutDotJson + SUFFIX_OF_PRICED_DATA)

    for count, data in enumerate(datas):
        prices = datas[data]

        tmp[str(count)] = prices[-1] - prices[0]

    with open(fileNameWithoutDotJson + SUFFIX_OF_FINAL_RTN, "w") as file:
        json.dump(tmp, file, indent=INDENT)
示例#3
0
def processBatchMDDAndDumpToJson(sourceFile, targetFile=False):

    tmp = dict()

    pricedDatas = loadJson(sourceFile + SUFFIX_OF_PRICED_DATA)

    for count, key in enumerate(pricedDatas):
        tmp[str(count)] = MDD(pricedDatas[key])

    if targetFile == False:
        targetFile = sourceFile

    with open(targetFile + SUFFIX_OF_MDD_DATA, "w") as file:
        json.dump(tmp, file, indent=INDENT)
示例#4
0
def processBatchPriceToRtn(filePath, dumpFileName, dataRange):
    '''
    Input : 
        The raw data from Alpha Vintage
        dataRange = [lowerBound, UpperBound]
    Output:  
        "metadata": {
            "flag":0,
            "underlying": xxxx,
            "lengthOfEachExperiment": None,
            "numberOfExperiments": None,
            "returnStyle":1,
            "time":str(datetime.today())
        },
        "data": [
            0,
            1.2,
            .
            .
            .
            1.3
        ]
        the first element in "data" satnds for the earliest data
    '''

    deserializedData = loadJson(filePath)
    pathForUnderlying = ["Meta Data", "2. Symbol"]
    underlying = findValueInDictByKeyName(pathForUnderlying, deserializedData)

    header = createHeaderOfTemplateForJson(0, 1, dataRange, underlying)
    pathForPrice = ["Time Series (Daily)"]
    priceData = findValueInDictByKeyName(pathForPrice, deserializedData)

    initPrice = 0
    for dailyPriceData in priceData:
        beforeUpperBound = convetStringToDate(dailyPriceData) <= dataRange[1]
        afterLowerBound = convetStringToDate(dailyPriceData) >= dataRange[0]

        if beforeUpperBound and afterLowerBound:
            currentPrice = float(priceData[dailyPriceData]['4. close'])
            rtn = (initPrice - currentPrice) / currentPrice
            header["data"].insert(0, rtn)
            initPrice = currentPrice
    header["data"].pop(-1)

    dumpDictToFile(header, dumpFileName)
示例#5
0
def dumpUniformDistribution(numberOfSampling,
                            sourceFileName,
                            numberOfExperiments,
                            targetFileName=False,
                            dataFromMemory=False):

    deserializedData = loadJson(sourceFileName)
    pathForUnderlying = ["metadata", "underlying"]
    pathForTimeRange = ['metadata', 'timeRange']
    underlying = findValueInDictByKeyName(pathForUnderlying, deserializedData)
    dataRange = findValueInDictByKeyName(pathForTimeRange, deserializedData)
    print(dataRange)

    dataToBeSerialized = createHeaderOfTemplateForJson(
        flag=2,
        returnStyle=1,
        underlying=underlying,
        dataRange=dataRange,
        lengthOfEachExperiment=numberOfSampling,
        numberOfExperiments=numberOfExperiments)

    if not dataFromMemory:
        with open(sourceFileName, "r") as fileOfData:
            deserializedData = json.load(
                fileOfData)["data"][-numberOfSampling:]

    elif dataFromMemory:
        deserializedData = dataFromMemory

    for eachExperiment in range(numberOfExperiments):
        listOfChosenIndex = randint(0,
                                    numberOfSampling - 1,
                                    size=numberOfSampling)
        pathOfReturn = list()

        for index in listOfChosenIndex:
            pathOfReturn.append(deserializedData[index])

        dataToBeSerialized["data"].append(pathOfReturn)

    with open(targetFileName, "w") as targetFile:
        json.dump(dataToBeSerialized, targetFile, indent=4)