Data selection in algorithmic scripting

Coinpocalypse
6 min readFeb 18, 2019

--

As I mentioned in one of the earlier pieces I wrote, data selection in scripting is in my opinion one of the harder subjects in creating a stable and profitable bot. In IT it’s a well known saying: “Garbage in, garbage out.” Writing a bot is one thing, but the data you decide to feed it and let it analyse, will determine - in great part - the reliability and stability of your bot and therefore your future outcome.

All calculations have a source (data type) and they need to be specified, so the code knows what to calculate with. It is absolutely worth spending some of your time on this subject, to get a good understanding of how these data types affect your scripts. It takes a while to wrap your head around them and how/when/where you apply them. FYI: I am by no means an expert in this field, so look at this as nothing more than anecdotal experience.

I will write my way through the basics of what I think will help you on your way and in this case that means plotting the OHLC candles.

There are four basic candle values: open, high, low, close. When you select one of them, it really means you select the following data: “The open/high/low/close data point of the time period you applied to the instrument.”

If we would plot these values in the simplest way, it would look like this in code (Figure 1) and on the chart (Figure 2):

Figure 1. Simple data type plot code
Figure 2. How the plot code looks on a chart

Something to think about: only the open is known at the start of a period, assuming the high or low of a period is not the same as the open (which it can be). Only the close is known when a period is ended.

So, now you know how TradingView candlesticks are plotted, you can imagine the line chart, which is only plotted using the close and is therefore a lot “cleaner”.

Different timeframes

What if I want to use data from a different timeframe then the one I’m currently looking at? This requires the use of the security() function, which in Pine Script is used like this:

Figure 3. Pine Script security function

This function will need a symbol (including an exchange) like the following: “BINANCE:BTCUSDT”, so it will know where to pull the data from. If you want to pull data from chart you’re looking at, just use tickerid.

Then it looks at the resolution, which is the timeframe, for the 15 minute chart this is “15”, for the 4 hour chart it’s “240” and for the daily chart it’s “D”. If you want to pull data from the timeframe you’re looking at, you enter period.

The expression it needs is the data type, which I just discussed.

gaps and lookahead are more advanced parameters of this function, which are by default set to off and most of you are better off for it. If you want to play around with these settings, you should check this link.

If I’m looking at a 15 minute Bitcoin chart and i want to see the daily data, I will need to code the following:

Figure 4. Different timeframe selection.
Figure 5. Result of different timeframe selection

This worked perfectly, but it would still be nice to turn this into a more dynamic piece of code, so you can edit the values through a menu. I would like a selection of resolutions and expressions to choose from.

Figure 6. Dynamic timeframe and data selection

The results from this code above is exactly the same as in Figure 5, so let’s a bit to give it more functionality. What if you don’t want to see all four plots? You should be able to switch them off. We do this by creating an option in the menu for each plot and altering the lines with plot function. You do this by using a language operator.

Figure 7. Language operator

You should read this as following: If ‘expr1' is, it will execute ‘expr2’. If not, then it will execute ‘expr3’. Without this making much sense in a grammatical way, it does show the opportunity for using this as a switch. Let me show you how this looks in code, it makes more sense in my opinion.

Figure 8. Language operator coded

You can see that I added a showPlot variable for each line, which can be set to true or false. Then, in the plot codes, I added the language operator which now reads a lot better: If showPlot is true, then show Data or else (false) do nothing (na). In the menu it looks like this:

Figure 9. A dynamic data selection menu

So, now i can plot one line (by turning 3 of them off) with a lesser used data type of my selection, HLC3 in this instance, which stands for (high + low + close) / 3, on a daily timeframe.

Figure 10. HLC3 `data type on daily timeframe

Exotic data inputs

Experimenting with data types makes your bot more versatile and sometimes it definitely makes more sense to use one instead of the other. What would happen if you choose to use very different inputs to use in your calculations? What about using OBV as the calculations for Bollinger Bands?

Extra Dimensions

If playing with data types makes your bot more versatile, then playing with timeframes opens a door to new dimensions for your bot, by being able to give it instructions such as:

  • Only buy/long when the daily close is above the SMA200 and the RSI is leaving oversold conditions on the 1 hour timeframe
  • Do not sell/short when the 4 hour close is above the Ichimoku Tenkan-sen and 30 minute OBV is still rising
  • Do not execute a buy/long or sell/short at all when the daily Bitcoin ATR (volatility) is below 4%.

These are just examples, but demonstrate the newfound abilities.

I hope this piece will help you in trying out a lot of combinations, i have no doubt there is winner in there, thanks for reading!

You can follow me on Twitter, where you can also find the link to my bot which shares it’s calls for free on Telegram! Im also on TradingView, where you can see and use my scripts.

--

--

Coinpocalypse

Technical Analyst with a focus on indicators and automated trading