PineScript Version 5: Seeking different time frames for price ticker and indicators - bots

In PineScript Version 5, I would like to have the price constantly updated on each tick but have all the buy/sell conditions that are based of indicators run on a changable time frame, say 3 month period.
Why? Because trades cannot be executed on the same bar (as far as I can tell). So I'd want to have the highest resolution possible, like the 1 second ticker update on price for example but use the 3-month resolution to calculate MACD, RSI, etc.
Outcome I'm seeking, maybe some PineScript Version 5 examples or documentation
Code needs to have RSI, MACD, etc would be 3-month and trigger buy/sell events and price could update with each tick so that if the price hits a target/stop loss it is executed on the 1 second bar and not a higher resolution where you might get worse prices.

Related

Number of days for delivery and number of orders delivered in two separate columns. Is there a way to get summary statistics about orders?

I've had a bit of trouble explaining this so please bear with me. I'm also very new to using excel so if there's a simple fix, I apologize in advance!
I have two columns, one listing number of days starting from 0 and increasing consecutively. The other column has the number of orders delivered. The two correspond to each other. For example, I've typed out how it would look below. It would mean that there were 100 orders delivered in 1 day, 150 orders delivered in 2 days, 800 orders delivered in 3 days, etc.
Is there a way to get summary statistics (mean, median, mode, upper and lower quartiles) for the number of days it took for the average order to get delivered? The only way I can think of solving this is to manually punch in "1" 100 times, "2" 150 times, etc. into a new column and take median, mean, and upper & lower quartile from that, but that seems extremely inefficient. Would I use a pivot table for this? Thank you in advance!
I tried using the data analysis add-on and doing summary statistics that way, but it didn't work. It just gave me the mean, median, mode, and quartiles of each individual column. It would have given me 3 for median number of days for delivery and 300 for median number of orders.
Method 1
The mean is just
=SUMPRODUCT(A2:A6,B2:B6)/SUM(B2:B6)
Mode is the value with highest frequency
=INDEX(A2:A6,MATCH(MAX(B2:B6),B2:B6,0))
The quartiles and median (or any other quantile by varying the value of p) from first principles following this reference
=LET(p,0.25,
values,A2:A6,
freq,B2:B6,
N,SUM(freq),
h,(N+1)*p,
floorh,FLOOR(h,1),
ceilh,CEILING(h,1),
frac,h-floorh,
cusum,SCAN(0,SEQUENCE(ROWS(values)),LAMBDA(a,c,IF(c=1,0,a+INDEX(freq,c-1)))),
xlower,XLOOKUP(floorh-1,cusum,values,,-1),
xupper,XLOOKUP(ceilh-1,cusum,values,,-1),
xlower+(xupper-xlower)*frac)
Method 2
If you don't like doing it this way, you can always expand the data like this:
=AVERAGE(XLOOKUP(SEQUENCE(SUM(B2:B6),1,0),SCAN(0,SEQUENCE(ROWS(A2:A6)),LAMBDA(a,c,IF(c=1,0,INDEX(B2:B6,c-1)+a))),A2:A6,,-1))
=MODE(XLOOKUP(SEQUENCE(SUM(B2:B6),1,0),SCAN(0,SEQUENCE(ROWS(A2:A6)),LAMBDA(a,c,IF(c=1,0,INDEX(B2:B6,c-1)+a))),A2:A6,,-1))
=QUARTILE.EXC(XLOOKUP(SEQUENCE(SUM(B2:B6),1,0),SCAN(0,SEQUENCE(ROWS(A2:A6)),LAMBDA(a,c,IF(c=1,0,INDEX(B2:B6,c-1)+a))),A2:A6,,-1),1)
=MEDIAN(XLOOKUP(SEQUENCE(SUM(B2:B6),1,0),SCAN(0,SEQUENCE(ROWS(A2:A6)),LAMBDA(a,c,IF(c=1,0,INDEX(B2:B6,c-1)+a))),A2:A6,,-1))
and
=QUARTILE.EXC(XLOOKUP(SEQUENCE(SUM(B2:B6),1,0),SCAN(0,SEQUENCE(ROWS(A2:A6)),LAMBDA(a,c,IF(c=1,0,INDEX(B2:B6,c-1)+a))),A2:A6,,-1),3)

