Calculate stock trading position for buy and take profit in ms sql - sql

I'm currently developing a trading diary. There are many positions having multiple buy and take profit transactions. Here's a sample:
The initial position size is 50 #20 USD
One week later, we sell 15 #22 USD
Another 2 weeks later we sell again 15 #23 USD
One week later, a pullback comes into the price so we buy another 25 #21 USD
One week later, we sell again 20 #23 USD
Another 4 weeks later we sell 25 shares #25 USD
One business rule is the following: To get the actual P/L calculation, the "last buy point" must always be used as long as we have shares in place.
In this case, we can deduct 2 x 15 Shares (330 USD + 345 USD) from the initial 50 shares (1k USD). After buying 25 shares (after the pullback), we first have to deduct 20 remaining shares (460 USD) from the initial position (50 shares for 1k USD). For the last sell point of 25 shares (625 USD) we have to use the second buy point of 25 shares #21 USD (525 USD).
Any help is appreciated.
Krgds
Sascha

Related

Pacing Period To Date Calculated Measure on SSAS cube - MDX

I want to produce a calculated member on our cube using MDX Expression that shows
revenue booked at any point in time up for the current date of a given
Measure
[Measures].[Rev]
Date Dimension
[Date].[Year]
Contract Dimension
[Contract].[Booking Date]
Year-Month
Rev
Pacing Rev
2021-01
10,000
10,000
2021-02
6,000
6,000
2021-03
12,000
12,000
2021-04
7,000
7,000
2021-05
9,000
4,500
2021-06
11,000
3,600
2021-07
13,000
1,000
Essentially I want to be able to determine how much Revenue was booked this time last year.
if today = 17/05/2022
I want to see Pacing Rev for all contracts booked for 2021 from any point in time up until today 17/05/22.
What I mean by 'from any point in time. i.e. a contract could have been booked in 2018, for 2021 (hence why I think I'll need to use the [Contract].[Booking Date] some how

How to handle the stock table quantity after sale

I have a problem. I am not understanding that how to deduct the quantity from stock after sale and we have multiple entries of same stock
like:
id
Medicine
Company
quantity
item price
is deleted
1
Panadol
Abbott
200
10.00
0
2
Panadol
Abbott
100
11.00
0
3
Panadol
Abbott
100
12.00
0
now how to deduct the quantity of stock and one thing is more that this product/Medicine has separate manufacturing and expiry date
please tell me how to handle it.
I was thinking that to make separate table for tracking the remaining stock quantity.
or runtime management to calculate all the sale quantity minus(-) sum of all quantities entered to get
the current stock quantity.
Please guide me.

Pandas :: How to plot based on sum amount each month

I made data frame shown below which has 3 companies A,B and C. Companies buy certain amount of vouchers during 2016 to 2018 period. Some days eg. Company A buys 100 pieces for $3000 other days no company buys any.
I'd like see how these three companies compare for last two years when it comes to money spent for vouchers so my ideas was following:
Sum all money spent each month for each company, and plot them in bar graph or just standard line - so three lines each with different color.
Since its 2 years of data, there would be roughly 24 date-points on x axis
I tried something like: plt.bar(A['Datetime'], A['PaidTotal'])
But get: ufunc subtract cannot use operands with types dtype('
But this is just for one company anyway not for all 3 in one graph
(I can sort those dates, thats not a problem)
Company Name PaidTotal Datetime
585 CompanyA 218916.0 2016-10-14 10:51:07
586 CompanyB 430000.0 2017-01-23 11:05:08
591 CompanyB 546217.0 2016-09-26 14:20:00
592 CompanyC 73780.0 2016-12-07 07:52:01
593 CompanyA 132720.0 2016-10-04 16:14:10
595 CompanyC 52065.0 2016-11-12 14:32:40
For a bar chart you can call df.groupby('Company Name')['PaidTotal'].sum().plot.bar():
To see a line chart of all three over time, you can try this (the axes are wrong, but this is the general idea):
sums = df.groupby(['Company Name', 'Datetime'])['PaidTotal'].sum().reset_index(level=0)
for company in sums['Company Name'].unique():
sums[sums['Company Name'] == company]['PaidTotal'].plot();

Find vacancy (no contract) days in each month for property/unit per year

I need to get number of vacant days in each month in every year
Leasing Contracts
So, my target is to occupy all units along the year.
For that I have to find how many days in each month units are not occupied (There s not contract) and focus on these units to reduce the price or give promotions.
So, I have lease start and lease expiry dates for the units in the first table and I want to know in summary table (second table) number of days in each month which are not belong to any of the leasing period (from/to range) from the first table for each unit.
I need Excel formulas and/or SQL scripts to be applied to find all days without rent for each project/unit in the selected year
Appreciate your support in advance!
Project Name Unit No Lease Start Lease Expiry Rent Per Annum
Building1 Unit1 01-01-2017 31-12-2017 70000
Building1 Unit1 01-01-2018 15-08-2018 60000
Building1 Unit1 01-10-2018 31-12-2018 60000
Building1 Unit2 01-01-2017 31-12-2017 60000
Building1 Unit2 01-03-2018 31-07-2018 60000
Building1 Unit2 01-09-2018 30-09-2018 45000
Vacancies Days multiply by last daily rate renting (rent per annum)/365
Project Name Unit No Jan-18 Feb-18 Aug-18 Sep-18 Oct-18
Building1 Unit1 0 0 16*164 30*164 0
Building1 Unit2 31*164 28*164 31*164 0 31*123
I would use a calendar table.
This you join with your lease table to get whether a particular day is leased or not.
Then you can group by unit and month and do a pivot to get the result you want.

SQL code guidance

I've recently had to start using SQL, and am struggling a little with how to code some more advanced capabilities etc, so was hoping for some guidance.
I have one database that contains high level data split by geography, NAICS industry and size of company, with field names such as:
CBSACODE VERTICAL COMPANYSIZE DEVICES
01010 Agriculture 1-10 100
01010 Education 20-99 50
01010 Healthcare 200-499 250
01010 Manufacturing 100-199 150
01010 Manufacturing 1-10 80
78910 Agriculture 1-10 25
78910 Government 500+ 400
78910 Agriculture 11-19 60
78910 Finance 100-199 310
78910 Retail 20-99 200
I have a second database which is at a customer level but has some overlapping fields
CUSTOMER NAME VERTICAL COMPANYSIZE ZIPCODE CBSACODE
Customer A Agriculture 1-10 12345 78910
Customer B Manufacturing 100-199 54321 01010
What I am trying to do is to display the list of customers, and then display the sum of 'Number of devices' based on matching the CBSA Code, Vertical Industry and Company Size for each customer against the first database.
CUSTOMER NAME CBSADEVICES CBSA+VERTICALDEVICES COSIZEDEVICES
Customer A 995 85 25
Customer B 630 230 150
I started trying to write a query using SUM and CASE WHEN etc but quickly got overwhelmed.
I'm using MS SQL Server 2016 (Express).
Any guidance would be greatly appreciated.....I've read through a number of various threads, but have yet to get my head around it sadly.