Study Plot Example
In this example we are going to create a Study Plot based on a simple MACD. Note: if you would like a more comprehensive MACD example, you can look at the source code for the MACD indicator that exists within MotiveWave™.
MACD stands for ‘Moving Average Convergence/Divergence’ and was written by Gerald Appel in the 1970s. If you would like more information on this study go to: http://en.wikipedia.org/wiki/MACD.
Here is a screen shot of what this study looks like:
Here is a screen shot of the Study Dialog that the user will use to configure the Simple MACD:
Let us start by looking at the source code for this study:
StudyHeader Annotation (@StudyHeader)
The main difference in the study header from the previous example is the ‘overlay’ tag is set to false. This indicates to MotiveWave™ that this study should be displayed in a separate study plot. You will notice here as well that we have included some HTML markup in the ‘desc’ tag. The description displayed in the Study Dialog supports HTML so you can put any valid HTML tags here (do not include JavaScript, this is not supported).
initialize method
We have defined a bit more in the initialize section from the previous example. To illustrate the usage of tabs, we have created 2 tabs: ‘General’ and ‘Display’. We have also defined the bars for the histogram (see BarDescriptor).
Indicators are displayed on the vertical axis (right side of the screen). By default, we are only going to show the first indicator (MACD), but we will allow the user to show indicators for the current signal value as well as the histogram. For this we will use the IndicatorDescriptor and set the values accordingly. We have organized these into a Setting Group called ‘Indicators’
The following screen shot (with markup) shows the part of the initialize method where we are describing the settings for the study:
Next, we need to describe the runtime parameters using the RuntimeDescriptor. For the label, we want to append the input, period, period2 and the signal period.
In this case, we are going to export 3 values: MACD, SIGNAL and HIST.
In order to display the histogram as bars, we use the ‘declareBars’ method on the study descriptor. This will tell MotiveWave™ to show vertical bars using the BarDescriptor identified by Inputs.BAR.
calculate Method
The calculate method is used to compute the values for each historical bar in the data series. In our case, we are going to do the following:
Retrieve User Settings – these are accessed from the getSettings() method.
Compute and Store the MACD – The DataSeries object contains the historical data as well as the utility methods for computing moving averages. The MACD value is stored in the data series at the given index using the key Values.MACD.
Compute and Store the signal – The signal is a moving average of the MACD. Use the data series to compute the moving average with Values.MACD as the key. The signal value is stored in the data series at the given index using the key: Values.SIGNAL.
Compute and store the histogram – The histogram is simply the difference between the MACD and the signal. This is stored in the data series at the given index using the key: Values.HIST.
Mark the index as ‘Complete’ - Finally, indicate that this index is ‘complete’. This allows MotiveWave™ to cache these values (to improve performance).
Last updated