trading-models


Nametrading-models JSON
Version 0.1.3 PyPI version JSON
download
home_pagehttps://github.com/SerenaTradingResearch/trading-models
SummaryMLP, CNN, Transformer models for time-series trading predictions.
upload_time2025-08-15 09:44:16
maintainerNone
docs_urlNone
authorRicky Ding
requires_python>=3.8
licenseMIT
keywords trading models neural networks time-series mlp cnn transformer
VCS
bugtrack_url
requirements torch matplotlib numpy Pillow
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
## Intro

- Neural Network models for trading: MLP, CNN, Transformer

## Usage

```bash
pip install trading-models
```

```py
import torch as tc
from torch.utils.data import DataLoader

from trading_models.simple_models import CNN, MLP, Transformer
from trading_models.utils import WindowDataset, model_size

"""
trading models

input: x.shape = (T, F)
output: a.shape = (T-W+1, A)

T: time
F: features
W: window length
A: actions

at each time step t in range(W-1, T),
the model looks at data in the window
x[t+1-W : t+1, :]
to make A actions
"""

T, F, W, A = 100, 2, 50, 1
device = tc.device("cuda" if tc.cuda.is_available() else "cpu")
x = tc.randn(T, F).to(device)

dataset = WindowDataset(x, W)
dataloader = DataLoader(dataset, batch_size=32, shuffle=False)

net1 = MLP([W * F, 64, 64, A]).to(device)
net2 = CNN([F, 64, 64, A]).to(device)
net3 = Transformer(W, F, A, d_model=64, d_ff=64, n_head=2, n_layer=2).to(device)

for net in [net1, net2, net3]:
    outputs = []
    for batch in dataloader:
        outputs.append(net(batch.to(device)).detach())
    output = tc.cat(outputs, dim=0)
    print(net, output.shape, model_size(net), "\n")

```

- Output

