This Rust program simulates a basic Yield Curve Trading Strategy. It allows users to model scenarios where a bond trader believes the yield curve will change in a certain way and calculates potential profits or losses based on initial and final yield curves.
- Model bonds with different durations and yield rates
- Create long and short positions
- Simulate yield curve changes
- Calculate profit/loss for individual positions and overall strategy
- Provide a summary of the trading strategy and its outcome
The program consists of several key components:
Bond
: Represents a bond with its duration and yield rate.Position
: Represents a trading position, including the bond, whether it's long or short, and the amount.YieldCurveTrader
: Manages the overall trading strategy, including positions and yield curves.
To use this simulator:
- Set up the initial and final yield curves using
HashMap<u32, f64>
. - Create a new
YieldCurveTrader
instance with these yield curves. - Add positions using the
add_position
method. - Call the
print_summary
method to see the results.
fn main() {
// Set up the initial and final yield curves
let mut initial_curve = HashMap::new();
initial_curve.insert(2, 0.0150);
initial_curve.insert(10, 0.0250);
let mut final_curve = HashMap::new();
final_curve.insert(2, 0.0180);
final_curve.insert(10, 0.0230);
// Create a new YieldCurveTrader
let mut trader = YieldCurveTrader::new(initial_curve, final_curve);
// Add positions
trader.add_position(2, false, 1_000_000.0); // Short 2-year bonds
trader.add_position(10, true, 1_000_000.0); // Long 10-year bonds
// Print the summary
trader.print_summary();
}
This example models a scenario where:
- The initial 2-year yield is 1.50% and the 10-year yield is 2.50%
- The final 2-year yield is 1.80% and the 10-year yield is 2.30%
- The trader takes a short position on 2-year bonds and a long position on 10-year bonds
Creates a new YieldCurveTrader
with the given initial and final yield curves.
Adds a new position to the trading strategy. Parameters:
duration
: The duration of the bond in yearsis_long
:true
for a long position,false
for a short positionamount
: The amount of the position in currency units
Calculates the total profit or loss for all positions.
Prints a summary of the trading strategy and its outcome, including initial and final yield curves, positions, and total profit/loss.
- This is a simplified model. Real-world yield curve trading strategies involve more complex calculations, including bond #, duration, convexity, and transaction costs.
- The profit/loss calculation is basic and doesn't account for many factors that would affect real trades.
- Error handling is minimal. In a production environment, more robust error handling would be necessary.
- The program currently only supports two points on the yield curve. It could be expanded to support a full yield curve.
Contributions to improve and expand this yield curve trading strategy simulator are welcome. Please feel free to submit issues or pull requests.
This program is for educational and illustrative purposes only. It should not be used for actual trading decisions. Always consult with a qualified financial advisor before making investment decisions.