System review: logic, status, trade processing order

Top  Previous  Next


Lets look at our Signal Rules program in its current state, and discuss how it all fits together. Remember, when running the Signal Rules, Mechanica only processes one symbol (one historical data price file) at a time.


Open Lesson2.sig, if it is not already open. (If you cant find Lesson2.sig, refer back to the 1st section in this lesson, “More Sig & Siz file basics, To save a SIG file to another name”.)
Add the statements shown on lines 5 & 6 to the bottom of your Resources page; They have been added to create a one-day offset for plotting the MIN and MAX functions during trade charting, and it is these columns (5, 6), along with Col2, that have been charted in the graphics below. (Please see Lesson 4 Time Orientation in Mechanica: Yesterday and Today, for more info.)

Note that the code lines on this page have been numbered. This was done strictly to facilitate discussion. Please do not add the numbers to your SIG file!

1. SYSTEM = 1

2. COL2 = SMA[CLOSE,28]      ' barometer of trend direction

3. COL3 = MIN[LOW,10,0]      ' long exit price

4. COL4 = MAX[HIGH,10,0]     ' short exit price 


5. COL5 = COL3[1]  ' day offset for charting (see Lesson 4)

6. COL6 = COL4[1]  ' day offset for charting (see Lesson 4)



7. ' The 28-day moving average should be a good barometer...

8. IF CLOSE[1] > COL2[1] THEN BUYOPEN   ' long entry signal

9. IF CLOSE[1] < COL2[1] THEN SELLOPEN  ' short entry signal



10. SELLSTOP = COL3[1] ' price at which longs are exited

11. BUYSTOP = COL4[1]  ' price at which shorts are covered



Resources page

The value assigned to the SYSTEM keyword will be used later by the Position Sizing Rules to associate each trade with the system that generated it. Therefore, assignment of a value to this keyword is enforced by Mechanica Basic.

In the next line, we defined Column 2 to equal the value of the 28-day simple moving average (SMA) of closing prices. As the comment says (and yes, we left the comment out earlier, and stuck it in here), this value will serve as a barometer of trend direction. Values such as a 28-day SMA are often referred to as “indicators,”  or “technical indicators.”

The 10-day low and the 10-day high in columns 3 and 4, using the MAX and MIN functions, are also indicators (columns 5 and 6 are used to offset columns 3 and 4 by one day, for trade charting purposes).        



The syntax of the MAX and MIN keywords as well as a few others provide a day offset (this is a legacy concession inherited from DOS TR.)

The day offset is the third argument; and it is assigned a value of zero in the example below:

       COL3 = MIN[LOW,10,0]

       COL4 = MAX[HIGH,10,0]

It is highly recommended that you keep the day offset equal to zero when using functions that have this day offset argument.

Mechanica is today-centric in its time orientation. This means that you always trade today, based on something that happened in the past, usually something that happened yesterday.

Thus, in your entry and exits statements, it is necessary to utilize a day offset in your references, exactly as we did in Lesson2.sig, above.

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



Keeping the day offset equal to zero when using the MIN and MAX keywords, and always employing a one day offset in all entry and exit statements, enforces consistent coding standard that reduces the chance of inadvertently introducing a postdictive error.

For more information, including the implication for charting trades when using these keywords, see Lesson 4, Time orientation yesterday and today.


As you can see in the chart, the 10-day high and 10-day low form a kind of channel around the price. Hence, systems that enter when price penetrates or “breaks out” above or below these bands, are typically known as...”channel breakout” systems. The Turtle systems were based on channel breakouts for both entry and exit. In this system, we use channel breakouts for the exits, with entry signaled by a close above or below the 28-day SMA of closing prices.

Entry logic


7. ' The 28-day moving average should be a good barometer...

8. IF CLOSE[1] > COL2[1] THEN BUYOPEN   ' long entry signal

9. IF CLOSE[1] < COL2[1] THEN SELLOPEN  ' short entry signal

