Category Archives: trading software

Bitcoin, Ethereum and Altcoins: How to get free daily and intraday Bitcoin historical prices

In order to analyze and build ‘crypto’ based trading strategies we need to get historical data for Bitcoin and other ‘large-cap’ coins such as Ether, Ripple, Dash, Monero, etc. But also for up and coming coins such as Neo, Stratis, IOTA and many more. In this post I will point you to two solutions:
1. Using simple Python scripts originally posted by QuantAtRisk.com
2. Using QuantShare software and a ready-made downloader.

1. Python scripts:
Get the latest list of cryptocurrencies, sorted by market capitalization from Coinmarketcap.com


Get Daily prices from Cryptocompare.com.

To get intraday data you can take a look  at the Crytocompare API and adapt this script.

The last script will create one csv file for each coin. You can then import these files into your own software.

2. Using QuantShare.

I actually use QuantShare for analysis and backtesting and wrote a Cryptocompare downloader for it. If you have QS the downloader is free from QS’s online trading objects library.

QuantShare Cryptos Bitcoin
QuantShare Cryptos Bitcoin

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.