TradingView Pine Script Tutorial 32: Pine Script Coding Conventions – Styling Guide

TradingView Pine Script Tutorial 32: Pine Script Coding Conventions – Styling Guide

YouTube Video

YouTube Video Transcript

ayyyy this is David for big bits and welcome back in this video we’re gonna be talking about pine script coding conventions you might also refer to this as a style guide for your pine scripts or your code and pine and that you use on tradingview now if you’re not familiar with the channel please subscribe because if you’re watching this video then you’re probably interested in pine script and this channel we do a lot of pine script tutorials and talk about tradingview cryptocurrency and things like that as well so definitely subscribe if you haven’t already and just watch the whole video because there’s a lot of good information and this one the whole way through I’m not just showing you something completed I want to go over all of the styling guide or the coding conventions and explain to you exactly what each of them mean and kind of why they’re important as well and we’re gonna be looking at an example here from the pine coders group now they is a group of people who were to support Pine and trading view in that aspect with pine and they have created this coding conventions I like to refer to them as a style guide because that’s what I’m used to referring to these sorts of things as so essentially we’re trying to structure our code and make it look a certain way over and over that way if somebody were to write a script it would be very easy for other more advanced programmers to pick it up and read it now if you’re a beginner programmer it doesn’t really matter too much about these although there are some general rules that you might like to know in here about the syntax of pine and things that will help you make your code look a little bit better regardless of how you write your variable names but for those of you who are a little bit more advanced or an intermediate level programmer this might be a good idea to do especially if you’re publishing scripts on tradingview I’m going to try to start using some of these but where I work with a lot of tutorial videos and beginners I tend to make it the most readable code that I can and some these names aren’t really that that well suited for being readable for beginners so let’s go ahead and get started there’s a four there’s four different categories here for the table of contents our script structure naming conventions spacing and line wrapping now the first two are probably gonna be the biggest ones but I wanted to share this link with you here this is the pine coders script that we’re gonna be using as a reference here for all the stuff they have most of the items in their style guide in here and what isn’t in here it’s gonna be easy enough to tell you what’s going on then I didn’t personally write a script for this one because it would’ve been wasting my time there’s plenty of other scripts out here that have done their code with this style guide this coding convention that I don’t really have to do it myself I can just show you what they’ve done okay so the first thing and we’ve talked about this in the territorial series is the first thing you need in your script structure is the version name and this is a comment that tells the compiler what version of pine you’re going to use to compile your code in and once it tries to compile it knows what rules to use from which particular version so this is the first thing you need to do in all of your scripts you can see here they have declared the version number first in that comment now you might also like to add other notes such as the author the version number the name of the script and some other notes that would be useful for this now they’ve also mentioned that here using the version you can add your comments whatever you’d like it doesn’t have to be structured the same way as this after you do your versioning now after you’re done with all of your comments including the versioning then they would like to see the actual study or strategy function called now if you’re not familiar with pine or you haven’t watched the tutorial series we’ve done these two functions the studying strategy are pretty much what tell pine whether you’re trying to actually execute or backtest a strategy or if you’re just going to kind of use this as an indicator and not perform any back testing or try to do any sort of automated trading through their connected brokerages now let’s actually get back here let’s take a look at the next one so this is something that’s kind of common for those of us who actually work with code on a more routine basis and that is working with inputs these are essentially variables and what you’re doing with your inputs you’re placing them directly after your study or strategy and that’s because first of all the studying strategy is required to be in the script so you might as well do that first we have to do our inputs next because we’re pretty much declaring our variables that we know we must have and these are also user settings that you can interact with on the chart and you can change by just selecting different values or typing in different values now if we go back here I don’t see okay here they are I thought they didn’t have them at first but here are the inputs now we we have covered all of these on our tutorial series so you should know what these are if you’ve watched those what they actually mean and we’ll go into these a little bit more a minute but your inputs and your variables you know those declarations are due here directly after the study or the strategy and that’s what it kind of mentions here the variable initialize initializations pretty much right after your inputs so perhaps here we would have moved the well actually the variables here are actually used as part of their options so that’s why they appear to be a bit out of order so they actually require these for their inputs so they do those first but you can see here there’s another variable and that is declared immediately after now for the rest of their code they’ve actually defined these sections for you so this is the section for the functions you can see there’s several functions in a row here and we’ll go over exactly kind of what’s going on here in a minute but our functions that we’re gonna use for the study or the strategy come next you see they have quite a few for this particular one now what we do after that is we work on our calculations so now we have the functions that we need to do our calculations or functions that we need for other reasons such as colors and we also have all the variables we’re gonna work with or at least the ones that we needed beforehand and now we can actually calculate values and save those or work with those however we need to so what they do next is they have a section for their calculations now there are a few new variables here but those are because we are working with existing variables and saving into new ones and you don’t necessarily have to do that if you want to update the value of an existing variable you can always do that with this operator here and this variable has already been declared and you’re resetting its value to whatever the result is of this statement okay so now that we have that and we have our calculations done we pretty much have all of our data that we’re going to be using for the study or the strategy we’ve already calculated everything so everything you can think of that you want to do with your script you should have all of the information you need to make your decisions at this point and what we’re going to be doing next is doing a plot now this is the part where we’re actually drawing on the chart if you choose to do so if you’re doing a strategy you don’t necessarily need to draw anything on the chart because you’re gonna be focused more on the backtest results although it is often pretty handy to help troubleshoot your strategies if you are plotting things on the chart and there’s a lot of different ways and different things you can actually plot on a chart so go back and check out videos on that or check out the pine script reference manual there’s a lot more things that we haven’t even covered that you can actually plot now as far as the plots go usually with studies that’s pretty much all you do is just plot things so that you can look at them and compare the data although it does get a little bit more interesting than that with using alert conditions and we’ll touch on that in just a moment but the plot calls where we’re putting things on the chart or on their own chart not overlaid on the one you’re looking at you can plot those as well now after we’ve plotted everything on the chart that we’re going to be using you can see they had a section for plots here then we would have a section for our strategy calls now this is if you were only using the strategy function at the very beginning so up here at the beginning we would have had to use strategy instead of study and we would have needed to call these strategy functions down here after our plots and we would have told it to enter or exit positions based on other logic that we have come up with or you can just tell it to do it based on whatever you come up with you don’t have to do a bunch of calculations if you don’t want to you can just do it however you need it okay now the alert conditions are the last thing and if you haven’t watched the video on the custom alerts that I’ve done that will help you a lot if you go back and check on what that is for the alert conditions essentially with indicators what you can do is you can create your own custom alerts so instead of just using the built-in alert conditions built into trading views such as you know moving average one crossing over moving average to you can actually get a little bit more in depth with a single alert you can say you know I want moving average one to cross over moving average to they both have to be above moving average three and moving average three has to be moving down something like that you can do that with just one alert that way and it will be very beneficial for you to reduce the amount of alerts you have in that way if you haven’t been using it that way so that kind of takes care of the structure and the order of how you should be doing things in pine now you’ll notice I’ve pretty much done it like this in most of my videos but you can always mix these up these are just style guidelines you don’t have to do it this way although you’ll notice if you try to do it a different way sometimes you’ll have issues with your order and the way things are made and you’re probably going to end up doing something like this anyway okay so next step is are the naming conventions and this is the part that’s really gonna throw the beginners off because it makes things look really difficult for beginners and it makes me look harder to read so with variable names we are using camelcase based on their coding conventions a camel case just means you start off in all lowercase and then the next word within that name is capitalized so you can see it starts lowercase here and then the next word is capitalized but if you have more than one word after that then all of the other words are capitalized as well so that’s important to note with camel case now for me personally I have been doing in the videos a little bit differently I will capitalize the first word and I will also capitalize the acronyms as well so EMA here based on theirs this is all lowercase but for me I would capitalize e in and a and then also length and mine now there’s show signal here I would have just capitalized the S at the beginning of show and also kept signal capitalized as well so like I said you can do this however you want however is best readable for you but if you’re gonna be publishing scripts a lot in pine I definitely recommend you follow these it’s gonna help you a lot now for function names this is simply just the name of your function all we’re doing is just prefixing the function with F underscore and then we also use the camel case after that so for example you can see one here f underscore days in months so the camel case starts after the underscore and you’re gonna be lowercase on the first one and then uppercase on the beginning of each word after that now for function parameter names this is often helpful in many different programming languages and that is you put an underscore before your variable names that you use as parameters in your functions now let’s go to one of the actual functions they have here and this might help some of you all right you can see this function underscore TF res in minutes it has a parameter underscore re s and you can see how it’s used here later on in the function so you’re passing in a value into the function and it’s referring to it with the name that you set in the parameter and with the naming on that I forgot to mention this is also camelcase as well similar to our variable names and obviously start with lowercase and the camel case afterwards I’m sorry I thought I got missed up there but it’s actually the parameter names that we’re starting with those should also be camel case but it doesn’t appear they have any better long enough although they might not be using camel case here I thought they did if they don’t that’s my apologies so yeah these are all lowercase for them although like I said you can always do these however you want but I would probably use camel case on the parameter names as well just because that’s what they’re dealing everywhere else here now their dependencies this is just letting you know that your function uses a variable that was already declared out outside of the scope of the function and what I mean by that is the function by default usually just works with the data within the function now a dependency is when you need to reference data outside of this particular function so for example we need to know the length multiplier but that was an input so we are depending on this length multiplier up here and we need to identify that if you’re following these coding conventions now they also recommend that you can also pass this in as a parameter that way you don’t have to actually define the fact that you’re using a dependency and this makes your code to where you have to have it correct before it will compile if you don’t pass in both of these then it won’t compile correctly it’ll throw an error so if you don’t pass in the length and you just do the multiplier then it will give you an error that you need to use both parameters and then you’ll have to put in the input value as your parameter and it’ll be completely fine after that now the local scope variables this is essentially just variables that we use within a function or within a as it says a local scope we’re not working with the entire study or strategy here these variables pretty much get destroyed when we’re done with the current segment of code that they’re in so within a function within an if statement or within a for loop if we declare a variable there once we are out of the function or out of the if or for loop then that variable it’s just kind of gone you don’t get to use it in the rest of your study or your strategy at that point now that pretty much covers all the variable stuff and like I said throwing in all its underscores lowercases prefixes things like that can confuse beginners but if you’re even intermediate or advanced and this is probably pretty common and something you’ve seen quite a bit a lot of all right onto spacing this is very simple let me know let me go up and make sure there aren’t any other sections spacing and line wrapping so these are the final two sections there’s not much to it here but this basically just says that if you are using an operator you need a space on both sides of it so you’re going to have a space before the operator and also after and that needs to be for all of them it’s pretty simple but it makes the code much more readable as well and I tend to do this myself anyways without having to have a convention to tell me that and you likely probably do as well just because it is easier to read for most people now line wrapping this isn’t something we’ve particularly covered although we have talked about spacing in the script tutorials before and how if you’re with inside of an if statement or a function or a for loop you have to do four spaces on the next line to have your code executed within that particular scope where as line wrapping is just allowing you to continue writing the current line of code that you’re in on the next line so that the code can be more readable now you can see this here as well we are working with a plot function here and there’s a lot of different properties to this plot and we’re defining those values but if this was all in one line it would come out really here and it might be a little bit harder to read so what you’re gonna do is you’re gonna start a new line and you’re just gonna do two spaces now you can’t use your tab key to do that because the tab key will do four spaces so you’re gonna have to manually tap that space key twice and then you can set your properties and make sure you use a comma between your properties just like you would if it was a single line and then you can start your new line as many times as you need so that pretty much does it for this particular video on the coding conventions of the styling guide for Pond scripty the people over at pine coders have done a great job I definitely recommend you check out their profile they have a bunch of great scripts with a bunch of great resources in it I can’t begin to say enough just how much great stuff they do for pine that a lot of people aren’t even aware of yet so definitely check out their scripts their profile on training view is of course pine coders now if you want to check out mine and you haven’t already you can always check out my profile in the description of the video and while you’re down there please like the video and subscribe as well we do these types of videos we can up-to-date with tradingview crypto and other things so definitely if you liked the video subscribe and you’ll get updates on when we do new videos here on the channel now also if you’ve been following along on pine or you’re interested in a paid plan on trading view please check out the referral link also in the description of the video ever get you $30 off your upgraded paid plan on trading view as well as $30 for me on my next paid plan as well and to help keep the channel going I’d really appreciate it if you could do that if you’re considering that paid plan but other than that we are done for this video I’d like to thank you and welcome you back to watch another video on pine here soon but other than that have a nice day [Music] [Music] [Music]

