TradingView Pine Script Tutorial 15 – UNIX Time – Bitcoin Supply

TradingView Pine Script Tutorial 15 – UNIX Time – Bitcoin Supply

YouTube Video

YouTube Video Transcript

[Music] hey this is David for big bits in this video we’re gonna take a look at coding with the UNIX timestamps in our pond script indicators and the the big indicator that I’ve created with this is the Bitcoin circulating supply overlay you can see that here I believe it’s the only circulating supply indicator on trading view at least when I search it that’s what it appears to be so uh take another look here yeah so it looks like it’s the only one that’s out there I think I searched if you search for Bitcoin there’s some similar thing number of coins it’s not open source though and really I think this is gonna be good going forward because it’s calculated on the estimated block rewards per day with the estimated upcoming having date so it’s really cool I’m going to show you how to do this but first I want to show you a few different things this of course is the supply over time this was during the having period of 50 the initial having period in the second period was 25 Bitcoin block rewards and the third was twelve and a half and I’ve got this chart pulled up or is it right here that tells you how many Bitcoin were created per block and how many were created during that having period this is a really good chart this is a URL I definitely recommend you check it out if you’re interested in that the having schedule and the effect of the block reward history but anyway back to the chart this of course is a circulating supply I added a few options I’ll go over those really quick we also have a log reduce supply you can see that really reduced those numbers and actually put it on a scale similar to the price and I’ll show you how I did that in a little bit as well and then there is also the market cap and these actually look a little bit better on log scale because market caps in the billions yeah you can see it follows along pretty nicely with the price but it’s also adjusted based on many coins are actually out on the network being used and while I’ve got it on long scale before I get into the code I want to show you the log reduce supply and this actually appears to have done a pretty good job of calling the market top during the last couple of cycles now I will let you know I don’t think this is going to work for a long time because the next log reduced supply after the next having is 180 mm so you know somewhere up here but if we continue to reduce with this log scale as I have for the past two having’s then eventually we won’t be able to reduce it anymore and we’ll just be multiplying that supply by one and since there’s a maximum of 21 million coins that means eventually the price of Bitcoin could get up to 21 million and that’s just absurd to think about and likely very very likely it’s not gonna happen so it seems more likely we might have to change the log reduction schedule just a bit off of this tenfold amount in the future as is that’s something to keep in mind but let’s look at the actual code that’s probably what you came here for this is a development series let me take myself out of the video now so you can see the code and we are going to be working with time stamps functions and of course our string options in our input here now as as always you can find this on my trading view profile or as I showed you before you can just search for it but let’s go ahead and take a look we’ve created a few functions the first is circulating supply log reduction and a multiplier and then we only have one simple line for plots I left it as a blue line people can change it however they want to there’s nothing else going on here so it didn’t need to have a special style to it in my opinion so for the circulating supply and this is where a lot of the hard work came into we had to first determine where we are like what having period are we in so if we are before the Genesis date which it happens to be today eleven years ago that would mean there were no coins circulating so we would have to return zero now if that is not the current schedule we are in the first having period which is a time greater than January 3rd of 2009 when it started are greater than or equal to excuse me but also less than the very first having date of November 28th 2012 and I got those dates from this particular picture here and let’s look a look at the reference here the time stamps returned a unix time of the specified date and time so what we’re doing is we’re passing into this this function this time stamp function particularly also I believe those are hours and minutes and it converts those into a unix time and if you’re not familiar unix time if I recall correctly is I think it’s like the number of seconds since a certain date in like 1971 it’s a really large integer number right now so it only gets larger and larger and it doesn’t return a typical like date datatype that you would work with in other languages like C sharp so that’s something to keep in mind with we’re actually working with a very large integer here so when we call this function and we get our date back it’s actually giving us just one really long number so we have to account for that so if we are in this first having period which is these numbers back here and the history for BLX doesn’t go back that far so we don’t see it start at 0 we only see it as of the day that their history starts in AD the supply was like say 40 oh that’s the log reducible excuse me the supply itself let’s go back here something like four million had already been generated at that time so we reduce it by quite a bit to get on the same schedule as we were down here but the thing we do to get this number of four million first we start off with the end amount from this having so we know at the end of the first having let me go back to the log reduce apply because it’s a lot easier to see when the having Sakura on that chart the first having occurs here and we know at the end of the first having there should be 10.5 million coins and that’s based on the Bitcoin algorithm for the block rewards you can see that’s on the chart here this is how many bitcoins are rewarded during this particular schedule and then of course there were five point two in the next one two point six in the next one and one point three going forward and it keeps reducing as the rewards reduce as well so we start off with the number that we know that we need then we get the date of the next having and we put it into this timestamp function which is gonna give us a really huge number back and we divide that by 86 million four hundred thousand that’s because that’s how many units and not eunuchs but how many units make up a single day in a unix time and i said it’s a really long integer so every day worth of time in a unix timestamp is eighty six point four million so when we divide the timestamp by that amount we get the number of days in unix then we can subtract the current date using that same timestamp function divided by eighty six point four million to get the UNIX days and we can see how far back from the upcoming having which we passed in here we currently are so for only one day back we’re just right here or for a couple hundred days back we’re back here so then not only do we have to take that into concern we have to look and we have to add in the rewards over so to do that we multiply that difference that we’ve gotten and we’ve already gotten our difference here and we know that block rewards that we’re getting for this entire first block happening then we divide that as it loads we divide that by the the total number of days in the halving period so we get how many days back we are all right so if we’re a hundred days back this number be 100 and then we multiply the entire batch of block rewards during this having period by the number of days in the halving period okay that gives us the amount of Bitcoin rewarded per day during this particular period now we have that number we have ten point five million times the daily reward or let’s say this we have the daily reward times the number of days before the next having okay so we get all the rewards until the next halving and then we take the amount of coins on the next halving and we subtract the amount of coins that we have left until the tanks having and that’s how we get this increasing amount over time it sounds like a lot and there’s a lot going on on this particular line of code so it can be a bit intimidating if you aren’t a very experienced programmer so not much I can do about that but if you follow it along you’ll see how these things work and I do want to point this out some of you might just skip to this video this first part here is condition and then it checks if it’s true and if it is it returns this value otherwise it’ll return the next value in the line and we just kind of cascade those down at the end of each line you can see well if that wasn’t it then check this if that’s not it you know check this and then of course it’ll default to nothing of course if we are after the next having date and that’s when I’ll have to hard code in a bit more information particularly it’s gonna be the the expected total blocker wards and blocker words block rewards per this having and once we get that information we can toss this into this particular indicator and then find the next estimated having date and will be done and that’s all we really have to do to update the circulating supply over time now I did mention earlier on this could be used I’m gonna bring myself back into the video now I did mention earlier on this could be used for other crypto currencies that we know the the having periods and the reward schedules very clearly is most likely would be best for you know I think like coin or other Bitcoin Forks like Bitcoin cash something like that where we know the rewards schedule ahead of time we can actually do this and it doesn’t have to be based on the same schedule as Bitcoin does have to be a hard fork a bit going to be any schedule as long as we know what it is and how many rewards come per block and how many blocks are or how many rewards are happening per day so we can really mold this to any if we wanted to now the log reduction is the other function that we came up with that I came up with that I used to fit this to a curve and though during the first having period this uses the same timestamp kind of functions that it did up here but the value were returning is just the multiplier that we’re gonna apply to the supply so during the first happening we wanted to reduce it quite a bit I mean the price was really low back then so it had to be reduced substantially to make it kind of in line with the prices at that time so on the log scale it was five decimal places zero zero zero one is what we multiply the supply by and then during the next having period I just multiplied that by point zero zero one point zero zero zero one and then the current having period point zero zero one and I already mentioned during the next one if I were to apply point zero one then it would be a line that starts off with a hundred and eighty two thousand which if it happened to align with the prices peaking during the last market phases that might be the top but like I mentioned it probably won’t work just because eventually that would mean the price would equal the supply and that’s just kind of silly to think about now the other function we have is just for the multiplier so we’ve returned our circulating supply when we’re going to do our plot down here and we have different modes so I did an if-else nested branch here and if the supply is the mode that we’re on we just multiply the circulating supply by itself or not by itself about one so it equals itself and if that’s not the case then if the mode is the log reduce supply then for the multiplier we return back the log reduction which is this function that would return back the multiplier that we’re talking about and clearly I need to update this we don’t want to multiply it by that much in the future but when that time comes I’ll get around to doing it if I don’t please remind me if you’re watching this video in the future and you notice the indicators broken please just let me know but the other one of course the last mode is the market cap and to calculate the market cap at the moment all you have to do is multiply the current closed price on the candle times the estimated supply and that’s the current market cap of Bitcoin so we took this one indicator and we really turned it into three with the different modes but really a lot of unique things to look at here the key takeaway if you’re following the series is how to work with the UNIX timestamps and how to compare those and you’ll particularly probably want to pay attention to I divided it by 86.4 million for a day now if you want to work with smaller amounts you could probably do that like if you wanted to work with one minute charts that might be sixty or six thousand on this scale I can’t remember exactly but if you take all the seconds in a day divide this number by all the seconds in a day and then you’ll get what you should divide it for to see it on a single seconds go so just something to keep in mind that you can do that and if you have any questions about this one because I know it’s really confusing and it’s really hard to look at with all of these functions within this one line please leave a comment and let me know that you need something here or you can reach out to me on Twitter or you know the discord or any of that stuff you can find all the links to that in the description for the video now if you liked the video or you like the indicator please like this video subscribe that helps a lot but you can also check out my training view profile you can see it there you can like the other scripts that I’ve got the other ideas that I’ve had and that stuff helps as well but like I said if you like this video please leave it a like and if you like these types of videos please like or subscribe to the channel and you’ll get to see more like it we have a lot of great stuff coming out here I’ve got several videos planned and some that are already recorded so that’s all for today thank you and have a great day [Music]