Skip to content

Instantly share code, notes, and snippets.

View miohtama's full-sized avatar
🏠
https://tradingstrategy.ai

Mikko Ohtamaa miohtama

🏠
https://tradingstrategy.ai
View GitHub Profile
@miohtama
miohtama / python-redis-json-dataclass-example.py
Created May 18, 2021 09:48
Python simple persistent database with dataclass, JSON and Redis.
"""A simple trade execution report database by using Python dataclass, Redis and JSON.
- A very simple database for arbitrage trade execution reports.
- Dataclasses are serialised and deserialised to JSON that is stored in the Redis.
- There is type validation for members and class-types.
- Class-types like Decimal are converted back to their original format upon deserialisation.
- Optional members are supported and the member presence is validated.
- Past trades can be iterated in either order by creation.
- A simple CSV exported is provided.
@miohtama
miohtama / purge_logs.sh
Created January 27, 2017 10:04
Remove old logs from Quassel SQLite database
#!/bin/sh
BAK_PATH="${HOME}/.config/quassel-irc.org/quassel-storage.sqlite.bak"
CURRENT_PATH="${HOME}/.config/quassel-irc.org/quassel-storage.sqlite"
# first day of data that will be maintained
# -15 day means that *every* chatline stored before 16 days ago and so on are going to be eliminated.
# only the last 15 days are keeped.
DATE_TO_PRUNE='-15 day'
die() {
@miohtama
miohtama / signal-vs-profit.py
Created November 8, 2023 22:33
Trading signal vs. price movement vs. profit analysis using Pandas
def calculate_signal_vs_profit(
df,
signal_window: pd.Timedelta,
profit_window: pd.Timedelta,
data_time_bucket: pd.Timedelta,
) -> pd.DataFrame:
"""Calculate signals and profits for all incoming candles."""
# Create entries for past price to be used for signal
# and future price (used for the price correlation)
@miohtama
miohtama / cloudflare-ip-list.sh
Created August 11, 2023 10:13
Cloudflare IP list printer
#!/bin/sh
#
# Prints out the latest Cloudflare IP list.
#
# Note: Third party Cloudflare workers can still access your site even if you
# block traffic from this range.
#
for i in `curl -s https://www.cloudflare.com/ips-v4`; do echo -n "$i "; done
for i in `curl -s https://www.cloudflare.com/ips-v6`; do echo -n "$i "; done
@miohtama
miohtama / resize.svelte
Created July 7, 2023 01:04
Resizing cross-domain iframe automatically in svelte
<!--
Page to display the strategy backtest results.
- We are working hard to make iframe resize to work cross-domain,
because trade-executor HTML report is being served from a different domain
and the web browser policy prevents us to access iframe content to read its internal height
- We work around this using a postMessage hack
https://stackoverflow.com/a/44547866/315168
# Erigon modified compose.
#
# - Assumes data volume is mounted at /bsc
# - Txpool disabled
#
#
# Useful commands
# - logs: docker compose logs --follow erigon
#
# To start:
"""A stateful event scanner for Ethereum-based blockchains using web3.py.
With the stateful mechanism, you can do one batch scan or incremental scans,
where events are added where the scanner left last time.
Copyright 2021 Mikko Ohtamaa, https://twitter.com/moo9000, licensed under MIT
"""
import datetime
import time
@miohtama
miohtama / launch.json
Created February 13, 2020 22:29
Launching Jest e2e+NestJS from Visual Studio Code - so you can hit breakpoints
// https://github.com/microsoft/vscode-recipes/tree/master/debugging-jest-tests
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
@miohtama
miohtama / bigquery.sql
Created January 28, 2021 11:47
How to get the total cost of approve() function calls on Ethereum blockchain
-- Web3.utils.keccak256("approve(address,uint256)").slice(0, 10);
-- '0x095ea7b3'
WITH txdata as (
SELECT tx.hash as txid, cast(tx.receipt_gas_used as numeric) * cast(tx.gas_price as numeric) as cost FROM
bigquery-public-data.crypto_ethereum.transactions as tx
where
tx.input
LIKE "0x095ea7b3%")
SELECT (SUM(cost) / POWER(10, 18)) as eth_cost from txdata;
@miohtama
miohtama / transactionawaretask.py
Last active December 3, 2022 14:46
Transaction-aware Celery tasks for Pyramid - only run the task after the transaction has committed
"""Transaction-aware Celery task handling.
Core originally written for Warehouse project https://raw.githubusercontent.com/pypa/warehouse/master/warehouse/celery.py
"""
from celery import Task
from pyramid import scripting
from pyramid.interfaces import IRequest
from pyramid.request import Request