YouTube Video Description

< br/> ๐Ÿ”ป๐Ÿ”ป๐Ÿ”ป๐Ÿ”ป IMPORTANT LINKS BELOW ๐Ÿ”ป๐Ÿ”ป๐Ÿ”ป๐Ÿ”ป
๐Ÿ“ˆ Sign up for a paid plan at TradingView and receive a $30 credit: https://www.tradingview.com/gopro/?share_your_love=BigBitsIO

๐Ÿ“ฐ๐Ÿ“ฐ๐Ÿ“ฐ๐Ÿ“ฐ๐Ÿ“ฐ๐Ÿ“ฐ๐Ÿ“ฐ๐Ÿ“ฐ๐Ÿ“ฐ๐Ÿ“ฐ
Relative URLs:
๐Ÿ“ฐ๐Ÿ“ฐ๐Ÿ“ฐ๐Ÿ“ฐ๐Ÿ“ฐ๐Ÿ“ฐ๐Ÿ“ฐ๐Ÿ“ฐ๐Ÿ“ฐ๐Ÿ“ฐ
๐Ÿ—ž https://www.pinecoders.com/coding_conventions/
๐Ÿ—ž https://www.tradingview.com/script/90mqACUV-MTF-Selection-Framework-PineCoders-FAQ/

๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ
Social and other public profiles
๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ
๐Ÿ’ป Website: https://www.bigbits.io
๐ŸŽฎ Discord: https://discord.gg/rapMn4z
๐Ÿฆ Twitter: https://twitter.com/BigBitsIO
๐Ÿ“” Facebook: https://www.facebook.com/BigBitsIO/
๐Ÿ‘จโ€๐Ÿ’ปGitHub: https://github.com/BigBitsIO
๐Ÿ“ˆTradingView: https://www.tradingview.com/u/BigBitsIO

๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ
Referral links
๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ๐Ÿ’ฐ
๐Ÿ“ˆ Buy, Sell and Trade Crypto on Binance.US with LOW fees: https://www.binance.us/?ref=35105151
๐Ÿ™‹โ€โ™‚๏ธ Want to buy crypto? Get $10 of bitcoin w/ your first purchase over $100: https://www.coinbase.com/join/johnso_dxz
๐Ÿ“ˆ Sign up for a paid plan at TradingView and receive a $30 credit: https://www.tradingview.com/gopro/?share_your_love=BigBitsIO
๐Ÿ’ป Browse privately and get rewarded with Brave Browser: https://brave.com/big406
๐Ÿ•น Receive bonus perks when purchasing Lightnite Game: https://lightnite.io/ref=BigBits
VIEW ALL HERE: https://bigbits.io/bigbits-referrals/

โ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธ
DISCLAIMER: All my videos are for educational and entertainment purposes only. Nothing in this or any of my videos should be interpreted as financial advice or a recommendation to buy or sell any sort of security or investment including all types of crypto coins and tokens. Consult with a professional financial advisor before making any financial decisions. Investing in general and particularly with crypto trading especially is risky and has the potential for one to lose most or all of the initial investment. In simple terms, you are responsible for your actions when trading.
โ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธโ—๏ธ

#bitcoin #crypto #cryptocurrencies #tradingview #binance #binanceUS #coinbase

This channel focuses on Bitcoin, Ethereum, LiteCoin, Ripple, Link, Basic Attention Token and almost all cryptocurrencies that demand attention. Please like the video if you liked the video, and subscribe if you like these types of videos. David from BigBits is an experienced Software Engineer, but no one is perfect, If you find any issues with any of the open-source, free code, or code shown in videos please comment to let us know what to fix, we listen to our viewers!