Planning sampling date using several criteria in Excel

I am trying to create a sampling plan for testing of raw materials. To provide some context, the criteria I am planning to factor in are the Priority level of the materials (e.g. P1 is the highest while P3 is the lowest), the delivery date of the material (Actual date of arrival), and the sampling capacity of each day (currently the capacity is at 30 SUs). If the sampling capacity of the day is exceeded, the date will be decided by back calculating using the lead times from the production need by date.
The main issue with the previous plan was that the dates were only planned around the priority level. This meant that regardless of the delivery date, whenever a new P1 item came in, it would push all the other lower priority items back, and create a backlog of untested items.
Using arbitrary values, if the material is P1, the date will be 12 days after the delivery date; P2, 13 days and P3, 14 days. After that, check if the sampling capacity is exceeded for the day. I tried to use Vlookup, but it only returns the first value rather than checking every row. I do not have Xlookup because I'm using Excel2016.
I want to create a code (either in VBA or excel) to factor in all these criteria to automatically plan a date.
The formula for sampling date based on just the priority level is =IF(C337="1", B337+12,IF(C337="2", B337+13,IF(C337="3",B337+14,""))), where C is the priority and B is the delivery date. This is what I currently have:
See below screenshot:

Facing difficulty in Excel when creating a logic for moving average of different variables

So, the point is, in my dataset I have to create a variable "Moving Avg. Amt paid per sq. ft." and the formula or the logic I need is to calculate the last five values as per most recent transactions. i.e. most recent sales by date. but this average should only return value in case it matches the same building and same area variable.
This is what my data looks like
Area ID has three categories. Building number has 5 categories. Date is sorted in ascending order. Now my variable moving average should calculate last 5 averages w.r.t date but for the same building in the same area. e.g. there are buildings 1 and 2 in area 102. I need my Mov Avg. variable to calculate using conditions when it matches criteria of building 1 in 102 for past five sales and when it finds building 2 in the building number variable, it should calculate average of last 5 sales of that building in area 102.
So my approach to this issue was (which is flawed at the moment):
I calculate average of amount paid per sq. foot w.r.t area & building based on dates using the formula
=AVERAGEIFS($N$2:$N$6547,$D$2:$D$6547,D14,$C$2:$C$6547,C14,$B$2:$B$6547,B14)
but I cannot make this formula work, to calculate moving average whenever it meets the criteria. I tried the offset the point as well by 5 but the logic is not right and hence its not working and returning #value in the cells. The formula I used to offset the above condition is
=AVERAGEIFS((OFFSET(N13,5,,5)),$D$2:$D$6547,D13,$C$2:$C$6547,C13,$B$2:$B$6547,B13)
(These formulae are used in column Q of my data)
Need a support from the community as I am badly stuck in making this data useful and I am out of any ideas to make this work.
Edit 1: I am not sure how I can attach my excel file here so you may review the dataset. I have uploaded it on a third party site, for which the link is shared below, so you can view the file in detail.
https://file.io/hlciAHJOHzWA
Expected result is as I have mentioned the instruction said
"Create a variable called "mov. avg amt. paid per sq ft". For each row, this variable should calculate average amt paid per sq ft for the most recent past five sales (by date) for the same building in the same area."
And my approach to build a logic or formula to make this variable calculate moving average w.r.t date for same building in the same area doesn't seem to work because there might be some flaws.
In Office 365 you could use:
=LET(f,FILTER($N$1:N13,($B$1:B13=B14)*($C$1:C13=C14),""),
c,COUNTA(f),
s,SEQUENCE(5,,c-5),
IFERROR(IF(c<5,SUM(f)/c,SUM(INDEX(f,s))/5),""))
If there's less than 5 matches prior to the current sales it'll calculate the average of the count. If 5 or more matches it'll calculate the average of the last 5 prior to the current sale.

Choose correct PRODUCT COST from ROW based on amount sold

