count how many times a conditional event occurs - mql4

I'm trying to count how many times a conditional event occurs.
But when I test it, it gives me unrealistic numbers.
I don't think I am coding it correctly anybody want to help me out??
for(i=0;i<Bars; i++)
{
//Condition I want met
if(Close[i+1]<Close[i+2]<Close[i+3]<Close[i+4]<Close[i+5]);
//Add 1 each time condition is met for sum total when the for loop ends
x=x+1;
Comment(x);
}

the ; of the if statement effectively terminates the conditional statements (so the x=x+1 will not be executed correctly according to the condition(s).
Hence , please change
if(Close[i+1]<Close[i+2]<Close[i+3]<Close[i+4]<Close[i+5]);
//Add 1 each time condition is met for sum total when the for loop ends
x=x+1;
to
if(Close[i+1]<Close[i+2]<Close[i+3]<Close[i+4]<Close[i+5])
{
//Add 1 each time condition is met for sum total when the for loop ends
x=x+1;}

Related

How to use a Lambda function with Reduce to repeat row N numbre f time in google sheets

Hello I've tried using text manipulation to achieve the results, and while it works - I don't think it's an efficient way to do it and there is limitations with how many times it can be done.
I was trying to figure out how to get it done with reduce but it having hard time to figure it out.
This is the current table
Unique ID
Some other Info
How many times to repeat
123
Some Info
2
456
Some Info
3
The result would be
Unique ID
123
123
456
456
456
Thank you.
Here's one way to do this:
=ArrayFormula(REDUCE("Unique ID",SEQUENCE(COUNTA(A2:A)),LAMBDA(a,c,{a;IF(SEQUENCE(INDEX(C2:C,c)),INDEX(A2:A,c))})))
Explanation
The LAMBDA inside REDUCE works by taking 3 parameters: an accumulator (a), a current value (c) and the operation to perform using them.
The accumulator (a) is initialized to the first argument of REDUCE, which is "Unique ID" and every time the inner LAMBDA is executed, the accumulator updates with the result of that execution.
The current value (c) is a variable parameter and it takes on all the values provided in the second argument of REDUCE SEQUENCE(COUNTA(A2:A)) (1).
Let's assume (1) returns:
1
2
The main work happens here:
{a;IF(SEQUENCE(INDEX(C2:C,c)),INDEX(A2:A,c))} (2)
Before this piece of code is executed, a has a value of "Unique ID" and c has a value of 1.
When it executes for the first time, a and c are replaced with their initial value, so we get:
{"Unique ID";IF(SEQUENCE(INDEX(C2:C,1)),INDEX(A2:A,1))}
Now c becomes 2 and a becomes
{"Unique ID";IF(SEQUENCE(INDEX(C2:C,1)),INDEX(A2:A,1))}
So when (2) is executed for the second time, this is what we get:
{{"Unique ID";IF(SEQUENCE(INDEX(C2:C,1)),INDEX(A2:A,1))};
IF(SEQUENCE(INDEX(C2:C,2)),INDEX(A2:A,2))}
We have now gone through all the values of c so the formula stops executing and that's effectively what it returns.
The amount of iterations REDUCE does depends on the size of its second parameter.
Let's see another example. Assume (1) returns:
1
2
3
First time c=1, a="Unique ID":
{"Unique ID";IF(SEQUENCE(INDEX(C2:C,1)),INDEX(A2:A,1))}
Second time c=2, a=PREVIOUSLY_RETURNED_ARRAY:
{{"Unique ID";IF(SEQUENCE(INDEX(C2:C,1)),INDEX(A2:A,1))};
IF(SEQUENCE(INDEX(C2:C,2)),INDEX(A2:A,2))}
Third and last time c=3, a=PREVIOUSLY_RETURNED_ARRAY:
{{{"Unique ID";IF(SEQUENCE(INDEX(C2:C,1)),INDEX(A2:A,1))};
IF(SEQUENCE(INDEX(C2:C,2)),INDEX(A2:A,2))};
IF(SEQUENCE(INDEX(C2:C,3)),INDEX(A2:A,3))}
And that's the array REDUCE returns.
Do you see a pattern?
A different approach could be-
=QUERY(FLATTEN(INDEX(SPLIT(REPT(A2:A3&"|",C2:C3),"|"))),"where Col1 is not null")

Google Sheets; Finding the first cell to contain 0 in a column where the row number is > the row number generated in another formula

I'm making a document in Google sheets that produces the start and end times where a number of people are in a section. I was able to produce a grid, but I can't seem to find a formula for calculating end times that occur after the start time.
Time
Section1
Section2
9:00am
1
0
9:30am
1
0
10:00am
1
1
10:30am
0
1
I want to make a series of formulas that list the start and end times when people are present in a section.
(Start time) Find the first cell to not contain 0 and return the value in column A {I have found a formula for this step}
(End time) Find the first cell to contain 0 where the row number is greater than the row number of the previous start time and return the value in column A
For the first part, I can find the start time with the formula
=IFNA(INDEX($A$2:$A,(MATCH(TRUE,INDEX($B$2:$B>0,0),0))))
But when I try to adapt it to the end time by changing the greater than (>) to equals (=), it gives me the first time there is a zero regardless of if it occurs before the start time. I feel like adding some kind of row check will be necessary to fix this, but I'm not sure how. I would really rather not learn Google API if at all possible,
If it's just to find the first 0 after a 1, you could benefit from the use of INDEX and MATCH to create a range that goes from the first 1 to the last row and look the 0 in that range. That is: INDEX(B2:B,MATCH(1,B2:B,0)):INDEX(B2:B,ROWS(B2:B)) So, inserted in the whole formula:
=INDEX(A2:A,MATCH(1,B2:B,0) + MATCH(0, INDEX(B2:B,MATCH(1,B2:B,0)):INDEX(B2:B,ROWS(B2:B)) ,0)-1)

To make endless row on google spreadsheet

Very simple question.
How do you make endless row on spreadsheet? Like Excel.
I have this problem when I use google sheet to scan barcodes.
When rows reached 1000, I need to add more manually.
But sometimes I forget, then I keep scanning.
After that I check my sheet, I missed a lot of input but I don't remember which barcode was the last one, so I have to do them all over again after increasing the rows.
If google sheets has the infinite rows like Excel, I won't have to worry about it no more.
Do you guys have any solutions on this?
Use Apps Script to Make Your Sheet Dynamic
With Apps Script, you can write a function to detect how many cells are between the data inserted and the end of the spreadsheet, and add rows if they are too close.
function addRowsIfCloseToEnd() {
let file = SpreadsheetApp.getActive();
// INPUT YOUR SHEET NAME HERE
let sheet = file.getSheetByName("Sheet1");
let maxRow = sheet.getMaxRows()
let lastRow = sheet.getLastRow()
// In this example, when the values are 100 rows
// from the end of the sheet, it will add 100 rows
// to the end. Change this to your liking.
if (maxRow - lastRow < 100) {
sheet.insertRowsAfter(maxRow, 100)
}
}
In this example, the function checks when values are less than 100 rows from the end of the spreadsheet, and if it is, it will add 100 extra rows to the sheet.
You should adjust these numbers to suit your workflow, I don't know how many bar codes you scan or how quickly.
You have two options for how to trigger this:
onEdit
This is a simple trigger designed to run a function every single time there is an edit on the sheet. You can call the previous function like this:
function onEdit() {
addRowsIfCloseToEnd()
}
If you have authorized your script, then this should run every time you make an edit:
In this example I only add 10 rows every time, to demonstrate.
Time-based trigger
Depending on how many barcodes you scan and how quickly, you may not want this function to run every single time you scan a barcode, in which case you can make a trigger to run every 5 minutes for example:
function createClockTrigger() {
ScriptApp.newTrigger("addRowsIfCloseToEnd")
.timeBased()
.everyMinutes(5)
.create();
}
References
Apps Script
Tutorials
Simple Triggers
onEdit
ClockTriggerBuilder

Need help nesting multiple formulas

I have three functions that will return the answer that I'm looking for but I don't want to have these functions separate because the sheet would get too cluttered. That said, I'm having difficulties nesting these formulas so that it returns only the final output. The formulas are listed below:
=UNIQUE(FILTER('Sheet'!$D:$D, 'Sheet'!$B:$B >= B$2,'Sheet'!$B:$B<C$2,regexmatch('Sheet'!$L:$L,"Trial")))
This function returns all unique ID's that meet the conditions stated.
=COUNTIFS('Sheet'!$B:$B,">="&C$2,'Sheet'!$B:$B,"<"&D$2,'Sheet'!$G:$G,">0",'Sheet'!$D:$D,B27)>0
This function returns T/F if the identified unique ID from first function exists next month. Returns 'True' if ID exists, 'False" if it does not. Cell B27 refers to the first cell row from the first function.
=COUNTIF(C27:C45,TRUE)
This function counts all True for each month. Range (C27:C45) references the output from the second function
I tried
=COUNTIF(countifs('Sheet'!$B:$B,">="&C$2,'Sheet'!$B:$B,"<"&D$2,'Sheet'!$G:$G,">0",'Sheet'!$D:$D,UNIQUE(FILTER('Sheet'!$D:$D, 'Sheet'!$B:$B >= B$2,'Sheet'!$B:$B<C$2,regexmatch('Sheet'!$L:$L,"Trial"))))>0,TRUE)
but this function returns the incorrect answer.
Any ideas? Here's the sheet: https://docs.google.com/spreadsheets/d/1l2dXCEE0enTRBzBZEwjN1Fj9-ovPjsi75WvTdgK7_Zg/edit?usp=sharing
use:
=COUNTUNIQUE(IFNA(FILTER(sheet!$D:$D, sheet!$B:$B >= B$2, sheet!$B:$B < C$2,
REGEXMATCH(sheet!$L:$L, "Trial"), sheet!$G:$G > 0)))
SOLVED: Had to use =arrayformula nested over the three functions.
Column B shows the number of new subscribers.
Column C:V shows how many of the specified subscribers from ColB is retained over period of time.

Looping a different number of times (determined by value in data) for each line

I have to create a loop, but the size of this loop have to change according the cell value. But I don't know how to reference a specific cell.
Example - my dataset has 2 rows:
Id value
1 10
2 20
For the first row I have to run this loop 10 times, but for the second row I have to run 20 times.
How I can do this?
How about this:
loop # = 1 to 1000. /*use the maximum number or runs necessary.
do if # <= value.
some transformations.
end if.
end loop.
This will run the transformations on all cases every time, but will stop when the number of required runs for each line is reached.

Resources