Monday 4 January 2021

Resampling a part of a dataframe with trades to ohlcv (pandas)

Okay so I want to do a backtest based on trade data. I want to loop trough the trade data and resample/agg an ohlcv based on this. If the length of the ohlcv is greater than the max of the parameters I would like to use the ohlcv to calculate the indicators values on it. But when I resample/agg my data I get always only one row (also later in the loop). Also I think this isn't the best way to loop trough a dataframe?

The csv data looks like this:

index,timestamp,trade_id,price,amount,taker_side_sell
10,1609688256174,359832187,33456.54,0.091,True
9,1609688256179,359832188,33460.03,0.003,False
8,1609688256179,359832189,33460.04,0.05,False
7,1609688256179,359832190,33460.66,0.029,False
6,1609688256251,359832191,33458.9,0.007,True
5,1609688256251,359832192,33458.8,0.007,True
4,1609688256251,359832193,33458.17,0.009,True
3,1609688256365,359832194,33461.89,0.001,True
2,1609688256399,359832195,33460.68,0.007,False
1,1609688256416,359832196,33460.67,0.223,True
0,1609688256458,359832197,33460.68,0.024,False

My code:

import pandas as pd

histTrades = pd.read_csv('data/binanceFutures/btc-usdt.csv')
histTrades = histTrades.set_index('timestamp')
histTrades.index = pd.to_datetime(histTrades.index, unit='ms')

ohlcv = pd.DataFrame()

for i in range(len(histTrades.index)):
    df = histTrades.iloc[:i]

    ohlcv['open'] = df.resample('1T')['price'].agg('first')
    ohlcv['high'] = df.resample('1T')['price'].agg('max')
    ohlcv['low'] = df.resample('1T')['price'].agg('min')
    ohlcv['close'] = df.resample('1T')['price'].agg('last')
    ohlcv['volume'] = df.resample('1T')['amount'].agg('sum')

    if len(ohlcv) > calcMinKlines(params):
        pass
        # Calculate the indicator values


from Resampling a part of a dataframe with trades to ohlcv (pandas)

No comments:

Post a Comment