twopirllc / pandas-ta

Technical Analysis Indicators - Pandas TA is an easy to use Python 3 Pandas Extension with 150+ Indicators
https://twopirllc.github.io/pandas-ta/
MIT License
5.2k stars 1.01k forks source link

AVWAPs from specific points #460

Open hadialaddin opened 2 years ago

hadialaddin commented 2 years ago

I am trying to draw AVWAPs just like TradingView but from specific point (in my case the main Pivot Points on the chart). I already know the Pivot Points, I just want to know how to set the Anchor value so that it starts from those points. Lets say I have the 1 hour chart pulled from ByBit Kline (which fetches the last 200 hourly candles), what's the Pandas TA code snippet to draw:

Here is a TradingView chart export just for the sake of example where the Yellow is the "Close" AVWAP, and the "Green" is the Low and Red is "High":

BTCUSDT_2021-12-28_03-42-55

twopirllc commented 2 years ago

Hello @hadialaddin,

I don't know off the top of my head. Is there a public TV source of available?

Hopefully someone else may has a good idea or has implemented something similar will share.

Kind Regards, KJ

hadialaddin commented 2 years ago

Thanks @twopirllc here is more on the Anchored VWAP Calculation (https://school.stockcharts.com/doku.php?id=technical_indicators:vwap_intraday):

The Anchored VWAP is calculated using the same formula as traditional VWAP. The only difference is in the bars that are included in the calculations.

With traditional VWAP, the calculation starts with the first bar of the day and ends with the last bar of the day. Because it only incorporates one trading day of data, traditional VWAP can only be used on intraday charts. With Anchored VWAP, the chartist chooses the first bar to use in the calculation (“anchoring” the indicator to that bar), and the last bar is always the most recent bar available. Because of the more flexible starting and ending points, Anchored VWAP is not limited to intraday charts.

Anchored VWAP While traditional VWAP starts at the first bar of the day and ends at the last bar of the day, Anchored VWAP allows the chartist to choose their own starting bar. The overlay is typically started at a significant high or low, earnings announcement, or some other indicator of a change in market psychology. This way, VWAP is calculated using only price action since the significant event occurred.

hadialaddin commented 2 years ago

@twopirllc any thoughts on this so far? If there is a quick hack to do this manually using the already available VWAP function or to modify it?

hadialaddin commented 2 years ago

Basically the Anchored VWAP needs a candle to start from, and to define whether to pick the "High", "Low" or "Close" of the candle to calculate the VWAP for since then.

twopirllc commented 2 years ago

Hello @hadialaddin,

No. I understand what you want. The only idea I have is to make a new indicator called avwap (see #355), copy the source of vwap, take out high and low from the signature, then add another argument to the signature like post_shift=0. Then in the code, rename typical_price to close, then shift the anchor when post_shift > 0 and finish the rest of the calculation. It's the only thing that comes to mind, but I don't know if that's the solution. 🤷🏼‍♂️

Unfortunately, it will be some time until I can address this feature request since I am steeped in outstanding Issues. Contributions and PRs are the quickest way for one to get something included in this library. Thus, I added a Help Wanted tag to the Issue so hopefully someone else can eventually help you and others with the same feature request.

Kind Regards, KJ

sloth456 commented 2 years ago

Hi all,

I ended up writing this function to calculate the current anchored VWAP from the 15 minute bybit chart. It takes an 'anchor' value which should be the datetime in UTC from the point you want the anchor to start, format like '2022-08-14 07:45:00'

# Define our modules here
from pybit import usdt_perpetual
import time
import pandas as pd

API_KEY = '<YOUR KEY HERE>'
API_SECRET = '<YOUR SECRET HERE>'

# Calculate anchored VWAP
def calc_anchored_vwap(anchor):
    session = usdt_perpetual.HTTP('https://api.bybit.com', API_KEY, API_SECRET)

    timestamp = int(time.time()) - 160000
    kline = session.query_kline(symbol="BTCUSDT", interval=15, from_time=timestamp)

    # convert kline.result to pandas dataframe
    df = pd.DataFrame(kline['result'])

    # Add new datetime column to dataframe based on open_time column
    df['datetime'] = pd.to_datetime(df['open_time'], unit='s')
    df.set_index(pd.DatetimeIndex(df["datetime"]), inplace=True)

    df['hcl3_vol'] = ((df['high'] + df['low'] + df['close']) / 3) * df['volume']

    #get all rows after the anchor
    df_after = df.loc[df.index >= anchor]

    sum_hcl3_vol = df_after['hcl3_vol'].sum()
    sum_volume = df_after['volume'].sum()
    vwap = sum_hcl3_vol / sum_volume

    return vwap

I know this snippet is not integrated into pandas_ta at all and it's very specific to calculating only the current anchored VWAP from bybits 15 minute chart, but I'm hoping it's ok to post this here in hopes that it may help someone else.