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....