DEV Community

Royal Gurbanov
Royal Gurbanov

Posted on

Write a Python function once, call it from anywhere

Every notebook or script I start begins the same way: copy-pasting the same helper functions from the last one. A Sharpe ratio here, a position-size calculator there, a small risk model… and when I fix a bug in one place, the other five copies still have the old version.

So I built Fintech Hub: a free online library for your own Python functions. You write a function once, and call it by name from anywhere: Jupyter, Google Colab, VS Code, a plain script, or your own server.

How it works

  1. Write a function or class in the online editor, and test it with Run right in your browser.
  2. Save it and press Go Live.
  3. Call it from any Python by its name.

Example: a Sharpe ratio you can use everywhere

In the Fintech Hub Workspace, create a function:

import statistics

def sharpe_ratio(returns, risk_free=0.0):
    """Mean excess return divided by its standard deviation."""
    excess = [r - risk_free for r in returns]
    return statistics.mean(excess) / statistics.stdev(excess)
Enter fullscreen mode Exit fullscreen mode

Save it, press Go Live, and wherever you use Python:

%pip install fintech-hub     # in a notebook; in a terminal: pip install fintech-hub
import fintech_hub as fhub
fhub.login("fhk_…")          # your key, from Workspace → Connect

fhub.sharpe_ratio([0.012, 0.018, -0.004])                   # 0.7621
fhub.sharpe_ratio([0.012, 0.018, -0.004], risk_free=0.001)  # 0.6741
Enter fullscreen mode Exit fullscreen mode

That's it. Fix a bug on the site, save, reconnect, and every notebook and script gets the new version.

Classes work too

class RiskModel:
    def __init__(self, returns):
        self.returns = returns

    def worst(self):
        return min(self.returns)
Enter fullscreen mode Exit fullscreen mode
risk = fhub.RiskModel([0.012, -0.031, 0.018])
risk.worst()    # -0.031
Enter fullscreen mode Exit fullscreen mode

Folders become one object

Put related functions in a folder called Crypto tools, and they load together:

fhub.crypto_tools.btc_to_eth(0.5)
Enter fullscreen mode Exit fullscreen mode

Any package you have installed

Your code runs in your own Python, not on a server. So it can use whatever is installed where you run it. For example, a function using yfinance:

import yfinance as yf

def last_close(ticker="AAPL", days=5):
    history = yf.Ticker(ticker).history(period="1mo").tail(days)
    return {day.strftime("%Y-%m-%d"): round(price, 2) for day, price in history["Close"].items()}
Enter fullscreen mode Exit fullscreen mode
fhub.last_close("TSLA")    # the last 5 closing prices, live from Yahoo Finance
Enter fullscreen mode Exit fullscreen mode

Private by default

  • Only functions you switch Live can be loaded, and only with your key.
  • Other users can't see or load your functions.
  • Leaked your key? Generate a new one; the old one stops working immediately.

Try it

It's free: royalfintech.net/fintech-hub. Your first function even starts with a ready-to-run example.

The client is on PyPI: pip install fintech-hub.

I'd love feedback: what functions would you keep in your own library?

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.