|
|
Source code |
1 |
=INDEX(A1:A6;VERGLEICH(MAX(C1:C6);C1:C6;0)) |
Users who thanked for this post:
DickT (14.12.2009), Perfect Trader (14.12.2009)
Nachtrag: Da war plasmapelz schneller, auch wenn das auf den ersten Blick für die Aufgabe etwas umfangreich aussieht, aber Hauptsache es tut.
Guten Abend, ich stehe vor dem Problem, wie man am Besten mit Hilfe von Excel Tickdaten in Minutendaten (OHLC) umwandeln könnte.
.|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
function [data, tdrange] = tick2ohlc(Time, Bid, Ask, t0, t1, timeframe)
t0 = datenum(t0);
t1 = datenum(t1);
%idx = find(self.Time >= t0 & self.Time <= t1);
idx = findrange(Time, t0, t1, 3000);
time = Time(idx);
bid = Bid(idx);
ask = Ask(idx);
tdrange = [idx(1) idx(end)];
rate = (bid+ask)/2;
% Start at next full minute/hour etc. Everything before goes in one
% OHLC, everything after in one.
dv0 = datevec(time(1));
if (timeframe < 60) % minute range
dv0(6) = 0;
if (mod(dv0(5), timeframe) ~= 0)
dv0(5) = dv0(5) - mod(dv0(5), timeframe) + timeframe;
end
elseif (timeframe < 1440) % day range
dv0(6) = 0;
dv0(5) = 0;
if (mod(dv0(5), timeframe/60) ~= 0)
dv0(4) = dv0(4) - mod(dv0(4), timeframe/60) + timeframe/60;
end
end
% Calculate the size of the data array. It is the span between t0
% and t1 divided by the timeframe
span = ceil((datenum(t1)-datenum(t0))/datenum([0 0 0 0 timeframe 0]))-1;
data = zeros(span, 5);
j = 1;
dn0 = datenum(dv0);
dn1 = datenum(t1);
while (dn0 < dn1)
dve = datevec(dn0);
dve(5) = dve(5) + timeframe;
dne = datenum(dve);
if (dne < dn1)
%idx22 = find(time >= dn0 & time <= dne);
idx = findrange(time, dn0, dne, 3000);
else
%idx22 = find(time >= dn0 & time <= dn1);
idx = findrange(time, dn0, dn1, 3000);
end
if (isempty(idx))
dn0 = dne;
continue;
end
rrate = rate(idx);
data(j,1) = time(idx(1));
data(j,2) = rrate(1);
data(j,3) = max(rrate);
data(j,4) = min(rrate);
data(j,5) = rrate(end);
j = j+1;
dn0 = dne;
end
data = data(1:j-1,:);
% Prepend values for before first time frame
idx = find(time >= datenum(t0) & time <= datenum(dv0));
if (~isempty(idx))
tdata = zeros(1,5);
rrate = rate(idx);
tdata(1,1) = time(idx(1));
tdata(1,2) = rrate(1);
tdata(1,3) = max(rrate);
tdata(1,4) = min(rrate);
tdata(1,5) = rrate(end);
data = [tdata; data];
end
end
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
#include <mex.h>
#include <matrix.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
/*
* Combined binary and serial search. The procedure looks up a range around
* x +/- theta using classical binary search, then it "refines" by looking
* for the closest value to x by serial search from beginning or end.
*/
int bsLookup(double *data, int len, double xval, double theta)
{
double lb, ub, currVal;
int start, end, middle;
lb = xval - theta;
ub = xval + theta;
start = 0;
end = len;
while (start <= end) {
middle = (start+end)/2;
currVal = *(data+middle);
if (lb <= currVal && currVal <= ub) {
int i = middle;
if (currVal < xval) {
//mexPrintf("smaller\n");
for (i = middle; *(data+i) < xval && i <= len; i++)
;
} else if (currVal > xval) {
//mexPrintf("bigger\n");
for (i = middle; *(data+i) > xval && i >= 0; i--)
;
i+=2; // Adjust for coordinate transformation (MATLAB starts at 1)
}
return i;
} else if (xval < currVal) {
end = middle-1;
} else if (xval > currVal) {
start = middle +1;
}
}
return -1;
}
/*
* Find range in an ascending array, using binary search. This should be
* faster than find().
* call: idx = findrange(array, x0, x1, theta);
*
* array: The array of ascending double numbers, e.g. datenums
* x0, x1: The Range, x0 < x1
* theta: A threshold to add/subtract to x0/x1 to start serial search.
*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *resIdx;
int idx0, idx1, i;
mwSize dims[2];
if (nrhs != 4) {
mexErrMsgTxt("Usage: idx = findrange(array, x0, x1, theta), where x0 < x1");
return;
}
/*
* Find x0 and x1. This returns the indices
*/
idx0 = bsLookup(mxGetPr(prhs[0]), mxGetNumberOfElements(prhs[0]),
mxGetScalar(prhs[1]), mxGetScalar(prhs[3]));
idx1 = bsLookup(mxGetPr(prhs[0]), mxGetNumberOfElements(prhs[0]),
mxGetScalar(prhs[2]), mxGetScalar(prhs[3])) - 1;
dims[0] = idx1-idx0+1;
dims[1] = 1;
plhs[0] = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
resIdx = mxGetPr(plhs[0]);
for (i = 0; i <= idx1-idx0; i++) {
*(resIdx+i) = idx0+i;
}
}
|
Forum Software: Burning Board® 3.1.6, developed by WoltLab® GmbH