OverviewWhat is SPECTR?
The SPECTR TutorialThis 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:
Creating Your First Trading ScriptThe 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?
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.
Validating Your Script to Check for ErrorsNow 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 DataOne 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.
Analyzing the Performance of Your ScriptAfter 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 CompetitionNote: 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 ContestNote: 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 & TricksHopefully 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. ConditionalsThe 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
IndexingThe 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). MacrosThe 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
|