You cannot readily do so. Well, you could if you reassigned print, but I only recommend that when absolutely necessary. What you really want to do [from your previous posts] is to save data generated in a loop so that you can use it outside the loop. One way to do that is to insert the data into a table. For example
T := table():
for i to 10 do
data := <generate data>:
T[i] := data;
end do:
A := convert(T, 'list');
restart;
with(Statistics):
# parameters
s[1]:=100;
d:=4;
n:=100;
# stock price
randomize();
# Assign table used to temporarily store results
T := table();
for cnt to 10 do
rand_ := Sample(RandomVariable(Normal(0,1)),n);
for i from 2 to n do s[i]:=s[i-1]+rand_[i] end do;
A:=[seq(s[i],i=1..n)];
# Trailing stop-loss
B[1]:=(A[1]-d);
for i from 2 to n do
B[i]:=`if`(A[i]>A[i-1] and A[i]>(A[i]-d) and (A[i]-d)>B[i-1],(A[i]-d),B[i-1])
od;
C:=[seq(B[i],i=1..n)];
r[1]:=0: for i from 2 to n do r[i]:=`if`(A[i]>C[i],0,1) od;
w:=[seq(r[i],i=1..n)];
wp[1]:=0: for i from 2 to n do wp[i]:=(w[i]+wp[i-1]) od;
w_w_:=[seq(wp[i],i=1..n)];
ww[1]:=0: for i from 2 to n do ww[i]:=(w_w_[i]+w_w_[i-1]) od;
w_w:=[seq(ww[i],i=1..n)];
# Final Corrections
C_[1]:=(A[1]-d): for i from 2 to n do C_[i]:=`if`(w_w[i]=0,C[i],C_[i-1]) od;
C_C:=[seq(C_[i],i=1..n)];
########### Results #########
# Exit period due to stop loss
u[1]:=0: for i from 2 to n do u[i]:=`if`(w_w[i]=1,i,0) od;
Exit_period:=convert([seq(u[i],i=1..n)],'`+`' );
# Exit price stop loss
Exit_price:=`if`(Exit_period=0,0,C_C[n]);
# Last close stock price
Last_close:=`if`(A[n]<0,0,A[n]);
# profit loss with stop loss
prof_s:=`if`(Exit_price=0,Last_close-A[1],Exit_price-A[1]);
T[cnt] := prof_s; # store result in table
# profit loss without stop loss
prof_:=Last_close-A[1];
#############################
end do:
# convert table to a list.
A := convert(T,'list');
save to table
You cannot readily do so. Well, you could if you reassigned print, but I only recommend that when absolutely necessary. What you really want to do [from your previous posts] is to save data generated in a loop so that you can use it outside the loop. One way to do that is to insert the data into a table. For example
T := table():
for i to 10 do
data := <generate data>:
T[i] := data;
end do:
A := convert(T, 'list');
ok, thanx Joe . How would
ok, thanx Joe . How would that look for our code:
restart;
with(Statistics):
randomize():
# parameters
s[1]:=100: # Starting price stock
d:=4: # Trailing distance
n:=100: # Number of periods
nq:=100: # Number of simulations
# stock price
to nq do # The starting point for the do-loop
rand_:=Sample(RandomVariable(Normal(0,1)),n):
for i from 2 to n do s[i]:=s[i-1]+rand_[i] end do:
A:=[seq(s[i],i=1..n)]:
# Trailing stop-loss
B[1]:=(A[1]-d):
for i from 2 to n do
B[i]:=`if`(A[i]>A[i-1] and A[i]>(A[i]-d) and (A[i]-d)>B[i-1],(A[i]-d),B[i-1])
od:
C:=[seq(B[i],i=1..n)]:
r[1]:=0: for i from 2 to n do r[i]:=`if`(A[i]>C[i],0,1) od:
w:=[seq(r[i],i=1..n)]:
wp[1]:=0: for i from 2 to n do wp[i]:=(w[i]+wp[i-1]) od:
w_w_:=[seq(wp[i],i=1..n)]:
ww[1]:=0: for i from 2 to n do ww[i]:=(w_w_[i]+w_w_[i-1]) od:
w_w:=[seq(ww[i],i=1..n)]:
# Final Corrections
C_[1]:=(A[1]-d): for i from 2 to n do C_[i]:=`if`(w_w[i]=0,C[i],C_[i-1]) od:
C_C:=[seq(C_[i],i=1..n)]:
########### Results #########
# Exit period due to stop loss
u[1]:=0: for i from 2 to n do u[i]:=`if`(w_w[i]=1,i,0) od:
Exit_period:=convert([seq(u[i],i=1..n)],'`+`' );
# Exit price stop loss
Exit_price:=`if`(Exit_period=0,0,C_C[n]);
# Last close stock price
Last_close:=`if`(A[n]<0,0,A[n]);
# profit loss with stop loss
prof_s:=`if`(Exit_price=0,Last_close-A[1],Exit_price-A[1]);
# profit loss without stop loss
prof_:=Last_close-A[1];
#############################
lprint(prof_s);
end do:
sample
Add a loop counter to the loop and replace the lprint with an assignment into the table:
can you please clarify
can you please clarify what you mean by posting the complete code, I am not sure I understand, if it is not to much trouble?!
done
You saved the day !! or
You saved the day !!
or should I say the week or even the month without exaggerating !
very grateful for your input and help ! :-)