Visible candle counter on pinescript v.5 - pine-script-v5

So I don't code at all but was looking for a simple script to tell me how many candles are currently on my chart. how many are visible. I used chatgtp to write me a script. No matter how many different ways i ask the question I always get the same error when compiling.
study("Visible Candle Counter")
count = 0
for i in range(bar_index - 300, bar_index):
if i < 0 or na(close[i]):
break
count := count + 1
plot(count, color=color.blue, linewidth=2, title="Visible Candles")
the error is:Script could not be translated from: |B|for i in range(0,
I've tried asking chatgtp different ways to write the script. i've gotten like 6 iterations of it but they all come up with that same error.

Related

ped.getGroup().size() weird behavior

I encountered a weird behaviour of one of my groups, am I missing something?
The groups of 1 to 4 are approaching a pedOutput block where under first exit there is this condition:
ped.getGroup().size() <= 2 && v_table02_occupied < 20;
The condition works fine for some time, as I intended ( groups of <=2 exit with 1, otherwise exit 2), until the group of 4 approaches and comes through exit 1. I have no idea why.
It shouldn't be connected to v_table02_occupied variable, but so you know, at the time this group enters the pedOutput block it prints as 16.
Did someone see groups misbehaving like that before?
Thanks,
Peter
The problem is that your v_table02_occupied +1 code is executed after the condition check.
Best execute the code before the condition by using a simple delay of 0 duration in front of the pedSelectOutput. Call the +1 code there in its "on enter" code box.
And then do some reading on how code is executed sequentially, this is a typical pitfall for many as it is so subtle :)
Stupid mistake on my part.
Condition statements seemingly should not end with ;.
The statement that worked:
ped.getGroup().size() <= 2 && v_table02_occupied < 20
Instead of:
ped.getGroup().size() <= 2 && v_table02_occupied < 20;
Thanks Benjamin for taking time to help.
//Peter

promql example with related fields but different labels

I'm using Prometheus and Grafana, and I'm trying to track a web server app.
I want to graph the average duration in ms of a particular query. I think I can get there from the data below, but I'm struggling.
My two sets of values:
rate(http_server_request_duration_seconds_sum[5m])
Element Value
{instance="dbserver:5000",job="control-tower",method="get",path="/api/control/v1/node/config.json"} 0.0010491088980113385
{instance="dbserver:5000",job="control-tower",method="get",path="/api/schedule/v1/programs/:id.json"} 0
{instance="dbserver:5000",job="control-tower",method="get",path="/api/schedule/v1/users.json"} 0
{instance="dbserver:5000",job="control-tower",method="get",path="/metrics"} 0.00009133616130826839
{instance="dbserver:5000",job="control-tower",method="post",path="/api/caption/v1/messages.json"} 0
{instance="dbserver:5000",job="control-tower",method="post",path="/api/caption/v1/sessions.json"} 0
{instance="dbserver:5000",job="control-tower",method="post",path="/api/schedule/v1/programs.json"} 0
{instance="dbserver:5000",job="control-tower",method="put",path="/api/caption/v1/sessions/captioners.json"} 0
{instance="dbserver:5000",job="control-tower",method="put",path="/api/control/v1/agents/:id.json"}
rate(http_server_requests_total[5m])
Element Value
{code="200",host="dbserver:5000",instance="dbserver:5000",job="control-tower",method="get",path="/api/control/v1/node/config.json"} 0.03511075688258612
{code="200",host="dbserver:5000",instance="dbserver:5000",job="control-tower",method="get",path="/api/schedule/v1/programs/:id.json"} 0
{code="200",host="dbserver:5000",instance="dbserver:5000",job="control-tower",method="get",path="/api/schedule/v1/users.json"} 0
{code="200",host="dbserver:5000",instance="dbserver:5000",job="control-tower",method="get",path="/metrics"} 0.06671043807691363
{code="200",host="dbserver:5000",instance="dbserver:5000",job="control-tower",method="post",path="/api/caption/v1/sessions.json"} 0
{code="200",host="dbserver:5000",instance="dbserver:5000",job="control-tower",method="post",path="/api/schedule/v1/programs.json"} 0
{code="200",host="dbserver:5000",instance="dbserver:5000",job="control-tower",method="put",path="/api/caption/v1/sessions/captioners.json"} 0
{code="200",host="dbserver:5000",instance="dbserver:5000",job="control-tower",method="put",path="/api/control/v1/agents/:id.json"} 0
{code="422",host="dbserver:5000",instance="dbserver:5000",job="control-tower",method="post",path="/api/schedule/v1/programs.json"} 0
{code="502",host="dbserver:5000",instance="dbserver:5000",job="control-tower",method="post",path="/api/caption/v1/messages.json"}
They have different labels. For this, I only care where path="/api/caption/v1/messages.json".
I think I need to use a combination of rate, sum, and "on" or "ignore", but I haven't been able to get on or ignore to work at all.
I can get the numerator (in seconds) with:
rate( http_server_request_duration_seconds_sum { path="/api/caption/v1/messages.json" }[5m])
And that returns:
{instance="dbserver:5000", job="control-tower", method="post", path="/api/caption/v1/messages.json"}
But the denominator can have different return codes, so I have to sum those, and I need to do some ignore or on or something, but I haven't found an example that helps me out, and I'm really new at this.
Anyone?
Okay, I continued to play. Because I only have one path I worry about, i figured out I could sum the rates. I think this works:
sum( rate( http_server_request_duration_seconds_sum {path="/api/caption/v1/messages.json"}[2h])) / sum( rate( http_server_requests_total{ path="/api/caption/v1/messages.json"}[2h]))
I changed the sample rate as my sample data fell off my 5-minute window, and I had zeros.
I THINK what this is doing is summing the rates, which gets rid of all the labels. And I THINK what it's also doing is using 2 hours of data. I think the rate value is how quickly the value changed over that 2 hour period.
I would love comments.
This solution won't work if I want one chart to include other paths, and I'm still not sure what to do about that, so this solves my current problem but still doesn't help me figure out how to do something similar with ignore or on.

