mpl_bsic.plot_trade#
- mpl_bsic.plot_trade(underlying, pnl, pnl_type, title, underlying_name, months_offset=3, sources='BSIC', entry_point_marker_loc='top', entry_point_marker_size=10, date_ticks_unit='W', date_ticks_freq=1, date_ticks_format='%b %d, %Y')[source]#
Plot a trade performance vs the underlying.
Create a figure with two subplots. On the top, the PnL of the trade is plotted, while on the bottom the underlying is plotted.
The function automatically applies BSIC Style and Logo through the respective functions.
- Parameters:
- underlying
pd.Series
Pandas series of the underlying’s prices.
- pnl
pd.Series
Pandas series of the pnl of the trade. This must be in nominal terms or in percentage terms, if you want to plot the percentages. You can also plot the cumulative PnL, the function will calculate it for you.
- pnl_type
Literal
[‘Nominal’, ‘Cumulative’] The type of PnL you want to plot. Can be “Nominal” or “Cumulative”. If “nominal”, the function will plot the PnL as is. If “cumulative”, the function will plot the cumulative PnL by doing .cumsum() on the PnL series.
- title
str
The title of the trade. This will be set as the suptitle of the figure.
- underlying_name
str
The name of the underlying that will be plotted on the bottom subplot, for example “Swap Spread”, “EURUSD”, etc.
- months_offset
int
, optional The months offset that will be used to calculate the first date plotted, by default 3. Since it is a trade, it is recommended to set a short offset (like 3 as the default), so that you can easily see the path of the underlying after you entered the trade.
- sources
str
|list
[str
], optional List of sources, by default “BSIC”. You can either specify a string (if you have only one source) or a list of strings (multiple sources).
Since BSIC is always a source, it will always be included.
NB: if when calling
plt.show()
the text seems cutted out, don’t worry. When exporting usingbbox_inches="tight"
, it will seamlessly fit within the figure. This happens because I want to make sure there is enough space between the plot and the sources text, so I position the text at the very bottom of the figure.- entry_point_marker_loc
Literal
[‘top’, ‘bottom’], optional The location of the marker for the entry point, by default “top”. Whether it is better to use “top” or “bottom” depends on the type of trade you are plotting and the shape of the plot. Choose so that the marker is clearly visible and does not overlap with the underlying’s plot.
- entry_point_marker_size
int
, optional The size of the entry point market, by default 10. Choosing this size will depend on the final size of the plot. Choose so that the marker is clearly visible and does not overlap with the underlying’s plot.
- date_ticks_unit
Literal
[‘Y’, ‘M’, ‘W’, ‘D’], optional The unit used to segment the datetime index (which is displayed on the bottom axis), by default “W”. Since you are probably going to plot a short timespan, it is recommended to use either “W” (weeks) or “D” (days). There should be no reason to use “Y” (years), and you should really question the timeframe you are using if you are choosing “M”.
- date_ticks_freq
int
, optional The frequency for the ticks in the unit specified, by default 1. For example, if you chose “W” as the unit, and 1 as the frequency, there will be 1 tick every week. Choose so that the axis is not cluttered.
- date_ticks_format
str
, optional The format used for the date ticks, by default
"%b %d, %Y"
. I recommend using the default, but you can change it to whatever you find more suitable.
- underlying
- Returns:
tuple
[matplotlib.figure.Figure
,matplotlib.axes.Axes
]A tuple containing the figure and the tuple of two axis just created. You can later use this to save the fig for export, do
fig.show()
, or further customize the plot.
See also
mpl_bsic.apply_bsic_style
Apply the BSIC style to a plot.
mpl_bsic.apply_bsic_logo
Apply the BSIC logo to a plot.
mpl_bsic.check_figsize
Check if the figsize is valid.
Examples
Examples will come soon.