Files
OpenBB/gamestonk_terminal/common/technical_analysis/volume_model.py
devwolff 7d9b1dacdd add Chaikin Osc (A/D Osc) to ta menu (#692)
* add yahoofinance undervalued & most shorted screeners

* .

* add chaikin osc to ta menu

* follow up

* .
2021-08-21 16:56:53 +01:00

103 lines
2.4 KiB
Python

"""Volume Technical Analysis"""
__docformat__ = "numpy"
import pandas as pd
import pandas_ta as ta
def ad(df_stock: pd.DataFrame, use_open: bool) -> pd.DataFrame:
"""Calculate AD technical indicator
Parameters
----------
df_stock : pd.DataFrame
Dataframe of prices
use_open : bool
Whether to use open prices
Returns
-------
pd.DataFrame
Dataframe with techinical indicator
"""
if use_open:
df_ta = ta.ad(
high=df_stock["High"],
low=df_stock["Low"],
close=df_stock["Close"],
volume=df_stock["Volume"],
open_=df_stock["Open"],
).dropna()
# Do not use open stock values
else:
df_ta = ta.ad(
high=df_stock["High"],
low=df_stock["Low"],
close=df_stock["Close"],
volume=df_stock["Volume"],
).dropna()
return pd.DataFrame(df_ta)
def adosc(df_stock: pd.DataFrame, use_open: bool, fast: int, slow: int) -> pd.DataFrame:
"""Calculate AD oscillator technical indicator
Parameters
----------
df_stock : pd.DataFrame
Dataframe of prices
use_open : bool
Whether to use open prices
Returns
-------
pd.DataFrame
Dataframe with technical indicator
"""
if use_open:
df_ta = ta.adosc(
high=df_stock["High"],
low=df_stock["Low"],
close=df_stock["Close"],
volume=df_stock["Volume"],
open_=df_stock["Open"],
fast=fast,
slow=slow,
).dropna()
else:
df_ta = ta.adosc(
high=df_stock["High"],
low=df_stock["Low"],
close=df_stock["Close"],
volume=df_stock["Volume"],
fast=fast,
slow=slow,
).dropna()
return pd.DataFrame(df_ta)
def obv(s_interval: str, df_stock: pd.DataFrame) -> pd.DataFrame:
"""On Balance Volume
Parameters
----------
s_interval: str
Stock data interval
df_stock: pd.DataFrame
Dataframe of stock prices
Returns
-------
pd.DataFrame
Dataframe with techinical indicator
"""
# Daily
if s_interval == "1440min":
df_ta = ta.obv(close=df_stock["Adj Close"], volume=df_stock["Volume"]).dropna()
# Intraday
else:
df_ta = ta.obv(close=df_stock["Close"], volume=df_stock["Volume"]).dropna()
return df_ta