using if statements to find daily stock holding and cash

1 view (last 30 days)
Suppose you owned 100 shares with no cash over a 17 day period and you bought 10 shares every day the price was below $193.50 and sold 10 shares every day the price was above $195.00. For each transaction (buying or selling shares) you will have to pay a flat commission fee of $10.00. I'm trying to find ...
(a) The daily amount your total stock holding in dollars.
(b) The daily amount your total cash in dollars.
(c) The total number of shares you own at end of 17 day period
(d) The net change in the worth (defined as sum of stock holding and cash) at end of 17 day period
The problem I'm having is that the if statements are not working and it seems like nothing happens.
Price=[199.54 195.89 195 192 193.29 197.09 197.78 190.34 189.55 193.30...
196 194.60 193.63 193 193.77 195.23 193.97];
Total_Shares=100;
Cash=0;
Fee=10;
Shares=10;
if Price(1:end)>195
a=Total_Shares-Shares
Value=a*Price(1:end)
elseif Price(1:end)<193.50
a=Total_Shares+Shares
Value=a*Price(1:end)
end
if Price(1:end)>195
c=Shares*Price(1:end)
Total_cash=c-Fee
elseif Price(1:end)<193.50
c=Shares*Price(1:end)
Total_cash=c-Fee
end

Answers (1)

Sahithi Kanumarlapudi
Sahithi Kanumarlapudi on 16 Dec 2020
Hi,
From the code snippet you have provided, it looks like you are trying to compare whether all the elements of the Price array are either greater than 195 or less than 193.50.
But both these conditions
if Price(1:end)>195
elseif Price(1:end)<193.50
are never true with the input 'Price' which you are trying to use because all the elements in Price are between 192 and 199. This is reason why you are not seeing any output.
If your intention is to compare each and every element of the Price array with the cost of the share, using for loop might be helpful.
Hope this helps!

Community Treasure Hunt

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

Start Hunting!