Wednesday, November 9, 2011

Why High Frequency Trading cannot be backtested

The reason is simple: as far as HFT impacts on the first price (the highest one for selling operations, the lowest for buying), in a real environment HFT changes the market.

The impact could be minimal for FX markets, where high volumes of transactions are placed, but exists and has effects. Or the impact could be enormous in local stock markets, for instance, where a "touch" to bid/offer prices changes them, modifying market prices and behavior of other actors.

Market impact could be simulated, but, in this case, we have to backtest the simulation.... an impossible fact by definition.

Saturday, November 5, 2011

What is Algorithmic Trading

My previous post, trying to define Systematic Trading, mentions Algorithmic Trading.

As it's described in wikipedia, use of computer programs to materialize trading orders using an algorithm which decides when and how should be traded.

For instance, an algorithmic trading algorithm may be "try to follow VWAP". Traders may use this algorithm to buy (or sell) 1Mill euro of TEF.MC, fixing a level of aggressive, maximum and minimum prices and time to place all stock orders.

Algorithms used in algorithmic trading are well-known. In a future post I will enumerate some of them.

As examples of Algorithmic Trading Systems are (both two commercial) Sungard GLStream, Progress and (opensource -under development-) AlgoTrading.

Algorithmic Trad. is, in fact, a particular case of Systematic Trad. necessary when not enough liquidity and/or high market impacts are expected when trading.

Tuesday, November 1, 2011

What is Systematic Trading

Systematic trading is a way of defining trade goals, risk controls and rules that can execute trade orders in a methodically way.

Thanks to the methodically approach, rules can be tested using past data.

It does not need to include the use of computers, but is almost impossible to achieve trading goals without using a computer and a systematic trading system in which rules are programmed. Systematic traders employ technology to capture profit opportunities.

The opposite is discretionary trading. Compared to systematic trading, discretionary trading has more influence from emotions, no easy possibilities of backtesting and a limited risk control

Similar ideas are algorithmic trading and quantitative trading. But algorithmic trading is more related to how to trade an order or a set of orders, using a set of well-known algorithms. Quantitative trading includes all those kind of trading (systematic, discretionary, algorithmic, HFT...) which uses too quantitative techniques to decide trading options and executions.



Monday, October 10, 2011

Mutual cointegration, single correlation

To clarify concepts; let X and Y two vectors (series datorum)
  • Cointegration between two data series:
X and Y are 1-cointegrated if exists a, ut such that: xt = a·yt + ut

with u stationary in average and variance.
Cointegration tries to capture historical trends and dynamics between two series.
  • Single correlation between two data series:
Correlation captures any kind of relationship between two series
Does not include any kind of historical trends, considering only how one variable explain point by point the other one.


More information on:


Thursday, May 5, 2011

QuantLib

14 hours to compile QuantLib in C++ and the Python wrapper..... 14 hours!

At least it looks interesting.


Monday, April 11, 2011

SVM ready for the race.

SVM with polynomial kernel (parameters 1,1) are providing the best results with revenues between 20% and 100% weekly.

