Transcript for:
Pricing Options in C++ using Monte Carlo Simulation

hello everyone my name is Jo scors Channel called ethernet win and in today's video we got another C++ kind of vibe going on I'm going to explain another C++ script I've been enjoying making them and learning things about the language I hope that you guys enjoy watching them uh the last video on C++ got a lot of views a lot of views for me so we're going to keep on going with it [Music] um we got a little bit of a setup going on I guess you could say still the same actual setup laptop camera headphone microphone but you know we got the blue going on I got this awesome theme in vs code so we're going to get a little theme going on whenever I code these videos I don't really code much python in C++ I in C++ I don't really code much python in vs code whenever I do I'll change the theme we get it going at least these lights will change I don't know about you guys but in my mind C++ is this icy blue Python's like orange or red like JavaScript is green Java is like yellow maybe you know that sounds right it's like how in school like the notebooks for each subject you know I mean like um like science is green Global or history whatever is blue you know what I mean what what what was your color combination tell me in the comments I'll love to see them I'm the kind of guy now in college I use just one huge like five subject normally just like one or two subjects takes up like three three sections of that book you know I mean that's what happens when you take a lot of math courses regard regardless though enough of me rambling let's get into the video then so I think this topic is awesome maybe it's just the nerd in me we're going to be pricing options in C++ using Monte Carlo simulation I know I put beginner friendly in the title it will be beginner friendly I don't care if you know nothing about a Monte Carlo simulation you're going understand what I'm getting at by the end of this video we're going to get it we're going to get you right I don't care how much you know about it you're going to learn something and we're going to start not at all in vs code but with this link I found I'm going to put in in the description but I think it explains how you can use Monte Carlo simulation to estimate something right so I'm going to read this then we're going to watch it for a little bit and then we're going to go into the code so a Monte Carlo simulation relies on a repeated random sampling of objects from a distribution and subsequent operation of them of them to create toate nums in this Sim weti random sampling 2D points AI distributor to estimate value of pi the circle has AIT radius whereas the square has a length of two distance units if the points are appreciated appreciably random and if we have generated enough number points then we go into this formula the number of points inside the circle over the total number of points everything is close enough almost equal to about the area of the unit circle over the total area which is again close to Pi so then Pi is about four the total number of points inside the circle and the total number of points so if we keep on going we can see all these random points being generated within this comp and then it estimates the value of pi and it'll get close the longer you do this the closer and closer it would get to 3.1415 whatever and then a little note down here after about a thousand points the have sample the estimated value of pi usually range between 3.12 and 3.16 so we're going to let this it's going back up now it's wiggling around you can see 3.19 3.18 there comes a point where it just all of a sudden starts to get really close we'll see if we can make it I'm not going to make you watch this that long I'll put the link to it put the link to it in the description so let's get into the code now uh cool I'm going to get my little script up and we're going to go through first of all let's talk about what we're actually trying to do with this pricing an option with mon Carlo simulation with that random sampling mumbo jumbo there so when you price an option I'm going to refer to the black schs model with this the black schs model is an option pricing model what does that mean when you have a stock price and the strike price and you know the risk-free rate of that volatility of it and when that option will expire you can price an option an option is the right but not the obligation to buy or sell 100 shares of a specific stock on a specific date on a specific strike actually you can exercise them early we're not going to get into that and buy or sell respective to call or put option right now let's say right our initial stock price and our strike price are right here say our strike price is still $100 so I have the right to buy 100 shares at 100 bucks and then all of a sudden the stock price goes to 110 that contract that agreement I have with whoever to buy these shares at $100 just became a lot more valuable right so that's how you trade options pretty much that's why options appreciate and depreciate is how how attractive is this agreement right now is actually what trading options is so how do we price that that's very hard there's a lot of a lot of discussion about it because right now whenever you open up your phone you go on Robin Hood you go on TD whatever Schwab now whatever ever you use the option prices that you see are from the black schs model and if You' ever read this book you know that it's inherently wrong it is wrong for a few reasons that we're going to get into but it's a good guess and the reason that we use it is because it's easy to use easy to use Easy to calculate when you make these assumptions these kind of um agreed upon misfortunes I guess you could call them in the pricing model to make them simpler and make it less of a headache to simply just get spit you out of price regardless of how accurate it actually is so let's first go over let's first go over these helper functions then we're going to go through our main and then we're going to go over the mon Carlo pricing function so now this function I'm not going to go through line by line because I feel like that would be a bit of a waste of time it's not really what this video is about but all this function does is every time you call it I get a random number with a mean of whatever stand new deviation of whatever it's on this random numbers in that distribution and then we just return that number simple enough in the example in this we use zero and zero as the mean and a standard deviation of one why this random number represents a percentage move of stock in a perfect rational World there will be a lot of little moves and not a lot of big moves we know that not to be true because I believe that there was the example of City Group trading dollar Yen spreads in the [Music] 1970s 40s and there was a big move in the Yen it was minus 9.2 perish it's in that book I showed you in this Behavior Market by um that big move down in in the Yen if you were trading since since when the Big Bang allegedly happened if you're trading since then you still shouldn't have seen that move in all those billions and billions of years since that alleged event you still should have never seen that happen yet there it was right there so that nullifies this whole notion of the market having a normal distribution know that it doesn't we know that the market is skewed to either direction whether the position of the market is long or short think about it like this the S&P 500 the NASDAQ yes it goes up because earnings grow but regular people just buy it it's inherently long something like copper Futures the position of that market can out to be long or short because who trades Futures contracts people that have a lot of money and are very confident trade Futures contracts like that right so when you a number of people hundreds of millions of people every day buy the Spy ETF how many people buy a copper future right it's way less and so when most the people buy copper Futures then the position Market's long most them sell it short it the the position of the market is short we know that to be the actual way it happens but this notion that we have random or normal distribution say that each price point is independent of the last and that that the percentage move should fall on this normal distribution we know that not to be true yet like I was talking about before this is one of those exceptions that we make to make this pricing model convenient let's say so bottom line This returns a random number on this distribution so now let's go through and also whenever you see European column option black sches priced options are considered European so function to calculate the payoff of a European call auction great so you'd have a double s a number represented s and and then k k would be our strike price and S would be the stock price at a given point in time so like I said before if this goes to10 I make $10 a share on my for my agreement with you I make $10 a share because oh wait no yep because my strike price is 100 my the stock price is at 110 so I could buy shares at 100 I just made 10 bucks right so that payoff is that price minus the strike price if it's greater than it like it is here we see we make $10 whatever or I went backwards and so for calls I'm talking about and for puts if this was $90 then we would make the money okay cool so now Joe why does say Max here we can lose money absolutely however we're pricing the options what does that mean that means that the price can't be below zero you're not going to get a credit for buying something I'm not going to pay you for buying something so that's why we floor it at zero if it would be a loss we're just going to add zero to our average if it would be a gain then we'll use that but this option price can't be below zero if not that would be the easiest no-brainer Buy in history I don't care what direction the equity I think it'll go I want some of those calls because that's just incredible value so we can't have them be negative next though let us go into the whole engine right the big V8 so we have all of our variables from before we have our initial stock price s of Z strike price risk-free rate excuse me our volatility time to expiration number of simulations and a call and if it's a call option or not now another fun thing to do excuse me if you care enough is to make these two things Dynamic for every simulation we know that the volatility and the risk rate of a stock is not constant over the whole life of the option so cool thing to be a cool thing to do would be to use a distribution that is not normal and to make these two numbers Dynamic if you so care enough regardless though let's keep on going through this so we have our payoff sum because we're going to find an average right so we're going to add up everything from these functions right we're going to add up everything from this um this calculation stored in this variable right so now for every single one of our simulations we're going to iterate through whatever this number is in this example it is 100,000 so we're going to iterate through that and so now what this is generate a random price path what this does is it estimates the stock price at the maturity of the option so we use this formula the initial price times and then this represents Oilers number raised to in these blue parentheses so Oiler number raised to the risk-free rate minus 12 time Sigma s right Sig squ time to experation plus Sigma sare of T time expiration and then times our random number percentage move and that is a random price path for how much stock should move according to this normal distribution so now if it's a call option or not we would calculate the payoff so our price at maturity and our strike price like that concept I explained to you earlier with the 110 or not on calls we made $10 a share on puts we lost $10 a share so with that Concept in mind we're going to add whatever payoff we get here to our payoff Su we're just going to keep on adding B up up and our average payoff is of course the sum over how many iterations and then we return we return the payoff sum discounted to a present value because we want to know what the option will be worth right now because we're trying to see if it's a good deal pretty much so this is Oilers number raised to negative risk free rate times attemp to maturity times the average payoff so now to recap on our parameters we have the initial stock price strike price free rate volatility time to maturity number simulations so we want to know a call Price so we're saying that it is a call put price we're saying that it's not a call and so now we just have to get our values and put them through so if we open up a terminal and we're in the right directory you need to say g++ whatever the name of your file is- o and whatever you want to call your output file I called it mon Carlo option pricing outex so you'd run that command then you just type in the name of this file click enter and there you go you get your values now Joe we discounted it to to present value to know what it'll Beth now why can't we just see what it'll be worth later and see if it's a good deal you're absolutely right so now if we were to do that can recompile it right we see that the average payoff we should make 50 cents 53 cents on our put on our calls and what is that 20 something cents on our puts in a year so it'll be up to you to decide how much you would if how how how good of a risk that would be because ideally you would know that you would risk you're risking pretty much you're risking $10.34 we know that with option prices it's um we multiply this by 100 excuse me by 10 am I tripping no by by 100 so this what what you would actually pay for this contract is $554 yes that is correct so you would risk $10.34 or $1,034 to make couple hundred bucks so you would have to decide if that risk was worth it but that is totally a way to do that that's the video that's what we're doing I hope that you guys found this video information video informational and it helped you get a grasp on pricing options and the nuances in it because it it is voodoo option price models are the biggest just all right let's go with it ever if you can build something that values options more efficiently than the black schs model make money with it for four decades publish it win a Nobel and go Drive Aston Martins all day I would drive Aston Martins when I was still making money with it too but that that would be that's what I would do but option pricing models is one of those things where how do you know you're right it's a pricing model at the end of the day it's worth however much someone's willing to pay for it but you still need still need that point in time because it is a derivative right at any point if I have if I have these variables at any point I can be tell you I I should be able to tell you what this contract is worth set in stone if you're willing to pay more or less that's the Market's own decision whether it's good or bad but that's how much this contract is worth so you should be able to tell is this contract over or undervalued the risk-free RTI should also be able to turn should also be able to change right we just went through a Fed hiking cycle volatility should be able to change things happen and that's why sometimes you get those massive options payouts because this normal distribution doesn't account for them so when that's when that big move comes that's when you get the the fat taals as Mr Mand BR doctor I'm not just I'm not certain but that's when mandal brought that's what Mand was talking about these models they don't account for those fat taals those big moves and that's what we need to account for as Traders as quants making these pricing models mathematicians computer scientists whatever you want to call yourself but yeah I'm going to put a link to this code in the description in GitHub I'm going to put a link to this cool little simulation in the description I'm also going to put a link to my company it is called Prometheus analytics I think I actually have my training view open right now I do so you can take a look at a recent indicator I made this reversal script can see a plots reversal on the screen no you can't know how I did it close Source indicator but you're more than welcome to get a subscription and use it for as long as youd like even if you only buy this indicator you would also get access to our disc where you can get alerts from this indicator you can't use it these things work on any time frame any assets and and yeah I don't know what else to say I thought I had another thing in the pocket but the link to this will be in the description the link to my personal Twitter be in the description and the link to the business Twitter will be in the description I hope that you guys enjoyed this video I hope that you subscribe and like the video and stick around for another one if you guys have any ideas for videos you want to see let me know in the comments I'm thinking my next one will be about um the correlation trading and stocks so kind of like Parish trading buying so I want to go long bonds let me also go long oil because they should move iners and I think I'll make money on both of them is kind of like what's what um correlation trading is like Parish trading like that buying some buying two things that move in Hing you make money on both but I hope that you guys enjoyed this video about options pricing it was a lot of fun to make I think that things like this is are really cool because it's just how do you know you're right you never do unless you make money so get out there have a good time coding let me know any ideas you have and yeah hope you have a good one goodbye