The Rules Editor  -   Build a simple system Part(1)

Top  Previous  Next

 

Understanding Mechanicas system of “pages”

Mechanica logically breaks the trading process down into five separate tasks:

Reading data and creating indicators and other critical values
Trade entry
Working with trades in progress, and trade exit(s)
Determining the initial size of positions (position sizing)
Conditionally resizing positions

To facilitate these various tasks, Mechanica employs the concept of pages.

Trading rules, position sizing rules, and other parts of your system are entered directly into the tabbed pages of Mechanicas Rules Editor, which is a structured text editing utility. Page is simply a metaphor used to refer to one of the five (5) tabbed text-editing screens it contains.

The Rules Editor is where the code that comprises your program, or system, is entered. Code refers specifically to the text portion of your system, which is composed of Mechanica Basic language keywords. The words program and system refer to the complete body of your code (and are used interchangeably in this document).

ThumbTack white

Very few Mechanica Basic keywords are legal on all five (5) pages. Why? Because most keywords fall into one of two major categories: Signal Rules, or Positions Sizing Rules.

The first three (3) pages contain the code for your Signal Rules. These rules are responsible for trade generation, and are stored in files with the extension *.sig.

The last two (2) pages contain the code for your Positions Sizing Rules. These rules are stored in files which carry the extension *.siz.

The Signal Rules are always run first, since they are responsible for generating the trades from your system(s). The Positions Sizing Rules are applied later.

 

Signal Rules pages

Lets examine the first three (3) pages of the Rules Editor.

The first page is the Resources page. This is where you tell Mechanica which data to display in the Grid.  The  grid is comprised of Columns which you populate with stored or computed data (High, Low, averages, etc.) and other technical indicators and values your program will look at or reference day-to-day.

Example:                COL1 = HIGH

The Trade Entry page is the place to tell Mechanica when and where to enter trades. The code you put here governs Mechanicas activities while it is searching for a trade entry signal (which it does any time your system is not currently in a trade).

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

The third page is the Trade in Progress & Exit page. This is where you tell Mechanica how to handle an exit stop, and when to exit a trade.

Example:        SELLSTOP = COL4[1]

The code on the Trade In Progress & Exit page governs Mechanicas activities while it is in a trade, such as determining when to exit, and tracking daily risk calculations.

Position Sizing Rules pages

The fourth and fifth pages are the Initial Size and the Resize pages. On the Initial Size page, you (a) tell Mechanica the conditions under which new trades are to be accepted or rejected, and (b) determine the number of contracts or shares to put on, once a trade is accepted.

Here is a simple Initial (position) Sizing statement:

Example:                NEWCONTRACTS = (TOTALEQUITY *.02) / NEWRISK

The Resize page is where you will tell Mechanica how, and under what circumstances, open trades are to be resized:

The code on these two pages is critical to the ultimate performance of the systems you create, and is discussed in detail later in this help document.

ThumbTack white

1.The information contained in these first three (3) pages of the Rules Editor comprises the code for your Signal Rules, and is stored in files with the extension *.sig. (Also called SIG files.)
2.The information contained in the last two pages (pages 4 & 5) comprises the code for your Position Sizing Rules, and is stored in files with the extension *.siz. (Also called SIZ files.)
3.SIG and SIZ files are created and maintained separately in Mechanica for the sake of organization, flexibility and convenience. The reasoning behind this concept will become more clear later on, as we learn how one SIZ file can direct the position sizing and resizing of trades generated by multiple systems running in Simultest mode.

 

Using the Rules Editor

Mechanicas Rules Editor is a full-featured Windows text-editing facility designed to make it easy for you to enter and modify your program code. When you launch Mechanica, the Rules Editor should be open and ready for use. If not, it opens automatically when you open a Signals file or a Sizing file (File menu, Open).

To begin work with the Rules Editor, select the page you want to edit by clicking on its tab, or by pressing its corresponding Function Key (F2 - F6). The cursor will automatically appear at the top left of each page.

Use the SHIFT+Arrow keys, or left-click and drag the cursor, to begin a block operation.
Once a block of text has been marked, CTRL+C or CTRL+INS will copy it into the Paste buffer. If CTRL+X or SHIFT+DEL are pressed, the block will be captured and then deleted (cut), instead of copied. If DEL is pressed by itself, the highlighted block will be deleted without being captured.
Use CTRL+V or SHIFT+INS to paste a captured block at a new location. You can cut text from one file and paste it into another, by copying text into the buffer, opening the file you wish to paste the text into, placing the cursor at the appropriate spot, and pressing CTRL+V or SHIFT+INS to insert the previously captured text into the new file.
Cut, Copy and Paste can also be executed via the Edit menu, and by clicking the right mouse button on the selected text and choosing the desired operation from the context menu.
Press CTRL+F6 to toggle between the Rules Editor and the Grid.

Other commonly used Rules Editor keystrokes are listed in the Navigating Mechanica section.

 

Notes on Terminology:

1.The words instrument and symbol are generic terms that refer to a commodity market or other futures contract, or the shares of a stock or a mutual fund. (Strictly speaking, as far as Mechanica is concerned, an instrument or a symbol is anything that can be represented as a continuous series of historical prices.)

The term symbol is always used in the context of Mechanica’s Symbol Manager, because under the Tag Symbols tab, we “tag” the symbols we want to test.

2.The long and short of it: Initiating a trade or a position in the long direction (“going long”) is the way most people invest in the stock market. They buy a company’s shares hoping that they will rise in price, and can be (later) sold at a profit.
3.“Going short” is just the opposite. You initiate a short position by selling an instrument. Then, if you buy it back later (this is called covering the short), for less than you sold it short for, the difference, minus any transaction costs, constitutes a profit. Investors buy-and-hold. Traders go long and short.
4.One of the benefits of futures is that they can be traded long and short with equal ease. Shorting stocks is more difficult; among other considerations (too numerous to mention here), shares must be borrowed, and only the most liquid stocks can be shorted. Then, there is the notorious “uptick” rule to contend with (this rule goes in and out of favor and is not an issue with single-stock futures). As a general rule, mutual funds cannot be shorted, although there are a few exceptions to this rule.