Basic Backtest Function for Matlab

[pnl,pnlvector, sh]= backtestlongAmount(data,signal,buyprice,delay,amount$$);

This is a very basic function for backtesting a strategy in Matlab.
All you need is a vector with 1s for Buy, -1’s for Sell and 0’s for Hold.

To use it we need
1. A vector of prices,i.e. SPY close prices
2. Signal: A vector of  1 for BUY, -1 for SELL and 0 for HOLD
3. Buyprice: A vector of Close prices if we buy at close or Open prices if we buy at Open
4. Delay: 0 if we buy “Today” – 1 if we buy tomorrow. So to Buy Tomorrow on Open:
4. Amount :i.e., 100,000


Please report any bugs or inaccuracies you find. Improvements are welcome.

Amibroker on Amazon’s EC2

This post is a technical description on how to set up Amibroker on EC2. For licencing Amibroker running on EC2 or another VPS contact

This is a rough guide on how to setup Amibroker to run on an Amazon EC2 micro instance.
Turns out, Amibroker runs fast even on this tiny micro instance.

Amazon provides one year free access to a basic micro instance to new users. It’s their way to let new people try out their services.
In a nutshell, setting up an EC2 instance is like setting up a small PC on the cloud. You can install anything on this PC. You can choose the base OS and then configure this virtual PC as needed.
I wanted to setup a “virtual” PC that would use Amibroker to run analysis and generate signals  for my EOD trading as well as auto-trade some paper systems on It would replace my laptop at home that does that every night.
I also wanted to try an alternative to running trading systems through Scripts on Google Docs.

You need to sign up for an account with unless you already have one. Even if you run the free  micro instance, you will need to give out your credit card to Amazon.
Please review Amazon’s offering. It should be enough to run free for a year.
If you exceed the quota they give you, you will be charged. 
Sett up the instance. I personally used the Windows 2008 32bit AMI, but I guess you can use the Windows 2008  64bit R2 one. The lighter the package, the better since the micro instance has minimal RAM and CPU power. Please watch this video. It will guide you through the steps:
(Start from the middle of the video where he sets up the actual instance).
If you don’t like videos you can read this how to guide:

The default Security Group does not allow Remote Desktop Connection.
When you get at the part of setting security groups, edit the default group and add port 3389 inside the “Port Range”. Press “Add Rule’. This will open up the port for Remote Desktop Connection which is the non-programmer’s way of controlling your remote PC. You can leave Source as is unless you have a static IP and are concerned over security.

Once you have set up port 3389 access you have the minimum to continue. Optionally you can open additional ports (SSH, FTP, etc).

Assuming you have followed the video tutorial you have now a micro instance running on EC2.

 1. Connect to the instance using Remote Desktop Connection. Best way is to go to the AWS control panel, right click on the instance and choose Connect. This will download a file and fire up RDC with the right settings. Alternatively, start RDC (Start—>search “remote”) and point it to the instance’s public IP address.

Choose “options” and type “Administrator” under User. Connect. Type in your instance’s administrator password when asked. A window should open with your new desktop.

2. At this point, I was asked to setup my network as Home, Work or Public. I chose Public. That brought problems and caused Internet Explorer to refuse to download stuff… So… I downloaded Firefox.

3. Get Firefox: Start Internet Explorer from the Start Menu. Navigate to “” and download Firefox. If IE refuses, try adding to the trusted sites.

4. Get Amibroker: After you install Firefox, launch it and go to Download the official version. Go to the Download folder and launch the installer. Amibroker will install as usual.

7. * Please check with for proper licencing terms.
Send a LostKey request to Amibroker: Assuming you have a valid license, go back to your regular computer (hide the RDC window). Navigate to and fill the form. Check your email. Copy the address that was sent to you (highlight it and right click copy).

8. License Amibroker: Now go back to the instance window. Open Firefox (in the instance) if it’s not open. Paste the address that was send on the email. Hit Enter. Check your Download folder for a small file. Double click it. This will authorize Amibroker under you existing license.

That’s it.

Now if you want to transfer files from your computer to your instance, the easiest way is by copy/paste. Select a file (or folder) in your computer desktop and right-click “Copy”. Now go to the instance desktop, right-click “Paste”. That simple.

If you have large files/folders to transfer, copy /paste takes forever. Another way is to set up a Dropbox or SugarSync account and upload your files there. Then from the Instance, turn your browser to that account and download into the instance (at a faster speed since you are using Amazon bandwidth ). Keep in mind that incoming traffic seems to be free, but you do get charged for outward traffic, after a certain quota is reached.

If you want to schedule tasks or custom Jscripts or VBScripts you will have to use the Windows Task Scheduler. Setting that up is more complicated than in your home PC. 

Note1:  For JScripts,”WScript.Echo()” does not seem to work as expected. You can use:
var WshShell= new ActiveXObject(“Wscript.Shell”);
WshShell.Popup (“Backtest has run”, 5);

Note2: If you are sending emails from the instance, keep in mind Amazon has restrictions on outgoing emails. My Jscript that calls CDO to email, failed because of that. Check Amazon’s aws FAQ for more info.

Note 3: For the Task Scheduler: I am still working on the setup. So far I can advise you that you will need to:
a. Provide a static “computer name” to your instance since the assigned name (i.e. IP-xxxx)  changes every time you reboot. Here’s how to do that:
b. Test the tasks using the default basic settings “Run only if User is logged In”  This way you can “see” them running and check the Popup messages.
c. Eventually, the instance should run unattended whether logged in or not. You need to change the settings so that the Task runs “Wether User Logged In or Not” and use “Highest Privileges”.

