Category Archives: Stock

Quantshare Trading Software on Amazon’s EC2

I wrote an article on how to automate the process of updating quotes, updating multiple trading strategies and e-mailing next day signals form the cloud to our own e-mail boxes. This time I am using QuantShare as the trading software and an Amazon EC2 micro instance as the host cloud.

You can read the article at the QuantShare Blog.
More…

QuantShare is a fairly new piece of software that can be used to develop and backtest trading strategies. It has many features (I will get into them in another article/review). I will just go through a few that interest me. Keep in mind that most of my system backtesting and development has been done in Amibroker.

1. It has a similar “language” and array based logic as Amibroker. That means one can develop quickly in Amibroker and then “detail” the strategy in QuantShare. More…

2. You can write a Money Management script in C# to extend the rules of the main strategy.
 More…

3. You can write your own indicators in C# and then call them from the array based language as functions.

4. It has a genetic optimizer that works with a “Rules” list. You can combine rules and “auto-generate” systems in minutes. Or better, take your existing system and “look” for filters or additional rules that might improve it.
 More

5. QS uses custom databases. They can hold quotes, fundamentals  Yahoo news, tweets, COT data, whatever. It has a downloaded tool that can be customized to handle all kinds of downloads and update the database. Ready made downloaders exists for different markets. These historical databases can then be called from the backtester. More…

6. There is a community and ready made “scripts” that can be downloaded through the “Sharing Server“.

7. You can “combine” systems. So you can see the equity curve of trading MySystem1 with MySystem2 and… MySystemN.  More…

8. Reasonably priced.

So what’s the downside?
The real-time version is still in Beta as of this writing ( 28/10/2012).  So you cannot connect to a broker or auto-trade, yet.

Uncover Hidden Market Relationships Using Fuzzy Logic

In the previous posts (pt1, pt.2,  pt.3) we talked a bit about how to take various indicators and fuzzify them.

Now I will show how we can quickly test for relationships in indicators.
So the question is: Can we only use RSI(3) and RSI(14) (short term and medium term Relative Strength Index) to trade the S&P500?
So in the Fuzzy Model afl script that I wrote, we input rsi3 and rsi14 as inputs and “Low,Neutral,High” as qualifiers.

We then let it optimize using the  CM-AES non-exhaustive optimizer (with all default settings). After a couple of minutes we get the results:

So the rules that the optimizer came up with are:

rule   0   rsi3  Low  AND  rsi14   Low  Then  SELL
rule   1   rsi3  Low  AND  rsi14   Neutral  Then  Do NOTHING
rule   2   rsi3  Low  AND  rsi14   High  Then  BUY
rule   3   rsi3  Neutral  AND  rsi14   Low  Then  Do NOTHING
rule   4   rsi3  Neutral  AND  rsi14   Neutral  Then  BUY
rule   5   rsi3  Neutral  AND  rsi14   High  Then  Do NOTHING
rule   6   rsi3  High  AND  rsi14   Low  Then  Do NOTHING
rule   7   rsi3  High  AND  rsi14   Neutral  Then  SELL
rule   8   rsi3  High  AND  rsi14   High  Then  SELL

Do these make sense? Unlike Neural Networks, these rules can be read and checked by common sense. What the above rules tell us is that:

Rule 2:
Buy when rsi14 is high (meaning we are in a mid-term bullish move) and rsi3 is low (meaning we are at a short-term correction). That does make sense!
rule   4 :
  rsi3  Neutral  AND  rsi14   Neutral  Then  BUY – Hmmm… This is another way of saying that SPY has a positive bias overall.

Many more possibilities

No-ones restricts us to a 2 input model (although complexity goes up extremely fast). No one also restricts us to oscillator-type of indicators. One can try the same with COT (commitment of traders) data or with the price of Gold vs Price of Bonds ratio or whatever. The bottom line is that this tool can help you search for fairly robust and linguistic base strategies that can be cross-checked with common sense.

Here is some interesting links:

http://www.nisis.risk-technologies.com/events/symp2007/papers/BB25_p_kaymak.pdf
http://www.iaeng.org/publication/WCE2007/WCE2007_pp142-147.pdf
http://www.cs.wayne.edu/~mdong/fuzzytech.pdf
http://sedok.narod.ru/s_files/poland/Dourra_Siy.pdf

* I am neither a programmer nor a fuzzy logic expert. The information given is to the best of my ability/knowledge and meant to un-intimidate and motivate self directed investors to use tools that proffesional Quants use. The information is not necessarily written with accuracy in mind but with practical usability for someone who trades.

Matlab for Amibroker Users – Backtesting Functions for Matlab

The point of this post is to provide some basic functions to non professional Matlab users that may help backtest a simple long only system the way Amibroker (and  most other software) backtest.

If you are an Amibroker user you are used to something this easy:

sma9=ma(close,9);
sma21=ma(close,21);
buy=Cross(sma9,sma21);  
sell=Cross(sma21,sma9); 
Wish you could do that in Matlab?
Here’s the Matlab Code for a simple Moving Average Crossover system using some custom functions

%START CODE
%You need these files in the “Path”  for the code to work
%sma.m, iif.m, Cross.m, backtestlongAmount.m, GetTickerV.m, stock.m
%Download SPY from Yahoo, 5 years history, daily bars. We get a “fints” object
% Function TickerV() is similar to function stock() except that 
%it adjusts for OHLC to Adjusted Close Data.

ticker_fints=GetTickerV(‘SPY’,’5y’,’d’);
%%Get the close price from fints and make it a vector
close=fts2mat(ticker_fints.close);
open=fts2mat(ticker_fints.open);
sma9=sma(close,9);
sma21=sma(close,21);
buy=Cross(sma9,sma21);  % custom function, NOT Matlab’s native cross( ) 
sell=Cross(sma21,sma9); 
%Signal should be 1 for Buy, -1 for Sell and 0 for noAction (or Hold)
signal= iif(buy==1,1,iif(sell==1,-1,0));
% Buy and Sell on next bar Open 
 [pnl,pnlvector, sh]= backtestlongAmount(close,signal,open,1,100000);
% [pnl,pnlvector, sh]= backtestlongAmount(close,signal,close,0,100000);
plot(pnl,’DisplayName’,’pnl’,’YDataSource’,’pnl’);figure(gcf)
%END CODE

You are free to copy the code and download the functions. I am not a professional coder. Most functions include pre-existing code that I combined and edited. It’s very probable that mistakes exist.
I am sharing these m files in hope others will check and improve them.

Disclaimer: This code may not provide accurate results. You will have to check yourself to make sure the code works as it should.

The function are:
sma.m
iif.m
Cross.m
backtestlongAmount.m
GetTickerV.m
stock.m

here’s the link:
Custom Functions


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:
backtestlongAmount(data,signal,open,1,amount$$);
4. Amount :i.e., 100,000

backtestlongAmount.m

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