iPyWidget - DatePicker interact with SelectionRangeSlider problem - jupyter-notebook

I'm trying to interact DatePicker with SelectionRangeSlider widgets. So far I could manage to link the slider with the DatePicker, but I'm getting trouble with the opposite way.
With this code the period slider updates the DatePicker:
from datetime import datetime
import pandas as pd
import ipywidgets as widgets
start_date = datetime(2021, 7, 1)
end_date = datetime(2021, 7, 5)
dates = pd.date_range(start_date, end_date, freq='D')
options = [(date.strftime(' %d %b %Y '), date) for date in dates]
pick_start = widgets.DatePicker(
description='',
disabled=False,
value = start_date
)
pick_end = start_day = widgets.DatePicker(
description='',
disabled=False,
value=end_date
)
selection_range_slider = widgets.SelectionRangeSlider(
options=options,
index=(0, len(options) - 1),
description='Period',
orientation='horizontal',
layout=widgets.Layout(width='100%', padding='35px')
)
def update_pick(*args):
pick_start.value = datetime.strptime(selection_range_slider.value[0].strftime("%Y-%m-%d"), "%Y-%m-%d")
pick_end.value = datetime.strptime(selection_range_slider.value[1].strftime("%Y-%m-%d"), "%Y-%m-%d")
def update_slider(*args):
selection_range_slider.value[0] = pick_start.value.strftime("%Y-%m-%d")
selection_range_slider.value[1] = pick_end.value.strftime("%Y-%m-%d")
selection_range_slider.observe(update_pick, 'value')
pick_start.observe(update_slider, 'value')
pick_end.observe(update_slider, 'value')
center_layout = widgets.Layout(display='flex',
align_items='center',
width='100%')
day = widgets.HBox(children=[pick_start, selection_range_slider, pick_end], layout=center_layout)
display(day)
Also try with this function without result:
def update_slider(*args):
start_date = datetime(pick_start.value.year, pick_start.value.month, pick_start.value.day)
end_date = datetime(pick_end.value.year, pick_end.value.month, pick_end.value.day)
dates = pd.date_range(start_date, end_date, freq='D')
options = [(date.strftime(' %d %b %Y '), date) for date in dates]
selection_range_slider.value[0] = options[0]
selection_range_slider.value[1] = options[len(options)-1]
Need help interacting DatePicker with SelectionRangeSlider
Thanks!

you have done a nice job of creating a standalone question here, thanks. A couple suggestions for your next Q to speed up an answer.
Please include your imports, I can't run your code without them:
from datetime import datetime
import pandas as pd
import ipywidgets as widgets
Please post any error messages that you see when your code runs. I got a ValueError which would have improved the question even more.
The error said there was an issue assigning to an index of a tuple, Here's my solution.
Assign the whole tuple directly, you cannot update index 0 and then index 1 of your selection_range_slider
Use raw datetimes to pass between widgets, your mixed string conversions were causing some issues. You can still change how the dates are displayed in the slider by using a dictionary for options.
from datetime import datetime
import pandas as pd
import ipywidgets as widgets
start_date = datetime(2021, 7, 1)
end_date = datetime(2021, 7, 5)
dates = pd.date_range(start_date, end_date, freq='D')
options = {date.strftime(' %d %b %Y '): date for date in dates}
pick_start = widgets.DatePicker(
description='',
disabled=False,
value = start_date
)
pick_end = start_day = widgets.DatePicker(
description='',
disabled=False,
value=end_date
)
selection_range_slider = widgets.SelectionRangeSlider(
options=options,
index=(0, len(options) - 1),
description='Period',
orientation='horizontal',
layout=widgets.Layout(width='100%', padding='35px')
)
def update_pick(*args):
pick_start.value = selection_range_slider.value[0]
pick_end.value = selection_range_slider.value[1]
def update_slider(*args):
selection_range_slider.value = (pick_start.value, pick_end.value)
selection_range_slider.observe(update_pick, 'value')
pick_start.observe(update_slider, 'value')
pick_end.observe(update_slider, 'value')
center_layout = widgets.Layout(display='flex',
align_items='center',
width='100%')
day = widgets.HBox(children=[pick_start, selection_range_slider, pick_end], layout=center_layout)
display(day)

