3
$\begingroup$

I am setting up a volume profile series over a stock data. I have implemented the market profile code from this github repo and the link to the data is here and the example here. Some Sample of data is here:

Timestamp          Close    High    Low     Open    Volume
2017-09-27 06:30:00 927.74  927.74  927.74  927.74  14929
2017-09-27 07:00:00 933.0   935.53  928.47  928.745 136776
2017-09-27 07:30:00 932.365 934.0   930.065 932.98  73146
2017-09-27 08:00:00 932.39  933.14  930.3   932.45  56624
2017-09-27 08:30:00 934.8675 935.62 931.1495 932.15 74960
2017-09-27 09:00:00 934.325 935.96  933.6 935.2308  62358
2017-09-27 09:30:00 935.48  935.7   933.56  934.36  45043
2017-09-27 10:00:00 935.5   935.56  933.71  935.385 32434
2017-09-27 10:30:00 935.96  936.86  935.21  935.5   58203

Here the author gets the output by applying the function over a slice of rows the index and apply the main function by applying this code:

mp = MarketProfile(df, tick_size=1)
mp_slice = mp[df.index.max() - pd.Timedelta(13, 'H'):df.index.max()]

Here the function takes a slice of indexes from whole dataframe and apply its function in that slice and the author get his results through this code:

mp_slice.as_dict()

What I want is to make rolling(w) of indexes and apply that function to the whole Data frame in pandas of index and make new columns in the data frame from the starting date. i.e df['poc_price'], df['value_area'], df[initail_balane'].etc. (all that includes in the as_dict() function output).

i tried many codes but although nothing get the final output that i want. My least code is:

def make_marketprofile(df, window=50, mode='vol', tick_size=1):
for i in range (0, df.shape[0] - window - 1):
    try:
        mpt = MarketProfile(df[i:i+window], mode=mode, tick_size=0.5)
        mpt_slice = mpt[df[i:i+window].index.min() : 
                    df[i:i+window].index.max()].as_dict()
    except KeyError:
        print('error: {}'.format(df_1['Date'][i+window]))
#        for key, value in mpt_slice():
    df.iloc[i+window] = mpt_slice #value
    try:
         df.drop(['Date'], axis = 1, inplace = True)
    except AttributeError:
         pass
for column in df:
    df[column][1:] = pd.to_numeric(df[column][1:])
    df = df.iloc[window:]
    df.fillna(method = 'ffill', inplace = True)
return df

but it throws value error:

ValueError: Must have equal len keys and value when setting with an iterable
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.