DATA Step, Macro, Functions and more

Weighted Average of a stock price

Reply
Occasional Contributor
Posts: 5

Weighted Average of a stock price

Dear All,

I am having trouble coding the average pricing of stock.

Data

FLAG QTY PRICE AVG_PRICE CALCULATION
Buy 50 100
Buy 50 101
Buy 50 102
Sell 100 101.5 101 (50*100+50*101+50*102)/150
Buy 50 99
Sell 50 99.5 100.5 (50*100+50*101+50*102+50*99)/(150+50)

I need to generate weighted average price against flag 'S' as shown above with calculation how to calculate avg_price.
In case of first sell,I need weighted average price of all buys. total quantity purchased before first sell is 150 and quantity sold is 100.
Balance quanity is 50. To generate price against second 'Sell' we nee to take average of first 3 buys and 4th buy.

Kindly help me out.

Regards
Sandeep Gupta

Frequent Contributor
Posts: 127

Re: Weighted Average of a stock price

Hello Sandeep,

 

The code here below should do the job:

 

data results (drop= SUM_:);
	set input;

	retain SUM_COST SUM_QTY;

	if _N_ = 1 then do;
		SUM_COST = 0;
		SUM_QTY = 0;
	end;

	if strip(upcase(Flag)) = 'BUY' then do;
		SUM_COST = SUM(SUM_COST, QTY * PRICE);
		SUM_QTY = SUM(SUM_QTY, QTY);
		AVG_PRICE = .;
	end; else

	if strip(upcase(Flag)) = 'SEL' then do;
		AVG_PRICE = SUM_COST / SUM_QTY;
	end;

run;

Considering that the content of your "input" dataset has the following colomns: FLAG, QTY, PRICE.

 

Please let me know should there be something missing in my answer.

 

Regards,

Florent

Occasional Contributor
Posts: 5

Re: Weighted Average of a stock price

Hi Florent,

 

Thanks for a quick reply.

I am getting the desired output for a single account. But if there are multiple accounts then the code is not giving desired output.

I am attaching the sample data for multiple accounts. Kindly help me out with this problem.

Sample data is as below:

ACCOUNT_NOTRADE_DATEFLAGQTYPRICE
13-Mar-14FRESH506233.75
13-Mar-14COVER506242.1
14-Mar-14FRESH506250.3
14-Mar-14FRESH506249
14-Mar-14FRESH506251.2
14-Mar-14COVER506267.8
14-Mar-14FRESH1006269.95
14-Mar-14FRESH506291.65
14-Mar-14FRESH506303.35
14-Mar-14FRESH506333
15-Mar-14COVER3506341
21-Apr-14FRESH2006751
21-Apr-14FRESH1006761
24-Apr-14COVER5006739
24-Apr-14FRESH2006769
29-Apr-14FRESH3006795
29-Apr-14FRESH2006828
215-Apr-14COVER5006787
328-Feb-15FRESH258858
328-Feb-15COVER258890
418-Feb-15FRESH508951.375
426-Feb-15COVER258750
428-Feb-15COVER258938
428-Feb-15FRESH258950
528-Feb-15FRESH258961.7
528-Feb-15COVER258949.8
620-Feb-15FRESH508857.2
620-Feb-15COVER258888
620-Feb-15COVER258848.3
623-Feb-15FRESH508859.225
623-Feb-15COVER258863
623-Feb-15COVER758740
624-Feb-15COVER258780.8
624-Feb-15FRESH258743
626-Feb-15COVER258684.35
628-Feb-15FRESH258924.35
628-Feb-15COVER258990
726-Feb-15FRESH508766.775

Regards

Sandeep

Frequent Contributor
Posts: 127

Re: Weighted Average of a stock price

[ Edited ]

Hi Sandeep,

