Skip to content

Instantly share code, notes, and snippets.

@beatmadsen
beatmadsen / gamma_per_dollar.py
Created March 3, 2021 08:43
Calculate (European option) gamma pr dollar spent on premium
import yfinance as yf
import pandas as pd
import numpy as np
from scipy.stats import norm
'''
Calculate realised volatility of stock
:ticker: yfinance ticker
:window: trading days in rolling window
@beatmadsen
beatmadsen / xlswithxmlparser.py
Last active August 15, 2020 18:34
python 3 parse badly encoded excel sheet
# Solution inspired by https://stackoverflow.com/a/33504236/2020801
from xml.sax import ContentHandler, parseString
class ExcelHandler(ContentHandler):
def __init__(self):
self.chars = []
self.cells = []
self.rows = []
self.table = []
self.sheet_name = None
@beatmadsen
beatmadsen / event-loop-future-example.scala
Created January 23, 2020 02:30
An example of an asynchronous service using futures to be run on an event loop
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
class FileApp(val fileCounter: FileCounter) extends Service {
private def countWordsV1(number: Int): Future[Int] = fileCounter.countWordsInFile(s"${number}a")
.flatMap(c1 => fileCounter.countWordsInFile(s"${number}b").map(c2 => c1 + c2))
private def countWordsV2(number: Int): Future[Int] = {
@beatmadsen
beatmadsen / fiber-example.rb
Created January 23, 2020 01:17
An example of how fibers work
f = Fiber.new do
a = 42
loop do
Fiber.yield(a)
a += 1
end
end
f.resume # => 42
f.resume # => 43
@beatmadsen
beatmadsen / two_methods_instead_of_boolean.rb
Last active October 26, 2018 23:58
Two methods instead of boolean parameter
# calling code:
age = user.belongs_to_system_a? ?
find_age_from_system_a(name: user.name) :
find_age_from_system_b(name: user.name)
# methods
def find_age_from_system_a(name:)
payload = build_payload(name)
response = call_system_a(payload)
@beatmadsen
beatmadsen / bad_reuse_with_boolean.rb
Last active October 26, 2018 23:56
Bad reuse with boolean parameter
# calling code:
age = find_age(name: user.name, divert: !user.belongs_to_system_a?)
# methods
def find_age(name:, divert:)
payload = { name: name, size: 42 }
response = divert ? call_system_b(payload) : call_system_a(payload)