Category Archives: trading software

QUANTtrader -The Logical-Invest investment software for building and backtesting rules-based strategies

Quanttrader Logical Invest's swiss army knife

Investment software to easily create and backtest a rules-based investment strategy

QUANTtrader is a swiss-made software tool used to develop, backtest and implement rules-based strategies. It was initially developed by Frank Grossmann as his personal investment software. After having sold two companies, Frank trades for a living and his software reflects this. QuantTrader is available from Logical-Invest.com for a monthly license.

Quanttrader Logical Invest investment Software demo

Since it is built by a trader and long-time  investor rather than by a  developer. QuantTrader’s main strength is in building medium to long term investment portfolios that are diverse, adaptive and can control risk. All this without writing a single line of code.

Continue reading QUANTtrader -The Logical-Invest investment software for building and backtesting rules-based strategies

The end of the end of month strategy

Has the end of month strategy stopped working?

Historically and up to 2013, equities have exhibited a positive bias during the end of the month.
Here is an example of buying the SPY etf on the first down-day after the 23rd and selling on the first up-day of the next month. Trading is at the same day close.EOM_All

This has been well documented in academic papers as well as blogs. The main reason quoted for this persistent bias has been end-of-month window dressing.

As one of my favorite author/blogger/trader, Mr. Grøtte, has also recently blogged the EOM bias is no more.

EOM_13-15

Why is this important to know?

A lot of investors re-balance monthly. The day of the re-balance used to be somewhat important as there was an EOM bias. So it was better to ‘buy’ at the end of the month rather than at the beginning of the month. As of late (2013) this is less true.

What this means in practice is that the specific timing for re-balancing monthly strategies may be less important than it used to be.

       
//Amibroker code:
Buy=Day()>=23 AND C<Ref(C,-1) ;//AND C>MA(C,100);
Sell= (Day()<11 AND C>Ref(C,-1));
SetTradeDelays(0,0,0,0);
slip=0.00;
BuyPrice=c+slip;
SellPrice=c-slip;
posqty=Param("nUMBER OF pOSITIONS",1,1,30,1);
SetOption("MaxOpenPositions",posqty);
PositionSize=- 98/posqty;
bars = 10; // exit after 10 bars
ApplyStop( stopTypeNBar, stopModeBars, bars, True );

CBA – Quick test drive

Inspiration strategy: http://empiritrage.com/2013/01/21/correlation-based-allocation/

Quoted from Empiritrage.com:
“We propose a model that is designed to identify bull-market and bear-market regimes. We examine correlation between stocks and bonds as a signal. Our hypothesis is that negative correlation between long bonds and stocks represents a bear-market regime, and a positive, or non-existent correlation, reflects a bull market regime.The model calculates rolling 90-day correlation estimates between the S&P 500 and long-bonds. At each month end, the model computes the 60-day MA of the correlation against the 600-day MA of the correlation. If positive, the model invests in risk; if negative, the model invests in riskless.”


I will do a different kind of implementation so my results will differ due to different data and different strategy. When referring to correlation I will be referring to the correlation of the SP500 to the inverse of the 10-year Treasury yield.

The system will either be long the SP500 or be in cash.
Each month it will look at the short and long term correlations and if stocks/bonds tend to be more correlated than usual, it buys the SP500. If not, it sells it and holds cash.

I will be using yahoo free data: ^GSPC (S&P500 Index) data as the “risk” asset and (1/ ^TNX, the reverse of the 10-year Treasury Yield) to represent long-bonds.

We ‘ll keep 2 parameters optimizable and lock the long moving average period to 600, to keep things simple.
Params [90,60]
a. CorelationPeriod (default=90)
b. MAshortPeriod (default=60)
risk=Foreign(“^GSPC”,”C”);
bond=1/Foreign(“^TNX”,”C”);

Corr=Correlation(risk,bond,CorelationPeriod);
Buy=MoreCorrelated=MA(Corr,MAshortPeriod )>MA(Corr,600);

 The above graph is using params [90,60]

Just for fun let’s do the exact opposite: We will buy when the short term correlation is less than the long term one. Same parameters.

Let us optimize the parameters to get a better sense of what is happening. Is there a “general acceptable area”?
params: [10–>300 10–>300]

