My Portfolio Trade

Overview

What is SPECTR?

  1. Specialized: SPECTR was created specifically for Equitrader by Equitrader.
  2. Programming: SPECTR includes its own scripting language.
  3. Environment: SPECTR includes multiple execution environments for testing with historical stock data or running live.
  4. Competitive: SPECTR supports running trading algorithms in Equitrader's weekly, monthly and quarterly contests.
  5. Trading: SPECTR is all about automated stock trading.
  6. Robots: SPECTR is a complete package for creating, testing and benefiting from autonomous stock trading "robots".

The SPECTR Tutorial

This tutorial will cover all of the basics to help you can get started in the world of autonomous stock trading. The topics covered by this tutorial include:

  1. Creating your first trading script
  2. Validating your script to check for errors
  3. Testing your script against historical stock data
  4. Analyzing the performance of your script
  5. Testing your script in the practice competition
  6. Submitting your script into a rated Equitrader contest
  7. Tips & Tricks

Creating Your First Trading Script

The first step in using the SPECTR environment is creating a simple trading script. This will require familiarizing yourself with Equitrader's custom scripting language, but don't worry... it's easy to learn. Let's jump right in and look at a very basic script.

Strategy MyTradingScript ('AAPL') using 1-Minute Intervals:
	Lot MyLot:
		OrderSize: 500
		OpenPosition: Buy Market
		ClosePosition: None
	EndLot
EndStrategy

What does this simple script show us?

  1. Scripts are made up of "strategies". A script can contain one or more strategies.
  2. Each strategy has a name, a set of stocks that it trades, and a preferred interval size. Strategies can operate against 1, 5 or 15 minute intervals as well as daily intervals. We'll learn more about this later.
  3. Strategies are made up of "lots". A strategy can contain one or more lots.
  4. Each lot has a name, an OrderSize clause, an OpenPosition clause and a ClosePosition clause. Lots grant the author more flexibility and allow splitting positions into distinct segments that can be bought and sold with different rules.

Of course, there's a lot more to it than that, but we'll get there slowly. Look back at the script and try to figure out what will happen when this script is executed.

Figured it out? The script will generate one order, an order to purchase 500 shares of AAPL at the current market price. The order will execute soon after and the script will hold onto the AAPL position indefinitely, because there is no order to close the position out.

Let's test it out. The first step is entering the script on the Equitrader website. Follow the steps below.

  1. Login to the Equitrader website if you haven't already.
  2. Navigate to Home > My Algorithms. This is your portal to the SPECTR environment.
  3. Click on the "Create New" button to create a new algorithm script. This will present you with the script editor.
  4. Enter a name for your script, such as "MyFirstScript".
  5. Enter the script shown above in the script editor.

Validating Your Script to Check for Errors

Now that you've entered your algorithm into the script editor, the next thing to do is to make sure the syntax is valid. Otherwise the SPECTR environment won't know what to do with your script.

Click on the Validate button below the editor area. If you did everything correctly, a big green check mark should appear above the editor along with the message: "Algorithm Validation Successful". This indicates that the SPECTR system was able to parse and validate your script... it's ready to run!

Before we continue, let's see what would've happened had we not done everything properly. Try validating some of the following scripts to see what happens.

Remove the OrderSize clause:

Strategy MyTradingScript ('AAPL') using 1-Minute Intervals:
	Lot MyLot:
		OpenPosition: Buy Market
		ClosePosition: None
	EndLot
EndStrategy

You should see an error very similar to the following text: line: 3, encountered token: "openposition", expected token: "ordersize"

This is telling us that at line 3 in our script, the parser found the text "OpenPosition" but was actually expecting to find our OrderSize clause. Oops! Now we know what's wrong and we can add the OrderSize clause to fix the bug.

Try to submit a Cover order after our Buy order:

Strategy MyTradingScript ('AAPL') using 1-Minute Intervals:
	Lot MyLot:
		OrderSize: 500
		OpenPosition: Buy Market
		ClosePosition: Cover Market
	EndLot
