Trading fees are easy to ignore until you add them up. This article builds a small offline calculator that reads your config and fills, applies a 20% rebate only where it is documented, and writes a per-fill CSV. It makes no network calls and needs no API keys, so you can run it on exported or hand-made data.
What the code changes
The UNLOCK20 code gives 20% off the trading fees on paid spot and futures orders, for both maker and taker fills. Buy/Sell and Convert are not covered. The rebate does not change your VIP level, so it sits on top of whatever tier you already have.
The code has to be entered when you sign up. It cannot be added to an account that already exists. Once you are registered, the Invite perks section shows the rebate rate attached to your account. If that figure reads 0%, the code is not attached, and the calculator treats it the same way.
Rates I used
The calculator uses the Regular rates listed for OKX:
- Spot maker: 0.08%
- Spot taker: 0.10%
- Futures maker: 0.02%
- Futures taker: 0.05%
I keep these in a JSON config file rather than in the code. When the published schedule changes, you edit one file and the arithmetic follows. The script never looks up current rates on its own, so check the fee schedule for your account before you rely on any number here.
Config and fills
The config file holds the rebate percentage, a flag that says whether the code is attached, the rate table, and a savings example block. Every value is a string that goes through Decimal, so the money arithmetic avoids binary floating-point rounding.
{
"rebate_pct": "0.20",
"code_attached": true,
"rates": {
"spot": {"maker": "0.0008", "taker": "0.0010"},
"futures": {"maker": "0.0002", "taker": "0.0005"}
},
"savings": true,
"savings_example": {"monthly_volume_usdt": "50000", "rate": "0.0010", "months": 12}
}
The fills file is an export you build yourself. Each row has a date, a product (spot, futures, buysell or convert), a side (maker or taker), and the notional value in USDT. The rows below are illustrative placeholders, not real trades.
date,product,side,notional_usdt
2026-11-01,spot,taker,20000
2026-11-02,spot,maker,5000
2026-11-03,futures,taker,30000
2026-11-04,buysell,taker,10000
2026-11-05,futures,maker,15000
The calculator
The script separates three cases. A product outside the eligible set (Buy/Sell and Convert here) is excluded and gets no fee figure. A product with no rate in the config is excluded too, which catches typos early. For everything else the fee is notional times rate, and the rebate is 20% of that fee when the code is attached. The script writes one row per fill to fees.csv and prints totals.
#!/usr/bin/env python3
"""Offline UNLOCK20 fee rebate calculator. No network calls, no API keys."""
from __future__ import annotations
import argparse
import csv
import json
from dataclasses import dataclass
from decimal import ROUND_HALF_UP, Decimal, InvalidOperation
from pathlib import Path
ELIGIBLE_PRODUCTS = {"spot", "futures"}
CENT = Decimal("0.00000001") # 8 decimal places
@dataclass(frozen=True)
class Fill:
date: str
product: str # spot, futures, buysell, convert
side: str # maker or taker
notional: Decimal # USDT
@dataclass(frozen=True)
class FeeRow:
fill: Fill
rate: Decimal | None
fee: Decimal | None
rebate: Decimal
net_fee: Decimal | None
flag: str
def load_config(path: Path) -> dict:
with path.open(encoding="utf-8") as handle:
return json.load(handle)
def load_fills(path: Path) -> list[Fill]:
fills: list[Fill] = []
with path.open(newline="", encoding="utf-8") as handle:
for row in csv.DictReader(handle):
fills.append(
Fill(
date=row["date"].strip(),
product=row["product"].strip().lower(),
side=row["side"].strip().lower(),
notional=Decimal(row["notional_usdt"].strip()),
)
)
return fills
def rate_for(config: dict, product: str, side: str) -> Decimal | None:
try:
return Decimal(config["rates"][product][side])
except KeyError:
return None
def compute(fills: list[Fill], config: dict) -> list[FeeRow]:
pct = Decimal(config["rebate_pct"])
attached = bool(config["code_attached"])
rows: list[FeeRow] = []
for fill in fills:
if fill.product not in ELIGIBLE_PRODUCTS:
rows.append(FeeRow(fill, None, None, Decimal(0), None,
"excluded: product not covered"))
continue
rate = rate_for(config, fill.product, fill.side)
if rate is None:
rows.append(FeeRow(fill, None, None, Decimal(0), None,
"excluded: no rate in config"))
continue
fee = (fill.notional * rate).quantize(CENT, ROUND_HALF_UP)
rebate = (fee * pct).quantize(CENT, ROUND_HALF_UP) if attached else Decimal(0)
flag = "ok" if attached else "code not attached: rebate 0"
rows.append(FeeRow(fill, rate, fee, rebate, fee - rebate, flag))
return rows
def savings_example(config: dict) -> str:
ex = config["savings_example"]
volume = Decimal(ex["monthly_volume_usdt"])
rate = Decimal(ex["rate"])
pct = Decimal(config["rebate_pct"])
months = int(ex["months"])
monthly_fee = volume * rate
monthly_net = monthly_fee * (1 - pct)
monthly_saving = monthly_fee - monthly_net
lines = [
"Savings example (illustrative):",
f" monthly volume {volume} USDT at {rate * 100}% = {monthly_fee} USDT",
f" with {pct * 100}% rebate: {monthly_net} USDT (saves {monthly_saving} USDT)",
f" over {months} months: {monthly_fee * months} USDT -> "
f"{monthly_net * months} USDT (saves {monthly_saving * months} USDT)",
]
return "\n".join(lines)
def write_csv(rows: list[FeeRow], path: Path) -> None:
with path.open("w", newline="", encoding="utf-8") as handle:
writer = csv.writer(handle)
writer.writerow([
"date", "product", "side", "notional_usdt", "rate",
"fee_usdt", "rebate_usdt", "net_fee_usdt", "flag",
])
for r in rows:
writer.writerow([
r.fill.date,
r.fill.product,
r.fill.side,
r.fill.notional,
"" if r.rate is None else r.rate,
"" if r.fee is None else r.fee,
r.rebate,
"" if r.net_fee is None else r.net_fee,
r.flag,
])
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description="UNLOCK20 fee rebate calculator (offline).")
parser.add_argument("--config", type=Path, required=True)
parser.add_argument("--fills", type=Path, required=True)
parser.add_argument("--out", type=Path, default=Path("fees.csv"))
args = parser.parse_args(argv)
try:
config = load_config(args.config)
fills = load_fills(args.fills)
rows = compute(fills, config)
except (OSError, ValueError, KeyError, InvalidOperation) as exc:
print(f"error: {exc}")
return 1
included = [r for r in rows if r.fee is not None]
total_fee = sum((r.fee for r in included), Decimal(0))
total_rebate = sum((r.rebate for r in included), Decimal(0))
print(f"fills read: {len(rows)}")
print(f"excluded rows: {len(rows) - len(included)}")
print(f"fees (included): {total_fee} USDT")
print(f"rebate (UNLOCK20): {total_rebate} USDT")
print(f"net fees: {total_fee - total_rebate} USDT")
if not config["code_attached"]:
print("warning: code_attached is false, so no rebate is applied")
if config.get("savings"):
print(savings_example(config))
write_csv(rows, args.out)
print(f"wrote {args.out}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
Run it with:
python3 fee_rebate.py --config config.json --fills fills.csv --out fees.csv
Worked example
Using the illustrative fills above and the config as written, the script produces these per-fill results. The two spot rows and the two futures rows are included, and the Buy/Sell row is excluded.
The 20,000 USDT spot taker fill carries a fee of 20 USDT at 0.10%. With the 20% rebate, 4 USDT comes back, so the net is 16 USDT. The 30,000 USDT futures taker fill carries 15 USDT at 0.05%, with a 3 USDT rebate and a net of 12 USDT. Across the four included fills, total fees come to 42 USDT, the rebate totals 8.4 USDT, and the net is 33.6 USDT.
The config also includes a savings block, which the script prints when savings is true. Take a steady 50,000 USDT of monthly spot taker volume at 0.10%. That is 50 USDT in fees per month, or 600 USDT over a year. With a 20% rebate, the monthly fee drops to 40 USDT, or 480 USDT a year. The annual saving is 120 USDT. The example assumes the same volume every month and a code that stays attached the whole time.
What the arithmetic does not cover
The script knows only what you tell it. It does not check your actual fills against the exchange, so reconcile the totals against your account statement before you rely on them. It applies the 20% to fees computed from the rate table, and if the platform rounds, nets or applies fees differently, your statement wins. Products that are excluded show up as blank fee cells, which keeps them out of the totals. Finally, the calculator does not know your VIP tier. The rebate is independent of it, but your base rates are not, so keep the rate table current with your own tier's schedule.
Wrap up
If you want to extend it, the cleanest next step is to add a per-month summary, or to validate the rate table against a schema before the run begins. Both changes stay within the standard library.
If you want to try the code on your own account, you can sign up at https://okx.com/join/UNLOCK20 using referral code UNLOCK20. The link above is an affiliate link.
Check the current program terms and fee rates for your region before you sign up or trade.
Top comments (0)