MrMarc

3163 Reputation

18 Badges

17 years, 133 days

MaplePrimes Activity


These are answers submitted by MrMarc

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?!

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:

 

ok, I think I found something useful

V := Vector(10,i->3*i+11);

V1,V2 := V[1..5],V[6..-1];

which splits the vector into two separete verctors

no, I just removed the time column because everything becomes so complicated

You will see that all 250 stocks dont start at the same month. but you can include it if you find a

way around it.  I thing the precis time of the series is not that important when we are going to calculate

the covariance and correlation matricies (as long as they are intact) all though we have to make sure we know which way is the most

resent observation for that stock otherwise we can buy a stock that is in a down trend which is not good

ok, I will upload the excel file. wait a second.....

your solution was nice Doug but I think my problem remains because

I am not sure how I can define 250 stocks individually.

I have attached the maple file so you can see how it looks .

The first column holds the close price and the second column the stock number

again I want it to be

 stock 1       stock (2)       stock i

   p (t)            p(t)                  p(t)

   p(t+1)       p(t+1)              p(t+1)

any help appreciated

View 8342_Import Data.mw on MapleNet or Download 8342_Import Data.mw
View file details

what a sweet trick!   My hat is of for you.  

That was really cool that the table updated itself  when you add more stocks without having fiddle around with the parameters.                        Nice!

ok, thanx Doug.  I will spend some time try to understand what you have done.

As always I appreciate your input !  :-)

an alternative way would be if there would be a way that i could load 250 separete txt (acii) files into maple

and organize the output from each txt file as column arrays

these individual txt files have the general structure

stock i

close t

close t+1

close t+2

I know that I can load the data from excel into Maple in three column arrays (time, stock, close) by using

A:=ExcelTools:-Import("C:\\...................data.xls", "Sheet1", "A1:C20000");

but as I explained previously the data organized in this way feels very strange and it is very

hard to calculate correlation and covariance matricies under such conditions.

sorry the time column should be

time

2006

2006

2005

2005

 etc

 

just for the record Joe help me with another one which is quite straight forward and working.

where everything after "to 10 do" should be without execute symbol >  (Achived by pressing shift-enter)

 

restart;
with(Statistics):

# parameters
s[1]:=100:
d:=4:
n:=100:

# stock price
randomize():
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]);

# profit loss without stop loss
prof_:=Last_close-A[1];
#############################
print(prof_);   # or print(prof_s) for that matter
end do:

 

I mean we now that markets are volatile which means that we need to protect our profits.

So the interesting question that aries is can we achive higher profits from using a trailing stop loss

than for example taking a naked position ( no stop loss)  in that stock.

I think that we can claim that this is the case after have run acers simulation that we can actually achive higher profits by using a trailing stop loss.

Note that we can investigating this hypotheis for a pure unit root, unit root with drift or some trend stationary process with some iceberg risk component.

I am not sure though how useful it will be for a pure unit root  though since the expected value is zero.

just for the record

if anyone wants to plot the original post ( one stock and one stop loss ) you can use

 

with(plots):

t:=[seq(i,i=1..n)]:

X:=plot(t,A, style=line, color=black):

Y:=plot(t,C_C, style=line, color=red,thickness=2):

q1:=`if`(Exit_price=0,Last_close,Exit_price): q2:=`if`(Exit_period=0,n,Exit_period):

Z:=PointPlot([q1], xcoords=[q2], thickness =20,`if`(Exit_price=0,color=white,color=black), symbol=solidbox, symbolsize = 15,labels=[Time,Stock_price]):

display({X,Y,Z});     

First 19 20 21 22 23 24 Page 21 of 24