PureBytes Links
Trading Reference Links
|
Hi Roy,
While trawling the web looking for help with my BarsSince() problem,
I saw your ancient post at:
http://www.purebytes.com/archives/metastock/2003/msg05324.html
(How did it get there?)
I notice you use BarsSince() in your code below, but you have to kick
start it with "Init". Do I need to do the same thing?
Regards,
Alan
--- In equismetastock@xxxxxxxxxxxxxxx, "Roy Larsen" <rlarsen@xxxx>
wrote:
> Stan
>
> > I've seen the term "latch" used when referencing certain
formulas. Can
> > someone tell me what it means to "use something as a latch" Thanks
>
> When I first created and started using a latch structure I called
it a
> "flag". This was confusing for those that think of flags as chart
patterns
> and so someone suggested the term "latch". In digital electronics a
latch is
> a bi-state memory device. It's either true or false (on or off).
>
> The following discussion on simple latches is a short excerpt from
the
> "Thanks for the memory" chapter of my upcoming book. The longer
discussion
> on PREV-based latches is not included. Please note that the text is
not yet
> in its final form, and that the line spacing has been distorted by
OE.
>
> Roy
>
> Definition:
>
> A Latch is a memory device that is used to catch and hold a value
or event.
> Latches enhance the ability of MetaStock to remember the timing of
related
> events and/or the values associated with specific events.
>
> Simple Latch Description
>
> The simplest form of a latch is presented in Exercise 1. It
requires two
> signals to function - typically a "set" and a "reset" signal. You
might like
> to think of these signals as the on and off buttons of the latch. By
> comparing BarsSince(set) to BarsSince(reset) we are able to
determine which
> signal occurred most recently, and therefore whether the current
state of
> the latch is set or reset.
>
> If the set signal occurred more recently then the latch will be in
the set
> state (on), and if the reset signal occurred more recently then the
latch
> will be in the reset state (off). MetaStock processes a chart from
the first
> bar on the left to the last bar on the right - from oldest to the
most
> recent data. The state of a latch is checked and updated as each
new bar on
> a chart is processed moving from left to right. The perspective
used with
> the BarsSince() function is always from the current bar, or bar
being
> examined, backward to the left side of the chart. The essence of a
simple
> latch is in knowing which of two events occurred more recently.
>
> All exercises in this chapter use the crossover of two exponential
moving
> averages to generate set and reset signals. These signals have no
inherent
> significance other than their usefulness in this series of
exercises.
>
> {Exercise 1} {simple latch}
> Set:=Cross(Mov(C,15,E),Mov(C,25,E));
> Reset:=Cross(Mov(C,25,E),Mov(C,15,E));
> BarsSince(Set)<BarsSince(Reset); {did a set occur since a reset?}
>
> This basic latch format works fine up to a point but one
shortcoming that
> needs to be addressed is that the output line (the last line) may
not plot
> any signal until many bars after the start of the chart. This is
known as an
> N/A plot, and it means that the formula is unable to plot a valid
value.
> This is also a common problem with the ValueWhen() function. The
N/A period
> will extend from the first of the chart until the first bar when
the set
> signal is active and the reset signal is valid. The use of a 25
period
> moving average in this exercise forces at least 25 N/A bars, 26 in
fact
> because an extra N/A is added by the Cross function, but most often
the
> number of N/A bars will be higher. An extended N/A plot can be
reduced to an
> absolute minimum by including an "INITIALISED" variable into the
formula. I
> call this variable 'I' or 'Init', and its purpose is to inform any
following
> code that the 'Set' and 'Reset' variables are both valid - they are
each
> capable of plotting a real value. MS v7.0 and later versions can
use the
> IsDefined() function to achieve the same effect as 'Init' but that
function
> is not available to MS 6.52 users, and neither does it provide any
real
> advantage.
>
> With exercise 2 the 'Init' variable is used to simulate initial set
and
> reset signals, and these signals in turn provide a specific
starting point
> for the latch to begin to function. If you can compare the plot from
> exercise 1 with the plot from exercise 2 then you will see that
exercise 2
> has an N/A period of 26 bars. Exercise 1 however will have an N/A
period
> that extends until the bar where exercise 2 is set for the first
time. This
> may occur many bars later.
>
> {Exercise 2} {simple latch with Init}
> Set:=Cross(Mov(C,15,E),Mov(C,25,E));
> Reset:=Cross(Mov(C,25,E),Mov(C,15,E));
> Init:=Cum(Set+Reset>-1)=1;
> Trade:=BarsSince(Init OR Set)<BarsSince(Init OR Reset); {Init
simulates
> first signals}
> Trade;
>
> Notice that the output variable has been named 'Trade'. Giving the
output
> variable a name is desirable because it will usually be followed by
code
> that needs access to the state of this latch (is it set or reset?).
Exercise
> 2 is a complete binary latch and it can only store two values -
typically
> those values will be ONE and ZERO (true and false) although other
values can
> also be used. This variation will be covered in a later paragraph.
>
> The 'Init' variable confuses many people so don't be alarmed if you
also
> struggle to comprehend its nuances. It will be true for the first
bar when
> both the set and reset signals are valid, for one bar only, and
false for
> every subsequent bar. Unless both the set and reset signals are
valid it
> will remain invalid itself and not plot any value (except N/A of
course).
> 'Init' being true for one bar provides the simulated set and reset
signals
> that are used to kick the latch into action. Each BarsSince()
function
> contains a simulated signal on the first possible valid bar (bar 26
for our
> Exercises), and that simulated signal provides a reference signal
for the
> other BarsSince() to relate to.
>
> Remember that construction of an 'Init' signal is not concerned
with actual
> values but whether or not 'Set' and 'Reset' are VALID. The sum of
the two
> inputs ('Set' and 'Reset') cannot be greater than -1 until both
plot a real
> value, i.e. each signal is valid. Both 'Set' and 'Reset' are binary
in
> nature, zero or one, so by definition neither signal can ever be
less than
> zero. The test for a Cumulative count of 1 ensures that this signal
is true
> for the first valid bar only and false for every subsequent bar.
That's
> quite OK because the whole purpose of 'Init' is to generate
a 'true' for
> just the one initial bar. This signal true for one bar informs us
that the
> set and reset signals are both INITIALISED.
>
> As I mentioned earlier it is possible to assign values other than
zero and
> one to a latch. The If() function can be used to select two
different
> constant values for the latch to alternate between. Unfortunately
the If()
> function does not allow the accurate storing of prices. Secondary
set
> signals would allow a stored value to change with each new signal if
> constants were not used.
>
> {Exercise 3} {simple latch with modified output}
> Set:=Cross(Mov(C,15,E),Mov(C,25,E));
> Reset:=Cross(Mov(C,25,E),Mov(C,15,E));
> Init:=Cum(Set+Reset>-1)=1;
> Trade:=If(BarsSince(Init OR Set)<BarsSince(Init OR Reset),3,-5);
> Trade; {swings between -3 and +5}
>
> Now that you are able to assign different values to the 'Trade'
variable
> using the If() function you also need to be aware that not all
methods of
> creating 'Trade' produce identical results. Here are four different
> constructions of the trade variable but only two of the four, {a}
and {b},
> are logically correct prior to the first set signal.
>
> {a} Trade:=If(BarsSince(Init OR Set)<BarsSince(Init OR Reset),1,0);
> Is functionally identical to
> {b} Trade:=If(BarsSince(Init OR Set)>=BarsSince(Init OR Reset),0,1);
>
> But neither is the same as
> {c} Trade:=If(BarsSince(Init OR Set)>BarsSince(Init OR Reset),0,1);
{wrong}
> or
> {d} Trade:=If(BarsSince(Init OR Set)<=BarsSince(Init OR
Reset),1,0); {wrong}
>
> The difference is in the use of "=" with ">" or "<" symbols. When
written
> the wrong way the plot of the latch prior to the first set signal
will be
> the inverse of its correct state. This may be only a small point
but for
> those few bars it is still wrong. The testing of historical data
requires
> that our test signals be as accurate as possible for all data under
test so
> please take care and avoid this trap whatever your use of the
simple latch
> may be. The correct constructions of Trade when not using the If()
function
> are demonstrated by {f} and {g}.
>
> {f} Trade:=BarsSince(Init OR Set)<BarsSince(Init OR Reset);
> {g} Trade:=BarsSince(Init OR Set)>=BarsSince(Init OR Reset)=0;
> {h} Trade:=BarsSince(Init OR Set)>BarsSince(Init OR Reset)=0;
{wrong}
> {i} Trade:=BarsSince(Init OR Set)<=BarsSince(Init OR Reset); {wrong}
>
>
> Signal Timing for a Simple Latch
>
> Exercise 4 can be used to show the precise relationships on any
MetaStock
> chart. Obviously the actual signals plotted will vary from chart to
chart,
> but the intrinsic relationships will remain the same.
>
>
> {Exercise 4} {signal timing for a simple latch}
> Set:=Cross(Mov(C,15,E),Mov(C,25,E)); Set+3.75;
> Reset:=Cross(Mov(C,25,E),Mov(C,15,E)); Reset+2.5;
> Init:=Cum(Set+Reset>-1)=1; Init+1.25;
> Trade:=BarsSince(Init OR Set)<BarsSince(Init OR Reset);
> Trade;
------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for your HP, Epson, Canon or Lexmark
Printer at MyInks.com. Free s/h on orders $50 or more to the US & Canada.
http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/mOAaAA/3exGAA/qnsNAA/BefplB/TM
---------------------------------------------------------------------~->
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/equismetastock/
<*> To unsubscribe from this group, send an email to:
equismetastock-unsubscribe@xxxxxxxxxxxxxxx
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|