plot()function might put you to sleep. Note: This article assumes the reader is at least familiar with accessing HaasScript via the Haas Online Trade Server. If you do not know how to do this, please take a look at the first HaasScript tutorial.
ScopePlotting forms an integral part of any strategy. Although it is not absolutely required for automated trading, plotting does allow us to get a quick feel for how a strategy is performing without the need to scan through detailed logs. Furthermore, if you only want to create indicators for discretionary trading, then plotting is one of the most important aspects of your code. In this article, we will take a look at a the
plot()function, feed it some dummy data and play around with the arguments one by one. By the end of the tutorial you should have some familiarity with:
- Plotting horizontal lines
- Plotting series data
- Plotting to the main chart or subplots
- Changing colors
plot()function will likely be one of the most common functions that you ever use. You can provide either a single value to create horizontal line or provide a series of data (such as
closeprices) for some squiggly lines. Here is a link to the official documentation. Documentation: https://help.haasonline.com/api/haasscript/commands/charting#plot So without further ado, let’s go through the plotting options (otherwise known as arguments).
chartIdThe first parameter is
chartId. This argument tells HaasScript which chart to plot the line on. Some indicators like an RSI appear as subplots below the candlestick chart whilst other indicators such as Moving Averages are plotted with candlesticks on the main plot. In the example below, we simply plot a single value around the current
BTC/USDprice (at the time of writing!). Copy the following line into a blank new script and then hit play to reproduce.
Plot(0, 'Horizontal Line', 8300)As you can see, the
0in this example. That means anytime you want plot a line on the candlestick chart, you need to use
0. In programming zero is always the first number/value and so it makes sense that a
chartIdof zero would be the first chart available. Next, let’s target a sub-chart. We can do this by using
0. If a chart with the id
1does not exist, then it will create a new sub-chart with that ID below the candlestick chart like so: To demonstrate the “targeting” aspect, we can add a second line using the same
Plot(1, 'Horizontal Line', 8300) Plot(1, 'Another Line', 8500)Finally, let’s change the
chartIdof the second line to
2to see an example of creating multiple sub-charts.
lineNameNext up is an argument we are already using, it is line name! This is fairly self-explanatory as it will give your line a title. You can see this title when you hover over the chart. The only thing you need to remember is that the title must be a string (and therefore placed inside quote marks like this”).
valueNow we move onto the final mandatory argument.
valueis the price level we want to the plot. In the examples above, we have shown how to plot a single value as a horizontal line. So in this example, let’s take a look at plotting a series of values that move around.
C = ClosePrices() Plot(1, 'Horizontal Line', C)
Optional ArgumentsOptional arguments by definition do not need to be provided to get things working. However, they often provide useful features and extended functionality. We can define colors, styles and a whole host of other things. In fact, there are so many options, that they have their own section in the documentation: https://help.haasonline.com/api/haasscript/commands/charting#lineoptions Let’s take a look at how to specify some of the options:
Passing OptionsA color can be specified directly in the
plot()function as the 4th argument you pass like so:
Plot(1, 'Horizontal Line', 9300, Red)There are a number of colors to choose from such as
DarkGreenetc. However, If you need to change any other option, you will first need to create a
LineOptionsobject. Doing this is simple and will allow you to specify other things like line width, line style and so on.
opt = LineOptions(DarkGreen,Smooth,Dashed,3) Plot(1, 'Close Prices', C, opt)As you can see in the example above, we create a
LineOptions()object on the first line. In it we specify that we want a dark green color, a smooth but dashed line and that the line should have a width of 3. We then pass the object (which is saved as
opt) to the
plot()function. Adding both the examples together, we will be left with code that looks like this:
C = ClosePrices() opt = LineOptions(DarkGreen,Smooth,Dashed,3) Plot(1, 'Close Prices', C, opt) Plot(1, 'Horizontal Line', 9300, Red)And a chart which looks like this: Take a look at all the options available and have a play around with each to find the line-styling that you like.
Find This Post Useful?
If this post saved you time and effort, please consider support the site! There are many ways to support us and some won’t even cost you a penny.
Backtest Rookies is a registered with Brave publisher!
Brave users can drop us a tip.
Alternatively, support us by switching to Brave using this referral link and we will receive some BAT!
Enjoying the content and thinking of subscribing to Tradingview? Support this site by clicking the referral link before you sign up!