logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Eroco  
#1 Posted : Monday, September 17, 2007 12:33:01 PM(UTC)
Eroco

Rank: Newbie

Groups: Registered, Registered Users
Joined: 9/17/2007(UTC)
Posts: 4

Hello,

I am new in the forum and also new programing with Metastock.

I wonder if you can confirm or disconfirm that the next Buy Order Formula does what I expect...

C>Valuewhen(1,H>Ref(HHV(H,2),-1) AND H>Ref(HHV(H,2),+2),H)

I want to buy only if the CLOSE is higher than the last higher high that is preceding by (at least) 2 lower highs and followed by (at least) 2 lower highs also.

I am not sure it this functinos will do the work, so I hope anyone that has more experience with this may help me.

Thanks in advance,

Eroco

wabbit  
#2 Posted : Tuesday, September 18, 2007 6:22:56 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Eroco,

The code you provided cannot be traded in real life!

Be very careful with the Ref() function, especially when you use +periods because this is looking into the future. Please reconsider the algorithm so that you are only looking back in time. The easiest way to do this is to draw a chart on a piece of paper with the "pattern" you are looking for and code it from the "perspective" of the last bar looking back into history (i.e. NOT from a history bar looking forwards).


Hope this helps.

wabbit [:D]


Eroco  
#3 Posted : Wednesday, September 19, 2007 12:23:51 PM(UTC)
Eroco

Rank: Newbie

Groups: Registered, Registered Users
Joined: 9/17/2007(UTC)
Posts: 4

wabbit wrote:
Eroco,

The code you provided cannot be traded in real life!


Wabbit,

I would love if you could explain why it can't be traded in real life, cause it's defenetly entering some trades when i backtest it with Metastock... and when I plot it on the chart it enters almost precisely where I want, but I don't know for sure if I am missing part of the trades that I also want to take...

Maybe you can explain in words which trades would this code be entering... maybe that will give me a better picture of what the Ref() function does, if it's not much to ask...

Thanks for the help!

Eroco

wabbit  
#4 Posted : Wednesday, September 19, 2007 7:20:33 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Eroco,

Code:
C>Valuewhen(1,H>Ref(HHV(H,2),-1) AND H>Ref(HHV(H,2),+2),H);


Let us pretend today is Wednesday. Your code is looking for the condition when Wednesday's CLOSE is greater than the expression in the ValueWhen() function. Let's also assume this expression is TRUE today, Wednesday. How can the expression in the ValueWhen() be evaluated as being TRUE today when it makes reference to the HHV(H,2) in two days time, that's Friday but today is only Wednesday?

The only way the expression can be evaluated on Wednesday is for Friday's information to be known, that is, on or after Friday, at which time the entry signal is placed on Wednesday's bar. If you have a broker that will allow you to buy stocks on Friday at Wednesday's prices, knowledgable of the price movement then please give their number to me! Your code will be giving you a signal today that you should have entered the trade two bars ago! Is this much use in real life?

You have to be very careful writing codes that use foresight or know the final result before it is possible for the final result to be known, or code that uses hindsight to add/remove indicators in the past. If you put your code into an exporation, you will notice that MS returns only N/A preventing any signals from being generated, and see on a chart there are no returns from the indicator on the last two bars of the chart; this is the effect of the forward referencing. MS will still show the signals generated before, but these cannot be traded in real life, the system tester thinks they can!

Rethink your algorithm from the perspective of Friday's bar, only looking backwards into the known history and I am sure you will find you have completely different results. Once you have your algorithm, run it in an exploration and see how many results it gives on the last bar. If the exploration rejects 100% there is probably something still wrong with the code.



Hope this helps.


wabbit [:D]



P.S. On a slightly different note; you should be careful about how MS counts bars. HHV(H,2) will compute the highest value on the most recent bar and the one before it, a total of two bars, whereas, Ref(H,-2) will return the value 2 bars displaced in history, that is not the most current bar, not the bar before the current bar, but the bar before that, so MS actually 'looks' at three bars. This can cause some confusion when trying to align, for example, HHV() and Ref() functions.

mstt  
#5 Posted : Wednesday, September 19, 2007 8:02:27 PM(UTC)
mstt

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 7/25/2005(UTC)
Posts: 1,042