```bash
MLP(
  (mlp): Sequential(
    (0): Linear(in_features=100, out_features=64, bias=True)
    (1): SiLU()
    (2): Linear(in_features=64, out_features=64, bias=True)
    (3): SiLU()
    (4): Linear(in_features=64, out_features=1, bias=True)
  )
) torch.Size([51, 1]) trainable: 10689/10689 

CNN(
  (cnn): Sequential(
    (0): Conv1d(2, 64, kernel_size=(3,), stride=(1,), padding=(1,))
    (1): SiLU()
    (2): Conv1d(64, 64, kernel_size=(3,), stride=(1,), padding=(1,))
    (3): SiLU()
    (4): AdaptiveAvgPool1d(output_size=1)
  )
  (fc): Linear(in_features=64, out_features=1, bias=True)
) torch.Size([51, 1]) trainable: 12865/12865 

Transformer(
  (proj): Linear(in_features=2, out_features=64, bias=True)
  (trans): TransformerEncoder(
    (layers): ModuleList(
      (0-1): 2 x TransformerEncoderLayer(
        (self_attn): MultiheadAttention(
          (out_proj): NonDynamicallyQuantizableLinear(in_features=64, out_features=64, bias=True)
        )
        (linear1): Linear(in_features=64, out_features=64, bias=True)
        (dropout): Dropout(p=0.1, inplace=False)
        (linear2): Linear(in_features=64, out_features=64, bias=True)
        (norm1): LayerNorm((64,), eps=1e-05, elementwise_affine=True)
        (norm2): LayerNorm((64,), eps=1e-05, elementwise_affine=True)
        (dropout1): Dropout(p=0.1, inplace=False)
        (dropout2): Dropout(p=0.1, inplace=False)
      )
    )
  )
  (fc): Linear(in_features=64, out_features=1, bias=True)
) torch.Size([51, 1]) trainable: 50689/50689 
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/SerenaTradingResearch/trading-models",
    "name": "trading-models",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "trading models, neural networks, time-series, MLP, CNN, Transformer",
    "author": "Ricky Ding",
    "author_email": "e0134117@u.nus.edu",
    "download_url": "https://files.pythonhosted.org/packages/d0/11/71b8fbc721ee3f026a347e2ccd23cc39b79729b2348b7404152713dee47e/trading_models-0.1.3.tar.gz",
    "platform": null,
    "description": "\n## Intro\n\n- Neural Network models for trading: MLP, CNN, Transformer\n\n## Usage\n\n```bash\npip install trading-models\n```\n\n```py\nimport torch as tc\nfrom torch.utils.data import DataLoader\n\nfrom trading_models.simple_models import CNN, MLP, Transformer\nfrom trading_models.utils import WindowDataset, model_size\n\n\"\"\"\ntrading models\n\ninput: x.shape = (T, F)\noutput: a.shape = (T-W+1, A)\n\nT: time\nF: features\nW: window length\nA: actions\n\nat each time step t in range(W-1, T),\nthe model looks at data in the window\nx[t+1-W : t+1, :]\nto make A actions\n\"\"\"\n\nT, F, W, A = 100, 2, 50, 1\ndevice = tc.device(\"cuda\" if tc.cuda.is_available() else \"cpu\")\nx = tc.randn(T, F).to(device)\n\ndataset = WindowDataset(x, W)\ndataloader = DataLoader(dataset, batch_size=32, shuffle=False)\n\nnet1 = MLP([W * F, 64, 64, A]).to(device)\nnet2 = CNN([F, 64, 64, A]).to(device)\nnet3 = Transformer(W, F, A, d_model=64, d_ff=64, n_head=2, n_layer=2).to(device)\n\nfor net in [net1, net2, net3]:\n    outputs = []\n    for batch in dataloader:\n        outputs.append(net(batch.to(device)).detach())\n    output = tc.cat(outputs, dim=0)\n    print(net, output.shape, model_size(net), \"\\n\")\n\n```\n\n- Output\n\n```bash\nMLP(\n  (mlp): Sequential(\n    (0): Linear(in_features=100, out_features=64, bias=True)\n    (1): SiLU()\n    (2): Linear(in_features=64, out_features=64, bias=True)\n    (3): SiLU()\n    (4): Linear(in_features=64, out_features=1, bias=True)\n  )\n) torch.Size([51, 1]) trainable: 10689/10689 \n\nCNN(\n  (cnn): Sequential(\n    (0): Conv1d(2, 64, kernel_size=(3,), stride=(1,), padding=(1,))\n    (1): SiLU()\n    (2): Conv1d(64, 64, kernel_size=(3,), stride=(1,), padding=(1,))\n    (3): SiLU()\n    (4): AdaptiveAvgPool1d(output_size=1)\n  )\n  (fc): Linear(in_features=64, out_features=1, bias=True)\n) torch.Size([51, 1]) trainable: 12865/12865 \n\nTransformer(\n  (proj): Linear(in_features=2, out_features=64, bias=True)\n  (trans): TransformerEncoder(\n    (layers): ModuleList(\n      (0-1): 2 x TransformerEncoderLayer(\n        (self_attn): MultiheadAttention(\n          (out_proj): NonDynamicallyQuantizableLinear(in_features=64, out_features=64, bias=True)\n        )\n        (linear1): Linear(in_features=64, out_features=64, bias=True)\n        (dropout): Dropout(p=0.1, inplace=False)\n        (linear2): Linear(in_features=64, out_features=64, bias=True)\n        (norm1): LayerNorm((64,), eps=1e-05, elementwise_affine=True)\n        (norm2): LayerNorm((64,), eps=1e-05, elementwise_affine=True)\n        (dropout1): Dropout(p=0.1, inplace=False)\n        (dropout2): Dropout(p=0.1, inplace=False)\n      )\n    )\n  )\n  (fc): Linear(in_features=64, out_features=1, bias=True)\n) torch.Size([51, 1]) trainable: 50689/50689 \n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "MLP, CNN, Transformer models for time-series trading predictions.",
    "version": "0.1.3",
    "project_urls": {
        "Homepage": "https://github.com/SerenaTradingResearch/trading-models"
    },
    "split_keywords": [
        "trading models",
        " neural networks",
        " time-series",
        " mlp",
        " cnn",
        " transformer"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "3584c91ac6c5a1f2e953d69b780469fdae56e31c282e37cdf3944820a5f0086d",
                "md5": "7f12c17ec9c2219b6562282235c604fd",
                "sha256": "6fdf1967f39c2d824ec42e005f75486c7c963ed7e5f1d0fe65e61a2ac5bcdbf7"
            },
            "downloads": -1,
            "filename": "trading_models-0.1.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7f12c17ec9c2219b6562282235c604fd",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 4986,
            "upload_time": "2025-08-15T09:44:15",
            "upload_time_iso_8601": "2025-08-15T09:44:15.675548Z",
            "url": "https://files.pythonhosted.org/packages/35/84/c91ac6c5a1f2e953d69b780469fdae56e31c282e37cdf3944820a5f0086d/trading_models-0.1.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "d01171b8fbc721ee3f026a347e2ccd23cc39b79729b2348b7404152713dee47e",
                "md5": "d770f2117a91973bfcf482dc4f1359d8",
                "sha256": "5f8abeacaa6846c083419dbaffa350cbb6bd31cb53907db1caa783f93f3bd820"
            },
            "downloads": -1,
            "filename": "trading_models-0.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "d770f2117a91973bfcf482dc4f1359d8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 4898,
            "upload_time": "2025-08-15T09:44:16",
            "upload_time_iso_8601": "2025-08-15T09:44:16.990886Z",
            "url": "https://files.pythonhosted.org/packages/d0/11/71b8fbc721ee3f026a347e2ccd23cc39b79729b2348b7404152713dee47e/trading_models-0.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-15 09:44:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "SerenaTradingResearch",
    "github_project": "trading-models",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "torch",
            "specs": []
        },
        {
            "name": "matplotlib",
            "specs": []
        },
        {
            "name": "numpy",
            "specs": []
        },
        {
            "name": "Pillow",
            "specs": []
        }
    ],
    "lcname": "trading-models"
}
        
Elapsed time: 2.21829s