I am making a sheet to CALCULATE REAL REVENUE from each sale I make on my online store.
The problem is that the COST of my products is not always constant. It varies depending on many factors so each time I make a purchase I add the NEW PRODUCTS COST (LATEST COST).
Each time I make a new purchase I will add the QUANTITY and the new COST. (PURCHASE 1, PURCHASE 2, PURCHASE 3,etc).
Screenshoot of my sheet with example on ROW 41
In Column B I want to know which is the CURRENT COST based on the amount of TOTAL SALES of each product.
For example:
If I have sold less than 100 ( Pruchase 1 QUANTITY) then I need the formula to choose value of E41 (PURCHASE 1 COST).
If I have sold MORE than 100 and LESS than 300 (which is the SUM of PURCHASE 1 & PURCHASE 2) I need the formula to choose value of G41 (PURCHASE 2 COST) AND SO ON...
The formula I have come up with so far is this:
=INDEX(41:41,,IF(C41<=D41,COLUMN(E41),IF(C41<=D41+F41,COLUMN(E41)+2,IF(C41<=D41+F41+H41,COLUMN(E41)+4,COLUMN(E41)+6))))
This formula WORKS but only for the first 3 PURCHASES.
I need a formula that has no limit but I don't know how to make a VARIABLE formula.
Please take my words literally when I say that I wouldn't waste one minute on trying to solve your problem with your current sheet design. You would need VBA, and then extract quantities and prices from each purchase without the ability to filter on columns. (Minute is up.)
What you need is a Purchase database: ItemID, Date, Quantity, Price, maybe Ref#. From that you can pull out the transactions for any item by filtering on the item and the cost by using functions like SUMIF. However, this just brings the real problem within reach without solving it.
The problem is that when you buy 100 pcs #42 your price is 42. Then you buy another 100 pieces #46 your average price is 44. But if you sold 50 pieces with a cost of 42 then the average cost of the remaining 150 is 45. Therefore you can't determine the average cost of any remainder without knowing the quantity sold and the average cost applied to that sale. To solve that problem you will still need VBA but the suggested db format of purchase record would at least support such a solution.
Not so long ago I programmed a solution where there were additional columns in the db and each sale was recorded in 3 columns (much like your present purchase record): date, Qty, Ref. In this way I could trace the sale of each individual purchase (this was for shares trading). The sale of the newer quantity wouldn't start until the earlier quantity was sold out.
Perhaps you don't need to trace where the purchased quantity went to and just need one column to count down the balance to zero. That would be much simpler but has the drawback that you can't roll back errors. In the end the rollback was the reason why I abandoned the design. The key to the ability to abandon it is a similar db for sales: date, qty, price, Ref#.
With such a setup you might design a system to either extract the average or FiFo price from the purchase side and associate it with a sale. If the condition is that it should be done with worksheet functions you could add a column for "current cost" in the purchase db, changing with each purchase, which you look up by date from the sales side using VLOOKUP or SUMPRODUCT, having set a cost price applicable from the day of purchase until the next. If that appeals to you, a method must be found to deal with days on which there are both purchases and sales.

Set stable prices of product, while breaking even (Excel)

The case:
Let us say a company is a monopolist in the market, and the supply of products is 100% met by demand every week. All (variable and fixed) costs are set. Based on estimations I have weekly units produced. In addition, the average price throughout the year has to hit approximately X. It is preferrable that the monthly results don't vary too much from break even. How can I quickly set the weekly price for the company’s product so that the company breaks even (Result = approx. 0), and at the same time keep the weekly price at the most stable price possible (as close to average as possible every week).
I have tried to use solver to minimize STD.DEV of prices (also tried skewness), with constraints that the average price is X and estimated result = 0, by changing the weekly prices throughoutthe year. However, this results in a few weeks of extreme price differences, which is the opposite of what I need.
Do somebody have a possible solution to the problem?
You could try the following:
minimize maxp - minp
maxp >= p(w) for all w
minp <= p(w) for all w
This will try to minimize the bandwidth (and additionally is completely linear)

Resources