EndStrategy

Error: Transaction types 'Cover' and 'Buy' are incompatible in lot MyLot

The system has detected incompatible order types. A Buy order can only be followed with a Sell order and a Short order can only be followed by a Cover order. If we want to hold both long and short positions, we need to use multiple lots.

Okay, that's enough validation for now, let's get to the fun stuff... running our script!

Testing Your Script Against Historical Stock Data

One of the coolest aspects of the SPECTR system is the "back-test environment". The back-test environment allows you to execute your scripts against stock data from the past. Imagine the alternative: you create a script and want to see how it performs. You have let it execute against live data for several hours or days (at least) to get any meaningful analysis regarding its performance. You tweak the script here and there and then you have to run it again for just as long to see how it changed the script's behavior. This is obviously very time consuming and not very efficient. Fortunately, Equitrader maintains a huge database of historical stock data, so we don't have to suffer that fate. Let's see how it works.

  1. First, save your algorithm by clicking the Save button under the script editor. If you forgot to edit the script back to a valid state, you'll see a warning message when you save it that indicates the script errors. Be sure to correct any errors before continuing.
  2. Click the Continue button to return to the My Algorithms page.
  3. Click the checkbox to the left of your new script to select it. Then click the Back Test button.
  4. Here, we see the same parameters that are found in Equitrader's trading contests. Things like the start and end date, the amount of starting cash, interest rates, etc. There are even several presets available to test your script with actual contest parameters from the past.
  5. Let's choose a small period of time for testing, such as Jan 15, 2007 - Jan 16, 2007.
  6. Leave the other settings at their defaults and click Submit to submit the backtest.
  7. A confirmation page will let you know that your backtest request has been received and will begin processing shortly. Click the Continue button.
  8. The "Test Status" column on the next page will keep you informed of the status of your backtest. Once it changes to "Complete", the results will be available for analysis. Click on the "Complete" status link to see the results.

Analyzing the Performance of Your Script

After your back-test has executed, the results can be found under Home > My Algorithms > Test Results by clicking on the Completed status link. This page will show you a summary of the results: the Total Return, % Return, Risk Factor, Score, Profit Factor, Profit Per Share and number of Shares Traded. This is very useful, but clicking on the Transaction Detail icon (it looks like a magnifying glass) will provide much more information. The main tab on this page displays the "Matched Trades" for your back-test. Because the scripts trade in lots, the system can easily matchup the orders that open and close each position. This allows you to identify how much was gained or lost each time a lot closed out its position. The raw transactions can be found under the "Transactions" tab and the back-test parameters used in the test can be found under the "Parameters" tab.

An "Export" button found on the earlier test results page allows you to export these results to a CSV file so you can analyze the results locally and keep track of how each change to the script affected its results. The Equitrader database will only store the results for a single back-test for each of your algorithms, so this is the best way to track changes across edits or multiple back-test executions.

Finally, you can compare the performance of two or more of your algorithms by selecting the algorithms on the My Algorithms page and clicking the Compare button. This brings you to a very similar results page where the summary for each algorithm can be seen. This comparison will be most meaningful when each of the compared algorithms was run against identical back-test parameters. In fact, when you submit a back-test you can select more than one algorithm for testing by checking the boxes to the left of each algorithm. Once the back-test completes for each algorithm, you can compare their results to see which one came out on top with the provided back-test parameters and date range.

Testing Your Script in the Practice Competition

Note: The following functionality has not yet been activated but is coming soon.