Was thanked: 57 time(s) in 54 post(s)

Eroco

What many MS users don't realize is the System Tester can access ALL data loaded, not just the portion of data up to the bar being tested. In practical terms this means that a forward reference, such as Ref(H,+2), will look two bars into the future. That works fine until it gets to the last two bars. The test then ceases to function for those two bars, but the System Tester will not report this as an error.

If the same code was used in the Explorer, where results are only recorded as at the last bar loaded, the result would be N/A. The Explorer does not allow the use of forward reference unless there is a corresponding backward reference to compensate for it and bring all data within the range of past or present only.

Roy

MetaStock Tips & Tools
Eroco  
#6 Posted : Thursday, September 20, 2007 6:46:35 AM(UTC)
Eroco

Rank: Newbie

Groups: Registered, Registered Users
Joined: 9/17/2007(UTC)
Posts: 4

wabbit wrote:
Eroco,

Code:
C>Valuewhen(1,H>Ref(HHV(H,2),-1) AND H>Ref(HHV(H,2),+2),H);


Your code is looking for the condition when Wednesday's CLOSE is greater than the expression in the ValueWhen() function.


Wabbit,

I think I see your point now, but I was under the impression that my code was looking for the condition when today's CLOSE is greater than the HIGH of the FIRST BAR where the EXPRESSION is true.

Isn't that right?

Valuewhen() function reads all the bars loaded from right to left and the exploration does it from left to right loading one bar at a time.

Thanks for the help you are giving me a great hand to figure this out, please bear with me I am not always this stubborn... [:D]

Eroco

wabbit  
#7 Posted : Thursday, September 20, 2007 6:37:58 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Eroco,

MetaStock uses a "fully evaluated" language, which means that every part of every function is fully evaluated as it is run. As part of this evaluation, MS applies two boundaries to each expression (and for expressions nested within expressions), these boundaries identify the first valid bar for the expression and the last valid bar.

Take for example a 100 bar SMA: MS sets the first valid bar as the 100th bar on the chart and the last valid bar is the last bar on the chart.

In your code: the first valid bar of HHV(H,2) is the second bar of the chart and is valid for all other bars; the first valid bar of Ref(HHV(H,2),-1) is the third bar of the chart and is valid for all other bars (we need two bars to make the HHV(H,2) and one more bar to make the Ref(x,-1) comparison). Ref(HHV(H,2),+2) is valid from the first bar because it is looking into the future to see the value in two bars time and is valid for all bars EXCEPT the last two bars on the chart. If we combined these two expressions MS will check the validity of the component expressions to return valid bars for all bars EXCEPT the first two bars on the chart and the last two bars. To see this, try plotting:
Code:
H>Ref(HHV(H,2),-1) AND H>Ref(HHV(H,2),+2)


The use of ValueWhen() can mess things up slightly from the left of the chart, it is invalid until the first time the component expression is true, after then the ValueWhen() returns a value for the remainder of the chart.

Even though the you might think you are looking for the "fractal" to have occurred some time ago and now the CLOSE price on the most recent bar has exceeded the HIGH price when the fractal pattern happened and are looking to enter, MS will still not show you any signals in the last two bars because the first valid bar for the entire expression is the first bar when the ValueWhen() expression triggered, and the last valid bar for the complete expression is two bars short of the right edge of the chart.

You must only use history to make your expression. Draw the pattern on a piece of paper and code the expression from the perspective of the last bar in the pattern only looking backwards in time, not the bar at the start or in the middle of the pattern that looks forwards in time!



wabbit [:D]

wabbit  
#8 Posted : Thursday, September 20, 2007 6:57:55 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
The post above partially explains why it is possible to turn an indicator "on and off" to limit the returns to a defined period, but this can only be done once i.e. once switched on, the indicator will continue to return values until the end of the chart or it is switched off and cannot be switched back on again.

If MS allowed the user to define multiple sets of first valid bar and last valid bar boundaries for each indicator, we could have indicators that returned values only when certain conditions are met and not return any values inbetween these times. For the mean time we cannot do this, cest la vie.



wabbit [:D]

Users browsing this topic
Guest (Hidden)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.