There seems to be a “better” area of settings than the one we are using.
Let’s try the backtest with params [40 220]. In other words we are calculating correlation using a 2-month window. We are then comparing the almost yearly moving average of that to the 600-day longer term one.

Using these optimized parameters, what would trading the 5 core etfs would look like? Risk on means buy SPY,EEM,EFA and VNQ. Risk off means buy IEF.

The equity is for the whole 5 -asset system. The lower pane shows the Emerging Markets time series (NYSEARCA:EEM).
You can see from the lower graph, this system did not buy EEM for the whole 2010-2013 period.

Going back to just trading the SP500, what would happen if we added the simple MA rule. Buy as before but only if price is above it’s 200-day moving average. Sell as before but also sell if price is less than it’s 200 moving average:

Disclaimer: This is a quick and dirty try-out of whether stock/bond correlation can help differentiate bull/bear regimes. It’s not meant to be precise but rather to get you started in performing your own tests.

Few thoughts on TAA:
I will caution the reader that when it comes to “TAA” strategies, for the past few years, holding both the S&P500 (SPY) and bonds (IEF) was an exceptional strategy whether you timed it or not. That is mostly due to the fact that IEF, the 10-year treasury proxy had both an exceptional run and a fairly uncorrelated one to the stock index. As others have mentioned, TAA systems may have a hard time delivering if Treasury yields stop dropping (below zero..?). But one way to look at the typical TAA strategy is that it is a diversified “short-the-dollar” strategy. Looking at it that way, there is room to diversify to non -U.S. denominates assets.

The code in Amibroker:

 
/
/----Code by Sanz P.-----------------------------------------------------// 
newMonth=Month()!=Ref(Month(),-1);
MAperiods=Param("MA Periods",200,100,400,5);
AboveMA=C>MA(C,MAperiods);
BelowMA=C<MA(C,MAperiods);
spy=Foreign("^GSPC","C");
ief=1/Foreign("^TNX","C");
Corrperiod=Optimize("corr Period",40,10,300,10);
MAshort=Optimize("MA short period",220,10,300,10);
Corr=Correlation(spy,ief,Corrperiod);
MoreCorrelated=MA(Corr,MAShort)>MA(Corr,600);
if(Name()=="^GSPC")
{

Buy=MoreCorrelated AND newmonth;// AND abovema;
Sell=(!MoreCorrelated /*OR !abovema*/) AND newmonth;
}
/*
if(Name()=="IEF")
{
Buy=!MoreCorrelated AND newmonth ;
Sell=MoreCorrelated AND newmonth;
}
*/
PosQty =Param("How many Positions",1,1,20,1);
SetOption("MaxOpenPositions", PosQty );
SetTradeDelays(0,0,0,0);
BuyPrice=SellPrice=C;
PositionSize=-96/PosQty ;

Visualizing Data

I have been looking for ways to visualize what happens to a trading system when we shift it’s parameters. I bumped into this little free tool that might help us do that. Let’s start from the beginning:

We will start with a hypothetical mean reversion system and check visually what happens as the parameters change.
We will run the backtests using the SPY etf (NYSEARCA: SPY) as a proxy for the S&P500.
Rules:
1. We can only buy when the close is above it’s 200 day moving average.
2. We buy when the RSI falls below 30.
3. We sell when the RSI rises above 55.

The easy case: Two parameters

We want to try different parameters. Instead of a 200-day filter we will try all moving averages periods between 10-days and 300-days, in 10 day increments.
For the RSI period we will try periods ranging from  2-days to 14-days.

So after optimizing the parameters we end up with a table listing the possible combinations of parameters and the profit they produce. In this case we are not using profit as a performance metric but Average Annual Return /Maximum drawdown (CAR/MDD).

Since we have 2 parameters we can visualize them. The higher the “mountain” (car/mdd) the better.

3D graphs by Amibroker

By visual inspection we can see that there is a fairly smooth area between 170-290 for the moving average and 2-6 for the RSI. Any choice of parameters inside those ranges would produce fair risk adjusted return.

What about 4 parameters?

Now we want to try out different thresholds for the RSI indicator. We have tried 30 as a “buy” threshold and 55 as the “sell” one. We ‘ll optimize those values as well. Now we have 4 parameters. How do we visualize them?

After optimization, we should have a table that lists the different combinations of parameters and the corresponding statistics. We ‘ll export it into a csv file*.

