Categories
Forex Daily Topic Forex System Design

Trading System design -Creating Your Strategy with Tradingview’s Pine Script – Part 2

In part 1 of this article series, we have created the Stochastic RSI indicator as part of our idea for a scalping strategy. Now that we have it functional, we will make the bull/bear phases and visually inspect whether it captures the turning market’s turning points.

Possible ways to create bull/bear slices

Our Stochastic RSI consists of two lines, k and d, and two trigger lines, ob and os. Therefore we can use multiple variants that may allow the creation of bull/bear price legs. Let’s consider the following 3

Variant 1 – The transition occurs at the SRSI entrance of the oversold or overbought regions.

Bull: The d-line crosses under the ob-line, which indicates it is into the overbought area
Bear: The d-line crosses over the os-line, indicating d‘s entry into the oversold area.

Code:
if crossunder (d, os)
    SRSI_Long := true
    SRSI_Short := false
else if crossover (d, ob)
    SRSI_Long := false
    SRSI_Short := true 
else
    SRSI_Long := SRSI_Long[1]
    SRSI_Short := SRSI_Short[1]

This code creates a condition SRSI_Long at the cross of d under os, which holds until d crosses over ob and reverses it, creating an SRSI_Short state. This condition is only modified by d crossing under os.
The else statement ensures the condition does not change from the previous bar.

Once we have defined the bull and bear segments, we can color-shade them to visualize them in the chart. To do it, we will use the bgcolor() function.

bgcolor(SRSI_Long ? color.green: na)
bgcolor(SRSI_Short ? color.red: na)

The first statement asks the condition of SRI-Long ( the ? sign). If true, the background color changes to green. Otherwise, no change (an). The second statement behaves similarly for SRSI_Short.

Let’s see how this piece of code behaves in the BTCUSD chart.

Variant 1 triggers the transitions too early. We see that on many occasions when the stochastic RSI enters the overbought or oversold region, it is more a signal of trend strength than a turning point.


Variant 2 – The transition occurs at D and K’s crossovers if in the overbought/oversold regions.

Bull: the k-line crosses over the d-line, if below os ( inside the oversold region. We ignore crosses in the mid-area)
Bear: the k-line crosses under the d-line, if above ob ( in the overbought area. We ignore crosses in the mid-area)

Code:

// creating the long and short conditions for case 2

if crossover(k,d) and d < os
     SRSI_Long := true
     SRSI_Short := false

else if crossunder(k,d) and d > ob
     SRSI_Long := false
     SRSI_Short := true
else
     SRSI_Long := SRSI_Long[1]
     SRSI_Short := SRSI_Short[1]
// bacground color change
bgcolor(SRSI_Long ? color.green: na) 
bgcolor(SRSI_Short ? color.red: na)

The last section for the background change is similar to Variant 1.

Let’s see how it behaves in the chart.

Variant 2 is an improvement. We see that the bull and bear phases match the actual movements of the market, although entries are still a bit early, and in some cases, it missed the right direction. It can be useful as a trigger signal, provided we can filter out the faulty signals.


Variant 3 – the transition occurs when d moved to the overbought or oversold region and, later, crosses to the mid-area.

Bull: the d-line crosses over the os-line
Bear: the d-line crosses under the ob-line.

if crossover (d, os)
    SRSI_Long := true
    SRSI_Short := false
else if crossunder (d, ob)
    SRSI_Long := false
    SRSI_Short := true 
else
    SRSI_Long := SRSI_Long[1]
    SRSI_Short := SRSI_Short[1]
// bacground color change
bgcolor(SRSI_Long ? color.green: na) 
bgcolor(SRSI_Short ? color.red: na)

 

And this is how it behaves in the chart.


Variant 3 lags the turning points slightly, but this quality makes it more robust, as, on most occasions, it’s right about the market direction. This signal, combined with the right take-profit, may create a high-probability trade strategy.

Let’s try this one. But this will be resolved in our next and last article of this series.

Stay tuned!

Categories
Forex Daily Topic Forex System Design

