System review: logic, status, trade processing order |
Top Previous Next |
Let’s 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.
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
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).
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 Mechanica’s 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.
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 today’s 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 don’t 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.
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 day’s 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.
|