Lowest low since barssince - Pine Script v4 - pine-script-v4

looking for a help on this issue...I want to get the lowest low since certain condition happened.
With barssince I get the number of bars since the condition happened (I plotted it and it's correct) but when I pass the variable into lowest function, it returns study error.
Here is a code which I hoped would return the lowest value since condition happened but it's not happening.
value_since_last_close = valuewhen((tradeline > tradeline[1]) or (tradeline < tradeline[1]), close, 0)
bars_since = barssince(value_since_last_close)
lowest_low = lowest(low, bars_since)
Many thanks in advance!

Related

Create a TextItem for each product in a spreadsheet and to set the title of this item as "(Name of the product) (remains : (number remaining))"

I want to make a Google Form for online order which would display the number of remaining units for each product, updated every time an order is passed.
As a first step, I try to create a TextItem for each product in a spreadsheet and to set the title of this item as "(Name of the product) (remains : (number remaining))"
var wsStocks = SpreadsheetApp.openById(ssID).getSheetByName("Stocks");
var form = FormApp.openById(formID);
function myFunction(){
var Products = wsStocks.getRange(1,1,wsStocks.getLastRow(),1).getValues();
for(j=1;j<Products .length+1;j++){
form.addTextItem().setTitle(wsStocks.getRange(j,1).getValue().toString()+" (remains: "+wsStocks.getRange(j,2).getValue().toString()+")");
};
}
When I run the code, the correct number of items are created, but the title seems to be attributed randomly: sometimes it is the expected title, sometimes it remains blank ("Question"). If I run several times the code, the distribution of correct and blank titles changes (sometimes the first and fourth are correct, the other ones are blank, sometimes it's just the second...)
I can check with Logger.log that "Products" does contain the list of products' names, and that the expression given as argument to SetTitle is indeed what I expect. I have no clue what's going on : /
Explanation / Issue:
It is not a good practice to use getRange and getValue within a for loop. You can construct the 2D array Products to include both column A and B and then you can just index both columns directly. I also used template literals to simplify your string expression:
var Products = wsStocks.getRange(1,1,wsStocks.getLastRow(),2).getValues();
for(j=0;j<Products.length;j++){
form.addTextItem().setTitle( `${Products[j][0]} (remains: ${Products[j][1]})` );
};
To answer your question, the issue with your current solution is that you are iteratively adding questions and setting their titles. However, this process needs some time to be completed and the for loop is always faster. You just need to slow down the requests.
Unlike SpreadsheetApp, FormApp does not support the flush method. But there is a workaround which you can use to slow down the requests and that is the sleep method. Of course, this approach will overall slow down your algorithm and if you specify a big enough time interval, your script might not be able to finish in time.
You need to choose the time interval wisely. In the following example, I used 4 seconds, but feel free to try a smaller or bigger time interval depending on the number of requests you need to process. For example, if you still see that you are getting wrong titles, increase that number.
Solution / Workaround:
var wsStocks = SpreadsheetApp.openById(ssID).getSheetByName("Stocks");
var form = FormApp.openById(formID);
function myFunction(){
var Products = wsStocks.getRange(1,1,wsStocks.getLastRow(),2).getValues();
for(j=0;j<Products.length;j++){
form.addTextItem().setTitle( `${Products[j][0]} (remains: ${Products[j][1]})` );
Utilities.sleep(4*1000); // 4 second delay
};
}

Why does my Apps Script reference the wrong row?

The Goal
I am trying to create a spreadsheet using some custom functions. The purpose for the sheet is to keep score in a quizzing competition. The top row has the question number, the second row the score, and the third number the number of fouls for that question.
The Problem
After noticing some problems with the score calculation, I was able to narrow the problem down to the part of the code where I add up the fouls that occurred prior to the current question. It seems that no matter what I do, the code sums over the question row, not the foul row.
Attempted Solutions
The extremely strange thing is that no matter what I change the reference cells to, it always produces the same result (i.e. it still references the question row same as it always has).
Example
I'm not sure if that makes any sense, but I've made an example sheet here so you can see what I'm talking about and try to figure out what is going on. Keep in mind that I'm well aware that I could accomplish what I'm trying to do in the example sheet with a simple built-in formula, but since there's no way to use worksheet formulas on the Apps Script side, I have to create my own version of it. I've made the example editable for anyone with the link, so you should have full access to it; but if you have problems, just let me know, and I'll see what I can do to fix it.
In your For loop, you are summing the indexes rather than the values:
Try:
for (var PrevValue in PrevValues[0]) {
Sum = Sum + Number(PrevValues[0][PrevValue]);
}
EDIT:
You'll also need to account for the case where you pass in a single cell rather than a range (=mySum($B4:B4)), because in that case the value is passed directly instead of an array.
if(PrevValues instanceof Array){
for (var PrevValue in PrevValues[0]) {
Sum = Sum + Number(PrevValues[0][PrevValue]);
}
}else
Sum = PrevValues;

cakephp2 return one random record from database

I am experiencing a problem with my find to return a random record.
The thing is, the condition is not working for some reason.
DB:
quotes:
title (varchar255)
content (varchar255)
published (tinyint(1) NULL default = 0)
$random_quotes = $this->Quote->find('all',array('condition'=>array('Quote.published'=>1),'order'=>array('rand()'),'limit'=>1));
It returns 1 quote no mather what published = 0/1. It does not use the condition at all in the find. Have tried a find first as well. still.. published 0/1 does not mather. It returns a record no mather what.
Anyone know why this is happening??
I only have 2 quotes in the db now, and both = published = 0, still the find returns a result.
Thanks for any help on this!!!
-Tom
The condition option should be conditions (plural), otherwise it queries with no conditions.

How to write a simple JQuery integer division function

My webpage displays realtime data that is updated every 10 seconds from our PI server(historian server). I was recently tasked to take two peices of data that are allready displayed on my page and divide them, displaying the new result on a graph. I have very little experience with AJAX/JQuery, so I was wondering if someone out there can lend me a hand?
Clarification:
Hypethetically-
Say I have "Number of Grades" and "Sum of Grades" displayed on my page as basic integers. They are updated every 10 seconds from the server. I want to take those two pieces of live data, divide them, and display the result (in this case it would be the overall average grade). So, my page will display Number of Grades, Sum of Grades, and Average Grade all updated in real time, every 10 seconds.
I am unclear as to whether JQuery can simply take those values off the server and perform division on them and return a value, or if other steps need to be taken to achieve this. I'm completely shooting in the dark here so sorry in advance for any vagueness or lack of required information.Thank you. Some example code is given below:
<span class = 'PIdata' data-tag = 'NumOfGrades' data-on_pi_change = 'NumOfGradeChange'></span>
<span class = 'PIdata' data-tag = 'SumOfGrades' data-on_pi_change = 'SumOfGradeChange'></span>
I want to display a value that divides NumOfGrades by SumOfGrades.
var sumOfGrades = parseFloat($.('#SumOfGradesId').text());
var numberOfGrades = parseFloat($.('#NumberOfGradesId').text());
var result = fn(sumOfGrades , numberOfGrades);
$.('#AverageGradeId').text(result);
This will set the required value.
Simple Maths with jQuery - division
The above link seems to have the answer to your question! Basically just access numbers by their id you set, get the value, parse them into integers or floats, perform your calculation, then store the value into the spot you would like it to appear!

Using logic within an update and returning updated fields using as few queries as possible

I'm writing a video game in javascript on a server that saves info in a mysql database and I am trying to make my first effect attached to simple healing potion item. To implement the effect I call a spells table using spell_id and it gets a field called effect containing the code to execute on my server. I use the eval() function to execute the code in the string. In order to optimize the game I want to run as few queries as possible. For this instance (and I think the answer will help me evaluate other similar effects) I want to update the 'player' table which contains a stat column like 'health' then I want it to add n which will be a decreasing number 15 then 250 ms later add 14 then 13 until that n=1 the net effect is a large jump in health then smaller and smaller accomplishing this is relatively easy if the player's health reaches his maximum allowed limit the effect will stop immediately...
but I'd like to do a single update statement for each increase rather than a select and an update every 250ms to check if health > max_health and make sure the player's health doesn't go above his max health. So to digress a bit I'd like a single update that given this data
player_id health max_health
========= ====== ==========
1 90 100
will add 15 to health unless (max_health-health) < 15... in this case it should only add 10.
An easier solution might be
if I could just return health and max health after each update I update it I don't mind doing a final
pseudo code
if health > max_health
update health set health = max health
So if anyone could explain how to return fields after an update that would help.
Or if anyone could show how to use logic within the update that would also help.
Also, If I didn't give enough information I'm sorry I'd be glad to provide more I just didn't want to make the question hard to understand.
update health
set health = least(max_health, health +<potion effect>)
where player_id = ...
EDIT
For your other question : normally, i think that update returns the number of affected rows. So if you try to update health when health is already = max_health, it should return 0.
I'd know how to do this in php, for example, but just said you where using javascript... so ?
http://dev.mysql.com/doc/refman/5.6/en/update.html
UPDATE returns the number of rows that were actually changed. The
mysql_info() C API function returns the number of rows that were
matched and updated and the number of warnings that occurred during
the UPDATE.
Use the ANSI standard CASE function, or the mysql only least function as in the other answer
UPDATE player
SET health = CASE WHEN health + [potion] > max_health
THEN max_health
ELSE health + [potion]
END CASE
WHERE player_id = [player_id]