mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-12 12:22:35 +00:00
change view_vwap to specify date range (#1790)
* change view_vwap to specify date range * test code change due to last commit * check end for calling end.date() Co-authored-by: Theodore Aptekarev <aptekarev@gmail.com>
This commit is contained in:
@@ -120,13 +120,13 @@ def zlma(values: pd.DataFrame, length: int, offset: int) -> pd.DataFrame:
|
||||
|
||||
|
||||
@log_start_end(log=logger)
|
||||
def vwap(day_df: pd.DataFrame, offset: int) -> pd.DataFrame:
|
||||
def vwap(df: pd.DataFrame, offset: int) -> pd.DataFrame:
|
||||
"""Gets volume weighted average price (VWAP)
|
||||
|
||||
Parameters
|
||||
----------
|
||||
day_df: pd.DataFrame
|
||||
Dataframe of dates and prices for the last trading day
|
||||
df: pd.DataFrame
|
||||
Dataframe of dates and prices
|
||||
offset: int
|
||||
Length of offset
|
||||
Returns
|
||||
@@ -136,10 +136,10 @@ def vwap(day_df: pd.DataFrame, offset: int) -> pd.DataFrame:
|
||||
"""
|
||||
|
||||
df_vwap = ta.vwap(
|
||||
high=day_df["High"],
|
||||
low=day_df["Low"],
|
||||
close=day_df["Close"],
|
||||
volume=day_df["Volume"],
|
||||
high=df["High"],
|
||||
low=df["Low"],
|
||||
close=df["Close"],
|
||||
volume=df["Volume"],
|
||||
offset=offset,
|
||||
)
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ __docformat__ = "numpy"
|
||||
import logging
|
||||
import os
|
||||
from typing import List, Optional
|
||||
from datetime import datetime
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import mplfinance as mpf
|
||||
@@ -120,6 +121,8 @@ def view_ma(
|
||||
def view_vwap(
|
||||
s_ticker: str,
|
||||
ohlc: pd.DataFrame,
|
||||
start: datetime = None,
|
||||
end: datetime = None,
|
||||
offset: int = 0,
|
||||
s_interval: str = "",
|
||||
export: str = "",
|
||||
@@ -135,6 +138,10 @@ def view_vwap(
|
||||
Dataframe of prices
|
||||
offset : int
|
||||
Offset variable
|
||||
start: datetime
|
||||
Start date to get data from with
|
||||
end: datetime
|
||||
End date to get data from with
|
||||
s_interval : str
|
||||
Interval of data
|
||||
export : str
|
||||
@@ -144,8 +151,23 @@ def view_vwap(
|
||||
"""
|
||||
|
||||
ohlc.index = ohlc.index.tz_localize(None)
|
||||
ohlc["Day"] = [idx.date() for idx in ohlc.index]
|
||||
day_df = ohlc[ohlc.Day == ohlc.Day[-1]]
|
||||
|
||||
if start and end:
|
||||
start_date = start.date()
|
||||
end_date = end.date()
|
||||
else:
|
||||
start_date = end_date = ohlc.index[-1].date()
|
||||
console.print(
|
||||
f"No Specified date range. load most recent trading data: {start_date.strftime('%Y-%m-%d')}"
|
||||
)
|
||||
|
||||
day_df = ohlc[(start_date <= ohlc.index.date) & (ohlc.index.date <= end_date)]
|
||||
if len(day_df) == 0:
|
||||
console.print(
|
||||
f"[red]No data found between {start_date.strftime('%Y-%m-%d')} and {end_date.strftime('%Y-%m-%d')}\n[/red]"
|
||||
)
|
||||
return
|
||||
|
||||
df_vwap = overlap_model.vwap(day_df, offset)
|
||||
|
||||
candle_chart_kwargs = {
|
||||
|
||||
@@ -312,17 +312,42 @@ class TechnicalAnalysisController(CryptoBaseController):
|
||||
help="offset",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--start",
|
||||
dest="start",
|
||||
type=valid_date,
|
||||
help="Starting date to select",
|
||||
required="--end" in other_args,
|
||||
)
|
||||
parser.add_argument(
|
||||
"--end",
|
||||
dest="end",
|
||||
type=valid_date,
|
||||
help="Ending date to select",
|
||||
required="--start" in other_args,
|
||||
)
|
||||
|
||||
ns_parser = parse_known_args_and_warn(
|
||||
parser, other_args, EXPORT_BOTH_RAW_DATA_AND_FIGURES
|
||||
)
|
||||
if ns_parser:
|
||||
# Daily
|
||||
if self.interval == "1440min":
|
||||
console.print("VWAP should be used with intraday data.\n")
|
||||
if not ns_parser.start:
|
||||
console.print(
|
||||
"If no date conditions, VWAP should be used with intraday data. \n"
|
||||
)
|
||||
return
|
||||
interval_text = "Daily"
|
||||
else:
|
||||
interval_text = self.interval
|
||||
|
||||
overlap_view.view_vwap(
|
||||
s_ticker=self.coin,
|
||||
s_interval=self.interval,
|
||||
s_interval=interval_text,
|
||||
ohlc=self.stock,
|
||||
start=ns_parser.start,
|
||||
end=ns_parser.end,
|
||||
offset=ns_parser.n_offset,
|
||||
export=ns_parser.export,
|
||||
)
|
||||
|
||||
@@ -544,6 +544,20 @@ class TechnicalAnalysisController(StockBaseController):
|
||||
default=0,
|
||||
help="offset",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--start",
|
||||
dest="start",
|
||||
type=valid_date,
|
||||
help="Starting date to select",
|
||||
required="--end" in other_args,
|
||||
)
|
||||
parser.add_argument(
|
||||
"--end",
|
||||
dest="end",
|
||||
type=valid_date,
|
||||
help="Ending date to select",
|
||||
required="--start" in other_args,
|
||||
)
|
||||
|
||||
ns_parser = parse_known_args_and_warn(
|
||||
parser, other_args, EXPORT_BOTH_RAW_DATA_AND_FIGURES
|
||||
@@ -551,13 +565,21 @@ class TechnicalAnalysisController(StockBaseController):
|
||||
if ns_parser:
|
||||
# Daily
|
||||
if self.interval == "1440min":
|
||||
console.print("VWAP should be used with intraday data. \n")
|
||||
return
|
||||
if not ns_parser.start:
|
||||
console.print(
|
||||
"If no date conditions, VWAP should be used with intraday data. \n"
|
||||
)
|
||||
return
|
||||
interval_text = "Daily"
|
||||
else:
|
||||
interval_text = self.interval
|
||||
|
||||
overlap_view.view_vwap(
|
||||
s_ticker=self.ticker,
|
||||
s_interval=self.interval,
|
||||
s_interval=interval_text,
|
||||
ohlc=self.stock,
|
||||
start=ns_parser.start,
|
||||
end=ns_parser.end,
|
||||
offset=ns_parser.n_offset,
|
||||
export=ns_parser.export,
|
||||
)
|
||||
|
||||
@@ -431,6 +431,8 @@ def test_call_func_expect_queue(expected_queue, func, queue):
|
||||
s_ticker="MOCK_TICKER",
|
||||
ohlc=MOCK_STOCK_DF,
|
||||
s_interval="MOCK_INTERVAL",
|
||||
start=None,
|
||||
end=None,
|
||||
offset=2,
|
||||
export="csv",
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user