How can I get calculate buy and sell quantity in pandas? - pandas

When I have data of quantity and buy and sell program, I want to get the cumulative quantity by adding a number when it is 'buy' and deducting a number when it is 'sell'. How can I do this in pandas? I know there is .cumsum() function to add it, but I don't know how to deduct it.

You can flip the integer when selling:
df['quantity'].where(df['buy&sell'].eq('buy'), -df['quantity']).cumsum()

Related

How to calculate outstanding amount and interest using dataframe

I want to subtract the repayment amount from the outstanding amount in the previous row and store the result as shown in the image and repeat it for the rest of the rows

AWS QuickSight: maxOver() calculated field?

I am a stock trader who visualizes data in QuickSight. I identify the trades I want to submit to the market, sometimes for the same stock, at the same time, but in opposite directions depending on the price of the stock at that time. See below for an example of trades I might identify for 1/19/22 0800:
Date
Hour
Stock
Direction
Price
Volume
1/19/22
0800
Apple
BUY
$10
2
1/19/22
0800
Apple
SELL
$20
1
1/19/22
0800
Microsoft
BUY
$15
3
Using QuickSight, I want to visualize (in pivot tables and charts) the volume that I trade, using the maximum possible trade volume. For example, QuickSight simply sums the Volume column to 6, when really I want it to sum to 5, because the max possible trade volume for that hour is 5 (the Apple trades in the example are mutually exclusive, because the stock price cannot be both beneath $10, triggering a BUY, and above $20, triggering a SELL at the same date-time. Therefore, I want the day's traded volume to reflect the MAX possible volume I could have traded (2+3)).
I have used the maxOver() function as so: maxOver({volume}, [{stock}, {date}, {hour}], PRE_AGG), but I would like to view my trade volume rolled up to the day as so:
Date
Volume
1/19
5
Is there a way to do this using QuickSight calculated fields? Should this aggregation be done with a SQL custom field?
Add a new calculated field called
volume_direction_specifier
{Volume} * 10 + ifelse({Direction}='BUY', 1, 2)
This is a single number that will indicate the direction and volume. (this is needed in cases where the max possible volume is the same for both the BUY and SELL entries within the same hour).
Then compute the maxOver on this new field in a calculated field called max_volume_direction_specifier
maxOver({volume_direction_specifier}, [{stock}, {date}, {hour}], PRE_AGG)
Add a new field which will give the Volume for rows that have the max volume_direction_specifier per hour
volume_for_max_trade_volume_per_hour
ifelse(volume_direction_specifier = max_volume_direction_specifier, {volume}, null)
And finally, you should be able to add volume_for_max_trade_volume_per_hour to your table (grouped by day) and its SUM will give the maximum possible trade volume per day.

Shopify api total sales

how can i get total sales from shopify store for api call? anyone have idea?I got total price by date but not total sale.
I need total sales by min and max date.
Thanks in advance.
You can get the total amount of orders that you need for the range that you need, then get the ceiling of the order count divided by the amount per page (this will get you the total amount of pages you need to get). For example, if you have 730 orders for a day and 250 orders per page, you will need to get 3 pages of orders. You can then get the 3 pages of orders and add up the totals.
https://docs.shopify.com/api/reference/order#count
you have to loop the order list response and sum up the "total_price" field. If you need accuracy you have to subtract any refunds sum from your total sum.
Yes there is a json object for refunds. In Python you can retrieve the refund information with json_response['orders'][number_in_the_list]['refunds']. Make sur to add to your GET request the status=any endpoint so you'll get /admin/api/2021-07/orders.json?status=any plus your other endpoints.

Oracle - How to calculate profit that is above average

I have a table that contains items, buyprice, and sellprice. I need to figure out how to return which items were sold for a profit above average. I realize that for each item that I need to calculate sellprice-buyprice to get the profit. I then need to take the profit from each item and calculate the average profit of all of the items. I then need to return the items that have profits that are above that average. I have tried various subselects and I just can't seem to figure out how to return what I want.
Seems simple enough:
SELECT * FROM TBL WHERE sellprice-buyprice > (SELECT AVG(sellprice-buyprice) FROM TBL )
Am i missing something?

Predictive Ordering Logic

I have a problem and was wondering if anyone could help or if it is even possible to have an algorithm for something like this.
I need to create a predictive ordering wizard. So based on previous sales, we will determine that that a certain amount of an item is required. E.g 31 apples. Now i need to work out the number of cases that needs to be ordered. If the cases come in say 60, 30, 15, 10 apples, the order should be a case of 30 and a case of 10 apples.
The number of items that need to be ordered change in each row of the result set. The case sizes could also change for each item. So some items may have an option of 5 different cases and some items may land up with an option of only one case.
Other examples would be i need 39 cans of coke and the cases come in only 24 per case. Therefore needing 2 cases. I need 2 shots of baileys and the bottle of baileys come in 50cl or 70cl. Therefore i need the 50cl.
The results sets columns are ItemName, ItemSize, QuantityRequired, PackSize and PackSizeMultiple.
The ItemName is the item to be ordered. ItemSize is the size the item is used in eg. can of coke. QuantityRequired how man of the item, in this case cans of coke, need to be ordered. PackSize is the size of the case. PackSizeMultiple is the number to multiply the item with to work out how many of the items are in the case.
ps. this will be a query in SQL Server 2008
Sounds like you need a UOM (Unit of Measure) table and a function to calc co-pack measure count and and unit count measure qty. with UOM type based on time between orders. You would also need to create a cron cycle and freeze table managed by week/time interval in order to create a freeze view of the current qty sold each week and the number of units since last order. Based on the 2 previous orders to your prior order you would set the current prediction based on min time between the last 2 freeze cycles containing an order and the duration of days between them. based on the average time between orders and the unit qty in each order, you can create a unit decay ratio percentage based on days and store it in each slice forward. Based on a reference to this data you will be able to create a prediction that will allow you to trigger a notice to sales or a message to the client to reorder. In addition, if you engage response data from sales based on unit count feedback from the client, you can reference an actual and tune your decay rate against your prediction. You should also consider managing and rolling up these freezes by month, so that you can view historical trending and forecast revenue based on velocity of reorder and same period last year. Basically this is similar to sales forcasting and we are switching out your opportunity percentage of close with Predicted Remaining Qty. percentage remaining.