Python: Calculate VWAP by Date

Question:

I’m trying to add to this code a function that would calculate vwap by date, but it isn’t working:

    def get_ohlc (pair, interval=1, since='last'):
    endpoint = 'https://api.kraken.com/0/public/OHLC'
    payLoad = {
        'pair':     pair,
        'interval': interval,
        'since' :   since
    }
    response = requests.get(endpoint, payLoad)
    data = response.json()
    OHLC = data['result'][pair]
    data = pd.DataFrame.from_records(OHLC, columns=['Time', 'Open', 'High', 'Low', 'Close', 'vwap', 'volume', 'count'])
    data['Time'] = pd.to_datetime(data['Time'], unit='s')
    data['Date'] = data['Time'].dt.date
    data.set_index('Time',inplace=True)
    data = data.drop(['vwap', 'count'], axis=1)
    data['Open']  = data.Open.astype(float)
    data['High']  = data.High.astype(float)
    data['Low']   = data.Low.astype(float)
    data['Close'] = data.Close.astype(float)
    data['volume'] = data.volume.astype(float)
    data['Vwap'] = data.groupby('Date', group_keys=False).apply(Vwap)
    return data

def Vwap(data):
    H = data.High
    L = data.Low
    C = data.Close
    V = data.volume
    return data.assign(Vwap = (V * ((H+L+C)/3)).cumsum() / V.cumsum())

I get the following error:

ValueError: Wrong number of items passed 7, placement implies 1

Asked By: GusC

||

Answers:

In my view, you have been mixing the "responsibilities" in your code:

  • the Vwap func should only take care of the calculation bit
  • you can create the vwap column in the get_ohlc function (btw: that is doing too many things in my view – maybe I would split the download from the manipulation of data).

Anyway, this is how I would write a quick solution to your problem:

import requests
import pandas as pd

def get_ohlc (pair, interval=1, since='last'):
    endpoint = 'https://api.kraken.com/0/public/OHLC'
    payLoad = {
        'pair':     pair,
        'interval': interval,
        'since' :   since
    }
    response = requests.get(endpoint, payLoad)
    data = response.json()
    OHLC = data['result'][pair]
    data = pd.DataFrame.from_records(OHLC, columns=['Time', 'Open', 'High', 'Low', 'Close', 'vwap', 'volume', 'count'])
    data['Time'] = pd.to_datetime(data['Time'], unit='s')
    data['Date'] = data['Time'].dt.date
    data.set_index('Time',inplace=True)
    data = data.drop(['vwap', 'count'], axis=1)
    data['Open']  = data.Open.astype(float)
    data['High']  = data.High.astype(float)
    data['Low']   = data.Low.astype(float)
    data['Close'] = data.Close.astype(float)
    data['volume'] = data.volume.astype(float)
    data = data.assign(vwap = data.groupby('Date', group_keys=False).apply(vwap_func))
    return data

def vwap_func(data):
    H = data["High"]
    L = data["Low"]
    C = data["Close"]
    V = data["volume"]
    res = (V * (H+L+C) / 3).cumsum() / V.cumsum()
    return res.to_frame()


data = get_ohlc(pair="XXBTZUSD")

print(data)

As you can see, there is no need to call vwap_func at the end, given that it is applied already in your get_ohlc function

Answered By: nikeros
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.