A bot that doesn’t trade: a trade journal in Python in one evening

A trader’s first automation isn’t a robot with signals. It’s a script that logs trades by itself and counts the result in R.

Published
Section
Automation
Reading
1 min

Demo post: the text and numbers are made up to show the layout.

A journal filled in by hand stops being filled in after the first bad week. That is exactly when you need it most. So logging trades is the first job worth handing to a script.

What the journal has to do

  • pull closed trades from the broker’s export;
  • count the result in R, not in money;
  • put everything into one file that opens in anything.

Counting R

All the logic is one function. Risk comes from the distance between entry and stop; the result is divided by it.

def r_multiple(side: str, entry: float, stop: float, exit: float) -> float:
    risk = abs(entry - stop)
    pnl = exit - entry if side == "long" else entry - exit
    return round(pnl / risk, 2)


r_multiple("long", 1.0840, 1.0810, 1.0903)  # 2.1

Building the file

import csv

with open("broker_export.csv") as src, open("journal.csv", "a") as out:
    writer = csv.writer(out)
    for row in csv.DictReader(src):
        r = r_multiple(row["side"], float(row["entry"]),
                       float(row["stop"]), float(row["exit"]))
        writer.writerow([row["closed_at"], row["symbol"], row["side"], r])

I run it once a day on a schedule. After a month, journal.csv holds what no broker report gives you: the distribution of results in units of risk.

What’s next

The next step is a “by the plan / with a violation” column. It is filled in by hand, and it is the only thing in the journal worth filling in by hand.