Saving multiple output in one variable from loop

2 vues (au cours des 30 derniers jours)
Wildman
Wildman le 21 Juin 2015
Commenté : Wildman le 20 Août 2015
Dear all,
could somebody please help me with my following problem(s). I'm currently coding a moving average crossover trading rule. This rule needs to generate a buy signal (value "1") when the fast moving average is above the slow moving average and sell signals (value "-1") vice versa. Where a slow moving average (variable "n" days) is calculated over a greater number of days than the fast moving average (variable "m" days).
Question: How can I store all those different fast-slow combinations of "n" and "m" in one variable (in this case the "tr_ma_cross" variable)? So I would like that each fast-slow combination is written in a new column in the same variable.
The code at this moment:
%Moving average crossovers
for n=[2 5 10 15 20 25 30 40 50 75 100 125 150 200 250];
for m=[2 5 10 15 20 25 30 40 50 75 100 125 150 200 250];
if n==m; continue, end
MA_slow=tsmovavg(price,'s',n,1);
MA_fast=tsmovavg(price,'s',m,1);
for k=1:31225;
if MA_fast(k,1)>MA_slow(k,1)
tr_ma_cross(k,:)=1; %Buy
elseif MA_fast(k,1)<MA_slow(k,1)
tr_ma_cross(k,:)=-1; %Sell
else
tr_ma_cross(k,:)=0; %Neutral
end
end
end
end
Additional question: I also would like the code to only put a buy ("1") or sell ("-1") signal at the positive or negative move through the "intersection" of the fast and slow moving average (so when the fast moving average is the first time above the slow moving average put a "1" and then just zeros till the opposite happens, the slow moving average is above the fast moving average than I want it to put a "-1").
Thanks and have a nice day,
Wildman
  3 commentaires
Wildman
Wildman le 29 Juin 2015
Sorry for my late response. Unfortunately I didn't saw your comment/got no e-mail notification.
I'm doing this on historical data.
dpb
dpb le 30 Juin 2015
OK, in that case you have all the data already so can vectorize at least pieces; maybe all. If you were doing this in real time where didn't get the last observation until did the computation, have to wait...

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 30 Juin 2015
Modifié(e) : dpb le 1 Juil 2015
OK, as per the comment, firstly, you're computing the n moving average m*n times...move it out of the loop over which it's invariant--
%Moving average crossovers
for n=[2 5 10 15 20 25 30 40 50 75 100 125 150 200 250];
MA_slow=tsmovavg(price,'s',n,1);
for m=[2 5 10 15 20 25 30 40 50 75 100 125 150 200 250];
if n==m; continue, end
MA_fast=tsmovavg(price,'s',m,1);
Then, there's no point in using a loop to do the computation over the two vectors; use a logical comparison instead--
tr_ma_cross=sign(MA_fast-MA_slow);
end
end
It also appears this should be symmetric, shouldn't it? If so, then F(n,m)==F(m,n) so could run the inner loop only over m>n instead of all m for every n.
I've got a meeting in a few minutes so don't have time to try to finish any further cleanup at the moment but see if that doesn't help and get back on the further clarification and I'll try to get back again later today/evening also.
ADDENDUM
Well, after fighting an unknown bug bite that "swoll up" my forearm about 2X its normal size for a couple of days, it's now improved to the point I can type again so--continuing on
To store the results of vector tr_ma_cross, simply use a cell array and a separate counter variable. You need a cell array since owing to the moving average (I presume) the length returned is shorter than the input vector by the number of elements averaged. Otherwise you would need to offset the starting point and fill with NaN say, to make a square array.
Prior to the loop over n
k=0; % initialize counter
then replace the computation with
k=k+1; % increment the counter
tr_ma_cross(k)={sign(MA_fast-MA_slow)}; % store as cell
For the last question regarding only saving the transition points, diff is your friend
cross=[0 diff(sign(MA_fast-MA_slow))]; % compute diff of signal
ix=abs(cross==2);
cross(ix)=(ix)/2; % scale transitions to +/-1
tr_ma_cross(k)=cross; % store as cell
NB: Can't just divide by 2 as that would scale any 0-1 transitions to 0.5.
  9 commentaires
Wildman
Wildman le 5 Juil 2015
No problem! Indeed writing the RHS as {RHS} gives the same results (I'm using that now instead of the "{k}").
Thanks for all your help! I can now proceed with my project by adding more conditions to this moving average trading rule and even coding some other trading rules. Due to my inexperience with Matlab that will be a harsh task, but because of you the basis is their!
Have a great day!
Wildman
Wildman le 20 Août 2015
Follow up on this topic (for the interested readers):
If somebody has some suggestions to the "adding filter" problems described in the above forum post it would be much appreciated!

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by