trading-models


Nametrading-models JSON
Version 0.1.1 PyPI version JSON
download
home_pagehttps://github.com/SerenaTradingResearch/trading-models
SummaryMLP, CNN, Transformer models for time-series trading predictions.
upload_time2025-08-11 15:33:13
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
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/9b/fb/b40a22769f17da03b2adf7e69be1a27f25703a1151dce1ffd1269a45d2c7/trading_models-0.1.1.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.1",
    "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": "8a5b9fccbd488de90b776edbf45358a4dedef5e1dffd7fb903d34de77bfd461b",
                "md5": "c110549aec69c491db9a1cc2bffd0044",
                "sha256": "aee97ff67028056122df462cfb5605ddb9ad883bc7c194df59edc0a18953c20f"
            },
            "downloads": -1,
            "filename": "trading_models-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c110549aec69c491db9a1cc2bffd0044",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 4374,
            "upload_time": "2025-08-11T15:33:12",
            "upload_time_iso_8601": "2025-08-11T15:33:12.071996Z",
            "url": "https://files.pythonhosted.org/packages/8a/5b/9fccbd488de90b776edbf45358a4dedef5e1dffd7fb903d34de77bfd461b/trading_models-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "9bfbb40a22769f17da03b2adf7e69be1a27f25703a1151dce1ffd1269a45d2c7",
                "md5": "e14afdf99de17c5f02ce65327e6b3061",
                "sha256": "35dff9fa30e1b34191ab484021b3ae0dbd0cd7de8cd9f47cbf67f77577489e04"
            },
            "downloads": -1,
            "filename": "trading_models-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "e14afdf99de17c5f02ce65327e6b3061",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 4044,
            "upload_time": "2025-08-11T15:33:13",
            "upload_time_iso_8601": "2025-08-11T15:33:13.026048Z",
            "url": "https://files.pythonhosted.org/packages/9b/fb/b40a22769f17da03b2adf7e69be1a27f25703a1151dce1ffd1269a45d2c7/trading_models-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-11 15:33:13",
    "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": []
        }
    ],
    "lcname": "trading-models"
}
        
Elapsed time: 0.81582s