* Python *汇总财务烛台数据

问题描述 投票:0回答:1

我有1分钟的OHLCV烛台数据,我需要对其进行汇总以创建15m烛台。该数据库来自MongoDB;这是干净的Python版本:

def get_candela(self,tf):
    c = dict()
    candel = dict()
    candele_finale = list()
    prov_c = list()
    db = database("price_data", "1min_OHLC_XBTUSD")
    col = database.get_collection(db,"1min_OHLC_XBTUSD")
    db_candela = col.find({}, sort = [('timestamp', pymongo.ASCENDING)]).limit(20)
    candele = list(db_candela)
    timestamp_calc = list()
    open_calc = list()
    max_calc = list()
    min_calc = list()
    close_calc = list()
    vol_calc = list()
    #for _ in range(len(candele)):
    for a in range(tf):
        if len(candele) ==  0:
            break
        prov_c.append(candele[a])
        c.append(prov_c)
        candele[:tf]=[]
    for b in range(len(c)):
        cndl = c[b]
    for d in range(tf):
        print(cndl)
        cnd = cndl[d]
        #print(len(cnd))
        timestamp_calc.append(cnd["timestamp"])
        open_calc.append(cnd["open"])
        max_calc.append(cnd["high"])
        min_calc.append(cnd["low"])
        close_calc.append(cnd["close"])
        vol_calc.append(cnd["volume"])
        index_close=len(close_calc)
        candel["timestamp"] = timestamp_calc[d]
        candel["open"] = open_calc[0]
        candel["high"] = max(max_calc)
        candel["low"] = min(min_calc)
        candel["close"] = close_calc[index_close-1]
        candel["volume"] = sum(vol_calc)
        #print(candel)
        candele_finale.append(candel)
        max_calc.clear()
        min_calc.clear()
        vol_calc.clear()
    return candele_finale

这将返回仅创建最后一个烛台的数组。这是熊猫的另一个版本:

db = database("price_data", "1min_OHLC_XBTUSD")
 col = database.get_collection(db,"1min_OHLC_XBTUSD")
 db_candela = col.find({}, sort = [('timestamp', pymongo.ASCENDING)]).limit(20)
 prov_c = list()


    for item in db_candela:
            cc={"timestamp":item["timestamp"],"open":item["open"],"high":item["high"],"low":item["low"],"close":item["close"],"volume":item["volume"]}
            prov_c.append(cc)
            print(prov_c)
        data = pandas.DataFrame([prov_c], index=[pandas.to_datetime(cc["timestamp"])])
            #print(data)
        df = data.resample('5T').agg({'timestamp':'first','open':'first','high':'max', 'low':'min','close' : 'last','volume': 'sum'})
        #print(data.mean())
        #with pandas.option_context('display.max_rows', None, 'display.max_columns',None):  # more options can be specified also
        pprint(df)

这将返回一个具有奇怪/随机值的数据框。

python mongodb finance algorithmic-trading ohlc
1个回答
0
投票

我有同样的问题,有人有解决方案吗?

© www.soinside.com 2019 - 2024. All rights reserved.