Skip to content

Instantly share code, notes, and snippets.

Jonas Byström highfestiva

Block or report user

Report or block highfestiva

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@highfestiva
highfestiva / calc_pi.py
Created Dec 15, 2019
Calculate PI with an incredibly low accuracy
View calc_pi.py
#!/usr/bin/env python3
import sys
terms = (int(sys.argv[1]) + 1) // 2
pi = 4 + sum(4/-(v*4+3) + 4/(v*4+5) for v in range(terms))
print(pi)
@highfestiva
highfestiva / bitmex-cross-margin-liquidation-calc.py
Created Nov 15, 2019
Optimized and simplified BitMEX's cross-margin calculation
View bitmex-cross-margin-liquidation-calc.py
def long_liq_price(wallet_balance_btc, qty, price):
usd = qty * int(-100000000 / price - 0.5)
a = wallet_balance_btc * 99925056.20784412 + 0.011498875843117663 * usd
a *= a>0
x = usd*1.004831 - a
return -100000000 / int(x / qty)
def short_liq_price(wallet_balance_btc, qty, price):
usd = qty * int(-100000000 / price - 0.5)
@highfestiva
highfestiva / bitmex-liquidation-calculator.py
Created Oct 26, 2019
cli bitmex liquidation calculation formula, minimal implementation
View bitmex-liquidation-calculator.py
#!/usr/bin/env python3
import argparse
from math import ceil, floor
sign = lambda amt: -1 if amt<0 else (1 if amt>0 else 0)
class dobj:
@highfestiva
highfestiva / minimal-web.py
Last active Aug 13, 2019
Minimal general-purpose test web server
View minimal-web.py
#!/usr/bin/env python3
from flask import Flask, request, jsonify
app = Flask(__name__)
methods = 'GET POST PUT DELETE'.split()
@app.route('/', methods=methods)
@highfestiva
highfestiva / knasboll.py
Last active Oct 1, 2018
Autotask to Exact Online invoice-to-book-keeping converter
View knasboll.py
#!/usr/bin/env python3
import argparse
import csv
from datetime import datetime
import dateutil.parser
from glob import glob
import xml.etree.ElementTree as ET
@highfestiva
highfestiva / memoize.py
Created Dec 30, 2017
Minimal 19 LoC memoization with timeout
View memoize.py
import time
class memoize(object):
def __init__(self, timeout=60):
self.cache = {}
self.timeout = timeout
def _flush(self):
now = time.time()
@highfestiva
highfestiva / coinplot.py
Created Dec 8, 2017
Plot some candlesticks based on crypto currency data. Prepped for use in real webserver.
View coinplot.py
#!/usr/bin/env python3
from bokeh.embed import components as plot_html
from bokeh.plotting import figure, show, output_file
import pandas as pd
import requests
import webbrowser
url = 'https://www.binance.com/api/v1/klines?interval=15m&symbol=ARNBTC&limit=100'
@highfestiva
highfestiva / websocket-chat-client.py
Last active Nov 7, 2017
CLI-based chat client in 14 LoC
View websocket-chat-client.py
#!/usr/bin/env python3
import asyncio
from threading import Thread
import websockets
loop = asyncio.get_event_loop()
def user_input(websocket):
while True:
View websocket-chat-server.py
#!/usr/bin/env python3
import asyncio
import websockets
connections = set()
async def broadcast(websocket, path):
connections.add(websocket)
try:
View naughts-and-crosses-headless.py
#!/usr/bin/env python3
# Server for remote naughts and crosses playing.
from flask import Flask, jsonify, request
map = [[' ' for _ in range(20)] for _ in range(20)]
map[10][10] = 'X'
turn = 2
webapp = Flask(__name__)
You can’t perform that action at this time.