With Overbasic programming language you can create custom indicators (User-Indicators) to add to your charts and/or use within Overcharts where permitted. Once created and compiled, these indicators can also be used within other indicators or trading-systems.
In charts, indicators are drawn in the form of a continuous line (curve), histograms, or in the various forms provided for standard indicators. A standard indicator is represented by a data series (array). The number of items contained in the data series is equal to the number of periods/bars in the chart. In other words, for each bar of the chart there will be the corresponding value of the indicator.
Create a New Indicator #
Before starting the development of an indicator, we recommend you carefully read the Overbasic language syntax documentation, keywords and available built-in functions.
To create a new indicator:
- Access Developer (the Overcharts development app):
- from Overcharts main window > Tools menu > Developer
- or from Workspace window > Tools menu > Developer
- Create the indicator from:
- File menu > New > Indicator – Overbasic
- or press New button on the top toolbar
- or press ‘+‘ button on the left vertical toolbar
- Define the indicator name. The name must comply with the following rules::
- can consist of letters (both Uppercase and lowercase) or numbers
- Cannot contain spaces. The only separator character allowed is underscore: “_“
- cannot contain special characters such as !”£$%&/()=?^|
- cannot contain special letters (typical of some languages)
- cannot be a number or start with a number
- Press OK to confirm the name and create the indicator.
Once the indicator is created, the editor opens automatically.
Afterwards, you will be able to access the editor to edit the indicator from indicator list on the left:
- by double clicking the indicator name
- or by pressing the corresponding Open button next to the indicator name
- or by right clicking the indicator name > Edit
MAIN function #
The main component of an indicator (the only really mandatory) is the Main function, i.e. the function containing indicator formula. The result returned by Main function MUST be numeric.
Main function is always run automatically by Overcharts. In particular:
– in calculating indicator historical values, Main function is called for each single bar of the chart and its result will be the value that the indicator will assume on that bar.
– in real time calculation, Main function is called for each new data.
The indicator below calculates the midpoint between High and Low for each bar. It will be drawn on chart as a curve passing through the midpoint of each bar.
Function Main() Return (High + Low) / 2 EndFunction
The Main function will be called for each bar of the chart and will return, using RETURN keyword, the indicator value for that bar.
In an indicator, the Main function can return up to 10 results which will correspond to 10 different curves on chart.
In an indicator, the Main function can return up to 10 results which will correspond to 10 different curves on chart. For example, it may be useful to return multiple values (and therefore multiple curves) in the same indicator instead of creating x different indicators, when formulas are very similar to each other or have a large part of the script in common. All curves will have the same Y-axis scale.
Let’s add a second result to the Main function from the previous example:
Function Main() Return (High + Low) / 2 As HL2, (High + 0.1 * High) As SuperHigh EndFunction
Note: When there are multiple results, it is mandatory to define a unique name for each result using AS keyword. For more information, refer to RETURN keyword documentation.
By defining one or more PROPERTYs, you can externally influence the indicator calculation. PROPERTYs are properties you can set in the indicator configuration on chart, or you can pass as an argument to the indicator function in case you use the indicator in the script of another indicator or trading-system.
We recommend you read PROPERTY documentation to learn more about PROPERTYs.
The indicator below represents the difference between two moving averages calculated over two different periods. Periods can be set externally using Period1 and Period2 properties.
Property Period1 As Numeric Range (5, 100) Default (20) EndProperty Property Period2 As Numeric Range (5, 100) Default (50) EndProperty Function Main() Return Mov(Close, Period1) - Mov(Close, Period2) EndFunction
A Pragma is a special parameter that allows you to influence the Indicator behavior.
Some Pragmas can be defined exclusively within the script, while others are used to set default values of properties accessible in the Indicator configuration window.
If you do not know what a PRAGMA is and how to use it, we recommend you read PRAGMA documentation.
Among the various pragmas available for indicators, we highlight two of them in importance:
- USE_REFERENCE_OBJECT_SCALE_AS_DEFAULT: allows to set the use of reference chart/indicator scale as default. Very useful for all indicators that need to be drawn in the same window as the reference chart/indicator such as, for example, a moving average.
Pragma ("USE_REFERENCE_OBJECT_SCALE_AS_DEFAULT", True) Function Main() Return (High + Low) / 2 EndFunction
- VALID_STARTING_FROM: defines the bar from which to start drawing the indicator curve. In other words, it is the index of the first valid value in the indicator values series.
Pragma ("VALID_STARTING_FROM", Period2) Property Period1 As Numeric Range (5, 100) Default (20) EndProperty Property Period2 As Numeric Range (5, 100) Default (50) EndProperty Function Main() Return Mov(Close, Period1) - Mov(Close, Period2) EndFunction
Indicator Description #
You can define a description for the indicator using INFO keyword. Description will be shown, for example, in the available-indicators window.
Info "Bar Midpoint" Pragma ("USE_REFERENCE_OBJECT_SCALE_AS_DEFAULT", True) Function Main() Return (High + Low) / 2 EndFunction
Indicator on indicator #
To calculate an indicator based on another indicator, you need to define in the script an Indicator-type PROPERTY. Using this property you will be able to access the external indicator data-series and use it in the calculation.
Let’s we calculate the difference between two indicators, both set externally in the indicator configuration window:
Property ExtInd1 As Indicator EndProperty Property ExtInd2 As Indicator EndProperty Function Main() Return ExtInd1 - ExtInd2 EndFunction
Compile/build the Indicator #
To use the indicator in Overcharts, you MUST compile it. Compiling an indicator means transforming the source code into a ‘something’ that the program understands.
In Developer, you can build a single indicator in one of the following ways:
- by pressing Compile button on toolbar inside the Editor window
- or by pressing ‘Compile’ icon (gear) next to indicator name in indicator list. The icon is ONLY visible if the indicator needs to be compiled.
You can massively compile multiple indicators, in one of the following ways:
- Developer main window > Compile menu > Select which items to compile
- Developer main window > press Compile button on main toolbar > Select which items to compile
- Indicator list > press Compile button on toolbar above the list > Select which items to compile
Using the indicator in an external script #
My.User_indicator.Private category contains all user-indicator functions.
To use an indicator within the script of another indicator or trading-system:
- Create and Compile the indicator
- Use the indicator function in the external script. You can find the indicator function in My.User_Indicator.Private category. Function has the same name as the indicator. If present, it is also necessary to set indicator PROPERTYs by passing them as arguments to the function.
In the example below, BarMiddleValue function represents a previously created and compiled indicator. Arguments 10 and 5 are the values of 2 PROPERTYs defined in the BarMiddleValue indicator script.
Function Main() Return BarMiddleValue(10, 5) EndFunction
Note: Usually, you can use the indicator function directly without specifying the full path: ‘My. User_Indicator. Private. BarMiddleValue(10, 5)‘. You need to specify the full path only if exists another function with the same name as the indicator.