Just to make sure, are the rows with the value "FRESH" in the variable 'FLAG' to be considered as the "Buy" you mentioned in your first message ? I would expect to have a Buy as first operation for each account (you cannot sell something you haven't bought earlier in my point of view).

Regards,
Florent

Occasional Contributor
Posts: 5

Re: Weighted Average of a stock price

Hi Florent

Fresh is buy and cover is sell.

Regards
Sandeep
Frequent Contributor
Posts: 127

Re: Weighted Average of a stock price

[ Edited ]

Hi Sandeep,

 

Would the following version of the code do what you want ?

 

data results (drop= ACCOUNT SUM_:);
	set input;

	retain ACCOUNT SUM_COST SUM_QTY;

	if ACCOUNT ne ACCOUNT_NO then do;
		ACCOUNT = ACCOUNT_NO;
		SUM_COST = 0;
		SUM_QTY = 0;
	end;

	if strip(upcase(Flag)) = 'FRESH' then do;
		SUM_COST = SUM(SUM_COST, QTY * PRICE);
		SUM_QTY = SUM(SUM_QTY, QTY);
		AVG_PRICE = .;
	end; else

	if strip(upcase(Flag)) = 'COVER' then do;
		AVG_PRICE = SUM_COST / SUM_QTY;
	end;

run;

 

It's important that all the data related to the same Account are consecutive (i.e. first all lines related to the account 1 then the ones of the account 2, ...etc) in your input otherwise this will not work as expected.

 

Regards,

Florent

Occasional Contributor
Posts: 5

Re: Weighted Average of a stock price

Dear Florent,

 

The code you shared is not giving the desired output. I am attaching excel containing desired output with formula to calculate the output. Let me know in case you require extra information.

 

ACCOUNT_NOTRADE_DATEFLAGQTYPRICEDesired_AVG_PRICE
13-Mar-14FRESH506233.75 
13-Mar-14COVER506242.16233.75
14-Mar-14FRESH506250.3 
14-Mar-14FRESH506249 
14-Mar-14FRESH506251.2 
14-Mar-14COVER506267.86250.166667
14-Mar-14FRESH1006269.95 
14-Mar-14FRESH506291.65 
14-Mar-14FRESH506303.35 
14-Mar-14FRESH506333 
15-Mar-14COVER35063416277.3
328-Feb-15FRESH258858 
328-Feb-15COVER2588908858
418-Feb-15FRESH508951.375 
426-Feb-15COVER2587508951.375
428-Feb-15COVER2589388951.375
428-Feb-15FRESH258950 
528-Feb-15FRESH258961.7 
528-Feb-15COVER258949.88961.7
620-Feb-15FRESH508857.2 
620-Feb-15COVER2588888857.2
620-Feb-15COVER258848.38857.2
623-Feb-15FRESH508859.225 
623-Feb-15COVER2588638859.225
623-Feb-15COVER2587408859.225
624-Feb-15FRESH258743 
626-Feb-15COVER258684.358743
628-Feb-15FRESH258924.35 
628-Feb-15COVER2589908924.35
726-Feb-15FRESH508766.775 

 

Regards

Sandeep

Frequent Contributor
Posts: 127

Re: Weighted Average of a stock price

Hello Sandeep,

I don't understand why the logic to calculate the DESIRED_AVG_PRICE of the cell F7 of your Excel does not take into account the QTY and PRICE of the of row 2, whereas the calculation of the DESIRED_AVG_PRICE of the cell F12 of your Excel takes into account the QTY and PRICE values of the rows 4 to 6. It seems to me that the logic is not in aligned...

I would expect to find 6246.0625 as DESIRED_AVG_PRICE in the cell F7 of your excel (calculated as with the formula =(D4*E4+D5*E5+D6*E6+D2*E2)/200).

Kr,
Florent
Occasional Contributor
Posts: 5

Re: Weighted Average of a stock price

Hi Florent,

 

Please find the attached excel. I have mentioned steps how to calculate weighted avg price. Kindly let me know if have any doubts regarding data or logic.

 

Regards

Sandeep

Frequent Contributor
Posts: 127

Re: Weighted Average of a stock price

Hi Sandeep,

 

Sorry for the delay but I had a lot to do on my projects.

 

I don't know if you have solved your problem in the meantime but I'll still give it a last try. Please find below a new SAS program which, according to the logic I saw in your Excel, should match with your expectations.

 

Do not hesitate to comment the DROP statements in the code so that you can have a look at the temporary variables that are created. Also, adding PUT statements before, inside, and  after the DO loops may help in visualizing what is happening during the execution of the datasteps.

 

Kind regards,

Florent

 

data tmp_Results (drop= tmp_: len_:);

	set input;

	length List_Row List_Qty List_Price $1000 tmp_Row $100;
	retain List_Row List_Qty List_Price;

	tmp_row_num = _N_;
	tmp_Total_Qty = Qty;

	/* To keep exactly the same amount of rows as in the excel example */
	if tmp_row_num <= 22 then do;

		/* In case of Fresh, add the values to the lists */
		if strip(upcase(Flag)) = 'FRESH' then do;
			List_Row = strip(List_Row) || strip(put(tmp_row_num, best12.))|| ';';
			List_Qty = strip(List_Qty) || strip(put(Qty, best12.))|| ';';
			List_Price = strip(List_Price) || strip(put(Price, best12.))|| ';';
		end; else

		/* In case of Cover, remove values from the lists */
		if strip(upcase(Flag)) = 'COVER' then do;

			do until (tmp_Total_Qty <= 0);
				tmp_Row = strip(scan(List_Row, 1, ';') || ';');
				tmp_Qty = input(scan(List_Qty, 1, ';'), best12.);
				tmp_Price = strip(scan(List_Price, 1, ';') || ';');

				len_Price = lengthn(strip(tmp_Price));
				len_Qty = lengthn(strip(scan(List_Qty, 1, ';') || ';'));

				if tmp_Qty >= tmp_Total_Qty and tmp_Total_Qty > 0 then do;
					tmp_Total_Qty = tmp_Total_Qty - tmp_Qty;

					if tmp_Total_Qty >= 0 then do;
						List_Row = strip(tranwrd(List_Row, strip(tmp_Row), ''));
						List_Qty = strip(substr(List_Qty, len_Qty+1));
						List_Price = strip(substr(List_Price, len_Price+1));
					end; else
					if tmp_Total_Qty < 0 then do;
						List_Qty = strip(put(-tmp_Total_Qty, best12.)) || ';' || strip(substr(List_Qty, len_Qty+1));
					end;
				end; else
				if tmp_Total_Qty > tmp_Qty and tmp_Total_Qty > 0 then do;
					tmp_Total_Qty = tmp_Total_Qty - tmp_Qty;

					List_Row = strip(tranwrd(List_Row, strip(tmp_Row), ''));
					List_Qty = strip(substr(List_Qty, len_Qty+1));
					List_Price = strip(substr(List_Price, len_Price+1));
				end;
			end;
		end;

		output tmp_Results;
	end;
run;


data Results (drop= List_: tmp_:);
	set tmp_Results;

	List_Row = lag1(List_Row);
	List_Qty = lag1(List_Qty);
	List_Price = lag1(List_Price);

	if strip(upcase(Flag)) = 'COVER' then do;
		tmp_nb_items = countw(List_Row);
		tmp_numerator = 0;
		tmp_denominator = 0;

		do tmp_i=1 to tmp_nb_items;
			tmp_Qty = input(scan(List_Qty, tmp_i, ';'), best12.);
			tmp_Price = input(scan(List_Price, tmp_i, ';'), best12.);

			tmp_numerator = sum(tmp_numerator, tmp_Qty * tmp_Price);
			tmp_denominator = sum(tmp_denominator, tmp_Qty);
		end;

		avg_price = divide(tmp_numerator, tmp_denominator);
	end;
run;

 

Ask a Question
Discussion stats
  • 9 replies
  • 949 views
  • 0 likes
  • 2 in conversation