-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
docs/finc50/fixed-income/vega-charts/bond-price-yield.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
{ | ||
"$schema": "https://vega.github.io/schema/vega/v5.json", | ||
"description": "A chart of bond's price and yield, made by Mingze Gao", | ||
"width": 700, | ||
"height": 300, | ||
"title": { | ||
"text": "Bond Price and Yield", | ||
"fontSize": 18, | ||
"anchor": "middle" | ||
}, | ||
"data": [ | ||
{ | ||
"name": "table", | ||
"transform": [ | ||
{ | ||
"type": "sequence", | ||
"as": "yield", | ||
"start": 0.0, | ||
"step": 0.5, | ||
"stop": 20.5 | ||
}, | ||
{ | ||
"type": "formula", | ||
"as": "price", | ||
"expr": "datum.yield>0 ? (10000*couponRate*(1-pow(1+datum.yield/100,-maturityInYears))/(datum.yield/100)+10000*pow(1+datum.yield/100,-maturityInYears)) : 10000*(1+couponRate*maturityInYears)" | ||
}, | ||
{ | ||
"type": "formula", | ||
"as": "price5", | ||
"expr": "datum.yield>0 ? (10000*0.05*(1-pow(1+datum.yield/100,-maturityInYears))/(datum.yield/100)+10000*pow(1+datum.yield/100,-maturityInYears)) : 10000*(1+0.05*maturityInYears)" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "scaledata", | ||
"source": "table", | ||
"transform": [ | ||
{ | ||
"type": "formula", | ||
"as": "maxV", | ||
"expr": "max(datum.price, datum.price5*1.2)" | ||
} | ||
] | ||
} | ||
], | ||
"signals": [ | ||
{ | ||
"name": "maturityInYears", | ||
"value": 10, | ||
"bind": { "input": "range", "min": 1, "max": 30, "step": 1 } | ||
}, | ||
{ | ||
"name": "couponRate", | ||
"value": 0.05, | ||
"bind": { "input": "range", "min": 0, "max": 0.1, "step": 0.0001 } | ||
} | ||
], | ||
"scales": [ | ||
{ | ||
"name": "x", | ||
"type": "linear", | ||
"domain": { "data": "table", "field": "yield", "sort": true }, | ||
"range": "width" | ||
}, | ||
{ | ||
"name": "y", | ||
"type": "linear", | ||
"domain": { "data": "scaledata", "field": "maxV" }, | ||
"range": "height" | ||
} | ||
], | ||
"axes": [ | ||
{ "orient": "bottom", "scale": "x", "title": "Yield (%)", "ticks": false }, | ||
{ "orient": "left", "scale": "y", "title": "Bond Price" } | ||
], | ||
"marks": [ | ||
{ | ||
"type": "rule", | ||
"encode": { | ||
"update": { | ||
"x": { "scale": "x", "value": 0 }, | ||
"y": { "scale": "y", "value": 10000 }, | ||
"x2": { "scale": "x", "value": 5 }, | ||
"y2": { "scale": "y", "value": 10000 }, | ||
"strokeWidth": { "value": 1 }, | ||
"strokeDash": { "value": [8, 3] }, | ||
"strokeCap": { "value": "round" }, | ||
"opacity": { "value": 1 } | ||
} | ||
} | ||
}, | ||
{ | ||
"type": "rule", | ||
"encode": { | ||
"update": { | ||
"x": { "scale": "x", "value": 5 }, | ||
"y": { "scale": "y", "value": 0 }, | ||
"x2": { "scale": "x", "value": 5 }, | ||
"y2": { "scale": "y", "value": 10000 }, | ||
"strokeWidth": { "value": 1 }, | ||
"strokeDash": { "value": [8, 3] }, | ||
"strokeCap": { "value": "round" }, | ||
"opacity": { "value": 1 } | ||
} | ||
} | ||
}, | ||
{ | ||
"type": "line", | ||
"from": { "data": "table" }, | ||
"encode": { | ||
"update": { | ||
"x": { "scale": "x", "field": "yield" }, | ||
"width": { "scale": "x", "band": 1 }, | ||
"y": { "scale": "y", "field": "price" }, | ||
"tooltip": { | ||
"signal": "{ 'Bond Price': format(datum.price, '$,.2f') }" | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"type": "line", | ||
"from": { "data": "table" }, | ||
"encode": { | ||
"update": { | ||
"x": { "scale": "x", "field": "yield" }, | ||
"y": { "scale": "y", "field": "price5" }, | ||
"stroke": { "value": "#d6001c" }, | ||
"tooltip": { | ||
"signal": "{ 'Bond Price': format(datum.price5, '$,.2f') }" | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"type": "text", | ||
"from": { "data": "table" }, | ||
"encode": { | ||
"update": { | ||
"x": { "scale": "x", "value": 20 }, | ||
"y": { "scale": "y", "field": "price", "offset": -5 }, | ||
"text": { | ||
"signal": "format(datum.price, '$,.0f')+'@'+format(datum.yield,'.1f')+'%'" | ||
}, | ||
"fontSize": { "value": 12 }, | ||
"align": { "value": "left" }, | ||
"baseline": { "value": "bottom" }, | ||
"fill": { "value": "black" } | ||
} | ||
} | ||
}, | ||
|
||
{ | ||
"type": "text", | ||
"encode": { | ||
"enter": { | ||
"align": { "value": "right" }, | ||
"baseline": { "value": "bottom" }, | ||
"fill": { "value": "rgba(0, 0, 0, 0.2)" }, | ||
"fontSize": { "value": 14 }, | ||
"x": { "value": 0, "offset": "width" }, | ||
"y": { "value": 0, "offset": "height*1.2" }, | ||
"text": { | ||
"value": "Assume $10,000 bond, annual coupons paid in arrears and effect annual discount rate." | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters