-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchart_utils.py
More file actions
59 lines (50 loc) · 1.93 KB
/
chart_utils.py
File metadata and controls
59 lines (50 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import pandas as pd
import mplfinance as mpf
import numpy as np
def create_trade_scatter(trades_df, trade_type, color, marker):
"""
Create a scatter plot for either buy or sell trades.
Args:
trades_df (DataFrame): DataFrame containing trade data.
trade_type (str): Type of trade ('B' for buy, 'S' for sell).
color (str): Color of the markers.
marker (str): Shape of the markers.
Returns:
mplfinance.plotting.make_addplot: Scatter plot object for mplfinance.
"""
filtered_trades = trades_df.copy()
filtered_trades.loc[filtered_trades['side'] != trade_type] = np.nan
label = "Buy" if trade_type == 'B' else "Sell"
scatter_plot = mpf.make_addplot(filtered_trades['price'],
type='scatter',
markersize=20,
marker=marker,
color=color,
label=label)
return scatter_plot
def create_pnl_plot(trades_df):
"""
Create a profit and loss line plot.
Args:
trades_df (DataFrame): DataFrame containing trade data.
Returns:
mplfinance.plotting.make_addplot: PNL line plot object for mplfinance.
"""
pnl_plot = mpf.make_addplot(trades_df['total_pnl'], panel=1, type='line', ylabel='PNL in $')
return pnl_plot
def format_chart_title(symbols, start_time, chart_schema):
"""
Format the chart title based on given inputs.
Args:
symbols (list): List of symbol strings.
start_time (str): Start time in ISO format.
chart_schema (str): Chart schema string.
Returns:
str: Formatted chart title.
"""
date_part = start_time.split('T')[0]
formatted_date = pd.to_datetime(date_part).strftime('%m/%d')
symbol = symbols[0] if symbols else 'Symbol'
interval = chart_schema.split('-')[-1].upper()
title = f"{symbol} - {formatted_date} - {interval}"
return title