# trade sign(buy or sell) for a stock based on simple trade data

4 views (last 30 days)
Zlatan Sacic on 16 May 2022
Edited: Zlatan Sacic on 7 Jun 2022
Hello,
I need help to write a simple "loop" or "if" function or a combination of both to identify what trade classification a stock transaction is: "buy", "sell" and possibly "not classifiable".
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (1)

Sailesh Kalyanapu on 19 May 2022
It is my understanding that you are trying to identify what trade classification a stock transaction is based on the two rules mentioned above in the question.
% Assuming you have the data loaded as a Table in a variable named data_sub
% The logic would look something like this
Total_buy_trades = 0;
Total_sell_trades = 0;
Total_not_classifiable_trades = 0;
for i = 1:height(data_sub)
% Extracting information about oen instance from the table
Ask = data_sub.prevailing_ask(i);
Bid = data_sub.prevailing_bid(i);
price = data_sub.price(i);
mid_quote = (Ask+Bid)/2;
% Quote Rule
if price > mid_quote
Total_buy_trades = Total_buy_trades + 1;
elseif price < mid_quote
Total_sell_trades = Total_sell_trades + 1;
else
% Tick Rule
% Finding Last_trade_price
Last_traded_price = price; %Initially assigning to the current value
for j = i:-1:1
if data_sub.price(j) ~= price
Last_traded_price = data_sub.price(j);
break;
end
end
if price > Last_traded_price
Total_buy_trades = Total_buy_trades + 1;
elseif price < Last_traded_price
Total_sell_trades = Total_sell_trades + 1;
end
end
end
##### 1 CommentShowHide None
Sailesh Kalyanapu on 20 May 2022
Hey Zlatan! Can you check if the Tick rule code you mentioned in the above comment is correct? Because it is increasing the Sell count in both the cases, i.e. when price(x)>price(y) and price(x)<price(y)

Sign in to comment.

### Categories

Find more on Financial Toolbox in Help Center and File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by