Skip to main content

Intro

  • Exploring ideas in deep learning for trading
  • Example training result:

  • Labeled data:

Usage

pip install deep-trader
import h5py
import numpy as np
import talib as ta
import torch as tc
import torch.nn as nn

from deep_trader.core import DeepTraderBase
from deep_trader.plot import plot1
from deep_trader.utils import D_TYPE, mlp, postfix, tensor


class DeepTrader(DeepTraderBase):
    periods = [2**n for n in range(4, 10)]

    lev = 2
    TO = 60
    TP = 0.02
    SL = -0.5
    fee = 1e-3

    epochs = 1000
    f_plot = 100
    f_test = 10
    plot_id = "./data/DeepTrader"

    @property
    def obs_skip(s):
        return max(s.periods)

    def make_obs(s, price: np.ndarray):
        obs = []
        for T in s.periods:
            obs.append(price / ta.SMA(price, T) - 1)
        return np.array(obs).T

    def loss(s, d: D_TYPE):
        logits = s.net(d["obs"])
        pos = tc.tanh(logits[:, 0])
        profit = s.pos_abs_times(pos, d["profit_SL"])
        pr_mean, pr_std = profit.mean(), profit.std()
        # sharpe = pr_mean / pr_std
        return {
            "pos.hist": pos,
            "profit.hist": profit,
            "pr_mean": pr_mean,
            "pr_std": pr_std,
            "loss": -pr_mean,
        }


def main():
    tc.manual_seed(0)
    path = "./data/futures_data_2025-07-01_2025-08-01.h5"
    s = DeepTrader()
    s.set_h5_file(h5py.File(path))

    d1 = s.get_period("2025-07-01", "2025-07-15")
    # plot_crypto_data(d1, "./data/data")
    d1 = s.label_data(d1)
    d2 = s.label_data(s.get_period("2025-07-15", "2025-08-01"))
    s.normalize_obs(d1, d2)
    plot1(postfix(d1, ".hist"), "./data/labeled_data")

    device = "cuda"
    d1, d2 = tensor([d1, d2], device)
    s.net = mlp([6, 32, 32, 1], nn.ReLU).to(device)
    s.opt = tc.optim.Adam(s.net.parameters())
    s.train(d1, d2)


if __name__ == "__main__":
    main()

Metadata

Release files for deep-trader 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for deep-trader 0.0.3
File Size Uploaded
deep_trader-0.0.3.tar.gz 27.9 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for deep-trader 0.0.3
File Interpreter ABI Platform
deep_trader-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 27.9 MB

Release files / deep_trader-0.0.3.tar.gz

Download URL deep_trader-0.0.3.tar.gz
Size 27.9 MB
Tags Source
SHA-256 checksum
How to use checksums
c4efac7ce599c498295ed7bda9d3a7c24fd50f3fb25b1eb05897f89e5ae2fc78
BLAKE2b-256 checksum
How to use checksums
baf6c46c69051b1b0a95d11cb9c0c1b63a652dc98ab6f3b691f078e89216224b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release files / deep_trader-0.0.3-py3-none-any.whl

Download URL deep_trader-0.0.3-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67ff0b19b72713626f4391ae143dafa72369f3b3ed768034ed46e8d5ae8174b3
BLAKE2b-256 checksum
How to use checksums
71c924d5234688e68e8ed95bf1c936a0698b73656e06379a2981e2281be7177e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page