barssince function problem - tradingview pine - charts

The following code is working. But the 2nd doesnt work. Why ?
specificTime = dayofweek == dayofweek.monday and hour == 0 and minute == 0 and second == 0
countbar = barssince(specificTime)
plot(countbar)
The following code doesn't work. I don't know why. Please help.
specificTime = dayofweek == dayofweek.sunday and hour == 23 and minute == 59 and second == 59
countbar = barssince(specificTime)
plot(countbar)
And I'd like to ask how to get the number of bars from last Sunday (I mean Sunday[1]) and previous Sunday (Sunday[2]). Thanks for your help.

Related

How do you get "1" as integer in if-else statement rather than as boolean in MATLAB?

I am trying to use MATLAB FUNCTION block in simulink .
The model is shown below
In the "time_calc" Function i want to manipulate the variable "Sector" as shown in the code below
if sector == 1 || 2
sec = 1
elseif sector == 3 || 4
sec = 2
elseif sector == 5||6
sec = 3
elseif sector == 7||8
sec = 4
elseif sector == 9||10
sec = 5
elseif sector == 11 || 12
sec = 6
end
The below is the scope and you can see the values of "sector" changing from 0 through 12 and then repeating itself
But I am getting the value of "sec" as constant "1"(shown below in figure)
(Maybe because it is evaluating the first "1" as boolean true and running that statement only over and over again)
How to correct it ?
if sector == 1 || 2 evaluates sector == 1, if it's true, the statement is true. If it's false, it evaluates 2, which is always true, and so the statement is always true.
What you intended to write was if sector == 1 || sector == 2. You can also write this as if any(sector == [1, 2]).
Your function is equivalent to:
sec=ceil(sector/2)
#Cris Luengo's answer shows why your code is wrong. But I suggest you change the entire thing by this one liner, that is much clearer.
Remove the elseif's and replace them all with just if's

I would like to change candle colors using the offset function with a variable/conditional offset time

Version 5.0. iff_17 determines whether my conditions are met. It then sets the candlecolor and the offset time (pasttime) which is either -4 or 0. However, it seems that pasttime is not recognized as a numerical value (also using int pasttime = iff_17 == 2 ? -4 : 0) does not work Help would be greatly appreciated. Thanks
iff_17 = va1 == 1 and ((_hh or _lh) or (_hl or _ll)) ? 2 : 0
candleColor2 = iff_17 == 2 ? candleColor1 : candleColor
pasttime = iff_17 == 2 ? -4 : 0
barcolor(candleColor2, offset = pasttime)

Flutter/Dart Date Set firstDayOfTheweek as Sunday

By default, Monday is set as the first day of the week. The middle east region, Sunday is considered the first day of the week.
var now = DateTime.now();
print("todays date is $now");
print("week day is: ${now.weekday}");
// todays date is 2020-09-13 12:20:02.417210
// week day is: 7
since today is Sunday, I want the now.weekday should return as 1.
DateTime.weekday returns an integer in the range 1..7 that corresponds to the DateTime constants of monday, tuesday, etc. The constants are assigned internal values with Monday as the smallest value, but whether you actually treat Monday as the first day of the week is completely up to you.
But if it's easier for you to have weekday values assigned where the value for Sunday is less than the value for Monday, you could just do:
int getAdjustedWeekday(DateTime dateTime) => dateTime.weekday % 7;
And now getAdjustedWeekday will return 0 for Sunday, 1 for Monday, and so on.
If you need values starting from 1 instead of 0, then just add 1:
int getAdjustedWeekday(DateTime dateTime) => 1 + dateTime.weekday % 7;
Note that you would not be able to use the existing DateTime.sunday, DateTime.monday, etc. constants.
You need to convert like below.
var now = DateTime.now();
final now1 = DateFormat("EEEE").format(DateTime.parse(now.toString()));
print("todays date is $now1");
Or if you need int of day then go like below
Declare a day
final day = now1 == "Sunday"
? 1
: now1 == "Monday"
? 2
: now1 == "Tuesday"
? 3
: now1 == "Wednesday"
? 4
: now1 == "Thursday" ? 5 : now1 == "Friday" ? 6 : 7;
And call from widget.
print(day);

Is this a weird date calculation bug in groovy/java?

