Plotly: highlight regular trading market hours - dataframe

Goal: highlight regular trading market hours in a plotly chart.
-Using a df with standard datetime and 1 minute intervals.
-Regular trading hours = 9:30am EST to 4pm EST
—-Incase interested:
——-pre market = 4am to 9:30am
——-post market = 4pm to 8pm
Stack overflow has great links for highlighting weekend data like this:
Nevermind that link was just removed by the author as I tried to post it, but it’s too difficult for me to translate that to specific times of day anyway.

This is relatively easy to do using fig.add_vrect()
I built a similar highlighting system for night and day:
time = df.index.get_level_values("time")
# Getting info for plotting the day/night indicator
# time[0].date() picks out 00:00 (midnight) and then we add 6 hours to get 6 am.
start_morning = pd.to_datetime(time[0].date()) + pd.Timedelta(
hours=6
)
end_morning = pd.to_datetime(time[-1].date()) + pd.Timedelta(
hours=30
)
num_mornings = (end_morning - start_morning).days
# Now we build up the morning times, every day at 6 am
mornings = [
start_morning + timedelta(days=x) for x in range(num_mornings)
]
for morning in mornings:
fig.add_vrect(
# Highlighted region starts at 6 am and ends at 6 pm, every day.
x0=morning,
x1=morning + timedelta(hours=12),
fillcolor="white",
opacity=0.1,
line_width=0,
)
For you, it would just be a simple matter of adjusting the times. So for instance, for 9:30 am you can use
morning = pd.to_datetime(time[0].date()) + pd.Timedelta(hours=9.5)
to get the first day of your data, at 9:30 am. Now in fig.add_vrect() use
x0= morning
x1= morning + timedelta(hours=6.5)
to highlight between 9:30 am and 4 pm.

Related

Calculating datetime around weekends and evenings

SSMS 2017
I'm attempting to calculate turnaround time between a file upload and file completion date, not including time after work or on weekends
I'm trying to do this with a case statement.
case
when datepart(dw,d.sduploadeddate) in (2, 3, 4, 5) and datepart(hh,d.sduploadeddate) > 16 then d.sduploadeddate+1
when datepart(dw,d.sduploadeddate) in (6) and datepart(hh,d.sduploadeddate) > 17 then d.sduploadeddate+3
when datepart(dw,d.sduploadeddate) in (7) then d.sduploadeddate+2
when datepart(dw,d.sduploadeddate) in (8) then d.sduploadeddate+1
else d.sduploadeddate
end [Uploadeddatetime2]
The above code is working without error, but its not solving the problem obviously - its just cutting down slightly on the turnaround time. I was just tinkering with a method to get a slightly more accurate turnaround time.
The working hours are 8 --> 1700, Monday -- Friday
Ideally I'd be able to say like if this date time is past 1700, then date+1 and hours = 800 (so Thursday at 1850, change it to Friday at 800). Similarly, Friday (or any weekday) before time 800 adjust to 800 of that same day.
For Friday and weekend days the same thing, but date +1,2 or 3 depending on the day and setting it up for 800 on Monday

Schedule a task to run two times a day between two date

I use SQL Server 2017. I need to schedule a job to run at 12am and 12pm for each days between 7th and 27th of each months. In the other word, i need to run my job two times per day between two date in each month.
Can i do that in one schedule task or I have to create a two jobs for each exact day?
12 am 7th month
12 pm 7th month
and so on and so on.
If i have to create a job for each hover of each day i will have several schedule.
Update 1: I did it by creating several steps in schedule tab but i am looking to do that in less steps.
Update 2:
If i can create two steps like below it will good for me.
1 : Occurs every month between 7th and 27th at 12 am
2 : Occurs every month between 7th and 27th at 12 pm
One way to do that is to check if the date is in between 7th and 27th using DATENAME or DATEPART.
--IF (DATENAME(DAY, GETDATE()) >= 7 AND DATENAME(DAY, GETDATE()) <= 27)
IF DATEPART(DD, GETDATE()) BETWEEN 7 AND 27
BEGIN
EXEC [Your Stored Procedure]
END
And then set the Daily frequency to start at 12:00 AM and Occurs every 12 hours.

SQL Date and TIme