Related

problem with twint while using specific time

we are trying to get data from 2021 and we have a problem with the month, which is we can't get the all month in 2021, but we only got the recent month and it just few of tweets. (sorry for my bad english)
import twint
import nest_asyncio
nest_asyncio.apply()
c = twint.Config()
c.Search = '"covid" lang:id'
#c.Limit = 25000
c.Limit = 10000
c.Since = "2021-01-01"
c.Until = "2021-12-31"
c.Store_csv = True
c.Output = 'drive/My Drive/skripsi2/datatwittercovid.csv'
twint.run.Search(c)
Try to include until:2021-12-31 since:2021-01-01 in the c.Search object
c.Search = '"covid" lang:id until:2021-12-31 since:2021-01-01'

How to create an custom entry/exit signal in VectorBT?

thanks for reading!
I'm trying to backtest a strategy that I wrote in Pinescrpt, and I struggle to create my entry conditions.
So this is the code
import numpy as np
import pandas as pd
import vectorbt as vbt
from datetime import datetime
from binance.client import Client
symbols=['SOLUSDT', 'BNBUSDT']
price =
vbt.BinanceData.download(symbols,
start= '5 days ago UTC',
end= 'Now UTC',
interval='30m', missing_index='drop'
).get(['High', 'Low', 'Open', 'Close'])
high = price[0]
low = price[1]
open = price[2]
close = price[3]
stoch = vbt.STOCH.run(
high=high,
low=low,
close = close,
k_window = 14
)
And I want to add
entries = abs(stoch.percent_k['SOLUSDT'] -
stoch.percent_k['SOLUSDT']) > 50 # (mi intention with abs is to get the absolute value)
exits = abs(stoch.percent_k['SOLUSDT'] -
stoch.percent_k['SOLUSDT']) < 5
portfolio = vbt.Portfolio.from_signals(price[3], entries, exits, init_cash=10000)
I pretend to trigger a short order in a symbol and a long order in the second simultaneously with those signals.
And if anyone has a recommendation about where to find educational resources about this particular package (besides the official web) is welcome. I have read the examples in the doc, but it still fills a bit too complex for my level.

Warp10 and streamlit integration?

Two simple questions:
Does Warp10 integrate into streamlit to feed visualisations?
If so, please would you specify how this can be accomplished?
Thanking you in advance.
Best wishes,
There's no direct integration of Warp 10 in streamlit.
Although streamlit can handle any kind of data, it's mainly focused on pandas DataFrame. DataFrames are tables whereas Warp 10 Geo Time Series are time series. So even if Warp 10 was integrated in streamlit, it would require some code to properly format the data for streamlit to give its full potential.
That being said, here is a small example on how to display data stored in Warp 10 with streamlit:
import json
from datetime import datetime, timedelta
import requests
import streamlit as st
from bokeh.palettes import Category10_10 as palette
from bokeh.plotting import figure
# Should be put in a configuration file.
fetch_endpoint = 'http://localhost:8080/api/v0/fetch'
token = 'READ' # Change that to your actual token
def load_data_as_json(selector, start, end):
headers = {'X-Warp10-Token': token}
params = {'selector': selector, 'start': start, 'end': end, 'format': 'json'}
r = requests.get(fetch_endpoint, params=params, headers=headers)
return r.text
st.title('Warp 10 Test')
# Input parameters
selector = st.text_input('Selector', value="~streamlit.*{}")
start_date = st.date_input('Start date', value=datetime.now() - timedelta(days=10))
start_time = st.time_input('Start time')
end_date = st.date_input('End date')
end_time = st.time_input('End time')
# Convert datetime.dates and datetime.times to microseconds (default time unit in Warp 10)
start = int(datetime.combine(start_date, start_time).timestamp()) * 1000000
end = int(datetime.combine(end_date, end_time).timestamp()) * 1000000
# Make the query to Warp 10 and get back a json.
json_data = load_data_as_json(selector, start, end)
gtss = json.loads(json_data)
# Iterate through the json and populate a Bokeh graph.
p = figure(title='GTSs', x_axis_label='time', y_axis_label='value')
for gts_index, gts in enumerate(gtss):
tss = []
vals = []
for point in gts['v']:
tss.append(point[0])
vals.append(point[-1])
p.line(x=tss, y=vals, legend_label=gts['c'] + json.dumps(gts['l']), color=palette[gts_index % len(palette)])
st.bokeh_chart(p, use_container_width=True)
# Also display the json.
st.json(json_data)

