Tradingview: Opening A Window

Often there are times in trading when we receive a signal but do not wish to act on it straight away. A classic example of this is waiting to confirm a signal before entering a position. In this post, we will take a look at how to open a window, keep it open and also close it appropriately when either the window expires or we enter a position.

The Window

Opening a window is as easy as entering a position. We just check if conditions to open it are true. The trickier part is to keep it open and then close it properly when the window is no longer valid/needed. We shall achieve this with a ternary conditional operator (confusing term? click the link!)

Beginners Beware

This post may not be suitable for absolute beginners. It assumes a certain level of knowledge in pine script. For example, further explanation is provided only the code related to opening and closing a window. If you are completely new to pine script, check out the getting started page.

Code Introduction

To give some context to the window, a little introduction to the strategy is required. The example code below will implement a crude breakout strategy. It will attempt to enter a position only after price has retraced by a given amount AND then bounced back to continue the uptrend/downtrend. Additionally, it needs to do this all within a certain time limit from a pivot. The reasoning for a time limit is that a quick recovery from a good size retracement might indicate strong sentiment.

To meet this objective, our code shall need to be able to:

  • Identify pivots/swings in price
  • Track how far price retraces from the swing/high low
  • Open a window if price retraces by x% or more
  • Catch a break above/below the swing high/low.
  • Catch the break within a given bar limit otherwise, close the window.

Note: This code is not intended to be used as a strategy in its own right. It does not have any exit, position management or sizing strategy. It will simply flip back and forth, reversing on the opposite signal. As such, this example should just be treated as a technical exercise.

Example Code

Code Commentary

The key part of the code that focuses on the opening and closing of the window is contained in the following lines:

Since the example uses version 3 of pinescript, we need to declare the buy_windowand sell_window as false to begin with. This allows us to reference itself in the following lines e.g. buy_window[1].

Following this, we dive into the window logic. We do this with the ternary conditional operator mentioned at the start of this post. Because a ternary conditional operator will take the value of the first condition that is true,  we need to create condition checks in the correct order. In this case, we shall put anything that should close the window first.

Using the buy_windowas an example, we first check to see if we are in a long trade. If so, we close the long window. It is no longer needed.  This is something to be aware of if you look at the charts and wonder why a window is not opening. It is likely you are already in a trade. To assist with this we plot some circles at the bottom of the chart to tell you if the strategy is in a long, short or flat position.

Next, we see if there is a new pivot. If there is, we reset the window because our breakout and retracement measurement levels will change. They are always measured from the most recent pivot in this example.

The final close check we make before we consider opening the window is to check whether we are within our breakout time limit. If not, we close the window again!

Assuming none of these are true, then we check to see our retracement level (the percentage price has moved from the pivot) is met. If so we have the conditions to open the window. Following this, the window should now stay open until any of the close conditions are met. The reason for this is thanks to the value is assigned to the window when NONE of the conditions described above are met. When no conditions are met (open or close), we simply pass it the previous bar’s value e.g buy_window[1]. So if it was open, it stays open, if it was closed, it stays closed!

On the Charts

Once up and running, you should see something which looks like the following image.

Opening a Window Example on the charts

Let us know if you have any difficulty with this post in the comments below!

Support The Site!

If you like this article and are thinking to become a Tradingview subscriber, you can support the site by clicking on the affiliate link below before signing up!

https://tradingview.go2cloud.org/SHpB