Skip to main content

Deep Learning for trading

Project description

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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

deep_trader-0.0.2.tar.gz (27.9 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

deep_trader-0.0.2-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file deep_trader-0.0.2.tar.gz.

File metadata

  • Download URL: deep_trader-0.0.2.tar.gz
  • Upload date:
  • Size: 27.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for deep_trader-0.0.2.tar.gz
Algorithm Hash digest
SHA256 4929ba3a6317c570b0a30af68d5c394d8b4fc875ca8ca59bd721d00d719ddf81
MD5 7516f8dff346430412c16551605f2e3b
BLAKE2b-256 0604d690be5e1ec3e63076e75d857894733aed6f82d42b6ed5d560f4b99b2f30

See more details on using hashes here.

File details

Details for the file deep_trader-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: deep_trader-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for deep_trader-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4a70223f9019385790c2dab3b6bf2c5b576feafb8b736fb8525ffec255c87852
MD5 aaf04301a60ccd810f5ec0bcc26c6fa7
BLAKE2b-256 8c7d323af873910a8fc2a5cbbb7c546b991b76aa23875c17acb406c54625db4b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page