using the same function but on different rows

I want to estimate the co(variance) matrix of two assets classes (with different derivatives) of every month.. So let's assume that each month is 25 days. I will get the following code,
covar=cov([elec(1:25,:) ngas(1:25,:)])
However, I have like 5 years of data so rewrite everything seems like a waste of time, and I think there has to be an easier way to fix this problem.
ps. I do think the answer to my question is already answered somewhere, but I do not know the words the search on. Thanks for your reply
Sounds like you just need a for loop?
counter = 0;
cover{floor(size(elec,1)/25)} = []; %//Pre-allocation
for day = 1:25:size(elec,1)
counter = counter + 1;
covar{counter}=cov([elec(day:day+25-1,:) ngas(day:day+25-1,:)])
end

How many iterations are saved by JAGS/BUGS when burnin and thinning are specified?

I have a quick question about the details of running a model in JAGS and BUGS.
Say I run a model with n.burnin=5000, n.iter=5000 and thin=2. Does this mean that the program will:
Run 5,000 iterations, and discard results; and then
Run another 10,000 iterations, only keeping every second result?
If I save these simulations as a CODA object, are all 10,000 saved, or only the thinned 5,000? I'm just trying to understand which set of iterations are used to make the ACF plot?
With JAGS, n.burnin=5000, n.iter=5000 and thin=2, means you keep nothing. You run 5000, discard the first 5000 of these 5000 and then only keep a half of the remaining values of the chain (keep 1 value and discard the next one ..).
Use for example n.burnin=2000, n.iter=7000, thin=50, n.chains=5 : so you have (7000-2000)/50 * 5 = 500 values.
Could you be more specific which software you're talking about? It looks like you're referring to the arguments of the function bugs() in the R2WinBUGS package (except that the argument is called n.thin not thin). Looking at help(bugs) it just says n.burnin is the "number of iterations to discard at the beginning". Which doesn't specifically answer your question, but looking at the source for bugs.script() in that package suggests to me that it would run 5000 iterations burn in, as you suspected. You could send a suggestion to the maintainers of that package to clarify their documentation.
In your example, bugs() would then run 0 further iterations after the burn-in. Here the documentation is clearer - n.iter is the total number of iterations including the burn-in.
For your second question, the CODA output from WinBUGS (and any software which calls WinBUGS or OpenBUGS) will only include the thinned sample.

Finding a Range in CoffeeScript

I understand how to define an array range in CoffeeScript
lng[1..10]
However if I have
data = 10
What's the best way to find if 10 is within a range of 1 and 11?
if data is between(1..11)
return true
There is no "between" keyword, but you can utilize a normal array-range:
if data in [1..11]
alert 'yay'
But that's a bit of an overkill, so in simple cases I'd recommend a normal comparison:
if 1 <= data <= 11
alert 'yay'
If you don't mind polluting the native prototypes, you can add a between method to the Number objects:
Number::between = (min, max) ->
min <= this <= max
if 10.between(1, 11)
alert 'yay'
Although i personally wouldn't use it. if 1 <= something <= 11 is more direct and everyone will understand it. The between method, instead, has to be looked up if you want to know what it does (or you'd have to guess), and i think it doesn't add that much.