Now we ‘ll get this free app called XDat. It reads csv files (with headers) and produces these weird looking graphs, called Parallel Coordinates.

Each column can be tweaked. Let’s say we wanted to see the most profitable parameters. We would go to the first column (Net % Profit) and raise the little red triangle all the way up:

We see that the most profitable systems correspond to parameters: MA period  around 300, rsi period 2, Buy level at 40 and sell level at 95. This is what we would get if we just did the optimization and looked at the sorted results. These would be the “best” parameters.

Now going back and lowering the triangle, we see that the most profitable systems correspong to just a few trials. Most of the trials are below that. So we should consider these “best” parameters as “outliers” and throw them out. We do that by lowering the top red triangle and cutting the top off.

Playing around a bit more we come up with this:

The upside to this procedure is that it’s easy and understandable for a few parameters.Maybe the next step in visualizing further parameters would be to use Self-Organizing-Maps. Anyone who has experience using SOM’s is welcome to comment. Other ideas are welcome.

____________________________________________________________________________

* I used Amibroker to optimize the parameters. Amibroker produces a table that includes many statistics as columns for each set of parameters: Profit, profit factor, k-ratio, % winning trades, etc. After exporting this table to csv, I manually deleted most of the columns (i.e., profit factor, etc…)  as well as some rows (i.e. parameters that resulted in less than 10 trades). This was done to decrease the size of the csv.

Mon AMMI

No, it’s not french and it’s not the movie.
It’s a fast-N-rough “Adaptive Multi strategy Multi Instrument” model.

Let’s assume we want to trade mean-reversion: If price moves down we buy, if it moves up we sell.

Possible Indicators from the blog-o-sphere:
RSI(2),RSI(3),RSI(4)
DV2 here and here
BSI here or here
BoilingerBands
CRSI here
TD9 here

Question 1: Which Indicator to use?

One way to choose an indicator is to run backtests for each one (and each one’s parameters) and pick the one that has performed the best in the recent past. “Performance” can be pure profit or it can be any other metric we choose:  CAR/MDD (annual return/MaxDrawdown), Profit Factor, Expectancy or your very own “Bliss function“.
So if DV2 has performed best in the past 6 months, maybe we should use that to trade for the next few weeks.

Question 2: What should we trade? 

Just one instrument?
Why not backtest all indicators on all instrument and let the data tell us which instrument behaves best with which indicator? We can then trade that one. Did someone say “overfit”?.
Why not a bunch of instruments? A portfolio! Yes and we ‘ll throw some weights in there, too:
30% SPY 10% AAPL 10% XLU 20% IBM 30% GLD  … wightN*InstrumentN

I guess now we have to backtest the different indicators with these different weights on our instruments, too. What are we doing here? Let’s step back.

Question 3: What are we doing here?

We start with a set of selected assets (StockA, StockB,… StockN).
We bring in a  set of selected indicators (ind1, Ind2, …IndN).
We then build an “ammi” that picks and chooses from these two universes.
We optimize that “ammi” using some type of non-exhaustive optimization technique.

In other words we are creating a portfolio and trading each instrument inside the portfolio with each own indicator/strategy. So what’s the catch? There’s nothing new here. We could just trade SPY with DV2 and AAPL with RSI(3). Allocate 30% of capital to strategy “SPY” and 10% to strategy “AAPL”. Is there an advantage in mixing it all up?

For the purposes of this post we will assume:
1. Our optimizing target  is % profit.
2. We are investing a fixed percentage of current equity on each trade (i.e., 10% of Equity). In other words we are compounding.

By optimizing one instrument and one strategy, the optimizer will look for the most consistent “timing” to enter and exit trades.

By optimizing the whole AMMI, the optimizer is handed one more tool. Since it wants to maximize compound profit it can do so by minimizing max drawdown which in turn gets accomplished by minimizing correlations between the “weighted and traded” equity curves. So, in theory, the optimizer should not necessarily choose the best timing of each instrument/indicator but rather the best combination of instruments/strategies that are somewhat profitable and least correlated*. We hope that this criterion is more robust criterion than just timing.

Almost forgot! We also want to be able to change both the “instrument” and the “strategy” as time passes. That means we re-optimize every x days and trade with the fresh settings. Hence, the “Adaptive Multi strategy Multi Instrument” , a.k.a, AMMI, nick-name.

