macro_rules! impl_graph_for_payoff_strategy {
($($t:ty),*) => { ... };
}Expand description
Macro impl_graph_for_payoff_strategy generates implementations of the Graph trait
for one or more given types. This is specifically designed for types that represent
payoff strategies, enabling them to produce graph data and configurations for financial
visualizations, such as profit/loss graphs based on an underlying price range.
§Usage
Use the macro to implement the Graph trait for multiple types. For example:
This will generate implementations of the Graph trait for TypeA and TypeB.
§Generated Trait Implementation
§graph_data
- Generates
GraphDatafor the associated type by calculating the profit/loss data for a price range obtained from theget_best_range_to_showmethod. - The result is displayed as two series:
- A “Positive Payoff” series (green) for profits.
- A “Negative Payoff” series (red) for losses.
- Handles profits, losses, and break-even points separately:
- Break-even points (
0) are shared between positive and negative series. - Prices where profit is positive are included in the positive series only.
- Prices where profit is negative are included in the negative series only.
- Break-even points (
- If
get_best_range_to_showfails, a default emptyGraphData::Seriesis returned.
§graph_config
- Produces a
GraphConfigstructure for styling and configuring the graph:- Title: Based on the strategy’s title via
get_titlemethod. - Width and height: Predefined dimensions (1600x900).
- Labels: X-axis represents “Underlying Price”, and Y-axis represents “Profit/Loss”.
- Other visual configuration properties include:
line_style: Set toLineStyle::Solid.color_scheme: Set to default.legend: Title is added to the legend, butshow_legendis disabled by default.
- Title: Based on the strategy’s title via
§Parameters
$t: One or more types for which theGraphtrait should be implemented.
This allows the MyStrategy type to generate graph data and configurations for visual representation
of its payoff strategy.
§Notes
- The macro assumes that the types implemented provide specific methods (
get_best_range_to_show,calculate_profit_at, andget_title) necessary for theGraphtrait. - Predefined values like colors, line widths, and dimensions can be adjusted within the macro if needed.
- The expansion reaches every item it names through
$cratepaths, so the calling module needs no imports beyond the types it lists.