The tricks were:
  1. To limit the amount of money, one unit if the previous operation was with profit>0.
  2. To move the amplitude of Bollinger bands (that are, jointly with RSI, limiting alfa signals) increasing in a factor of 1.5 if the previous operation was failure (with a limit of 5*Amplitude) or decreasing in a factor of 1.5 with a minimum of Amplitude/1.5), letting be Amplitude the value of initial amplitude Bollinger (decide for each pair of forex).
  3. To avoid operations if training was against trend (this improves slightly returns, but it doesn't turn them into negative even in the worst cases).
Next step: implement in MQ4 the trading system.


Wednesday, March 16, 2011

Python and Quant

I was looking for doing easier to stress my portfolios, I got to http://appropriatesoftware.net /quant/Documentation.html.... but, what a pity, it's not finished, it's not usable.

It's better to create my own library. And, to simulate my loan (in Swiss Francs) join with a call and a put, I would like to write a code like:


nov29 = CalUtils.createStrikeDate(29, 11, 2010)

portfolio = Portfolio()
market = Market()
# EURCHF - how many CHF to buy 1 EUR
op1 = EuropeanLongCall(0.01440, 1.3335, nov29, "EURCHF", 20000)
op2 = EuropeanShortCall(0.00220, 1.3615, nov29, "EURCHF", 200000)
# CHFEUR - how many EUR to buy 1 CHF
debt = Loan(nov29, "CHFEUR", 1300)

portfolio.add(op1)
portfolio.add(op2)
portfolio.add(debt)


# Simulation
print "EURCHF", ",", "Payoff", ",", "Loan"
for a in xrange((1.3800 - 1.3280) / 0.0001):
x = a * 0.0001 + 1.3280;
market.setPrice(nov29, "EURCHF", x)
market.setPrice(nov29, "CHFEUR", 1/x)
portfolio.setMarket(market)
print x, ",", portfolio.getProfit(nov29) - portfolio.totalCost(), ",", debt.getProfit(nov29)






Maybe in future I will extend my library to create the portfolios and even to manage my SVM and other automatic trading. And, for sure, I will develop a "secret weapon" to join fundamental with technical analysis; who knows...

Monday, March 14, 2011

Black Mondays

Why am I so silly? If I know (thanks to my simulations) that Monday my machines have no information enough to trade, why do I forget to stop them?

They've lost all the past weeks profits, and a little more.

Right! I should to include a "non trade in Monday" condition....

Now the question is "why are they making mistakes?". "Why the market is different in Monday than in Tuesday?"
Maybe human traders are getting their weekly positions. I'm not a trader (this smelly work should be done by machines) so I don't know.

Wednesday, March 2, 2011

MQ4, solution to get historical prices

Again again, dealing with the problems that arise when trying to get the history of recent prices in MQ4 to calculate LSSVM and Simplistic values.

I've trying to limit the algorithms to the second half of each minute.... but it fails sometimes.

Now, I present here the solution for getting historical prices. I will use it in my implementation of LSSVM and Simplistic to recreate the machines described previously.



//+------------------------------------------------------------------+
//| collectPrices.mq4 |
//| luisf.canals@.... |
//| |
//+------------------------------------------------------------------+
#property copyright "luisf.canals@..."
#property link ""
#property library


#include "..\include\collectPrices.mqh"



/**
* Gets the matrix of prices for currencies.
*
* Return a Matrix with first line for prices on moment T-K,T-K+1,...T-1.
*
* Each line has: High, Low, Open, Close, Volume and RSI for symbol[0],
* symbol[1],...,symbol[N-1].
*
* 'prices' matrix should have 1 + K x 6*ArraySize(symbols) dimensions,
* the first one for timestamps.
*
* Returns false if prices cannot be get.
*/
bool collectPrices(string symbols[], int K, double &prices[][]) {
int period = PERIOD_M1;
int timebase = ((TimeCurrent()/60) - 1) * 60;
if(timebase<=prices[K-1][0]) return (false);

double line[];
ArrayResize(line, (6*ArraySize(symbols))+1);

for(int i=0; i<ArraySize(line); i++) line[i]=0;

while(true) {
for(int s=0;s<ArraySize(symbols); s++) {
int j=6*s + 1;
Print(symbols[s] + ":" + iTime(symbols[s], period, 1)
+ " - timebase=" + timebase);
if(line[j]==0 && iTime(symbols[s],period,1)>=timebase) {
// Go back in time if iTime>timebase
if(iTime(symbols[s],period,1)<timebase
&& symbols[s]==Symbol()) {
return (false);
}
if(iTime(symbols[s], period, 1)>timebase) {
return (false);
}
int t = 1;
line[j] = iHigh(symbols[s], period,t);
j++;
line[j] = iLow(symbols[s], period,t);
j++;
line[j] = iOpen(symbols[s], period,t);
j++;
line[j] = iClose(symbols[s], period,t);
j++;
line[j] = iVolume(symbols[s], period,t);
j++;
line[j] = iRSI(symbols[s], 0, 14, PRICE_CLOSE, t);
j++;
}
}
bool completed = true;
for(s=0; s<ArraySize(symbols); s++) {
if(line[6*s + 1]==0) {
completed = false;
break;
}
}
if(completed) break;
Sleep(200);
}

line[0] = timebase;
for(i=0; i<K-1; i++) {
for(j=0;j<ArraySize(line); j++) {
prices[i][j] = prices[i+1][j];
}
}
for(j=0; j<ArraySize(line); j++) {
prices[K-1][j] = line[j];
}

return (true);
}



Sunday, February 27, 2011

The contest

I've been testing six different type of applied machines, during 15 days, from January to February 2011.

Participants were:
  • LSSVM with polynomial kernel, with parameters (1,1)
  • Simplistic as was described in a previous post
Each participant was using three different sets of data:
  • sequence of earnings got with the indicator AIS, described in a previous post, minute by minute (I named it earn)
  • sequence of Bollinger Bands values: mean, low and upper band and %b (relative amplitude of the bands) (I named it bolli)
  • sequence of high, low, open, close prices, RSI(14,close) and volume minute by minute (I named it rstuvw... don't ask me why)
Combining them, we get six different possibilities. I'm working, as usually, with six forex (EURCHF, USDCHF, USDJPY, GBPJPY, GBPUSD, EURUSD).

Final results were:

eurchfusdchfusdjpygbpjpygbpusdeurusd
LSSVM earn37-35126251070
LSSVM bolli1147170127591
LSSVM rstuvw2-902241891331
Simpl. earn-10-435178941931
Simpl. bolli-1330198-320133-251
Simpl. rstuvw-651623-117386-476

Numbers indicate 1e-4 proportion of revenue (over 1, thus 1 is 100%)
Total final revenue is 2.117 (211'7%).
Proportions are calculated following this formula for long positions (calculus is quite similar for short positions):

There are let three positions at the same time open for each combination and strategy (long and short). Spread is the number of pips for each FX, depending on the platform you use. In my case, using a Hanseatic account, spread pips are for each mentioned FX:
0.0005, 0.0005, 0.0005, 0.0005, 0.0002, 0.0003

Underlying prices used even for training and testing the machines are:
  1. 360 minutes (six hours) from 3AM to 9AM Madrid Time as training data
  2. 24 hours, from the end of training (9AM) to 9AM the following day
In the other side, limitations to LSSVM and Simplistic machines are the same for every one: RSI and Bollinger should recommend the entrance, and RSI and time is used to signal when to close the position. No positions are opened for more than 40 minutes.

To investigate:
  1. 24 hours as training data
  2. Risk and efficient portfolio of machines
  3. Which machines are again and again good for which FX.
That's all, folks.


Friday, February 25, 2011

Maximum profits indicator

Here it is: the Matlab code for "ais" indicator, as I've commented in a previous post.



function [long short] = ais(HLOC, timeframe, pips)
%
% ais
%
% [long short] = ais(HLOC, timeframe, pips)
%
% Indicator of maximum profit for each moment open a position in just
% this moment
%
% Parameters:
% HLOC, matrix with High,Low,Open,Close prices values in columns.
% timeframe, size of the window to before closing the position
% pips, spread between opening and closing position
%
% Output:
%
%
[p1 p2] = size(HLOC);

long = [];
short = [];
for ii=1:p1-timeframe
% Long positions
profit = -inf;
for jj=ii+1:ii+timeframe
profit2 = (HLOC(jj,1) - HLOC(ii,3)-pips)/HLOC(ii,3);
if profit2 > profit
profit = profit2;
end
end
long = [long
profit];

% Short positions
profit = -inf;
for jj=ii+1:ii+timeframe
profit2 = (HLOC(jj,3) - HLOC(ii,2)-pips)/HLOC(ii,3);
if profit2 > profit
profit = profit2;
end
end
short = [short
profit];
end

Thursday, February 24, 2011

MQ4/Metatrader historical data bug

Only to avoid missing this information:
working with MQ4 and Metatrader platform, I'm having some problems trying to get historical data from several FX at the same time. This bug is affecting my Simplistic and LSSVM machines and moving them to make mistakes.

The bug is the following: when I get open,close,high,low,rsi and volume information for the last ten minutes for six forex, I get information with a minute of delay for one or of the forex.

The problem arises only when the request is executed in the first 30 seconds of the minute.

Requesting the data during the last 30 seconds, there is no problem....

Sunday, February 20, 2011

Stability and unsuitability for simplistic model

The main problem related to simplistic is the "stability" against training variances.

When training set suffers a little change (for instance 10 minutes more or less), the results are unpredictable, incurring into great loses or earnings…. apparently randomness (is chaotic, now I remember!)



I've tried to solve it using a linear LSSVM. With a RBF LSSVM, stupid results are got (everything has the same value per day, depending only on the set of train data… mmmmm….. it would be interesting….). With polynomial kernel, better stability and no loss in precision is got. Ok, let's try with LSSVM and polynomial kernels.

Correlation among profits

We define an indicator:


aispos(t) = max. profit if a long position has been opened at time t-T

aisneg(t) = max. profit if a short position has been opened at time t-T


With this indicator, we try to study correlation and mutual information between different forex.


Considering the forex EURCHF, USDCHF, USDJPY, GBPJPY, GBPUSD, EURUSD numbered from 1 to 6, I'm showing here the graphical results:



What does it mean for me? That it could be frustrating trying to predict when it's a good moment to buy a long position (the same analysis should be done for short positions) for these FX with low correlation and low AMI for every other FX.

Maybe I should explain what AMI is...

Saturday, February 19, 2011

Simplistic explanation

Here I write down what I'm calling "Simplistic": a simplistic machine is intent of generate linear chaotic buy signals. Linear, to be easy and fast to compute, and chaotic to try to recreate market behaviour (oohhh, I would like to say to avoid Platonic market assumptions...).

Ok, let's go to the formulae:

Evaluation of simplistic trying to predict a market short term trend:

If the result is greater than zero, we should buy. If not, we should not. So simple.
In the formula, x is a set of values (prices, indicators...) in the moment t.

Simplistic is trained with positive and negative cases (thus when should buy or when should not). The previous evaluation is made for a training set, maximizing the sum of positive cases restricted to f(x)<0 for negative cases.

I'm very lazy, so I use a stupid Simplex to look for solutions.

What in hell is the x? In my first approach, I use only prices for one Forex (EURCHF) trade.
But, as I'm pretend I'm a trader, and I know that risks must be reduced diversifying investments, and blah blah blah, I'm currently evaluating several values for several FX:
  • High, Low, Open and close prices for each minute
  • Volumes
  • RSI
  • for EURCHF, USDCHF, USDJPY, GBPJPY, GBPUSD and EURUSD
  • and I'm inverting positions to get a chain (I don't know why, but it looks pretty) : EURCHF > 1/USDCHF > USDJPY > 1/GBPJPY > GBPUSD > 1/EURUSD

My dream is: if I'm able to find when the previous chain is not zero-sum (that is, if a move my money from EUR to EUR going through the chain of changes, I should have to get the same money -ignoring commissions, spreads pips an so), I would be ready to predict that, in a very short future the chain will return to zero-sum status.

The way I'm composing the x vector is:
being h,l,o,c,vol,rsi the values previously described, and T the timeframe for the machine.

As Simplistic is too nervous. Should be limited by RSI and Bollinger Bands controls.
And techniques to abandon a failed position.

Wednesday, February 2, 2011

Simplstic better than LSSVM


Simplistic predictors are better than LSSVM ones.

The first one are earning. The second one thinks that the best option is to be quiet, not investing at all...

Simplistic had a programming error (in MQ4 code): it confuses estrategies working only on short positions.

It has been added a new feature: exiting when RSI is signaling no more profits w
ill be get, but "take profit" condition has not been yet reached.

Another thing more: they work better as far as they are trained frequently, with
recent prices

Thursday, January 27, 2011

Why are they earning?

I don't know exactly why... but Simplistic and LSSVM are winning...

I'm using alfas and sample-points from machines machines on January 24th, adjusting thresholds according to the "please please, don't lose my money at all" principle (thus, every open position should result in earnings) applied the day before.

And they are earning money, about 6% in two days (with a deposit of 1.000 euros).

I need to test it more....

Thursday, January 20, 2011

SimpleSVM with RSI limits for EURCHF: 9 to 4

I don't know, but working with SimpleSVM (limited by RSI, as I explained before) in the following way:
- 9:00 AM, get 720 samples of 1 minute for EURCHF, EURUSD, USDCHF
- Training SimpleSVM with this values and getting started
- It goes fine, for five hours (16:00)... and starts to fail until the night
.
I don't know why, so I'm testing again and again. Maybe the machine is learning
some kind of human pattern appliable at workable hours. I don't know.
~

Friday, January 14, 2011

SimpleSVM with RSI limits for EURCHF

Trading against the wind!

The idea is: in an ascending trend, open long position when RSI cross 30 and short positions according to SimpleSVM limited to RSI conditions (like this post, but acting over EURCHF).

Starting operations on January 11th, this system has opened 3 short positions, earining 0.2% (for each one) and one long position earning 0.2% in one day.

Let's see how it's going on...

Tuesday, January 11, 2011

RSI signaling for EURCHF

As simple experiment, I'm trying to earn money buying and selling CFD for EURCHF.

Sample time was 1 minute.

Using this simple pattern: open short position if RSI(14) is over 73, open long position if it's under 26, profit was about 0.2% during January 11th 2010 in the first 30 minutes.... But lot of short positions were being opened thanks to RSI(14) were over 73, losing more than 5% in the following 12 hours.

As I'm trying to perform High Frequency Trading, these results are invalid. What a pity!

Monday, January 10, 2011

SimpleSVM with RSI limit for SP500 and EUREX

After analyzing what appened here, I've detected RSI indicators where "out of recommended ranges", thus, near to 70 when opening failure LONG positions, near to 30 when opening failure SHORT positions...

So, I've tried to include a "RSI limit" acting in this way:
  • Don't open long positions if not RSI (t-K)>70 and RSI(t-K/2)<70 and RSI(t)<70, at the same time, -for ascending trend-; don't open long positions if not RSI(t)>30 when prices are descending.
  • Don't open long positions if RSI(t)>=70 when trend is ascending and don't open short positions when trend is descending and RSI(t-K)<30,RSI/t-K/2)>30, RSI(t)>30 are not verified.
Thus, the idea is: don't go if RSI is not confirming the signal.

Machines with these modifications are being tested against SP500 and Eurostoxx from this moment..... we will see if they're working or not.....


)>

