I have a following condition logic and want to make it simple, is there a way to make it better without too much || && ? Some sample or example will be lovely! I would love to hear from you!
Logic that I want to achieve
From 0:00 on and 3 days before the birth month,
condition is the birth month is once or January 31st and
the present is run every December 29-31
val birthYear = child.birthYear!!.toInt()
val birthMonth = child.birthMonth!!.toInt()
val date = DateTime()
val year = date.year
val month = date.month
val day = date.day
val maxDayOfMonth = date.maxDayOfMonth
class DateTime {
private var mCalendar: Calendar = Calendar.getInstance().also { it.clear() }
val year: Int
get() = mCalendar.get(Calendar.YEAR)。。。。。
The following is the logic that I want to make it simpler
return date.year > child.birthYear!!.toInt() &&
((birthMonth == 1 && month == 12 || birthMonth - 1 == month)
&& day >= maxDayOfMonth - 2 || birthMonth == month)
Related
I need to find the first working day of the month. Ideally it should be 01/MM/YYYY but the condition is that it should not be a Saturday or Sunday or a day from the List of Holidays.
List being:
ListOfHoliday
Name Day
Someday 01/01/2021
Someday 01/02/2021
Someday 31/03/2021
Someday 04/07/2021
Someday 25/12/2021
I was trying to use the below algo:
FWD = Text.concat(01, DateTime.CurrentMonthOfToday, DateTime.CurrentyearOfToday)
foreach(holiday in ListOfHoliday)
{
if (FWD == holiday)
{
DateTime.AddDays(ConvertTo.DateTime(FWD), 1)
if (ConvertTo.DateTime(FWD).DayOftheWeek == Saturday)
{
FWD=ConvertTo.Text(DateTime.AddDays(ConvertTo.DateTime(FWD), 2));
} else
{
if ((ConvertTo.DateTime(FWD).DayOftheWeek == Sunday))
{
FWD=ConvertTo.Text(DateTime.AddDays(ConvertTo.DateTime(FWD), 2));
}
}
}
}
But the result I get for the current month is 01/01/0001.
Here is the python code to find the first working day of a given month.
from datetime import datetime, timedelta
def working_day(temp,holid):
if temp.strftime('%A') == 'Saturday':
temp += timedelta(2)
return working_day(temp,holid)
elif temp.strftime('%A') == 'Sunday' or temp in holid:
temp += timedelta(1)
return working_day(temp,holid)
else:
return temp.strftime('%A')
date_format = "%d/%m/%Y"
holidays = ['01/01/2021','01/02/2021','31/03/2021','04/07/2021','25/12/2021']
holid = []
for day in holidays:
holid.append(datetime.strptime(day, date_format))
month = int(input('Enter the month')) # input should be 1,2,3....12
today ='01/'+str(month)+'/2021'
today = datetime.strptime(today, date_format)
print(working_day(today, holid))
output:
Enter the month 12
Wednesday
I'm working with an IT department that wants to see their trend for submitted and closed tickets MTD vs Previous Month, but they want to account for the number of working days so far this month, and compare to the amount of tickets in the same number of working days the previous month. I have a date table with a column marking work days, and another to count the working day in each month.
This is what I had before to find the +/- compared to the previous month, but comparing the current date to the same day last month:
IF(
TOTALMTD(CALCULATE(COUNTA(' Ticket'[Name]),
'Ticket'[CreatedDateID]<>BLANK(), USERELATIONSHIP('Ticket'[CreatedDateID],'Date'[DateID]) ),
'Date'[Date])
>
CALCULATE(COUNTA('Ticket'[Name]),
AND('Date'[Date]>=DATE(YEAR(TODAY()), MONTH(TODAY())-1, 1), 'Date'[Date]<=DATE(YEAR(TODAY()), MONTH(TODAY())-1, DAY(TODAY()))),
USERELATIONSHIP('Ticket'[CreatedDateID],'Date'[DateID]) )
,
"+" & TOTALMTD(CALCULATE(COUNTA('Ticket'[Name]),
'Ticket'[CreatedDateID]<>BLANK(), USERELATIONSHIP(' Ticket'[CreatedDateID],'Date'[DateID]) ), 'Date'[Date])
-
CALCULATE(COUNTA(' Ticket'[Name]),
AND('Date'[Date]>=DATE(YEAR(TODAY()), MONTH(TODAY())-1, 1), 'Date'[Date]<=DATE(YEAR(TODAY()),MONTH(TODAY())-1,DAY(TODAY()))),
USERELATIONSHIP('Ticket'[CreatedDateID],'Date'[DateID]) )
,
TOTALMTD(CALCULATE(COUNTA('LoanForce Ticket'[Name]),
'LoanForce Ticket'[CreatedDateID]<>BLANK(), USERELATIONSHIP('LoanForce Ticket'[CreatedDateID],'Date'[DateID]) ), 'Date'[Date])
-
CALCULATE(COUNTA('LoanForce Ticket'[Name]),
AND('Date'[Date]>=DATE(YEAR(TODAY()), MONTH(TODAY())-1, 1), 'Date'[Date]<=DATE(YEAR(TODAY()),MONTH(TODAY())-1,DAY(TODAY()))),
USERELATIONSHIP('LoanForce Ticket'[CreatedDateID],'Date'[DateID]) )
)
Sample of Date Table
Can anyone assist in a measure that will take total tickets created this month and compare it to the tickets created previous month in the same amount of work days?
Where is the problem? As you say, you have "Working Days MTD" then we should use it.
Measure =
var __CurrentMonthWorkDaysPassed = calculate( MAX(Table[Working Days MTD]), FILTER(ALL(Table[Date]), Table[Date] >= DATE(year(TODAY()),MONTH(TODAY()),1) && Table[Date] <= TODAY()))
var __TicketInCurrentMonth = calculate(count(Table[ColumnToCount]), FILTER(ALL(Table), Table[Working Days MTD] <= __CurrentMonthWorkDaysPassed &&
Table[Date] >= DATE(year(TODAY()),MONTH(TODAY()),1) && Table[Date] <= TODAY() && Table[Is Working day] = 1 ))
var __TicketInPreviousMonth = calculate(count(Table[ColumnToCount]), FILTER(ALL(Table), Table[Working Days MTD] <= __CurrentMonthWorkDaysPassed &&
Table[Date] >= DATE(year(TODAY()),MONTH(TODAY()) -1 ,1) && Table[Date] < DATE(year(TODAY()),MONTH(TODAY()) ,1) && Table[Is Working day] = 1 ))
return
__TicketInCurrentMonth / __TicketInPreviousMonth
I'd like to make a very basic strategy for intraday momentum.
However, I'm stuck wondering: is it possible to use hour as a trading condition?
For instance:
strategy("Strat - Intraday Momentum", overlay=true)
market_open = hour == 9
market_open_hour = hour == 10
market_close = hour == 15
if (market_open)
strategy.entry("Open", strategy.long)
if (market_open_hour)
strategy.entry("Open +1 Hour", strategy.long)
strategy.close_all(when= hour == market_close)
Seems simple enough but so far haven't been able to make it work.
Was playing off this idea:
// Simple Example Code
// Designed to play around with Tradingview's close, close_all and exit functions
strategy("Close Example", overlay=true, pyramiding=2)
monCondition = dayofweek == dayofweek.monday
wedCondition = dayofweek == dayofweek.wednesday
if (monCondition)
strategy.entry("Monday", strategy.long)
if (wedCondition)
strategy.entry("Wednesday", strategy.long)
strategy.close_all(when=dayofweek == dayofweek.friday)
Source: https://backtest-rookies.com/2019/03/01/tradingview-strategy-close-strategy-close_all-vs-strategy-exit/
It's possible for sure, but your code's robustness should be improved. Would be preferable to build conditions on transitions. Example 2 is from the usrman:
//#version=4
study("Time begin", "", true)
// #1
timeOpen1 = hour == 9
timeOpenOk1 = not timeOpen1[1] and timeOpen1
plotchar(timeOpenOk1, "timeOpenOk1", "", location.abovebar, text = "▲")
plotchar(timeOpen1, "timeOpen1", "", location.belowbar, text = "•")
// #2
sessSpec2 = input("0900-0959", type=input.session) + ":1234567"
is_newbar(res, sess) =>
t = time(res, sess)
na(t[1]) and not na(t) or t[1] < t
timeOpenOk2 = timeframe.isintraday and is_newbar("1440", sessSpec2)
plotchar(timeOpenOk2, "timeOpenOk2", "", location.abovebar, color.orange, text = "▲\n")
I am working on chronic care health project in which i have to set future dates for patient injection. The doctor select start date for injection and then he select 3 days (i.e. Monday, Wednesday, Friday etc) which means patient will come 3 times a week.
The total injection are 20 that are divided by 3 times a week.
The problem that i am facing is that the parse function gives incorrect result if doctor select a day that matches today.
Lets suppose today is Wednesday and date is 2019-02-06 and doctor select 3 days that are:
Friday
Monday
Wednesday
Output should be:
- 2019-02-08
- 2019-02-11
- 2019-02-13
My code for controller:
if( $visitstart_date != null)
$startDate = Carbon::parse($visitstart_date);
else $startDate = Carbon::now();
if($perweek_visit1_day != '')
{ //Get date of selected day
$perweek_visit1_dayDate = $startDate->parse($perweek_visit1_day);
}
if($perweek_visit2_day != '')
{ //Get date of selected day
$perweek_visit2_dayDate = $startDate->parse($perweek_visit2_day);
}
if($perweek_visit3_day != '')
{ //Get date of selected day
$perweek_visit3_dayDate = $startDate->parse($perweek_visit3_day);
}
But when dump the 3 values it provide me :
2019-02-08
2019-02-11
2019-02-06 (incorrect)
For testing purpose I also parse the next visit date with each other like:
if( $visitstart_date != null)
$startDate = Carbon::parse($visitstart_date);
else $startDate = Carbon::now();
if($perweek_visit1_day != '')
{ //Get date of selected day
$perweek_visit1_dayDate = $startDate->parse($perweek_visit1_day);
}
if($perweek_visit2_day != '')
{ //Get date of selected day
$perweek_visit2_dayDate = $perweek_visit1_dayDate ->parse($perweek_visit2_day);
}
if($perweek_visit3_day != '')
{ //Get date of selected day
$perweek_visit3_dayDate = $perweek_visit2_dayDate ->parse($perweek_visit3_day);
}
but it again provided me the same output as :
- 2019-02-08
- 2019-02-11
- 2019-02-06
I don't know where i am going wrong, any support or help in this regard will be highly appreciated.
Thanks,
I have 2 tables:
'AllowedDates'
- DayID int PK
- Day datetime
'AllowedTimes'
- TimeID int PK
- DayID int FK
- Hour int
- Minute int
also I have table 'Users':
- ID int PK
- FirstName nvarchar(max)
...
and table 'UserDeniedTimes':
DayID int FK
UserID int FK
Hour int
Minute int
I need to select users, which don't have deny time (record in UserDeniedTimes) for concrete DayID/Hour/Minute
I try to do the following:
var result = from i in _dbContext.Users
where i.UserDeniedTimes.All(
p => (!p.AllowedDate.AllowedTimes.Any(
a1 => a1.DayID == aTime.DayID
&& a1.Hour == aTime.Hour
&& a1.Minute == aTime.Minute
))
)
select new ...
it works correctly, but with one exception. If user has record in UserDeniedTimes for some day, but another time, this user is not selected too. For example, UserDeniedTimes has record:
DayID = 10
UserID = 20
Hour = 14
Minute = 30
this user will be not selected if aTime has the following values:
DayID = 10
Hour = 9
Minute = 30
but will be selected if DayID = 11. Why?
[ADDED]
it works correctly when I limit only by day:
var result = from i in _dbContext.Users
where i.UserDeniedTimes.All(
p => (!p.AllowedDate.AllowedTimes.Any(
a1 => a1.DayID == aTime.DayID
))
)
select new ...
but not works when I write:
var result = from i in _dbContext.Users
where i.UserDeniedTimes.All(
p => (!p.AllowedDate.AllowedTimes.Any(
a1 => a1.Hour == 14
))
)
select new ...
why? What is difference between magic DayID and Hour ?
[ADDED #2]
((time == null) || i.UserDeniedTimes.All(p =>
//p.AllowedDate.AllowedTimes.Any(a1 => a1.DayID != 33) &&
(p.AllowedDate.AllowedTimes.Any(a2 => a2.Hour != 14)
))) &&
does not work
((time == null) || i.UserDeniedTimes.All(p =>
p.AllowedDate.AllowedTimes.Any(a1 => a1.DayID != 33) &&
//(p.AllowedDate.AllowedTimes.Any(a2 => a2.Hour != 14)
))) &&
works
why?
Sometimes it helps to rephrase the problem: if I read you well, you don't want users, that have a deny time with at least one specified DayID/Hour/Minute:
where !i.UserDeniedTimes.Any(
p => (p.AllowedDate.AllowedTimes.Any(
a1 => a1.DayID == aTime.DayID
&& a1.Hour == aTime.Hour
&& a1.Minute == aTime.Minute
))
)
This should select the users you want. If not, please tell in some more words what exactly you
are trying to achieve.