I need to take the list of students with Application acceptance Date & time in University database. The report is sent on 6 PM evening to the management but the management can run it on 7 PM 8 PM or any time within the night. The application should be just 2 days ahead to the report running date.
ORACLE: I have created a query but that will give the application of entire 24 hours of a day. The problem with this query is when management runs a query on 6 PM and 8 PM , and any students are accepted in between this time. The result will be different.
select
to_char(application_accepted_date, 'DD-MON-RR:HH24:MI:SS')
from
tbl_application_accepted_date
where
to_date(application_accepted_date, 'DD-MON-rr:HH24:MI:SS') =
to_date(trunc(sysdate-2), 'DD-MON-rr:HH24:MI:SS')
;
If any application is accepted between 6 ahead I need the list in next days report. Means, I should have the list of accepted students from 6PM onwards of previous day to 5:59 application day.
I am getting application accepted after 6 PM, which I don't need in todays report, I should get this on next day report. The report is run on 4th OCT 2018
If it needs to cut off at 6pm, then just add 18/24 (18 hours) to the truncated date. You are also doing a lot of unnecessary casting. As long as application_accepted_date is a date field, then you can just compare it as a date.
select to_char(application_accepted_date, 'DD-MON-RR:HH24:MI:SS')
from tbl_application_accepted_date
where application_accepted_date >= trunc(sysdate-3)+18/24
and application_accepted_date < trunc(sysdate-2)+18/24
This will give you any applications starting at 6pm 3 days ago until just before 6pm 2 days ago.
EDIT: You could also do this with interval literals if you want. Same query as above, but more explicit in your intentions.
select to_char(application_accepted_date, 'DD-MON-RR:HH24:MI:SS')
from tbl_application_accepted_date
where application_accepted_date >= trunc(sysdate-3) + interval '18' hour
and application_accepted_date < trunc(sysdate-2) + interval '18' hour
Assuming that application_accepted_date is a DATE datatype in your data model, try this:
select
to_char(application_accepted_date, 'DD-MON-RR:HH24:MI:SS')
from
tbl_application_accepted_date
WHERE application_accepted_date BETWEEN to_date(To_char(Trunc(SYSDATE - 1),'YYYY-MM-DD')
||'18:00:00','YYYY-MM-DD HH24:MI:SS) and to_date(to_char(trunc(sysdate),'yyyy-mm-dd')||'17:59:59','yyyy-mm-dd hh24:MI:SS)
That should always return data from 6:00pm yesterday to 5:59:59pm today.

Subtracting two dates (including hours and minutes) considering only working time vba or Excel

I need to subtract two dates (including hours and minutes), but I only need to consider working hours. That is, I need to omit lunch time (from 13 to 14 hrs), weekends and hours after 18 hrs and before 9 hrs of the following day, in a working day (from Mo to Fr). Any thoughts?
I don't mind if it's an Excel formula or a vba code.
I have this formula, but it doesn't omit lunch time:
9*(NETWORKDAYS(initial_time;ending_time)-1)-24*((MOD(initial_time;1)-MOD(ending_‌​time;1)))
Here's a possible solution. It assumes an 8 hour work days for all but the start and end date. Also that start date/time is 9:00 or after and end date/time is 18:00 or earlier and that both are on a weekday.
=(NETWORKDAYS(A2,B2)-2)*8+IF(MOD(A2,1)>0.58333,(TIME(18,0,0)-MOD(A2,1))*24,(TIME(18,0,0)-MOD(A2,1))*24-1)+IF(MOD(B2,1)>0.58333,(MOD(B2,1)-TIME(9,0,0))*24-1,(MOD(B2,1)-TIME(9,0,0))*24)
.58333 equates to 14:00. The formula:
multiplies networkdays * 8
+ hours from start date/time until 18:00 subtracting 1 hour if start time is before 14:00
+ hours from 9:00 until end date/time subtracting 1 hour if end time is after 14:00
Of course this doesn't take any holidays into account.

How to add working hours depending on weekday in sql

I want to create a basic case logging system and when somebody opens a new issue, the issue is assigned a Sr_number with a given number of hours. For example Sr_number 1 is 4 hours, 2 is 6 hours, 3 is 8 hours and 4 is 24 hours.
Now adding hours onto a time stamp is easy but the catch is I need to take into account working hours which are 09:00 to 17:00 Monday to Friday.So if a case is given a 12 hour Sr_number and the deadline for this falls at 16:00 on a week day then the deadline is extended to the next working day. Basically the deadline is 12 working hours.And calculation should be 1 hour worked for the issue logged on same day and remaining 11 hours to next working day.
If in case it is sun, it should consider directly go to monday.
Example:
Case created on: 10/06/2015 12:04:39 PM- with Sr_number 1 (12 Hours) Deadline is now: 10/07/2015 12.05 PM
Make sense?
Another catch is I need to take into account hours On Hold and these two have to be only within working hours.
For some case,saturdays is working ,for some its holiday.
How should i proceed.
I tried performing datepart,dateadd and datediff functions.But i could find only weekday.
I am new at sql.Please guide for the same