Friday, January 7, 2011

About me

Ugh, I've forgotten it!

My name is Luis F., I'm a Spaniard from Spain, MSc in Maths, programmer since 1999 (with all those unuseful certifications from Sun and Microsoft), former information security auditor (CISA, CISM certified), former enterpreneur (please, don't try to create an enterprise in Spain, don't try) and Masters in Quant Finances.

I love computer programming. I'm earning money programming computers and making people enjoy the art of dealing with computers.

I love Maths. I would like to make money with Maths.

I'm turning into Quant Analyst (to manage my money) because computer programming has no secrets, and has no new challenges for me.

That's all. I will no tell you anything about my family and hobbits, because they are too valuable to share with you (remember, I don't know who you are, your intentions...).

But, if you want more information on the algortihms, or you would like to investigate with me (and get rich, of course), email me. Don't worry.

Ah, and I'm happy.

Bye!


ModifiedSVM for SP500 and EUROSTOXX

Trying to apply modified SVM (a quite simplified method a will describe in future) only to index data (open/close/high/low/volume), the results were:

  1. For SP500 long prediction is quite good. SP500 is going up with a huge loses ModifiedSVM is avoiding. Short prediction is "almost" good: no points were predicted to get short.
  2. For EUROSTOXX, machine crashes: market was long, but, suddenly, (january 6th, at 13:30) it started to get very short. My ModifiedSVM decided to go in long at 13:30.... oohh!!!

Conditions:
  • Samples each 15 minutes
  • Training with 96 samples, the most recent ones
  • Stop Loss decide after testing training, looking for the greatest benefit
  • Two machines for each index: one for short and one for long positions.

Tools:
  • MQ4 code (working on MetaTrader) to get index values
  • MQ4 code to operate in real time on MetaTrader platform
  • Matlab to train and test the machine


Presentation

Welcome to my own list of practices I'm trying to get rich trading with forex, indexes, stocks in a CFD, options or spot way.

Remember: I don't need you to visit this blog; I'm going to get rich, so go away! (And don't discover my secrets...)