2nd Pine Script Lesson: Coding the Entry Logic - Bollinger Band

Welcome back to our Trading View tutorial series! In this second lesson, be learning how to code the entry logic for a Bollinger Band indicator using Pine Script.

If you're new here and missed the first lesson, we highly recommend starting there as it provides a solid foundation for understanding the concepts we'll be covering today:
1st Pine Script Lesson: Coding an Indicator - Bollinger Band


In this hands-on lesson, we'll guide you through every step of coding the entry logic for your own Bollinger Band indicator using Pine Script. By the end of this lesson, you'll have a functional indicator that you can use to inform your trading decisions. So, sit back, grab a cup of coffee, and let's get started!

Code the entry logic

a) This is where we are calling the Mikilap function with two arguments:
- the coinpair and
- the timeframe we want to use.

// Calling the Mikilap function to start the calculation




b) In the function initiation we convert the strings into simple strings.

// Definition of a Pine Script individual function to handle the Request and avoid Repainting Errors




c) As we are calling the function to get an integer value, we have to define an output variable as an integer and place this variable as the last line in the local scope of the function code to return the integer value.



Step 1:
Using the lower bandwidth of the Bollinger Band based on SMA (close, 21) and a standard deviation of 2.0 and try to highlight bars, where close is next to the lower band


a) Requesting the values for the coinpair with request.security()



We recommend using repainting functions like request or barstate only in a local scope (inside a function) and not to request complex calculated values. For saving calculation capacity it is useful to only request the classic four OHLCs and do any calculation with these four after the request.security().


b) Calculation of the lower Bollinger Bands values as we need the global info, which type of source, length, and deviation value to use for the calculation, let‘s cut & paste the input for the Bollinger Band in the general starting section of the code and as we want to look for close values „next“ to the lower bandwidth, we need to define what „next“ means; let‘s do it in another input variable, perhaps we want to play with the definition later.



First, let‘s make it visible on the chart by re-writing the Bollinger Bandplot, which is not needed anymore.

// Calling the Mikilap function to start the calculation



// Output on the chart
// Part 2 - plotting a Band around the lower bandwidth of a Bollinger Band for the active CoinPair on the chart




c) Now we use the same calculation for the coinpair inside the function and start with the selection of the source (OHLC) to use, which is activein the respective input variable.

// Defintion of a Pine Script individual function to handle the Request and avoid Repainting Errors



d) As the bandwidth for the interesting close values is defined by our band, the only thing missing for the part of the Bollinger Band in our Mikilap indicator is to check if the close value of a bar is inside our band. As we are talking about closed bars, let‘s be sure that it is really closed by using barstate.isconfirmed (repainting built-in function!) and save it in a variable in the head of the function to avoid requesting this info too often.



Now let‘s check if the close value of a bar is inside our band.



And increase the output variable by 1 in case the close value is inside.



By using bb_entry[1], we are referring to the last bar next to the actual bar, because we want to enter on the opening of the bar after the criteria has been met.


e) And to make these possible entries visible, we want to place a label below the bar and show the entry price (=open value of the bar) as mouseover (tooltip). This should only happen if the active coinpair on the chart is the same coinpair, which is in the calculation of the function.




Note:
You will love labels (!) and in case you are looking for text symbols that can be used as labels, look here: messletters.com/en/symbols/

If you need help use the Pine Script Reference Manual, which explains 99% of everything in Pine Script, here: tradingview.com/pine-script-reference/v5/


f) As our function now returns different integer values (0 or 1), we can use this info to color the background on the actual chart in case it is 1.

// Calling the Mikilap function to start the calculation




g) To finish this little Pine Script lesson and to achieve our initial targets, we just need to integrate the second indicator (RSI) into the function. We want to use the RSI for 0,5 days (12 hours) and use it to ensure to not go into a long entry in an oversold (< 25) or overbought (> 70) market. We will use RSI (low, 12) within 25 to 45 as the range to go for.


Your tasks:
  • define new input variables for RSI: src_rsi and length_rsi
  • define new input variables for the RSI range we want to use: rsi_minand rsi_max(please use the „inline“ format of an input type)
  • calculate the RSI (src_rsi, length_rsi) inside our Mikilap-function
  • define a boolean variable (rsi_entry) to check if the calculated RSI value is inside the range (please add as last check the barstate_info)
  • add the RSI entry check to the Bollinger Band entry check to combine them



Congratulations on finishing the second lesson on Trading View - we hope you found it informative and engaging!

We're committed to providing you with valuable insights and practical knowledge throughout this tutorial series. So, we'd love to hear from you! Please leave a comment below with your suggestions on what you'd like us to focus on in the next lesson.

Thanks for joining us on this learning journey, and we're excited to continue exploring Trading View with you!
Beyond Technical Analysisbollinger_bandsbollingerbandstrategyBollinger Bands Width (BBW)BTCUSDTcopytradingTechnical IndicatorsmizarpinescriptTrend Analysis

También en:

Publicaciones relacionadas

Exención de responsabilidad