Skip to content

Commit

Permalink
Adds lineplot
Browse files Browse the repository at this point in the history
  • Loading branch information
javadebadi committed Jul 29, 2024
1 parent 72e2a46 commit 46b83b3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
36 changes: 14 additions & 22 deletions xarizmi/ta/obv.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import matplotlib.pyplot as plt
import numpy as np
from talib import abstract

from xarizmi.candlestick import CandlestickChart
from xarizmi.utils.plot.timeseries.lineplot import lineplot_timeseries


class OBVIndicator:
Expand Down Expand Up @@ -37,24 +37,16 @@ def plot(
) -> None:
if self.indicator_data is None:
raise RuntimeError("No data to plot")
plt.figure(figsize=fig_size)
dates = [candle.datetime for candle in self.candlestick_chart.candles]
if any(value is None for value in dates):
plt.plot(self.indicator_data, label="OBV", color=color)
else:
plt.plot(
dates, # type: ignore
self.indicator_data,
label="OBV",
color=color,
)
plt.title("On-Balance Volume (OBV)")
plt.xlabel("Time")
plt.ylabel("OBV")
plt.legend()
plt.grid(True)

if save_path:
plt.savefig(save_path)
else:
plt.show()
lineplot_timeseries(
data=self.indicator_data,
dates_data=[
candle.datetime for candle in self.candlestick_chart.candles
],
fig_size=fig_size,
save_path=save_path,
color=color,
label="OBV",
xlabel="Time",
ylabel="OBV",
title="On-Balance Volume (OBV)",
)
Empty file added xarizmi/utils/plot/__init__.py
Empty file.
42 changes: 42 additions & 0 deletions xarizmi/utils/plot/timeseries/lineplot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from datetime import datetime

import matplotlib.pyplot as plt
from matplotlib.axes import Axes
from matplotlib.figure import Figure


def lineplot_timeseries(
data: list[float],
dates_data: list[datetime | None],
fig_size: tuple[int, int] = (10, 6),
save_path: str | None = None,
label: str = "",
xlabel: str = "",
ylabel: str = "",
title: str = "",
color: str = "blue",
) -> tuple[Figure, Axes]:
fig, ax = plt.subplots(1, 1, figsize=fig_size)
# ax = subplots[0]
if any(value is None for value in dates_data):
ax.plot(data, label=label, color=color)
else:
ax.plot(
dates_data, # type: ignore
data,
label=label,
color=color,
)

ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.legend()
plt.grid(True)

if save_path:
fig.savefig(save_path)
else:
plt.show()

return fig, ax

0 comments on commit 46b83b3

Please sign in to comment.