Let's deal with the easier part first...
mastermedea wrote:2) Whether it is possible to allow some functions such as Ref, Peak etc. having a number-returning formula in place of the constant among its parameters. For example, is it possible to arrive at such formula as Ref(c,-1*peakbars(1,c,5)-1) ?
Most of the indicators in MetaStock only allow "static" values to be input as an argument to the function. There are some ways to "convert" the dynamic value (such as the result from a computation) into a static value; this usually involves the LastValue() function and some PREVs and has limited applications and some serious drawbacks to the real life trader and the speed of execution of the code. To alleviate some of these limitations, a Team (comprising nearly solely of Patrick) wrote some functions that instead of receiving static values, received dynamic values, making them "adaptive" (which was the buzz-word of the time). These functions were published in the freely available Forum.dll addon.
Included in the exisiting package are functions such as variable-length moving average and variable length RSI functions. Instead of entering, for example RSI(14) you could have an adaptive RSI function between values such as RSI(7) to RSI(21). Similar for the moving averages and the variable period lookback function or Ref(). Only those indicators that were considered valuable by traders were coded, Peak, Trough, Zig etc were not re-coded and included in the addon as these dynamic functions cannot be used by the majority of traders, or should not be used as a basis for making for trading decisions.
To achieve the aim of the code snippet you provided:
Code:prd:=PeakBars(1,C,5)-1;
data:=ExtFml("Forum.Ref",C,prd);
{return}
data;
Notice here, the Forum.Ref function does not take a negative value, the reasons for this are explained in the Forum.dll Users Manual (Hopefully at some time I will get some time to do some more reviewing of the manual too?)
mastermedea wrote:1) Why should the rsi() function requires the number of data loaded even though it already specifies "rsi(14)"? It took me a lot of time to get to understand why the rsi(14) in my exploration returns a different value from what actually appears on chart until I found out that "loaded periods" issue. The problem I realize among my friends is they all try to study MS on their own and details such as loaded periods for RSI calculation is too subtle for them to recognize.
From my experience most people trying to "learn MS" in fact don't try to learn anything. They install the program and then expect to start making money without doing anything, and particularly without learning anything. There is a lot to learn and most of it has absolutely nothing to do with MetaStock. People hear words like "stochastics", "RSI", "moving averages (or MA)" and think they can make money with these just because they happen to discover MetaStock has an implementation of these. Many people aren't even aware that "Stochastics" as applied in the field of Technical Analysis has absolutely nothing to do with the proper definition of stochastics as it applies to mathematical statistics! They hear the word and think because it sounds technical it must be good!
Every function, every system, every step of every process used in making trading decisions must be fully rationalised and re-discovered or proven from first principles to make sure it applicable to the trader and their trading style. Only people who understand the construction of the RSI(), for example, can understand its capabilities and limitations and successfully apply it to their trading. I suspect there are many traders who get lucky without knowing why but I suspect there are many more who have no idea why their RSI-based system performed or failed to performed in particular situations. Not only does the trader have to understand the concept as conceived, they have to understand the implementation of that concept in their software package of choice. For example, AmiBroker, BullCharts and MetaStock all have different inplementation of the Average True Range function. A trader with an ATR based system in AmiBroker may not have the same success with the system should they choose to export the idea to MetaStock, and vice-versa. The original ideas of people like Welles Wilder etc have been implemented in many many systems and each time a small change to the algorithm here and a small tweak there changes the underlying fundamental operation of the indicator, which changes its performance and its limitations. There are some algorithms that have been through so many changes it has obfuscated the original recipe completely!
The reason that functions like the RSI() require a lot more data than the 14 days specified in RSI(14) is because the function uses Wilders smoothing, which like exponential moving averages are Infinte Impulse Response (IIR) filters. IIRs always have a little bit of history included in them, which gets less significant with the passing of time, but never disappears completely. Conversely, Finite Impulse Response (FIR) filters such as simple moving averages e.g Mov(C,21,S) only contain data from the last 21 bars. On the next bar, the information from the bar that is now 22 bars old is dropped and forgotten. It can be demonstrated that you need to load at least five times the required amount of data to avoid precision errors with IIR filters in explorations and their charted comparison. If you are trying to observe the RSI(14) you should load at least 70 bars of data in an exploration. But why not load more? Many people think will slow down the exploration, but I ask, what would you prefer, correct results attained eventualy or wrong results achieved quickly? Load as much data as you can, it is safer. Some time later, when you have a better grasp of the indicators and how they all interact you might consider reducing the data load.
It is possible to re-write the RSI() function removing the Wilders smoothing IIR filter, allowing the old data to "fall out" of the recent computations. This can be said for just about all of the functions, but the trader has to be aware of the ramifications of making these changes. Replacing IIRs in indicators with FIRs may change the fundamental operation of the indicator making it more dangerous than the original.
Many of these issues have been mentioned in some detail in the MS Users Manual and in some excruciating detail at some point in time on this Forum. There is a very useful search function that should allow any Forum member to attain whatever information they require on just about any indicator; the beauty of the peer-to-peer support forum is that if after doing a thorough search the member cannot find the required information they can ask for assistance from their peers. Noone needs to learn in isolation.
Hope this helps.
wabbit [:D]