Crafting a Basic Trading Strategy in TradingView: A Guide
Written on
Chapter 1: Introduction to the Stochastic Oscillator
The Stochastic Oscillator is widely recognized as a valuable technical indicator. It utilizes a straightforward yet effective formula to identify rapid oversold and overbought conditions in the market. This article details a specific trading strategy based on this indicator.
I recently published a new book following the success of my earlier work, "The Book of Trading Strategies." This new edition includes advanced trend-following indicators and strategies, supported by a continuously updated GitHub repository. Additionally, it retains original colors while being optimized for printing costs. If this piques your interest, feel free to check out the link to purchase it on Amazon, or reach out to me on LinkedIn for a PDF version.
Chapter 2: Understanding the Stochastic Oscillator
The Stochastic Oscillator is a bounded technical indicator that aims to identify oversold and overbought conditions by utilizing high and low values within a specific normalization formula. This formula ensures that the oscillator remains within a range of 0 to 100. Typically, traders use this oscillator in a contrarian manner, interpreting oversold and overbought signals to anticipate market corrections.
An overbought condition suggests that the market is exceedingly bullish and is likely to consolidate, hinting at a potential bearish trend. Conversely, an oversold condition indicates that the market is excessively bearish and may soon rebound, signaling a bullish opportunity. Thus, the Stochastic Oscillator acts as a contrarian indicator, forecasting expected market reactions during extreme scenarios.
Why do we focus on "Creating the Stochastic Oscillator in Pine Script" when it is already integrated into TradingView? The reason is to empower ourselves with the coding skills necessary to develop any indicator from scratch in Pine Script. This knowledge enables us to confidently back-test and modify any strategy. Furthermore, it opens the door to coding variations of the indicator, such as the Stochastic Smoothing Oscillator, which has been discussed in previous articles.
Chapter 3: Coding the Stochastic Oscillator in Pine Script
To begin, we need to select the version of Pine Script, which is TradingView's programming language. We will opt for the latest version. Since we're creating an indicator, we must declare this intention using the indicator() function. We will provide two arguments: the title, "Our Stochastic Oscillator," and a Boolean argument for overlay. By setting this to false, we indicate that the oscillator will be displayed in a separate panel.
indicator("Our Stochastic Oscillator", overlay = false)
Next, we define input values such as the lookback period, oversold level, and overbought level. The input() function allows us to adjust these parameters directly from the chart without needing to recode them. The title denotes the variable name, and defval specifies the default value, which we will set to 13, 20, and 80 respectively.
lookback = input(title = 'Period', defval = 13)
oversold_level = input(title = 'Oversold', defval = 20)
overbought_level = input(title = 'Overbought', defval = 80)
Now we calculate the highest high and the lowest low over the lookback period using the ta.highest() and ta.lowest() functions.
highest = ta.highest(high, lookback)
lowest = ta.lowest(low, lookback)
The final step involves applying the formula. We define the stochastic_K (%K) and stochastic_D (%D), where %D is the 3-period simple moving average of %K. The average is computed using the ta.sma() function.
stochastic_K = ((close - lowest) / (highest - lowest)) * 100
stochastic_D = ta.sma(stochastic_K, 3)
Let’s visualize our results and compare them to the built-in stochastic oscillator script.
plot(stochastic_K)
plot(stochastic_D, color = color.orange)
hline(oversold_level)
hline(overbought_level)
Chapter 4: Analyzing and Implementing the Strategy
The trading strategy is straightforward: we will enter positions based on the exit from extreme conditions. The trading criteria are as follows:
- A buy signal occurs when the current 13-period stochastic oscillator is above 20, while the previous value is below 20.
- A sell signal arises when the current 13-period stochastic oscillator is below 80, and the previous value is above 80.
We will begin coding these signal conditions. The stochastic_K[1] notation references the previous value of the current oscillator.
buy_signal = stochastic_K > oversold_level and stochastic_K[1] < oversold_level
sell_signal = stochastic_K < overbought_level and stochastic_K[1] > overbought_level
To simulate orders, we will use the strategy.entry() built-in function, which allows us to take long or short positions whenever a signal is triggered.
strategy.entry('Buy', strategy.long, when = buy_signal)
strategy.entry('Sell', strategy.short, when = sell_signal)
Chapter 5: Conclusion and Final Thoughts
Always remember to conduct your back-tests. It's essential to maintain a critical perspective on the strategies of others. What works for one trader may not necessarily work for you.
I advocate for learning through practice rather than mere imitation. Understanding the functions, intuition, and conditions of a strategy enables you to refine and enhance it before deploying it in live trading. My choice not to provide specific back-testing results encourages readers to explore and develop the strategy further.
If you're interested in additional technical indicators and strategies, my book may be of value to you.
Lastly, I have recently launched an NFT collection that aims to support various humanitarian and medical initiatives. The Society of Light collection will contribute a percentage of sales to the designated charity associated with each NFT. Here are some highlights of owning these NFTs:
- Potential for high returns: Focusing on marketing and promotion, I aim to maximize their secondary market value.
- Art collection and portfolio diversification: Investing in meaningful collectibles can be both rewarding and fulfilling.
- Flexible charitable donations: This approach allows for tailored contributions to your preferred causes.
- Free PDF copy of my book: Buyers of any NFT will receive a complimentary digital copy of my latest work.
Explore more about the Society of Light and help support a good cause.