Using yesterday and today

Top  Previous  Next

 

Well look at several examples to make sure this critical concept is clear.

Example 1

Lets say that you want to find out if there's been a breakout to the upside. Youll use Mechanica Basics MAX function to accomplish this. Heres the syntax:

MAX[v,n,z]

 

Where v is the price (O,H,L,C) or column number that MECHANICA is to search for a maximum value, n is the total number of days to search, and d is the number of days ago to start the search.

Now that you know the syntax, whats the correct way to find a 13-day breakout to  the upside? (You could code this in one of two ways.) 

Method A        

COL2 = MAX[H,13,0]   

 

Method B

COL2 = MAX[H,13,1]  

 

Actually, either method is correct, depending on how COL2 is referenced later.

Notice in Method A that the 3rd parameterthe offsetis zero. When entering on a breakout, you want to know whether the price today has exceeded yesterdays highest high (or lowest low), and enter the market at that point using the BUYSTOP command, for example. With Method A, you would need to use a one day offset on the Trade Entry page; the syntax would be: BUYSTOP = COL2[1]. Excluding the one day offset would produce a postdictive error.

NOTE This method of codingusing a one day offset in all entry and exit statementsis highly recommended

With Method B, the COL2 definition is already offset by one day, so a further offset is unnecessary. On the Trade Entry page, the syntax would be BUYSTOP = COL2.

Remember: In Mechanica, you always trade today, based on something that happened in the past.

 

Example 2

Suppose you want your system to look for a crossover (the numerical point where two converging indicators become equal). When it sees one, you want it to wait an additional two days, then trade on the open the following day.

In order to translate this into terms that Mechanica understands, first change the time-orientation in your own mind to early in the morning, and ask this question each day: Did a crossover occur three days ago?

If the answer is no, there is nothing else to do today. Just wait until the next day and ask the question again (of course, Mechanica does this for you automatically when processing SIG files). If the answer is yes, a trade will execute on todays open. In pseudo-code, the entry statement would look something like this:

 

   IF Crossover[3] THEN BUYOPEN 

Example 3

Always think in terms of taking action today, based on past events.

And exercise caution when using indicators that employ closing prices, but trade on the open. For example, you may want to buy today's open if yesterday's close is higher than the 28-day moving average. So if you typed the following statement:

IF CLOSE > SMA[C,28] THEN BUYOPEN

 

...this would tell Mechanica to execute the trade on today's open. But because today's close is being used to calculate the 28-day moving average, that information would not have been available in the real world, even though it is available in your data set. Instead, you should type the following:

COL1 = SMA[C,28]

 

IF CLOSE[1] > COL1[1] THEN BUYOPEN

 

Together these two statements tell Mechanica that, if yesterday's close is greater than yesterday's 28-day moving average, then buy today's open. Again, it is important that you understand and remember this distinction.