display the contents of a sqlite3 database according to the dates

I use python 3.6 and sqlite3, and I have a problem when I want to read the content of the table between 2 variables
first, when I use two string format dates ("2019-01-01" and "2019-05-20") the following code works correctly :
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from tkinter import *
import time
import calendar
import sqlite3
import datetime
fen = Tk()
fen.title('ILS system ')
fen.resizable(0,0)
largeur=920
hauteur=660
fen.geometry('{}x{}+0+0'.format(largeur,hauteur))
def CreateTable_releves():
conn1_releves = sqlite3.connect('bdd/test.db')
curseur1_releves = conn1_releves.cursor()
curseur1_releves.execute('''CREATE TABLE IF NOT EXISTS releves (id INTEGER PRIMARY KEY,time_releves TEXT NOT NULL, monitor1_releves TEXT NOT NULL,monitor2_releves TEXT NOT NULL)''')
curseur1_releves.close()
def Add_To_Table_releves():
time_releves_get = str(date_choisie_ils35R)
monitor1_releves_get = str(ent_releves_mesure_mon1_axe_loc35R.get())
monitor2_releves_get = str(ent_releves_mesure_mon2_axe_loc35R.get())
conn2_releves = sqlite3.connect('bdd/test.db')
curseur2_releves = conn2_releves.cursor()
curseur2_releves.execute('''INSERT INTO releves (time_releves,monitor1_releves,monitor2_releves) VALUES (?,?,?)''',(time_releves_get,monitor1_releves_get,monitor2_releves_get))
conn2_releves.commit()
curseur2_releves.close()
# CreateTable_releves()
def monitoring () :
fen_monitoring=Toplevel(fen)
fen_monitoring.title('ddm axe')
fen_monitoring.geometry('450x400+300+150')
s= Scrollbar(fen_monitoring)
T= Text(fen_monitoring,bg='powder blue',width=450,height=400)
s.pack(side=RIGHT, fill=Y)
T.pack(side=LEFT, fill=Y)
s.config(command=T.yview)
T.config(yscrollcommand=s.set)
def update_releves():
T.delete('1.0', END)
T.update()
conn = sqlite3.connect('bdd/test.db')
curseur = conn.cursor()
for resultats in curseur.execute('SELECT * FROM releves where time_releves between "2019-01-01" AND "2019-05-20" ORDER BY time_releves DESC'):
contenu_table_releves= str(resultats[1])+"\t\t"+ str(resultats[2])+"\t\t"+ str(resultats[3])+"\n"
T.insert(END,contenu_table_releves)
T.after(1000,update_releves)
curseur.close()
update_releves()
bt=Button(fen,text='click on\nthe button', command=monitoring)
bt.pack()
here is the image of the contents of the toplevel window :
enter image description here
but if I modify the code by input two variables (first_day) and (last_day) in the sql requet :
for resultats in curseur.execute('SELECT * FROM releves where time_releves between "first_day" AND "last_day" ORDER BY time_releves DESC'):
there is the complet modify code :
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from tkinter import *
import time
import calendar
import sqlite3
import datetime
fen = Tk()
fen.title('ILS system ')
fen.resizable(0,0)
largeur=920
hauteur=660
fen.geometry('{}x{}+0+0'.format(largeur,hauteur))
#to calculate first and last day of the month:
now = datetime.datetime.now()
start_month = datetime.datetime(now.year, now.month, 1)
date_on_next_month = start_month + datetime.timedelta(35)
start_next_month = datetime.datetime(date_on_next_month.year, date_on_next_month.month, 1)
last_day_month = start_next_month - datetime.timedelta(1)
last_day=last_day_month.strftime('%Y-%m-%d')
first_day=start_month = datetime.datetime(now.year, now.month, 1).strftime('%Y-%m-%d')
def CreateTable_releves():
conn1_releves = sqlite3.connect('bdd/test.db')
curseur1_releves = conn1_releves.cursor()
curseur1_releves.execute('''CREATE TABLE IF NOT EXISTS releves (id INTEGER PRIMARY KEY,time_releves TEXT NOT NULL, monitor1_releves TEXT NOT NULL,monitor2_releves TEXT NOT NULL)''')
curseur1_releves.close()
def Add_To_Table_releves():
time_releves_get = str(date_choisie_ils35R)
monitor1_releves_get = str(ent_releves_mesure_mon1_axe_loc35R.get())
monitor2_releves_get = str(ent_releves_mesure_mon2_axe_loc35R.get())
conn2_releves = sqlite3.connect('bdd/test.db')
curseur2_releves = conn2_releves.cursor()
curseur2_releves.execute('''INSERT INTO releves (time_releves,monitor1_releves,monitor2_releves) VALUES (?,?,?)''',(time_releves_get,monitor1_releves_get,monitor2_releves_get))
conn2_releves.commit()
curseur2_releves.close()
# CreateTable_releves()
def monitoring () :
fen_monitoring=Toplevel(fen)
fen_monitoring.title('ddm axe')
fen_monitoring.geometry('450x400+300+150')
s= Scrollbar(fen_monitoring)
T= Text(fen_monitoring,bg='powder blue',width=450,height=400)
s.pack(side=RIGHT, fill=Y)
T.pack(side=LEFT, fill=Y)
s.config(command=T.yview)
T.config(yscrollcommand=s.set)
def update_releves():
T.delete('1.0', END)
T.update()
conn = sqlite3.connect('bdd/test.db')
curseur = conn.cursor()
for resultats in curseur.execute('SELECT * FROM releves where time_releves between "first_day" AND "last_day" ORDER BY time_releves DESC'):
contenu_table_releves= str(resultats[1])+"\t\t"+ str(resultats[2])+"\t\t"+ str(resultats[3])+"\n"
T.insert(END,contenu_table_releves)
T.after(1000,update_releves)
curseur.close()
update_releves()
bt=Button(fen,text='click on\nthe button', command=monitoring)
bt.pack()
the toplevel window appear but empty like in the next picture :
enter image description here
I modified the code so that it is clearer by integrating widget (entry, button), and then I added the setpoint of "R Spark". I had as a result the following code which apparently works for the moment. (I will redo other tests soon)
if there are any comments about this new this would be welcome.
The new code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from tkinter import *
import time
import calendar
import sqlite3
import datetime
fen = Tk()
fen.title('ILS system ')
fen.resizable(0,0)
largeur=920
hauteur=660
fen.geometry('{}x{}+0+0'.format(largeur,hauteur))
#to calculate first and last day of the month:
now = datetime.datetime.now()
start_month = datetime.datetime(now.year, now.month, 1)
date_on_next_month = start_month + datetime.timedelta(35)
start_next_month = datetime.datetime(date_on_next_month.year, date_on_next_month.month, 1)
last_day_month = start_next_month - datetime.timedelta(1)
last_day=last_day_month.strftime('%Y-%m-%d')
first_day=start_month = datetime.datetime(now.year, now.month, 1).strftime('%Y-%m-%d')
def CreateTable_releves():
conn1_releves = sqlite3.connect('bdd/test/test.db')
curseur1_releves = conn1_releves.cursor()
curseur1_releves.execute('''CREATE TABLE IF NOT EXISTS releves (id INTEGER PRIMARY KEY,time_releves TEXT NOT NULL, monitor1_releves TEXT NOT NULL,monitor2_releves TEXT NOT NULL)''')
curseur1_releves.close()
def Add_To_Table_releves():
time_releves_get = str(date_choisie.get())
monitor1_releves_get = str(ent_mon1_mesure.get())
monitor2_releves_get = str(ent_mon2_mesure.get())
conn2_releves = sqlite3.connect('bdd/test/test.db')
curseur2_releves = conn2_releves.cursor()
curseur2_releves.execute('''INSERT INTO releves (time_releves,monitor1_releves,monitor2_releves) VALUES (?,?,?)''',(time_releves_get,monitor1_releves_get,monitor2_releves_get))
conn2_releves.commit()
curseur2_releves.close()
CreateTable_releves()
def monitoring () :
fen_monitoring=Toplevel(fen)
fen_monitoring.title('ddm axe')
fen_monitoring.geometry('450x400+300+150')
s= Scrollbar(fen_monitoring)
T= Text(fen_monitoring,bg='powder blue',width=450,height=400)
s.pack(side=RIGHT, fill=Y)
T.pack(side=LEFT, fill=Y)
s.config(command=T.yview)
T.config(yscrollcommand=s.set)
def update_releves():
T.delete('1.0', END)
T.update()
conn = sqlite3.connect('bdd/test/test.db')
curseur = conn.cursor()
for resultats in curseur.execute('SELECT * FROM releves where time_releves between ? AND ? ORDER BY time_releves DESC;', (first_day, last_day)):
contenu_table_releves= str(resultats[1])+"\t\t"+ str(resultats[2])+"\t\t"+ str(resultats[3])+"\n"
T.insert(END,contenu_table_releves)
T.after(1000,update_releves)
curseur.close()
update_releves()
ent_mon1_mesure=StringVar()
ent_mon2_mesure=StringVar()
date_choisie=StringVar()
ent_mon1=Entry(fen,textvariable=ent_mon1_mesure)
ent_mon1.pack(side=LEFT,anchor=NW,padx=10,pady=10)
ent_mon2=Entry(fen,textvariable=ent_mon2_mesure)
ent_mon2.pack(side=LEFT,anchor=NW,padx=10,pady=10)
ent_time_releve=Entry(fen,textvariable=date_choisie)
ent_time_releve.pack(side=LEFT,anchor=NW,padx=10,pady=10)
bt_valider=Button(fen,text='valider', command=Add_To_Table_releves)
bt_valider.pack(side=LEFT,padx=10,pady=10)
bt_monitoring=Button(fen,text='afficher monitoring', command=monitoring)
bt_monitoring.pack(side=LEFT,padx=10,pady=10)
fen.mainloop()
Thanks R Spark for help