Lines 8 & 9 define the entry logic. The keyword IF tells us that these are conditional statements, meaning that IF the specified condition is met, THEN the statement will execute (by entering a trade). In this case (referring to line 8), IF the closing price yesterday is greater than Column 2 yesterday, THEN buy this instrument today to initiate a trade“go long”at the opening price. Since our system specifies an entry today contingent upon the behavior of the closing price yesterday, it might be helpful to think of the entry as having two components; the “signal day” (yesterday, when the condition for entry was satisfied on the close), and the actual entry on the open today.

Page Processing Order

Mechanicas first stop at the end of each trading day is the Resources page, where it calculates and collects all relevant values. When the system is “flat” (not in a trade), Mechanica then consults the Trade Entry page, where it reads the two entry statements to see if either condition has been satisfied. If an entry is not signaled, then Mechanica moves on to the next trading day for this instrument, and repeats the cycle until one of the entry conditions is met.

In the example trade selected for this discussion, the condition in line 8 was met on Friday, Dec-01-2000, when price closes above the 28-day SMA. This is our signal day. A long trade was entered at the opening price on the next trading day (Monday, Dec-04-2000). On the chart, the green up arrow underneath the price bar designates a long entry (circled in red) in the of the British Pound. With a trade underway, control passes to the Trade in Progress & Exit page; the Trade Entry page will not be consulted again until the current trade is exited.


Exit logic


10. SELLSTOP = COL3[1] ' price at which longs are exited

11. BUYSTOP = COL4[1]  ' price at which shorts are covered


Since the system is now in a long trade, we need to look at the corresponding exit statement, SELLSTOP = COL3[1], located on line 10. This statement, while not explicitly conditional, tells Mechanica, IF price today hits the value in Column 3 yesterday, THEN exit the trade at that price today. (If the market gaps down and opens below the sell stop today, then the trade will be exited at todays opening price.)

Keeping track of trade status

Mechanica knows that a long trade is in progress, and keeps track of this condition internally (thus ignoring the Buystop command on line 11). It is important to note that  Mechanica Basic ignores the Buystop command (on line 11) when it is in a long trade, just as it will ignore the Sellstop command when it is in a short trade. This is one of the many details that Mechanica tracks and manages internally, so you dont have to.

Page Processing Order

Mechanica is now focused solely on detecting when the Sellstop has been hit. Every day, Mechanica visits the Resources page to calculate and gather all pertinent values (in this case the value in Column 3), then consults the Trade in Progress & Exit page, where it reads the Sellstop statement on line 10 to see if the exit condition has been met. If an exit is not signaled, then Mechanica moves on to the next trading day for this instrument, and repeats the cycle.


Exiting the trade

As you saw in the Indicators Chart, the 10-day lowest low in Column 3 serves both as a disaster stop and a trailing stop. Price finally hits this stop on Jan-12-2001, and the trade is exited at a profit.

The down arrow with the short line underneath it (directly above this days price bar on the chart), designates a long exit. The system is now flat, and Mechanica resumes cycling from the Resources page to the Trade Entry page in search of the next entry signal.

ThumbTack white

1.Since Mechanica cannot exit a trade at one price, and then re-enter at a different price on the same day (use Simultest to do that), the open of the price bar on the day after exit is, theoretically, the next potential entry point.
2.For a trade in the long direction, a sell stop order is always placed beneath the current market price. (If you put in a buy stop below the market you will be filled instantly at the market.) If youre not familiar with the various order types, this is a good time to review them. Your broker should have this material readily available, and there is also an abundance of information on this topic on the Web.
3.The example trade of the British Pound used in this section is a trend-followers dream. After entry, price trends up smoothly, and the trade exits before it has the opportunity to give back a lot of open profits. This particular example was chosen for no other reason than to aid in clearly illustrating the material in this lesson. It is not a typical trade for this system (or for most systems, for that matter).
4.If youre beginning to wonder how Mechanica deals with trade processing for the multiple instruments in a portfolio (after all, this is portfolio engineering software!), well cover that in detail, in the Initial Sizing and Resizing sections. But this is a good time to reiterate the main point in the previous section: When running Signal Rules, Mechanica processes one symbol (price file) at a time.