Using request.security data to use for calculations - pine-script-v5

I want to achieve the following...
For every candle, I want to calculate the "max range" of the previous x candles.
I then want to use this max range values and compare it with the ATR of the daily timeframe, to get the "max range" value as a percentage of the daily ATR
Point 1, was the easy part and I was able to achieve that with the below code.
// USER INPUTS
iLookBack = input.int(defval = 4, title = "Lookback Period")
// MAX RANGE OF THE LAST x CANDLES
maxRange = math.abs(ta.highest(iLookBack) - ta.lowest(iLookBack))
plot(maxRange, color = color.aqua)
Below a picture of it.
Point 2 is where I got stuck, and where your help is needed.
I tried fetching the daily ATR data using the request.security function
datr = request.security(syminfo.tickerid,"D", ta.atr(14))
On the current day, I get a DATR reading on all candles of the day.
But on the prior day, I only get a DATR reading on the FIRST CANDLE OF THE DAY.
So far so good...
But when using the DATR data and trying to plot the result of the calculation (below) I only get the result of the calculation plotted for the 1st candle of the day.
plot(maxRange/datr)
And the result is looking like this..
This is not what I want..
What I want is, that for every candle, I want to compare the "maxRange" with the DATR reading. And that this result is being plotted.
My guess is, that the data fetched using the request.security function is only valid for ONE candle at the lower timeframe? I'm actually not sure.
I looked up the documentation but couldn't find the answer to my problem..
I hope my problem is clear. And what I'm trying to achieve is making sense to you.
Thank you very much in advance.

Related

Power BI - Timeseries compare two different start dates

I want to compare how different campaigns are progressing based on number of days into the campaign rather than by date (see day1, day2, etc... on the x-axis below).
Here is my DAX code, but I can't get it to work. Any help would be much appreciated...
**Normalised Campaign Metrics =
VAR DateReached = CALCULATE(MIN(Days[Day]),db[PAYMENT_DATE]<> BLANK(), KEEPFILTERS(db[PRODUCT_CODE SWITCH]))
VAR MaxDate = CALCULATE(MAX(db[PAYMENT_DATE]),KEEPFILTERS(db[PRODUCT_CODE SWITCH]))
VAR DayNo = SELECTEDVALUE(Days[Day])
RETURN CALCULATE(count(db[PAYMENT_DATE]),
FILTER(ALL(db[PAYMENT_DATE]),
DateReached+DayNo && DateReached+DayNo<=MaxDate))**
Many thanks!
enter image description here
I would recommend solving this through manipulating your actual data rather than a complex DAX measure. If you are familiar with star schema modelling, I would solve this problem by adding a new column to your fact table that calculates how many days from the start date the payment occurred and then connect this column to a new "Days Passed" dimension that is simply a list of numbers from 1 to however many days you need. Then, you can use this new dimension as the source data for your x axis and use a standard payment amount measure for your y axis.
I recommend to create a dimension table as the relative basis to comparison with inactive relationship. Here is a video about it:
https://youtu.be/knXFVf2ipro

How can i create a mongodb query that calculates the difference of a value along other timestamps using a unique id

I have collected various data via the Twitch API and would now like to prepare and display it in a mondodb chart.
What I want to display are the viewer counts along the measurement points (timestamps). The IDs per timestamp should be compared with the viewer count. If the viewer count is equal to the previous timestamp, nothing should happen, if the value has changed, the difference to the current value should be entered e.g. in a new field. At the end I want to see on the x axis the timestamps and on the y axis the viewer count for the timestamps
here is a axample of the Object. Eevery minute I get one such object per active livestream (mostly 20 objects per request/timestamp)
_id:604672c85f68ef1c71546c71
id:40931788941
user_id:493094533
game_name:"Science & Technology"
title:"Arduino Lasershow aus dem 3D-Drucker. #arduino #3D-Druck #CNC #Program..."
viewer_count:151
started_at:"2021-03-08T14:50:25Z"
language:"de"
time:2021-03-08T18:54:00.911+00:00
__v:0
most important attributes:
id:40931788941
time:2021-03-08T18:54:00.911+00:00
viewer_count:151
My problem is that I don't know exactly how to calculate the difference to the previous object with native mongo. If you have another solution, bring it to me ;-)
EDIT:
You can see in the following screenshot the x axis the dates and on the y the viewer_count for this date. But the viewer_count is not meaningful because its a sum of all viewer_count values of all objects for the specific date.
I only want to get the cumulative value of the viewer_count attribute for the specific date.
Other aggregations for this attribute do not help much here either.