Now, here’s the tricky part: If you try to test a JScript with these settings, you may not see any feedback or Popups while you are logged in. This does not mean it is not running! You may need other ways to monitor. Feel free to contribute on how to do that…

What is left to do:
The initial idea was to set up a EC2 instance that would START up, run for 1 hour ONLY, update quotes, run backtests/exploration, email the trading signals and then STOP. That would translate into less than 30 hours/month EC2 usage time or less than $5 for a Small Windows instance. And even less for a micro. 
Turns out, there is no easy way to schedule the instance to START at a certain time (the stopping part can be scheduled inside the instance). I hope a solution is found before the free tier offer expires. The cloud can get very expensive…

Any ideas or suggestions are welcome.

Project: Google Code to

I just finished part of my summer project. I now have a “private”  fund called TRPL at

The system driving the fund resides on my Google Docs account. It is written in JavaScript. It triggers once a day so it takes up negligible resources. It pulls historical data from Google Finance, runs the proper algos and comes out with the proper fund allocation. It updates my fund at and  emails me a confirmation of the new positions.Now I can go on vacation without worrying about my internet connection or possible power failure. is an online social investing site and a sister site to Collective2. It lets you trade a system online and possibly earn revenue by allowing subscribers to “buy” your fund at their broker. Even if no-one does, it’s a great way to forward test a simple strategy. They do have a basic WebAPI entry interface. They do charge if you choose to  take your Fund “public” (meaning others can subscribe to it).

I do manage a second fund at the site called “DGCP“. It is far too complicated to code in Javascript. So my next project is to automate Amibroker to trade DGCP automatically.

Trading Log for Amibroker

I have been looking into software to log my trades and keep track of actual trade performance. I was looking for:

1.  Simple import process from Interactive Brokers TWS Log Files.
2.  Be able to code and run custom statistics on portfolio level trades, not just per instrument.
3.  Be able to see my entry and exit on a price chart.
4.  Visualize Portfolio Equity Curve.

There are some great commercial software out there but they mostly lack the ability to visualize entry and exit points. I liked trading diary pro as it works well with IB. But it’s more of a bookkeeping tool than a custom analysis tool.
 I also tried Excel to no avail.

So I wrote my own script for Amibroker (mostly based on an existing script in the AB library written by Herman).
The scripts reads the trades from IB’s TWS export.
Here is a screenshot:
Note: The equity curve is for ALL instruments traded.

There’s still a lot to be done:
1. Export sorted trades and make  backup copy to Excel.
2. Append TWS daily .csv files automatically.
3. Equity Curve taking into account daily prices, not just entry and exit. (Hard).
4. Equity Curves per Instrument.
5. More Stats: Drawdown, CAR/MDD, etc.

Best Day to buy Bovespa – Part 2

If you bought Bovespa on Thursday and sold on Friday every week since 2003, you would be rich (if you compounded). We know this. If one was to follow such a strategy, in real-time, would it make money?

In the following strategy an investor would buy at the Close of the “optimal” day that performed best in the 5 past years. She would buy on this day (sell on the next close) for 6 months. At the end of 6 months she would re-optimize, find the “optimal” day of the past 5 years from the current date and follow it for another 6 months. So here is a walk-forward back-test that shows how she would do.

Results are NOT compounded (hence the huge difference).
Initial Capital:   $100,000 – Profit:  $127,438.7

End No. Net Profit Net % Profit day of week Profit $$
34820 34826 1 3809.68 3.81 1
34826 35186 1 9849.84 9.85 4 13659.52
35186 35192 1 15842.02 15.84 4 29501.54
35192 35551 1 6754.01 6.75 4 36255.55
35551 35557 1 12394.78 12.39 4 48650.33
35557 35916 1 -19902.37 -19.9 4 28747.96
35916 35922 1 10016.43 10.02 1 38764.39
35922 36281 1 22435.88 22.44 1 61200.27
36281 36287 1 -16342.55 -16.34 1 44857.72
36287 36647 1 15916.31 15.92 4 60774.03
36647 36653 1 4610.77 4.61 4 65384.8
36653 37012 1 7781.22 7.78 4 73166.02
37012 37018 1 -10523.76 -10.52 4 62642.26
37018 37377 1 -12518.35 -12.52 1 50123.91
37377 37383 1 -3076.38 -3.08 4 47047.53
37383 37742 1 4480.75 4.48 4 51528.28
37742 37748 1 9594.63 9.59 4 61122.91
37748 38108 1 3838.22 3.84 4 64961.13
38108 38114 1 21109.48 21.11 4 86070.61
38114 38473 1 8833.46 8.83 4 94904.07
38473 38479 1 3687.74 3.69 4 98591.81
38479 38838 1 8690.9 8.69 4 107282.71
38838 38844 1 8881.05 8.88 4 116163.76
38844 39203 1 -4282.91 -4.28 4 111880.85
39203 39209 1 12311.72 12.31 4 124192.57
39209 39569 1 -767.54 -0.77 4 123425.03
39569 39575 1 -2413.08 -2.41 4 121011.95
39575 39934 1 -9401.48 -9.4 4 111610.47
39934 39940 1 14681.36 14.68 4 126291.83
39940 40299 1 3449.71 3.45 4 129741.54
40299 40305 1 -3704.01 -3.7 4 126037.53
40305 40664 1 1401.17 1.4 4 127438.7

Do-it-yourself Investing, Quant tools and thoughts on the market