This is a trading object shared by a member. You can use this object as well as hundreds of others with QuantShare - Trading Software.
Click on the Like button or on one of the share buttons to share this object and spread the word about QuantShare.
QuantShare is an advanced trading software for stock, forex, futures, ETF and options markets. It is the best and the most complete EOD trading software software and it is FREE (beta version).
This function calculates the number of bars, value, sum, average and rate of return of a specified vector and using reference points. For example, if you use the rate of return indicator (ROC), the function accepts as input a vector and a lookback period (number of bars). If we choose 10 as lookback period and we are in the bar number 20, we then will calculate the rate of return using the current bar (20) and the bar number (10). On the next bar (21), the reference bar will move also to the bar number (11).
This function instead uses a fixed reference bar. This fixed reference bar is calculated using a rule you provide as an input to the function.
Let us say, you want to use a fixed bar that is calculated using the following rule: rsi(14) > 70. The bars where the RSI rule is met will be chosen as reference bars. Therefore, the lookback period used to calculate the ROC will change and will not be fixed as it is using the standard functions. Each time the rule is met the lookback period will be re-initialized (set to zero).
The function has three inputs: The rule used to calculate the reference point, the vector to use for the calculation, and a string that defines the type of calculation that will be made.
The third input can accept the following values:
nb: Calculates the number bars
value: Calculates the value of the provided vector on the bar the last time the rule was true
sum: Calculates the sum
avg: Calculates the average
roc: Calculates the rate of return
-----
Example: anchor(rsi(14) > 70, 1, 'nb');
This function will calculate the number of bars since the RSI rule was true.
In this particular case, the function could be translated to: barssince(rsi(14) > 70);