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.3.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.3-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: deep_trader-0.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 c4efac7ce599c498295ed7bda9d3a7c24fd50f3fb25b1eb05897f89e5ae2fc78
MD5 4673e5a7eb16f497848f9861cdad8d16
BLAKE2b-256 baf6c46c69051b1b0a95d11cb9c0c1b63a652dc98ab6f3b691f078e89216224b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: deep_trader-0.0.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 67ff0b19b72713626f4391ae143dafa72369f3b3ed768034ed46e8d5ae8174b3
MD5 501638ccd91036c9ede3985501bd4f48
BLAKE2b-256 71c924d5234688e68e8ed95bf1c936a0698b73656e06379a2981e2281be7177e

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