Function which finds temperatures for a given month from data

I'm having some issues creating a function with the following parameters:
Ndata = extperiod(data, year, month,time)
The data is a table with 3 columns, which from left to right are:
year/month/date, time, temperature
My goal is to create a function which can extract a time and a year/month, irrespective of the date and find it's corresponding temperature.
I need to avoid using for loops
I've been advised to use floor and find, where floor(YYYYMMDD/100) = YYYY*100 + MM, which I somehow want to integrate to my function.
I've previously found a way to extract all temperatures from the data for a given day, as follows:
k = find(data(:,1)==19750101);
data(k(1):k(end),3)
I'm trying to incorporate this method, but I think that the hint "floor(YYYYMMDD/100)" throws me a of a little.
I have tried with find(data(:,1)==floor(YYYYMMDD/100)), where I would think that I'd be given all dates with a specific year and month. For example:
find( data(:,1) == floor(19660101/100) )
I thought this would give me all points in the column vector where the value is 196601. But it doesn't.
What could I try differently?
From your explanation, your want to get all temperature for a given month, no matter time and day.
So you want to find dates that are comprised in the range [YYYYMM ; YYYY{MM+1}[ or [YYYYMM ; {YYYY+1}01[ in the case of selecting December.
Recall that you store the complete date in your table. So you need to apply your operator floor to both sides of your query, not only on the query value, because no date is floor(YYYYMMDD/100)!
As a result, try the following:
find( floor(data(:,1)/100) == floor(19660101/100) )

Matlab: How to plot data of a selected time range?

I have some data matrix, the first column is date and the second column is time.
I changed it into a table:
H1 = readtable(filename, 'ReadVariableNames', 0);
H1.Properties.VariableNames =...
{'Date', 'Time', 'Value'};
t = datetime(H1.Date, 'InputFormat', 'yyyy.MM.dd');
H1.Date = t;
clear t
Now, I want to make a sub-table. In this table, the date of the data doesn't matter, but the time of the data must be between 18:00 and 11:00 the next day.
My time format is hh:mm (24 hours).
How could I do it?
I think I could make another column. For each time string, I take only the first 2 characters and convert it to a number (e.g. '05:00' will be changed to '05' and then 5). After that I could index the new column I created.
Nevertheless, I think there must be some other and easier ways to do it.
How?
In addition, if this works, I want to write a function based on this.
I want to give a certain day and time, and plot all the data +-48 hours (or -24 hours and +72 hours) around this day and time.
For example, if I give '2016-06-22', '05:00', then I want to plot all the data ranging from '2016-06-20', '05:00' to '2016-06-24', '05:00'. Any strategy suggestion?
Given that my data size is quite big, speed is a factor that we need to take into consideration.
Thanks!

Tableau - Dividing two calculated fields, getting wrong result of 1

I've been ripping my hear out trying to figure out why this is happening, any help would be very much appreciated!
I'm trying to divide the result of a calculated field by another calculated field in order to get a percentage ("X% of transactions were at or below SLA" essentially). Not sure if the problem is how I'm calculating the two fields, or this new calculation I'm using.
Here is the calculation I'm using: sum([CALC1]) / ([CALC2]) --I'm getting a result of 1 using this, but am expecting something like .982.
CALC1: IF [Total Time (seconds)] > 180 THEN 1 ELSE 0 END
CALC2: COUNT([Trans Id])
I thought it would make sense to add this in the answers portion, even though the correct answer is listed in the comments.
The issue is with how you are displaying the answer. Right click on the measure in the left hand pane and scroll down to "default properties". Go to "Number format" and adjust to the desired number of decimal places.