Python find index of all dates between two dates

I am trying to implement equivalent of numpy.where for dates as follows:
from datetime import date, timedelta as td, datetime
d1 = datetime.strptime('1/1/1995', "%m/%d/%Y")
d2 = datetime.strptime('12/31/2015', "%m/%d/%Y")
AllDays = []
while(d1<=d2):
AllDays.append(d1)
d1 = d1 + td(days=1)
validDate = AllDays
trainStDt = '1/1/1995'
trainEnDt = '12/31/2013'
testStDt = '1/1/2014'
testEnDt = '12/31/2015'
indTrain = (validDate >= datetime.strptime(trainStDt,'%m/%d/%Y')) & (validDate <=
datetime.strptime(trainEnDt,'%m/%d/%Y'))
indTest = (validDate >= datetime.strptime(testStDt,'%m/%d/%Y')) & (validDate <=
datetime.strptime(testEnDt,'%m/%d/%Y'))
trainDates = validDate[indTrain]
testDates = validDate[indTest]
print trainDates[0]
print trainDates[-1:]
print testDates[0]
print testDates[-1:]
However:
(1) indTrain doesn't work as it is trying to compare list to datetime
(2) my solution is to loop through each element of validDates
Is there a better way to do it?
Just turn your list into an array. Add import numpy as np to the top of your script, and after your while loop, add:
AllDays = np.array(AllDays)

Resources