Files
OpenBB/openbb_terminal/common/technical_analysis/volume_model.py
Colin Delahunty dcf3bfe1f7 Refactor common/ta (#3226)
* Handle no adj close

* Added fixes for bad data handling

* Finished switching model layout

* Fixed all the bugs

* Big fixes

* Handle a lot more bugs

* A couple more enhancements

* Fixed issue

Co-authored-by: Henrique Joaquim <h.joaquim@campus.fct.unl.pt>
Co-authored-by: James Maslek <jmaslek11@gmail.com>
2022-11-02 11:30:28 -04:00

110 lines
2.6 KiB
Python

"""Volume Technical Analysis"""
__docformat__ = "numpy"
import logging
import pandas as pd
import pandas_ta as ta
from openbb_terminal.decorators import log_start_end
from openbb_terminal.common.technical_analysis import ta_helpers
from openbb_terminal.rich_config import console
logger = logging.getLogger(__name__)
@log_start_end(log=logger)
def ad(data: pd.DataFrame, use_open: bool = False) -> pd.DataFrame:
"""Calculate AD technical indicator
Parameters
----------
data : pd.DataFrame
Dataframe of prices with OHLC and Volume
use_open : bool
Whether to use open prices
Returns
-------
pd.DataFrame
Dataframe with technical indicator
"""
kwargs = {}
if use_open:
kwargs["open"] = data["open"]
df_ta = ta.ad(
high=data["High"],
low=data["Low"],
close=data["Close"],
volume=data["Volume"],
**kwargs
).dropna()
return pd.DataFrame(df_ta)
@log_start_end(log=logger)
def adosc(
data: pd.DataFrame, use_open: bool = False, fast: int = 3, slow: int = 10
) -> pd.DataFrame:
"""Calculate AD oscillator technical indicator
Parameters
----------
data : pd.DataFrame
Dataframe of OHLC prices
use_open : bool
Whether to use open prices
fast: int
Fast value
slow: int
Slow value
Returns
-------
pd.DataFrame
Dataframe with technical indicator
"""
if use_open:
df_ta = ta.adosc(
high=data["High"],
low=data["Low"],
close=data["Close"],
volume=data["Volume"],
open_=data["Open"],
fast=fast,
slow=slow,
).dropna()
else:
df_ta = ta.adosc(
high=data["High"],
low=data["Low"],
close=data["Close"],
volume=data["Volume"],
fast=fast,
slow=slow,
).dropna()
return pd.DataFrame(df_ta)
@log_start_end(log=logger)
def obv(data: pd.DataFrame) -> pd.DataFrame:
"""On Balance Volume
Parameters
----------
data: pd.DataFrame
Dataframe of OHLC prices
Returns
-------
pd.DataFrame
Dataframe with technical indicator
"""
close_col = ta_helpers.check_columns(data, high=False, low=False)
if close_col is None:
return pd.DataFrame()
if "Volume" not in data.columns:
console.print("[red]Volume column not found[/red]\n")
return pd.DataFrame()
return pd.DataFrame(ta.obv(close=data[close_col], volume=data["Volume"]).dropna())