Skip to content

Instantly share code, notes, and snippets.

@addiversitas
addiversitas / perpetual_rollover.py
Last active September 12, 2024 10:45
Perpetual Rollover for Futures contracts
import numpy as np
import pandas as pd
#load data from your preferred source
data = pd.read_csv("PATH/TO/FILE/futures_rolling.csv", index_col = [0], parse_dates=True)
june_21 = data["VX-2021M"]
july_21 = data["VX-2021N"]
def perpetual_rollover(previous_futures, next_futures, roll_date, number_of_rolls):
@addiversitas
addiversitas / proportional_adjustment.py
Created August 7, 2021 12:24
Proportional Adjustment for Futures contracts
import pandas as pd
#load data from your preferred source
data = pd.read_csv("PATH/TO/FILE/futures_rolling.csv", index_col = [0], parse_dates=True)
june_21 = data["VX-2021M"]
july_21 = data["VX-2021N"]
def proportional_adjustment(previous_futures, next_futures, roll_date):
roll_proportion = next_futures[roll_date]/previous_futures[roll_date]
@addiversitas
addiversitas / panama_adjustment.py
Created August 7, 2021 12:11
Panama Adjustment for Futures contracts
import pandas as pd
#load data from your preferred source
data = pd.read_csv("PATH/TO/FILE/futures_rolling.csv", index_col = [0], parse_dates=True)
june_21 = data["VX-2021M"]
july_21 = data["VX-2021N"]
def panama_adjustment(previous_futures, next_futures, roll_date):
roll_gap = next_futures[roll_date] - previous_futures[roll_date]
@addiversitas
addiversitas / animatedMapShinyApp.R
Created October 24, 2020 16:49
animated map shiny app
#load R packages
library(shiny)
library(leaflet)
library(RColorBrewer)
library(xts)
library(rgdal)
#helper function for choropleth animation
setShapeStyle <- function( map, data = getMapData(map), layerId,
@addiversitas
addiversitas / sliderInputAnimation.R
Created October 24, 2020 16:23
slider input animation
sliderInput("dateSel", "Date",
min = min(covidData$Date_reported),
max = max(covidData$Date_reported),
value = min(covidData$Date_reported),
step = 1,
timeFormat = "%d %b %y",
animate = animationOptions(interval = 500, loop = FALSE)
)
@addiversitas
addiversitas / choroplethMap.R
Last active October 24, 2020 16:12
choropleth map
#create choropleth map
leaflet(world_spdf) %>%
addTiles() %>%
setView(lat = 0, lng = 0, zoom=2) %>%
addPolygons(
layerId = ~ISO2,
fillColor = ~colorPalette(Cases),
stroke = TRUE,
fillOpacity = 1,
@addiversitas
addiversitas / markersMap.R
Last active October 24, 2020 16:12
markers map
#create map with circle markers
leaflet(world_spdf) %>%
addTiles() %>%
setView(lat = 0, lng = 0, zoom=2) %>%
addCircleMarkers(lng = ~LON,
lat = ~LAT,
radius = ~log(Cases) * 2,
weight = 1,
opacity = 1,
@addiversitas
addiversitas / basicDataSetup.R
Created October 24, 2020 16:02
basic data setup for sample covid chart
library(leaflet)
library(rgdal)
library(RColorBrewer)
#load spatial data
world_spdf <- readOGR(
dsn = getwd() ,
layer = "TM_WORLD_BORDERS_SIMPL-0.3",
verbose = FALSE
)
@addiversitas
addiversitas / loadingCovidCaseData.R
Created October 24, 2020 15:36
loading covid case data
covidData <- read.csv("https://covid19.who.int/WHO-COVID-19-global-data.csv", fileEncoding="UTF-8-BOM", stringsAsFactors = FALSE)
covidData <- na.omit(covidData)
@addiversitas
addiversitas / worldBorders.R
Created October 24, 2020 15:20
loading the world borders data set
library(rgdal)
#you only have to do this once!
download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip" , destfile="world_shape_file.zip")
system("unzip world_shape_file.zip")
#loading the world borders spatial polygons data frame
world_spdf <- readOGR(
dsn= getwd() ,
layer="TM_WORLD_BORDERS_SIMPL-0.3",