Skip to main content

AI trading assistant

Project description

Netrade

Netrade is an AI trading assistant with human trader approach

Table of contents

  1. How it works
  2. Performance Result
  3. Installation
  4. Usage
    1. Training
  5. Inference Mode or Testing
    1. Prepare Real-time Data
    2. Run in real world and real-time data

How it works

This AI model will predict the price will go up or go down based on chart pattern and candlestick pattern

The data is available here

Chart Pattern

Chart pattern is enough for analyzing price will go up or go down as usualy traders do, here's some example:

chart-pattern

Candle Stick

Bechause of chart pattern has a limitation we need candle stick pattern to decide when we should buy or sell, here is some example

first row is bearish candle stick pattern and the second row bulish candle stick

chart-pattern

Performance

We've been tested this model in 1 week and here's the result:

  • model accuracy & loss

chart-pattern

  • profit

chart-pattern

  • Win & loss rate ( 61% win 39% loss )

chart-pattern

Installation

  • github
    git clone https://github.com/rizki4106/netrade.git
    
    cd netrade && pip3 install torch torchmetrics scikit-image Pillow torchvision
    
  • pypi
    pip3 install netrade
    

Usage

This step devided into 2 step

Training

download pre-trained model here https://github.com/rizki4106/netrade/releases/ but if you want to train with your own data, here's the step

  • Prepare The data
    You should put your image in this pattern:

    chart:
    ----up:
    ------image1.png
    ------image2.png
    ----down:
    ------image1.png
    ------image2.png
    candle:
    ------image1.png
    ------image2.png
    ----down:
    ------image1.png
    ------image2.png
    
  • Make csv file that contain this field

    chart_name candle_name path label
    filename.png filename.png down 0
    filename.png filename.png up 1
    filename.png filename.png down 0
    filename.png filename.png down 0
    filename.png filename.png up 1

    you can do it by using data preprocessing helper easly

    from netrade.data import DataPreprocessing
    
    # initialize class
    chart = "/path/to/somwhere/chart/"
    candle = "/path/to/somewhere/candle"
    
    prep = DataPreprocessing(chart_path=chart, candle_path=candle)
    
    # create dataframe
    frame = prep.create_frame()
    frame.head()
    
  • Create image transformer

    from torchvision import transforms
    
    # this is for chart pattern
    chart_transformer = transforms.Compose([
        transforms.Resize((224, 224)),
        transforms.ToTensor()
    ])
    
    # this is for candlestick pattern
    candle_transformer = transforms.Compose([
        transforms.Resize((64, 64)),
        transforms.ToTensor()
    ])
    
  • Load your data with data loader

    from netrade.data import NetradeDataLoader
    
    # supposed you have created csv file like i mention above
    frame = pd.read_csv("file_training.csv")
    
    # load data and turn it into tensor
    train_data = NetradeDataLoader(
        chart_dir="/path/to/root/chart-pattern/",
        candle_dir="/path/to/root/candle-stick/",
        frame=frame,
        chart_transform=chart_transformer,
        candle_transform=candle_transformer
    ) # this data loader will return [chart image, candle image and labels]
    
  • Create bathes

    from torch.utils.data import DataLoader
    
    dataloader = DataLoader(
        train_data,
        batch_size=16,
        shuffle=True
    )
    
  • Run training loop

    from netrade.core import Netrade
    import torch
    
    # initialize the model
    netrade = Netrade()
    
    # run training
    model, history = netrade.train(X_train=dataloader, epochs=10)
    
    # model is pure pytorch nn module that has been trained with your own data you can check it model.parameters()
    # history is the result from training loop
    
    print(history)
    
    # save the model's state
    torch.save(model.state_dict(), "name-it.pth")
    

Inference mode / Testing

Real-time Data

If you want to use this model in real time data, you should prepare the comodity price history i.e bitcoin or tesla stock price. in this example I'll be using yfinance to grab the historical data

from netrade.data import data_creation
import yfinance as yf
import matplotlib.pyplot as plt

#
ticker = yf.Ticker('BTC-USD')
data = ticker.history(period="7d", interval="15m")

# create chart image from realtime history data
# take the last 50 candle stick bar
chart_image = data_creation.create_image(data=data[-50:, :])

# take the last 3 candle stick bar
candle_image = data_creation.create_image(data=data[-3:, :])

# create_image returns PIL image class that you can use directly with pytorch

# you can show it by the way
plt.imshow(chart_image)
plt.imshow(candle_image)

Final Preparation

It's time to predict real - time price, let's put everything together

from netrade.data import data_creation
from netrade.core import Netrade
from torchvision import transforms
from PIL import Image
import yfinance as yf

# initialize the model
netrade = Netrade(saved_state_path="path-to-saved-state.pth")

# create image transformer
# this is for chart pattern
chart_transformer = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor()
])

# this is for candlestick pattern
candle_transformer = transforms.Compose([
    transforms.Resize((64, 64)),
    transforms.ToTensor()
])

# load realtime data
ticker = yf.Ticker('BTC-USD')
data = ticker.history(period="7d", interval="15m")

# create chart image from real-time data
chart_image = data_creation.create_image(data=data[-50:, :])
candle_image = data_creation.create_image(data=data[-3:, :])

# turn image into tensor
chart_image = chart_transformer(chart_image)
candle_image = candle_transformer(candle_image)

# run prediction
preds = netrade.predict(chart_image=chart_image, candle_image=candle_image)

# print the result
print(preds.argmax(1)) # 0 price will go down, 1 price will go up

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

netrade-1.2.3.tar.gz (12.0 kB view details)

Uploaded Source

File details

Details for the file netrade-1.2.3.tar.gz.

File metadata

  • Download URL: netrade-1.2.3.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for netrade-1.2.3.tar.gz
Algorithm Hash digest
SHA256 47ddf7f6929798a7baf44cb2377b6a9a63a251a1a5c88ee807e876b85a260cb2
MD5 e421e1afb85f2892cfb60878a5547b30
BLAKE2b-256 99a32403e1fa915cccf0f955a82f9d7b6ae86680b5f3d45921568de3701d1641

See more details on using hashes here.

Supported by

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