Let's say that I have a bond that pays coupon on a semi-annual basis. Therefore, the price of this bond can be calculated using the following formula:
$$ P = \sum_{i=1}^N \frac{CF_i}{(1 + YTM/2)^{2t_i}} $$
First derivative of the above is:
$$ \frac{\partial P}{\partial YTM} = \frac{1}{(1 + YTM/2)} \sum_{i=1}^N \frac{-2t_iCF_i}{(1 + YTM/2)^{2t_i}} $$
Second derivative (aka convexity) of the Price function is:
$$ \frac{\partial^2 P}{\partial YTM} = \frac{1}{(1 + YTM/2)^2} \sum_{i=1}^N \frac{({4t_i}^2+2t_i)CF_i}{(1 + YTM/2)^{2t_i}} $$
And the generalized form of the convexity formula for bonds that pay multiple coupons per year is:
$$ \frac{\partial^2 P}{\partial YTM} = \frac{1}{(1 + YTM/f)^2} \sum_{i=1}^N \frac{({(ft_i)}^2+ft_i)CF_i/f}{(1 + YTM/f)^{ft_i}} $$
I am getting slightly different results when I compare my results with Bionic Turtle. Is there any mistake in my derivation?
Thank you!