 Okay, Extraders, and welcome back to the Thinkorswim ThinkScript series, and I promise is a promise. So, the last time we made a video we covered four quick tips, alright, and as promised today we are going to cover some actual scripts, some ThinkScripts, which have to do with return, basically, return on your investment, right? We're going to look at a monthly and yearly return, which is going to be a column script, and we're going to look at a way to create a script for a chart where we will display the price action plus the dividend return of a particular stock. And as a freebie or as an extra, well it's not really an extra, I promise I actually talked about it in the previous video, which was about the four quick tricks that could be had in Thinkorswim, and I'm going to link to that video right here actually. I talked about S&P 500 Sectors, and this has to do with the investment strategy video that I made a while ago. Now I was actually looking for a second video, I don't know if you remember in the previous video I talked about there being two investment strategy videos and one risk management video, but when I went back to look to try to link to them, I don't know if you noticed, but there's only one investment strategy video which actually takes place on the trading view chart itself where I just threw the stuff up on the chart there, on a text widget, if you will, and then the risk management one. It turns out that those investment strategy videos, I actually covered them in what were options trading specific videos, so I'm going to link to them this time, but I do cover the investment strategy presentation and I'll probably make a new video for that anyway because it's a little bit outdated. So anyway, I did cover or mention the S&P 500 Sectors and how important it is to keep an eye on different things and that's actually what that previous four quick tips or four, you know, think or some tricks, if you will, was actually about. How to keep in your view at your fingertips certain information which is important for trading, okay, and taking a look at the S&P 500 Sectors is one of those very important things no matter what it is that you're trading. I don't know if you guys are clear on this, but basically, you know, the Dow 30, the Dow is actually just an index of 30 stocks, that's just what it is, you know, the Dow 30, and then there's the NASDAQ which is very heavily weighted towards technology stocks which means that it is very high growth, so if you invest a thousand bucks in the, you know, the Dow 300 or the Dow 30, sorry, and you invest a thousand bucks on the NASDAQ, you'll probably get more of a percentage change, if you will, in the NASDAQ one than you will out of the Dow 31. Now I'm not saying you're gonna make more money, I'm saying you're gonna get a larger percent change, whether it's up or down on a NASDAQ investment than you would on the Dow investment with equal amount and some money to start with. The reason being that the Dow is only comprised of 30 stocks which are the 30, and then they change, they go in and out not as much as NASDAQ or not as much as as in P500, for example, but you know they do get swapped in and out every now and then, but they are very mature, very, they're called blue chip stocks, okay, these are stocks that are very mature in the industry, they've been around for a very long time, it's very hard that they're going to go bankrupt tomorrow, but their growth is very slow, which is the typical growth of a mature company. The NASDAQ on the other hand is a very high growth bunch of stocks, so you will get very wild swings on the NASDAQ, so that swing could at some moment in time be positive and definitely net you a lot more money than the Dow investment would. Well, the S&P 500 is, it's only 500 companies, there's other indices that contain more stocks than that, such as the Russell, but the S&P 500 kind of comprises more even distribution of high growth, blue chips, incredibly risky growth, kind of mature growth stocks, so it's a very important index, the S&P 500, and index sectors that you want to keep your eye on whenever it is that you're trading, whatever it is that you're trading, so okay, let's go ahead and get started with today, let's see over here, got the, okay, so we come over here to the thinkorswim and we go over to tools, and I just noticed, so I'll very quickly switch over to the actual portfolio, not the paper of anyone, because is this one, if you recall, is empty, so I'll be right back. Okay, Extraders, so here we are, and as promised, we're going to be looking at the monthly and yearly return script, okay, so now if you recall from our presentation, as I just mentioned, the monthly and yearly return is actually a column script, okay, so I don't know if you recall, we covered the two types of scripts, the one that pop out, or that show up on the chart, right, which we did the example for the average volume, so this is a script here, and these are more sophisticated scripts we will look at later, indicators, strategies, and studies, and then there are column scripts, column scripts show up on the market watch list, or they show up on these custom watch lists, so you can add them, right, okay, so let's go ahead and look at a script real quickly, now there's a couple ways to do this, we can go over to the gear or cog wheel icon over here, and hit customize, right, or we could also just, you know, hit double click here, and then hit customize, and it takes us to the same place, it takes us to whatever this customize column headers window is all about, so okay, let's go ahead and create our monthly return, so the way to do this we mentioned was you can basically import an empty script and use it, right, you can do that, or you could also just, oops, sorry, did not mean to do that, or you could simply use one of these empty custom ones, which are somewhere here, we go, okay, so you can just kind of hijack one of these guys, so let's go ahead and click on this one, all right, and we will remove, well actually we'll just, because you can do it, you can create it using the wizard, or you can create it using the thingscript editor, so let's go ahead and call this the monthly, see I'm thinking that's not going to fit, so let's just say mo return, because you got to think of how this is going to show up in your columns, right, okay, and okay, so let's switch over to thingscript editor and start basically typing, all right, so remember that we talked about the pound sign being a way to comment, to add, you know, natural language comments on that, on that script, so in this case we are going to use this to just basically say one month, oops, it'll try to auto-complete for you, one month, and we'll say trading days, I'll talk about that in a bit, return, okay, and you might want to add the date, so created July 25th, 2023, all right, you want to, you know, you might want to put the, you know, the author or whatever in there or your particular name, you know, see how it also always tries to auto-complete, okay, so there we go, let's start with the coding, now we are going to want to create a variable, okay, to define the time period, the one month or the equivalent trading days, so let's, let's, you know, let's pretend, let's say that it's going to be, you know, period, all right, of course sometimes that could, that could also be, let's just call it time period, okay, because we could have different periods, okay, so that's going to be our variable, now you notice it's marking it as, because it doesn't recognize that as a, as a reserved or a keyword, right, if you type in something that is a reserved or a keyword, such as moving average, right, average, then it won't mark it as red, because it recognizes it as a reserved or keyword, in this case for the moving average function, so it won't tell you that there's something wrong there, it won't mark it as red, but the reason why I'm doing it like this is because I want to show you, okay, so I'm, this is what you, normally this is what you would think of first, okay, this is my variable, this is what I want to add to it, and I want to make it equal to 25 or 21 trading days, whatever, you know, 21, because 25 wouldn't be, if you have 30 days, typically in a month, minus the five, minus the four Sundays, minus the four Saturdays, then you only have about 21 trading days, so you know that this is, naturally this is the way you would think about this in your head, okay, I want to create a variable, I want to call it time period, I want to sign it a value of 21, in this case, days, but we're going to end it with the semicolon because that's how individual lines end in ThinkScript and many other coding languages, and now we could do this, right, because there's two ways to define a variable, either Duff for define or input, if the user wants to change it, now, if we don't want the user to change it, then we'll just go ahead and do this, right, and the user won't be able to change anything, but let's go ahead and just make it an input type for now, okay, which means that the user will actually be able to change this in the future, all right, so, all right, now, what we're going to do is we're going to define a monthly period, okay, so let's define, let's say, you know, monthly period, okay, because what we want to do is the following, we want to take the close, which means, and you'll see that it'll recognize this as a reserved word, the close means the closing price, all right, so that closing price minus whatever the closing price was, however many days ago, okay, so if closing price maybe 21 days ago was whatever, then we'll go ahead and say, okay, then make it the time periods, daily close, way back then, so we'll make it the time periods, daily close, way back when, so now we're subtracting whatever today's close is, and we're subtracting the 21 days ago closing price, all right, so once we have that, we're going to just divide that, whatever that was, by the close 21 days ago, okay, oops, having issues typing here, okay, there, that is the time period, or the close 21 days ago, all right, now this whole expression is going to give us, let's think about this, let's just end it like that, okay, and it recognizes as a, it recognizes it as a complete and valid line of code, now let's think about that, and it's going to be a little bit complicated because remember the auto complete sometimes trips us up, but basically what we're doing is we're taking today's close, right, and we're subtracting from it, let's call it yesterday's close, okay, yesterday's close, that's what we're doing in this first part, okay, in this first parenthesis, if you will, you can see when you highlight, when you mouse over a parentheses or a square brackets or whatever, it'll highlight whatever the closing equivalent of it is, okay, so we're taking today's close from it, we're subtracting whatever the close was a couple of days ago, so we'll do an example here, let's say, i.e., this is going to be 100, right, which is today's close minus, let's say, 75, which was 21 days ago, okay, we're assuming in this case that the stock went up from 75 to 100, so we're subtracting the close, so that's going to give us 25, right, and we're going to divide that 25, okay, so that's this division right here by 21 days ago's close, which was 75, okay, and that's going to give us, if we pop our calculator here, 25 divided by 75 is going to give us 0.33, right, so what we want to do is we want to multiply that by 100, so that we can get 33.3% return, all right, so to do that we wrap this whole thing in parentheses, like so, okay, and this whole thing we want to multiply that by 100, okay, so there we go, now we have an actual percentage return that we can work with, all right, and what do we want to do with that, we're going to leave that comment in there, come all the way over here, and we're going to add a label, okay, in this case we're not going to add it to the chart, in this case we're going to add it as a column script, because that's the kind of script that we're working on, and we have here the, I don't know if you remember from the average volume script, but basically we want it to be visible immediately, yes, we don't have to, we don't want to condition its visibility, and we want to concatenate it, concat, to concat means to make a chain out of, you know, two or more things, so what do we want to concatenate, we want to concatenate the result that we got, but we also want to round it, okay, monthly period, okay, so we want to take the 33.333, because if you recall, if you do that multiplication, let me see if that's still up there, if you multiply this by 100, it's still going to be 33.333, which is not very useful, so we want to round whatever that 33.333 is, so we don't get all those decimals, okay, so that's what that function is going to do right there, the round, right, monthly period, that's what that's going to do, and we want to round that to exactly two decimal points, all right, so that's where that ends, but we also want to concatenate or chain together that result with, and what we're going to do here is we're going to put something in between parentheses, all right, and then we're going to close it off and end the line, so what is it that we're going to add in parentheses, well, we could add whatever we want, but we don't want to make it too long, so we're just going to add the percent sign, okay, so that is what this whole line of code is going to end up being, all right, okay, so let me see here, add the label, yes, and concat, and round, and then the comma two, and then the comma, and we're missing, see, this refers to the round one, if you could see right before the R of the round is where this parentheses ends, but then there's the one right before the Y of the yes, and that is why it's telling you that there's an error with that line, so you're missing, basically, the parentheses, you add it, then you're done, okay, now our code is complete, let's go ahead and hit apply, okay, and hit okay, and now we want to add this, okay, we called it mo return, so let's go ahead and add this, okay, and bring it up, okay, let's bring it up to here, that one is a different one, okay, and here we go, so here is our monthly return, all right, and we are going to now look at the yearly, so I've already modified it, because to be honest, I had to edit the video quite a bit, so basically the difference between the monthly return obviously and the yearly return is precisely that, the time period, so we can come over here, and actually this is a quicker shortcut, because instead of customizing and then going and looking for your script, you just edit the formula of the script that you selected here, so we come over here, and basically you can see that the only thing that changed here is we changed the 21 to a 252, all right, and so that's obviously gonna change, and 252 is because that's the number of trading days more or less that are in a year, so this is going to go back 252 periods, and look at the close and do the exact same calculation we did before, now there's a couple of other ways you can do this, all right, and we are actually going to look at one of those, and you could simply divide the time period over the, I mean the, let's call this in, the close in brackets versus the close today, which is the close without brackets, so this is let's say the close today, this is the close in the past, you can just divide that by, and I can show you this here real quick, you can just divide one by the other, and it gives you, you know, the same thing, so let's come over here and click on this, all right, and all right, it's gone on here, and I think or swim, let me see, I wonder if I actually have to hit reformat code, okay, there we go, so we can come over here and hit enter, and we'll find out that something is going on with this laptop, so let's hit cancel, and do that again, you want to edit the formula, and this is the formula we want to edit, and it should, there, I don't know what happened, that was weird, okay, so let's take the example we have above, right, so we had 75 as one of the prices, and then 100 as the other price, I believe, and we basically got the difference which was 25, and then divided it by 75, now you can also do 100 divided by 75, and then do that whole thing minus one, right, so let's just make sure here, right, so you can take 100 and divide it by 75, you get 1.33 minus the one, you get 33.3, right, so that's just another way of doing the same thing, so you could take the close at whatever period, right, let's call this in the past, and simply divide that by day's close, right, and that would be the same thing as this expression that we have up here, right, and you don't need, well yeah, you do have to times it by 100 as we, you know, as we start right now, so that's one way of doing it, we can actually start labeling these, so this is one way of doing it, let's say way, this option A, option B, and then option C, which we will learn in future lessons as we spoke about, is the fold command, right, and that is something that is very, it's used quite a bit, right, quite a lot, because it is a way to look back in time, at past time periods, if you will, and evaluate some condition of some variable in those time periods, and then perform a calculation based on that, and return some value, all right, so to do the yearly, like I said, all we have to do is switch this, this was on the monthly, this was 21, all we need to do is switch that to 252, all right, and that's what this yearly is here, okay, so I just basically added some text, I didn't do anything, and you get your yearly return column, and if you notice, and if you notice, Kimco, which was actually positive for the month, is negative for the year, and McCormick, which was negative for the month, is actually positive for the year, all right, so that's peculiar, but obviously very common, just to make sure, right, so we had, in the yearly, we have negative Kimco, positive McCormick, on the monthly, instead of a negative positive, we have a positive negative, there it is, all right, so that's the script for monthly and yearly returns, okay, and I will actually modify this script and see if I can bring you some updated version of this one, and a few other ones that I've run into, which are actually pretty cool, and they'll help us, they'll help us go through some of this very useful code and learn how to do things and think or swim and how to make them look better, okay, so as promised, I'm back here with the other way to calculate the monthly yearly return, which we talked about, which was a little bit of an improvement, so I'm going to click here and edit this formula, all right, and the thing we mentioned, I believe in the previous video, were these three, let's see here, let's see if this is acting up again, because it sure looks like it is, let me hit okay, and do that again, so I'm going to hit edit formula, all right, there we go, yeah, it's working out, so we talked about the three ways of doing this, the long way, which is the original way we did it here, and then the more compact way, which is just the past close divided by the new close, minus one, did do that, sorry, and then times 100, okay, and then we can cat that with the percentage sign, and the fold, which we will see in a more advanced video, but in this case, I wanted to look over this, let's say, let's call this the new and improved return calculation, now it is fundamentally different in this case, because we are not just calculating its return, okay, and what I've done is I've added all the code in as comments, and of course, we're going to basically uncomment the new stuff, right, and we're going to comment this old stuff, and so that will effectively leave this code as the working code, and just, you know, kind of forget about that, so okay, what are we going to do, let's start by commenting this code, which we have up here, all right, and we will uncomment, not that one, because we want to leave that line as a comment, but we want to uncomment these lines of code, okay, and I already found an error, or it found an error, let's go ahead and take a look at what that error is, okay, so, okay, let's see here, oh yes, I can see my error, okay, there it is, I should fix that, and okay, all right, so okay, here it is, I know I was missing something, okay, let's do that, there we go, okay, actually let me leave some space here, I'd like this to be neat, okay, so let's look at our working code, our working code is we're defining a variable, we're calling it stock, you know, some stock, it doesn't matter which one, and we're going to round whatever is in these parentheses, okay, so what is it that we're doing, this is actually option B up here, okay, we do the past close divided by the current close, and there we go, so we do that, and in this case we're subtracting by one, and we are multiplying by 100 to get the percentage, and then we just tag on that too, because that's the number of decimal places, we saw this in the previous video, that we want to have, okay, so now we put in here basically two, three months, right, we decided sort of up here that this was going to be, I'm going to type it here, okay, calc, okay, we decided that this was going to be based on, based on 21 day months, right, okay, so 21 day months, so one month would be 21 days, which is what we used up here, and two months would be 41, three months would be 63, all right, so in this case we're doing it for 63, we could do it for whatever number you want, and I will actually show you another trick, because as you can see we're calculating it for the stock, and we're also doing the exact same calculation for a benchmark, okay, in this case we want to compare the return of our stock, not just to its previous close, but we want to actually then also compare it to the previous close, the same calculation of today's close versus the previous close of some index, okay, which might be relevant to our stock, in this case we're using the S&P 500, whose symbol is SPX, right, and we want to basically determine which one has fared better, right, so a stock that does better than the benchmark, well that's pretty good, you know, or obviously the other way around, if it's worse than the benchmark, well then that's pretty bad, all right, so I have another trick we can use here, because we're comparing basically the return on two different stocks, right, our particular stock, and the SPX ticker symbol, right, and then we're going to do some simple arithmetic to compare those two, but as you can see we're using the number 63 exactly twice, right, whenever you see something repetitive like that in code it usually means that you can do better, so we're going to do something better, we're going to call this def and we're going to call this time period, which we actually used above, right, you know what, I'll actually do something even better, well I'd already sort of typed that in, but I'm going to come up here and I'm actually going to uncomment this, all right, time period, and then I'm going to come over here and say 63, all right, and then I'm going to replace wherever I have 63 by the word time period, like so, and then I'm period, there we go, all right, okay, so we're basically calculating the return for a stock and then we are calculating the return for our benchmark we want to use, and then we're actually doing a comparison, we're simply subtracting whatever the return of our stock is, we're subtracting the return of the benchmark, so basically whatever this stock return is, it is the stock's return by itself minus any, let's say, help that it could have gotten from a benchmark, so if it's still positive then that's pretty good, all right, and what we do now is we're going to plot that data and we're going to start like signing values and colors to things, and what we're going to do is we're going to take that data, right, and if we do this plot data equals stock, so whatever we throw in here, we're going to be able to now take that data because it is a plot type and we can assign colors to it, in this case we're going to assign the color black, but we're going to go a step further, we're going to say, okay, assign the background color to that cell, because remember this is a column script, and if the value of comparison, right, is less than zero, then give it color black, which is the color it has, else if comparison is greater than zero, then we're going to give it a color red, oh that didn't work, and it's color red, and else it's just going to be color orange or whatever, okay, so let's go ahead and hit apply, and you can see it loading in the background, and there are the colors, now remember for comparison, it's going to be red if the comparison is greater than zero, so if the stock did better than the benchmark, then it's going to be, it's going to be colored in red, a red background, right, with the black data, the black colored letters, as you can see we have some issues, because the background of Thinkorswim is black, or the Thinkorswim table in watch lists and the market watch list is also black colored, so that is not a good idea, so let's go ahead, and we could do a couple of things, we could either come over here and say we want this to be white, right, and you can mess with these, and we quickly see that now we can actually see the letters, because the background is black, right, and what we're saying there is that if it is less than the benchmark, then it is this color, right, white on black, if it is more than the benchmark, it is white on red, all right, so we might want to actually give this a little bit more of a, you know, regular or normal, if you will, meaning, so what do I mean by that, well we actually want, if it's less than, right, what I would do is I would make, if it's less than, I would make it red, and if it is greater than, I would make it green, all right, and then we'll leave anything else as orange, okay, so let's look at our final results, and there it is, so we get a, yeah, I don't like that either, let me go ahead and tweak that, because I didn't consider that the value, that it was going to be white, so let's switch this back to black, all right, all right, okay, apply an okay, and there it is, so now any cell that is red, that particular stock did less than the benchmark, so teledyne did less than the monthly benchmark, okay, in this case it did less than the monthly benchmark, but Voya Financial is still a positive monthly return, again, let me go and edit that, because we've tweaked this so much, time period is now 63, let's take that back, let's leave it at that, and let's call it three month return, right, okay, there it is, so the three month return, these in green are better than the benchmark, right, and these in red are less than the benchmark, right, even if they're positive, they're less than the benchmark, so you could see, you could tell that the benchmark is somewhere less than 13, right, I can't, that's the smallest number I can find in green, and it is greater than 12, right, makes sense, less, it's somewhere between 13 and 12, so let's go ahead and do something like, instead of market watch, let's pop this open, like so, I believe I have some return, some SPX here, right, okay, I've got an idea, let's just do this watch list, we'll add SPX right there, okay, and we're actually going to remove some of these, because these might be taking up let's remove month, or M and Y, right, and we'll remove the think script squeeze, we haven't looked at that, okay, and we will go ahead and add the three month return, okay, and we'll actually plot this one, right there, that's fine, okay, let's hit the okay, and there it is, right, so the SPX is actually returning 12.16, which is exactly what we expected, somewhere between 12 and 13, and that's why it's being given the orange color, all right, so that is the more sophisticated monthly return, if you want to do three months return, if you want to do yearly return, all you got to switch is this number here, and you automatically modify everything below without having to go and change the 63 every time, so I hope you enjoyed this video, remember to leave a like, and very important also leave comments, leave comments on whatever scripts you would like to see me try and do, I'm no expert by any means, but you know, it'll give you a chance to see me struggle with writing some code, and trying to figure things out online, and we'll all learn something from it, okay, don't forget also at the bottom of every description of every video, YouTube video that I post, I post my invite link to the Xtrades community, that means that if you want to buy an Xtrades membership, you can use my invite link, and we both benefit. Me, I get points, forgetting of course, an invite, an invitee to join Xtrades using my link, and you, I will give you a personal, video guided tour, I'll go online with you on whatever streaming platform you want, whether it's Meet or Zoom or whatever, and I will take you through the Xtrades community, that means I will take you through the Discord and show you the do's and don'ts, and the advantages, pros and cons, different things you can do in Discord, Xtrades community, in order to take alerts and whatnot, and I will also give you of course the guided tour to the Xtrades web app, and the web app is awesome because you can filter alerts, right, and that means that you will only get alerts from, let's say, analysts that you have grown confident with, right, analysts that you like the way they present, analysts that you like the way they, you know, they give you their entries and exits and some sort of update throughout the, you know, throughout the duration of the trade, it lets you filter through certain stocks and options, if you only want stocks, then you only get stock alerts, if you only want options, then you only get options alerts, you will also only get those alerts for certain risk levels, right, if you are high or moderate or not very not very keen on risk, then you won't get the risky lottoes, for example, and you can also filter them by whether you want to just day trade or swing trade, okay, or scalp, now some people love to scalp and they make, you know, 10, 20 bucks every trade and they do that five, six, seven times a day and they end up with, you know, over 200 bucks a day, so all right, that was it for this ThinkScript video and I will see you in the next one. Have a great one, guys.