pythonpyalgotrade

Create vwap in angel broking smartapi


How can I calculate vwap using angel broking smartapi data.

Able to create smartapi object data.

totp=pyotp.TOTP(Token).now()
SMART_API_OBJ = SmartConnect(api_key)
# ************login api call************
history_data = SMART_API_OBJ.generateSession(clientId, pwd, totp)        
to_date= datetime.now()
from_date = to_date - timedelta(days=maxDays)
from_date_format = from_date.strftime("%Y-%m-%d %H:%M")
to_date_format = to_date.strftime("%Y-%m-%d %H:%M")
try:
    historicParam={
    "exchange": "NSE",
    "symboltoken": getToken(symbol),
    "interval": interval,
    "fromdate": from_date_format,
    "todate": to_date_format
}
try:
    candel_json  = SMART_API_OBJ.getCandleData(historicParam)
    columns = ['timestamp', 'O', 'H', 'L', 'C', 'V']
    df = pd.DataFrame(candel_json['data'], columns=columns)
except:
  pass
except:
  pass

Solution

  • from ta.volume import VolumeWeightedAveragePrice
    
    
    def setVWAP(df, label='VWAP', window=14, fillna=True):
        df[label] = VolumeWeightedAveragePrice(high=df.H, low=df.L,close=df.C,volume=df.V, window=window,fillna=fillna).volume_weighted_average_price()