Adaptative Dynamic loop mechanism

1 view (last 30 days)
Davin
Davin on 13 Dec 2014
Commented: Davin on 15 Dec 2014
Hello,
I am struggling with the code with some dynamic/adaptative buy and sell signals. I would be very grateful if you could help me if you have some ideas to do that.
I have 2 arrays, a buy array with 2 columns, date and price, and other sell array, with same 2 columns. These 2 arrays are of different sizes.
So what I want to do, is to take the first cell in the first array say, its 01/03/2014, I will look for the first date after this date in the sell array first column say 01/05/2014, in order to get the correspond sell signal. Then I need to get the next buy date, which will be the first date superior than 01/05/2014 in the buy array, then look for the first sell date after 01/05/2014 in the sell array 1st column, then again go back to the buy array to get buy signal after 01/05/2014 in buy arrays 1st column etc etc.
I have tried with a loop on buy then on sell, but I am struggling to make it stop at the first date superior to the buy date. I am bit confused on how to implement this.
Could anyone help me please?
D
  2 Comments
Guillaume
Guillaume on 13 Dec 2014
Edited: Guillaume on 13 Dec 2014
One important bit missing from your description is whether or not the two arrays are sorted by date. Are they?
Also, how is the date stored? A string? a datenum? a datevec?
Davin
Davin on 13 Dec 2014
Hello Guillaume,
Yes they are. they are also in datenum form :
An example :
Buy Array
730441 1365.4
730443 1341.03
730460 1328.72
730476 1328.05
730477 1322.18
730481 1305.33
730484 1313.71
730485 1300.29
730488 1297.8
730567 1281.43
730568 1301.93
730569 1298.16
730572 1327.68
730730 1330.77
730734 1344.16
731708 1332.84
731714 1323.59
731717 1336.61
Sell Array
730370 1349.82
730371 1328.44
730411 1322.85
730412 1319
730414 1289.48
730422 1306.17
730425 1336.12
730517 1358.83
730518 1356.85
730562 1360.04
730594 1362.8
730595 1350.91
730596 1342.83
730602 1335.18
730603 1354.63
730604 1332
730609 1347.31
730610 1332.05
Hope it answers yr question.
Thanks
D

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 13 Dec 2014
Does this do what you want?
buyarray = [730441 1365.4
730443 1341.03
730460 1328.72
730476 1328.05
730477 1322.18
730481 1305.33
730484 1313.71
730485 1300.29
730488 1297.8
730567 1281.43
730568 1301.93
730569 1298.16
730572 1327.68
730730 1330.77
730734 1344.16
731708 1332.84
731714 1323.59
731717 1336.61];
sellarray = [730370 1349.82
730371 1328.44
730411 1322.85
730412 1319
730414 1289.48
730422 1306.17
730425 1336.12
730517 1358.83
730518 1356.85
730562 1360.04
730594 1362.8
730595 1350.91
730596 1342.83
730602 1335.18
730603 1354.63
730604 1332
730609 1347.31
730610 1332.05];
currentdate = buyarray(1, 1); %starting date
lookin = 1; %1 for search in sellarray, 0 for search in buyarray
while true %loop until break
if lookin %search in sellarray
sellrow = find(sellarray(:, 1) > currentdate, 1); %find first value greater than currentdate
if isempty(sellrow) %reached end of array
break;
end
sellsignal = sellarray(sellrow, 2);
currentdate = sellarray(sellrow, 1);
fprintf('sell: %f (date: %d)\n', sellsignal, currentdate); %do whatever you want
else %search in buyarray
buyrow = find(buyarray(:, 1) > currentdate, 1);
if isempty(buyrow) %reached end of array
break;
end
buysignal = buyarray(buyrow, 2);
currentdate = buyarray(buyrow, 1);
fprintf('buy: %f (date: %d)\n', buysignal, currentdate); %do whatever you want
end
lookin = ~lookin; %switch array to look in.
end
  1 Comment
Davin
Davin on 15 Dec 2014
Thanks Guillaume, it worked fine. I will try to replicate this way of doing in the future...

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!