Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Help executing function
Date: Tue, 3 Nov 2009 00:19:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 41
Message-ID: <hcnstm$fp3$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1257207542 16163 172.30.248.35 (3 Nov 2009 00:19:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 3 Nov 2009 00:19:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2073737
Xref: news.mathworks.com comp.soft-sys.matlab:581939


I need to write a function that buys a stock at the max and sells at the minimum points.  Assume I use all my money to buy a stock and that I could buy fractions of a stock.  This is as far as I could get.  I was wondering how I would actually go about buying and selling the stock now.

%Lets find the critical points, where we may want to purchase the stock
rowloc=[];
locationsize = 1;
for i = 1:(i)
    if y(i) < y(i+1)
        rowloc(locationsize)=1;
    else
        rowloc(locationsize)=0;
    end
locationsize = locationsize+1;
end
rowloc(locationsize)=0;
%Now that we have found where we may want to buy the stock, lets see where
%we may want to sell the stock
rowloc2=[];
locationsize=1;
for i = 1:(i)
    if y(i) > y(i+1)
        rowloc2(locationsize)=1;
    else
        rowloc2(locationsize)= 0;
    end
locationsize = locationsize+1;
end
rowloc2(locationsize)=0;
%We have found the critical points of the stock, but we need to know what
%the value of the stock is at the critical point.
for x = 1:i+1
newvec(x) = y(x);
end
buystock = newvec.*rowloc;
sellstock = newvec.*rowloc2;
%This creates a matrix of the values.
totals = [buystock ; sellstock]
%Now lets use this information to buy and sell the stock