Trading System design -Creating Your Strategy with Tradingview’s Pine Script – Part 1

As promised, in this article, we will go through the steps to create a custom strategy, from the initial idea to the implementation of signals, stops, and targets.

The skeleton of a trading Strategy

To create a strategy programmatically is relatively simple. We need to define the Parameters and the trade rules first, followed by the position sizing algorithm, the entry commands, and the stop-loss and take-profit settings.

Visualizing the idea

Human beings are visual. We may think our trading idea is fantastic, but translating it into code may not be straightforward. It is much easier to detect the errors if we see our rules depicted on a chart.

With the parameter declarations and trade rules, we can create an indicator first, so we can see how it appears. After we are happy with the visual 

The idea

For our example, we will use a simple yet quite exciting indicator called Stochastic RSI, which applies the Stochastic study to the RSI values. This operation smoothes the RSI, and it reveals much better the turning points on mean-reverting markets, such as in Forex. Let’s see how it behaves as a naked strategy.

Diving into the process

First, you need to open an account with Tradingview. Once we are in, we create a new layout.

Then we open the Pine Editor.

It appears in the bottom left of your layout. Click on it… and it shows with a basic skeleton code.

The Stochastic RSI code.

As said, to create the Stochastic RSI indicator, we will make the RSI and then apply the stochastic algorithm to it.

1 study(title="Stochastic-RSI", format=format.price, overlay = false)

This first line declares the code to be a study, called Stochastic-RSI.  

format = format.price is used for selecting the formatting of output as prices in the study function.

Overlay = false means we desire the RSI lines to appear in a separate section. If it were a moving average to be plotted with the prices, overlay should be set to true.

RSIlength = input(14, "RSI-Length", minval=1)

We define the RSI length as an input parameter called RSI-Length.

src = input(close, title="RSI Source")

The variable src will collect the input values on every bar. The default is the bar close, but it may be modified by other values such as (o+c)/2.

myrsi = rsi(src, RSIlength)

This line creates the variable myrsi that stores the time series of the rsi.

This completes the calculation of the RSI. 

smooth_K = input(3, "K", minval=1)
smooth_D = input(3, "D", minval=1)

These two lines create the smoothing values of the stochastic %K and %D. Since it comes from input, they can be changed at will.

Stochlength = input(14, "Stochastic Length", minval=1)

This code defined the variable lengthStoch, computed from the input parameter.

k = sma(stoch(rsi1, rsi1, rsi1, Stochlength), smooth_K)
d = sma(k, smooth_D)

These two lines completes the calculation of the stochastic rsi.

plot(k, "K", color=color.white) - Plot a white k line 
plot(d, "D", color=color.red) - Plot a red d line.

To end this study, we will plot the overbought and oversold limits of 80 and 20, filling the mid-band with a distinctive color.

t0 = hline(80, "Upper Band", color=color.maroon)
t1 = hline(20, "Lower Band", color=color.maroon)
fill(t0, t1, color=color.purple, transp=80, title="Background")

The complete code ends as:

 

// This source code is subject to the terms of 
// the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © forex-academy
//@version=4
study(title="Stochastic-RSI", format=format.price, overlay = false)

RSIlength = input(14, "RSI-Length", minval=1)
src = input(close, title="RSI Source")
myrsi = rsi(src, RSIlength)

smooth_K = input(3, "K", minval=1)
smooth_D = input(3, "D", minval=1)
Stochlength = input(14, "Stochastic Length", minval=1)

k = sma(stoch(myrsi, myrsi, myrsi, Stochlength), smooth_K)
d = sma(k, smooth_D)

plot(k, "K", color=color.white)
plot(d, "D", color=color.red)

t0 = hline(80, "Upper Band", color=color.maroon)
t1 = hline(20, "Lower Band", color=color.maroon)
fill(t0, t1, color=color.teal, transp=80, title="Background")

This code is shown in our layout as

Stay tuned for the second part of this article, where we will evolve the Stochastic RSI into a viable strategy.