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);
}