The next step is testing your algorithm in a live competition. When you create a new algorithm, it is automatically registered into a dedicated Algorithm Practice competition. Your algorithm's practice portfolio will be displayed on your Portfolios page along with your own portfolios. Clicking on the pie chart icon will allow you to view your algorithm's portfolio but you will not be allowed to submit any trades here. For the practice competition, you are allowed to start and stop the execution of your algorithm and you can also reset its portfolio. By default, algorithm portfolios in the Algorithm Practice competition are stopped. That is, your algorithm will not be executed and will not place any trades. To activate your algorithm in the practice competition, click on the Start Algorithm button on its portfolio page. As it runs, you can monitor its progress against real-time data just as you monitor your own portfolios. Algorithms are executed once per minute in a real-time environment. Note that performance might be slightly different here than in the back-test environment, due to subtle but important and unavoidable variations between the real-time and back-test environments. It's important to perform ample testing in the practice competition before moving on to the next step...

Submitting Your Script into a Rated Equitrader Contest

Note: The following functionality has not yet been activated but is coming soon.

Finally, after lots of tweaking and testing, you are ready to submit your algorithm into a rated Equitrader competition. All you need to do is navigate to the normal competition registration page and select your algorithm in the drop-down list before completing the registration.

There are two options when submitting your algorithm into a rated competition: a) registering your algorithm as a stand-alone competitor and b) registering your algorithm as a helper. In the latter scenario, you will share a portfolio with your algorithm. This can be particularly useful if you would like to decide when to open a position and allow the algorithm to decide when to close the position (or vice-versa). To employ this strategy, simply set the OpenPosition (or ClosePosition) clause of your lot to "None".

After the competition is over, your algorithm will receive its own rating, just like a regular trader. Your algorithm also has its own profile page where users can view its statistics. Note that once you submit an algorithm into a rated competition, you are no longer permitted to edit the script. This is to prevent invalidating the rating and statistics of the algorithm.

Tips & Tricks

Hopefully this tutorial has been helpful but we've only covered the very basics. This section will go over some of the more useful features of the SPECTR scripting language. Each example below builds upon features revealed in earlier examples.

Conditionals

The language supports logic similar to if/else statements in traditional programming languages. These can be used in all of the clauses found within a lot (OrderSize, Activation, OpenPosition, ClosePosition). Here's an example:

Strategy MyTradingScript ('AAPL') using 1-Minute Intervals:
	Lot MyLot:
		OrderSize: 100 when Close < 50 Else 500
		OpenPosition: Buy Market when Close > 100 Else Buy Limit(Close)
		ClosePosition: Sell Limit(Close) when Close > BuyPrice Else None
	EndLot
EndStrategy

Indexing

The language supports indexing many of the built-in fields, allowing you to look into the past. For example, if we wanted to compare the close price of the most recent 1-minute interval with the close price of the previous 1-minute interval, it would look something like this:

Strategy MyTradingScript ('AAPL') using 1-Minute Intervals:
	Lot MyLot:
		OrderSize: 500
		OpenPosition: Buy Market when Close < Close[1] Else None
		ClosePosition: None
	EndLot
EndStrategy

The index is specified in brackets after the field identifier. Most stock fields and interval fields can be indexed in this manner. The index can be a complete mathematical expression or simply a constant as shown above. Actually, when the index is not specified, it's implicitly defined to be zero, referring to the most current interval. Indexes always move back in time: you cannot reference the future (negative indexes are not allowed).

Macros

The language supports "macros", which are simply shortcuts that allow you to use an expression in multiple places without copying and pasting it everywhere. Here's a very simple example:


Strategy MyTradingScript ('AAPL') using 1-Minute Intervals:
	MyMacro: 500
	Lot MyLot:
		OrderSize: MyMacro
		OpenPosition: Buy Market
		ClosePosition: None
	EndLot
EndStrategy

The order size of the Buy order will be 500 shares. Here's another example:

Strategy MyTradingScript ('AAPL') using 1-Minute Intervals:
	ConditionA: Close > Close[1]
	ConditionB: Close < Close[1]
	Lot MyLot:
		OrderSize: 500
		OpenPosition: Buy Market when ConditionA else Buy Limit(Close) when ConditionB
		ClosePosition: None
	EndLot
EndStrategy
Return to Help Topics