What is cFormula?
This is an indicator like any other cTrader's custom indicators, but the data output of this indicator is calculated based on mathematical formulas or expressions that are provided by the user. If you have ever used formulas in spreadsheet programs this is very similar. You can run up to five formulas in the same indicator instance and get five outputs for each of your formulas.
You can access all of the available chart data in your formula along with all of your platform symbols data and time frames, or you can use another time frame or symbol data in your formula.
The formulas are identical to conventional mathematical expressions; you have to learn some keywords that allow you to use the price data in your formulas.
After writing your formulas in a text file, the indicator will read the file and execute your formulas.
cFormula has two different versions; the normal version shows its data in a separate indicator window at the bottom of your chart. The "Overlay" version shows its data on top of your current chart.
Let's try some simple formulas:
(2 + 2) / 8
This is a very simple formula which will not have any use but is included here for demonstration purposes. It shows you how you can use contact numerical values in your formulas which will result in the formation of a straight line at 0.5.
In this formula we use the price data. It will display the range of bars; as you can see I have accessed the EURUSD price data, followed by the current time frame, which is the current chart time frame, and also the high and low price series. The zero inside the bracket refers to the current bar. If you use anything greater than zero it will indicate x previous bars. Suppose if I used 1 instead of zero, then it would mean the range of previous bars and not the current bar.
You can replace "eurusd" in any of your platform symbol names.
I can write the above formula like this:
Here I used the "current" keyword instead of "eurusd"; here the current keyword means the current chart symbol, so if you change the chart symbol it will show the new symbol bars range. If you used a previous formula and changed the symbol of your chart, the indicator will still show the EURUSD and not the new symbol. If you wish to write more adaptive formulas, use the current keyword instead of explicitly typing in the symbol or the time frame name.
(current.current.close + current.current.close + current.current.close) / 3
This formula shows the 3 period simple moving average. As you can see, we have added the first and the three previous bars' close prices, and then we divided the result by 3.
You may think that if we try to use a longer period then the formula might become very long, so in order to solve this issue you can use cFormula functions. Now let's write the same formula by using the "avg" function:
Here we use the average function, and we give its last three bars close price data. For more information about functions please check the functions page.
current.daily.high - current.daily.low
This formula shows the daily chart day range. It automatically adapts the formula based on each chart bar day. Other time frames data can be used in similar way, you just have to replace the daily to other time frames. A list of time frame names can be found on the formula references page.
The formula is not case sensitive so you do not have to consider the case sensitivity of your formulas. You can use the space inside your formula without any issue
How it Works?
Whenever you provide a formula for an indicator, it will first tokenize your formula into different tokens, after which it will create a mathematical expression from your formula based on the new bars data. It will execute that expression and then assign the value to one of the indicator outputs.