For the sake of simplicity, we will consider indicators RSI(2), RSI(3) and RSI(4). Possible thresholds are 0–>50 for a Buy signal and 50–>100 for a Sell signal. We will use 10 instruments that can have weights ranging from 5% –> 100%. We assume the standard 2x leverage can be used.

Let’s run an Out Of Sample test. In_Sample (IS) Optimization period is 2 years, Out_Of_Sample trading period (OOS) is 3 months.**
Optimization is done using Amibroker’s non-exhaustive CMAE plug-in set to run very few “runs” so as not to overfit (and not wait too long…). Target for the optimization is pure % Profit (hence the huge draw-downs).

Here’s the  result trading a few instruments and using RSI(2), RSI(3) and RSI(4) as possible indicators with varied thresholds. This is an OOS (out of sample, i.e., realistic) equity. At 40%+ (in the “good” version) draw-downs, it’s not something I would trade***. It’s just for illustration purposes.

Again. this is a rough “model”. It is best used as a starting point to get ideas going. It might also help to get a sense of how different data groups react to different indicators.  Instead of mean reverting indicators you could have trend following , or pattern based indicators. And instead of optimizing precise values you could fuzzify them.

———————————————————————————
* Adding a losing strategy to a profitable strategy may increased the Sharpe Ratio of the system.

**The In-Sample optimization will gives us the weights on the instruments as well as the indicator and the thresholds that were used to produce the best performance. An example would be 5% InstrA, 0% InstrB, 0%InsrC,70% InstrD,100% InstrE, 25% InstrF, etc. Indicator would be RSI(4) and lower and upper thresholds would be 30 and 70. Those set of parameters whould then be used to trade “live” (Out-Of-Sample) for the next three months. After that we would re-optimize (inclusive of the 3 months that were traded) and change param for the next 3 months.

***Unless you know what you are doing, optimizing a system for Max Profit, is a fairly bad idea. It will tend to pick few winners and not diversify (i.e., in hindsight it will tend to pick 100% APPLE). 

———————————————————————————

The code in Amibroker:

 
//----Code by Sanz P.-----------------------------------------------------//
//----Place the instruments on a watchlist and select it under Parameters
//----Optimize/Walk Forward on that watchlist
//------------------------------------------------------------------------//
function Set2D( tablename, x, y, value )
{VarSet( tablename + StrFormat("%03.0f%03.0f", x, y ), value );}
function Get2D( tablename, x, y )
{return VarGet( tablename + StrFormat("%03.0f%03.0f", x, y ) );}
OptimizerSetEngine("cmae");
OptimizerSetOption("Runs", 3 );
OptimizerSetOption("MaxEval", 300 );
WatchlistNumber =Param("Choose Watchlist with Tickers",12,0,30,1);
TickerList= CategoryGetSymbols( categoryWatchlist , WatchlistNumber ) ;
tablename="a";
//init
for( n=0; (instrument=StrExtract( TickerList, n)) != ""; n++)
set2d( tablename, n, 0, n );
Buy=Sell=0;
for( n=0; (instrument=StrExtract( TickerList, n)) != ""; n++)
{
set2d( tablename, n, 1 , Optimize("Ticker"+n+ "Position",20,0,50,5) ); //pos
set2d( tablename, n, 2 , Optimize("Ind"+n,2,0,2,1) ); //ind
set2d( tablename, n, 3 , Optimize("Thrsh"+n,20,0,100,5) ); //thr
}
for( n=0; (instrument=StrExtract( TickerList, n)) != ""; n++)
{
if (Name()== instrument)
{
indicator=0;//init
indicatorcode=get2d( tablename, n, 2 );
switch(indicatorcode)
{
case 0:
indicator=RSI(2);
break;
case 1:
indicator=RSI(3);
break;
case 2:
indicator=RSI(4);
break;
}
PositionSize= - get2d( tablename, n, 1 );
thresh=get2d( tablename, n, 3 );
Buy=Cross(thresh,indicator);
Sell=Cross(indicator,thresh);
}
}
SetTradeDelays(1,1,1,1);
BuyPrice=SellPrice=ShortPrice=CoverPrice=O;
SetOption("MaxOpenPositions",10);