I'm trying to calculate the number of days between two specific pairs of dates but the assert is failing on the second test, which is only a week further apart from the first test.
The code is below.
Is there a bug in my code? Or is this a weird java/groovy bug?
use(groovy.time.TimeCategory) {
def duration = Date.parse("yyyy-MM-dd", "2013-03-10") - Date.parse("yyyy-MM-dd", "2012-12-30")
assert duration.days == 70
def duration2 = Date.parse("yyyy-MM-dd", "2013-03-17") - Date.parse("yyyy-MM-dd", "2012-12-30")
assert duration2.days == 77
}
#Damien_The_Unbeliever had it right. Since EST was being used, around March, it is switched to EDT which is 1 hour ahead (so converting 2013-03-17 EDT to EST would mean it would lose one hour.)
I've changed the test to confirm that this is true. The second and third asserts pass.
use(groovy.time.TimeCategory) {
def duration = Date.parse("yyyy-MM-dd", "2013-03-10") - Date.parse("yyyy-MM-dd", "2012-12-30")
assert duration.days == 70
def duration2 = Date.parse("yyyy-MM-dd", "2013-03-17") - Date.parse("yyyy-MM-dd", "2012-12-30")
assert duration2.days == 76
assert duration2.hours == 23
}

NSDate Units and Calculations

I'm working on a project that needs to check the time difference from a first launch date. I'm using the NSDayCalendarUnit and NSWeekCalendarUnit. Basically for the first 2 weeks on every second day I want to perform something. The object I'm using needs to be in a certain state for each two days at a time.
So for example
Day 1 & 2, Week 0 - State 1
Day 3 & 4, Week 0 - State 2
...
Day 1 & 2, Week 1 - State 8
Day 3 & 4, Week 1 - State 9
Here is my code:
// get the data/time difference from the first launch
int daysDifferent = [[dateDifferenceInfo objectForKey:#"days"] intValue];
int weeksDifferent = [[dateDifferenceInfo objectForKey:#"weeks"] intValue];
if(daysDifferent == 2 | daysDifferent == 3 && !weeksDifferent && _dot.age != 2){
// set state
}
if(daysDifferent == 4 | daysDifferent == 5 && !weeksDifferent && _dot.age != 3){
// set state
}
if((daysDifferent == 6 && weeksDifferent == 0 | daysDifferent == 0 && weeksDifferent == 1) && _dot.age != 4){
// set state
}
if(daysDifferent == 0 | daysDifferent == 1 && weeksDifferent == 1 && _dot.age != 5){
// set state
}
if(daysDifferent == 2 | daysDifferent == 3 && weeksDifferent == 1 && _dot.age != 6){
// set state
}
if(daysDifferent == 4| daysDifferent == 5 && weeksDifferent == 1 && _dot.age != 7){
// set state
}
if((daysDifferent == 6 && weeksDifferent == 1 | daysDifferent == 0 && weeksDifferent == 2) && _dot.age != 8){
// set state
}
if(weeksDifferent >= 2 && !(daysDifferent % 2)){
// set state
}
Side note: I know it's bad code, I plan to replace this with switch cases, I just need to logic sorted first.
My question is is there a better way of calculating this kind of pattern?
I'm not totally clear on your goal, as you say:
for the first 2 weeks on every second day I want to perform something.
So for example
Day 1 & 2, Week 0 - State 1
Day 3 & 4, Week 0 - State 2
...
Day 1 & 2, Week 1 - State 8
Day 3 & 4, Week 1 - State 9
Where I would have thought it would be
Day 1 & 2, Week 0 - State 1
Day 3 & 4, Week 0 - State 2
Day 5 & 6, Week 0 - State 3
Day 7 Week 0 & Day 1 Week 1 - State 4.
...
So if your example is right, the calculation is different.
Based on the simpler example, I would calculate the time interval since the first date, and divide by twice the length of a day, taking the floor. That is the state:
NSDate *firstDate = ...
NSDate *secondDate = ...
NSTimeInterval interval = [ secondDate timeIntervalSinceDate: firstDate ];
double secondsPerDay = 60 * 60 * 24.0; // s per min times min per hr times hrs per dy.
int state = (int) (interval / secondsPerDay * 0.5 ) + 1;
If, on the other hand, it really is as you have described, the approach I would use as fast and simple would be to do a similar calculation - leaving out the 0.5 factor in the last line, and then use a simple table lookup to get the state from the day:
int table[ 14 ] = { 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 7, 8 };
int state = table[ (int) (interval / secondsPerDay ) ];