Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVWAPs from specific points #460

Open
hadialaddin opened this issue Dec 28, 2021 · 6 comments
Open

AVWAPs from specific points #460

hadialaddin opened this issue Dec 28, 2021 · 6 comments
Labels
help wanted Extra attention is needed info Informational question Further information is requested

Comments

@hadialaddin
Copy link

hadialaddin commented Dec 28, 2021

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:

  • Close AVWAP from the 10th candle
  • Low AVWAP from the 50th candle
  • High AVWAP starting from the 100th candle

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

@hadialaddin hadialaddin changed the title AVWAPs from a specific points AVWAPs from specific points Dec 28, 2021
@twopirllc twopirllc added help wanted Extra attention is needed info Informational question Further information is requested labels Dec 28, 2021
@twopirllc
Copy link
Owner

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
Copy link
Author

hadialaddin commented Dec 28, 2021

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
Copy link
Author

@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
Copy link
Author

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
Copy link
Owner

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
Copy link

sloth456 commented Aug 14, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed info Informational question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants