Hi
There is a good reason for the k3 variable, otherwise known as an
"Initialised", "Init" or "I" variable, and often used with the
BarsSince(VariableA) < BarsSince(VariableB) type of latch. Essentially the
BarsSince() type of latch is used to determine when the most recent of two
different events occurred. A typical latch can be used to monitor Buy and
Sell signals. Iff the Sell signal was the most recent then no trade is open.
However, if the Buy signal was the most recent the a trade will be active
(until a Sell signal is given).
The problem with just using raw Buy and Sell signals to switch the state of
the latch is that the first signal in the series is completely missed. This
might not be a problem for most uses and users, but the fact that one signal
is missed means that the resulting latch is not as accurate or complete as
it could be.
The solution is to create an "I" variable (or a k3 variable in your example)
that generates a signal as soon as both primary inputs (Buy and Sell for
example) are VALID. This signal is ORed with both the Buy and Sell signals
in the respective BarsSince() functions. Since this initial signal generates
both a Buy AND a Sell signal the latch immediately generates a FALSE result
- it cannot generate a TRUE result because the simulated initial Buy and
Sell signals occurred on the same bar, thus the latch cannot report a Buy as
more recent than a Sell, but that reference point (the "both inputs
initialised / valid") then allows the very first available Buy signal to set
the latch TRUE. This type of latch will not function properly if normal Buy
and Sell signals can occur on the same bar. Allowing that to happen,
however, would not be a smart thing to do
To summarise, the "I" bar (or "k3" bar in your case) allows the BarsSince()
type of latch to pick up the first signal once both inputs are valid.
Without the "I" (or "k3") bar inclusion in the latch you will miss one
complete cycle of the latch. In other words the "I" bar signal allows the
latch to plot ASAP and not miss a signal.
The I or k3 variable can be constructed in a number of different ways. In
essence it only generates a TRUE for one bar, and then a FALSE for every
subsequent bar. The trick is to identify the first bar that both signals are
valid, and using the CUM() function to identify a count of 1 is an easy
enough way to do that.
Regards
Roy
yuzde:=Input("yuzde-% of trailing stop",0,100,2);
period:=Input("period",1,100000,3);
a1:=Mov(C,period,E);
a2:=a1-(a1*yuzde/100);
a3:=a1+(a1*yuzde/100);
b1:=If(a1<PREV,a2,Max(a2,PREV));
b2:=If(a1>PREV,a3,Min(a3,PREV));
k1:=Cross(a1,Ref(b2,-1));
k2:=Cross(Ref(b1,-1),a1);
k3:=Cum(k1+k2>-1)=1;
k4:=Cum(k1)=1;
BarsSince(k3 OR k1) < BarsSince(k3 OR k2)+k4;
_____
From: [email protected] [mailto:[email protected]]
Sent: Monday, 19 October 2015 7:44 a.m.
To: [email protected]
Subject: [EquisMetaStock Group] a question on metastock code
hello,
I have the following ms code:
yuzde:=Input("yuzde-% of trailing stop",0,100,2);
period:=Input("period",1,100000,3);
a1:=Mov(C,period,E);
a2:=a1-(a1*yuzde/100);
a3:=a1+(a1*yuzde/100);
b1:=If(a1<PREV,a2,Max(a2,PREV));
b2:=If(a1>PREV,a3,Min(a3,PREV));
k1:=Cross(a1,Ref(b2,-1));
k2:=Cross(Ref(b1,-1),a1);
k3:=Cum(k1+k2>-1)=1;
k4:=Cum(k1)=1;
s1:=BarsSince(k3 OR k1)
< BarsSince(k3 OR k2)+k4;
...
I cant understand k3 and k4. Wont they be meaningful only 1 time?
k1 and k2 are always greater than 0. After the first time slot, k3 is 1 and
later on it is 0.
k4 is 1 after the first time k1 is 1 and later on it is 0.
Do i miss something?
thanks in advance.