Skip to content

N.A.Y.trading is a machine learning application that mimics stock trading decisions of the user.

License

Notifications You must be signed in to change notification settings

chrwoizi/naytrading

Repository files navigation

N.A.Y.trading

N.A.Y.trading is a machine learning application that mimics stock trading decisions of the user.

The user's analysis philosophy must be purely technical, i.e. the only resource data for a buy/sell decision is the price history of the past 5 years. No stock name, no news, no ratings.

📝 Recording the user's trade decisions

The website NAYtrading presents a stock price chart to the user and lets him/her choose whether to buy, ignore, or sell the stock. The price history and the decision are stored in a database.

Try the official online version: naytrading.com

How to install

NAYtrading needs a custom stock data provider implementation. This repository includes an example plugin in NAYtrading.Plugin that can be used to quick start the development of such a provider.

# install tools
root@host:~$ apt-get install sudo
root@host:~$ apt-get install curl
root@host:~$ apt-get install git

# install nodejs
root@host:~$ curl -sL https://deb.nodesource.com/setup_8.x | bash
root@host:~$ apt-get install -y nodejs
root@host:~$ apt-get install -y build-essential
root@host:~$ npm i -g sequelize-cli
root@host:~$ npm i -g node-autostart

# install mysql
root@host:~$ apt-get install mysql-server
root@host:~$ mysql_secure_installation
root@host:~$ mysql -u root -p
[press enter on password prompt]
MariaDB [(none)]> create database naytrading;
MariaDB [(none)]> create user 'naytrading'@'localhost' identified by 'naytrading';
MariaDB [(none)]> grant all on naytrading.* to 'naytrading' identified by 'naytrading';
[Ctrl+C]

# install python 3.x
root@host:~$ apt-get install python3
root@host:~$ apt-get install python3-pip
root@host:~$ pip3 install --upgrade pip
root@host:~$ pip3 install argparse
root@host:~$ pip3 install datetime
root@host:~$ pip3 install noise
root@host:~$ pip3 install numpy

# create user
root@host:~$ adduser naytrading
[enter secure password]
[leave details empty]
root@host:~$ su naytrading
naytrading@host:/root$ cd ~

# setup naytrading
naytrading@host:~$ git clone https://github.com/chrwoizi/naytrading.git
naytrading@host:~$ cd naytrading
naytrading@host:~/naytrading$ cd NAYtrading
naytrading@host:~/naytrading/NAYtrading$ npm install
naytrading@host:~/naytrading/NAYtrading$ npm run build
naytrading@host:~/naytrading/NAYtrading$ cd app/config
naytrading@host:~/naytrading/NAYtrading/app/config$ cp config.mandatory.json config.json
naytrading@host:~/naytrading/NAYtrading/app/config$ cp database.mandatory.json database.json
naytrading@host:~/naytrading/NAYtrading/app/config$ vi config.json
[set production.admin_user to your email address]
[set production.proxy if you access the web through a proxy]
[set production.python to your python executable, e.g. python3]
[set the instruments_providers and rates_providers to your custom implementation, e.g. "instruments_providers":{"e": "../../../NAYtrading.Plugin/providers/example/example_instruments_provider"}]
:wq
naytrading@host:~/naytrading/NAYtrading/app/config$ cd ../..
naytrading@host:~/naytrading/NAYtrading$ chmod +x dbmigrate.sh
naytrading@host:~/naytrading/NAYtrading$ chmod +x production.sh
naytrading@host:~/naytrading/NAYtrading$ chmod +x upgrade_production.sh
naytrading@host:~/naytrading/NAYtrading$ ./dbmigrate.sh

# run naytrading
naytrading@host:~/naytrading/NAYtrading$ autostart enable -n "naytrading" -p "/home/naytrading/naytrading/NAYtrading" -c "./production.sh"
naytrading@host:~/naytrading/NAYtrading$ ./production.sh &

# back to root
naytrading@host:~/naytrading/NAYtrading$ exit

# ROUTING OPTION A: redirect port 80 to 5000
root@host:~$ apt-get install iptables-persistent
root@host:~$ iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 5000
root@host:~$ iptables-save > /etc/iptables/rules.v4

# ROUTING OPTION B: using nginx
root@host:~$ apt-get install nginx
root@host:~$ vi /etc/nginx/sites-available/reverse-proxy
limit_req_zone $binary_remote_addr zone=one:10m rate=30r/m;
limit_req_zone $binary_remote_addr zone=two:10m rate=10r/m;
server {
    listen       80;
    server_name  [YOUR DOMAIN];
    location /api/login {
        proxy_pass http://127.0.0.1:5000;
        limit_req zone=one;
    }
    location /api/register {
        proxy_pass http://127.0.0.1:5000;
        limit_req zone=two;
    }
    location / {
        proxy_pass http://127.0.0.1:5000;
    }
    proxy_connect_timeout       600;
    proxy_send_timeout          600;
    proxy_read_timeout          600;
    send_timeout                600;
}
[save and exit]
root@host:~$ ln -s /etc/nginx/sites-available/reverse-proxy /etc/nginx/sites-enabled/reverse-proxy
root@host:~$ nginx -t
root@host:~$ service nginx restart

# OPTIONAL: activate HTTPS using letsencrypt.org

# if using Debian 8:
root@host:~$ echo deb http://ftp.debian.org/debian jessie-backports main>/etc/apt/sources.list.d/jessie-backports.list
root@host:~$ apt-get install certbot -t jessie-backports
# else:
root@host:~$ apt-get install certbot

# register with letsencrypt
root@host:~$ su naytrading
naytrading@host:~/naytrading/NAYtrading$ certbot certonly --config-dir=./letsencrypt/config --logs-dir=./letsencrypt/logs --work-dir=./letsencrypt/work-dir -n --webroot --agree-tos --domains "[YOUR DOMAIN]" --webroot-path /home/naytrading/naytrading/NAYtrading

# link the certificate
naytrading@host:~/naytrading/NAYtrading$ ln -s ../letsencrypt/config/live/[YOUR DOMAIN]/privkey.pem ./keys/privkey.pem
naytrading@host:~/naytrading/NAYtrading$ ln -s ../letsencrypt/config/live/[YOUR DOMAIN]/cert.pem ./keys/cert.pem
naytrading@host:~/naytrading/NAYtrading$ ln -s ../letsencrypt/config/live/[YOUR DOMAIN]/chain.pem ./keys/chain.pem

# enable https
naytrading@host:~/naytrading/NAYtrading$ vi app/config/config.json
[add the following line to the production section]
"https_enabled": true
[save and exit]
naytrading@host:~/naytrading/NAYtrading$ killall production.sh
naytrading@host:~/naytrading/NAYtrading$ killall node
naytrading@host:~/naytrading/NAYtrading$ ./production.sh &

# ROUTING OPTION A: redirect port 443 to 5001
root@host:~$ iptables -t nat -I PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 5001
root@host:~$ iptables-save > /etc/iptables/rules.v4

# ROUTING OPTION B: using nginx
root@host:~$ vi /etc/nginx/sites-available/reverse-proxy
[append the following text]
server {
    listen       443 ssl;
    server_name  [YOUR DOMAIN];
    ssl_certificate /home/naytrading/naytrading/NAYtrading/letsencrypt/config/live/[YOUR DOMAIN]/fullchain.pem;
    ssl_certificate_key /home/naytrading/naytrading/NAYtrading/letsencrypt/config/live/[YOUR DOMAIN]/privkey.pem;
    location /api/login {
        proxy_pass http://127.0.0.1:5000;
        limit_req zone=one;
    }
    location /api/register {
        proxy_pass http://127.0.0.1:5000;
        limit_req zone=two;
    }
    location / {
        proxy_pass http://127.0.0.1:5000;
    }
    proxy_connect_timeout       600;
    proxy_send_timeout          600;
    proxy_read_timeout          600;
    send_timeout                600;
}
[save and exit]
root@host:~$ ln -s /etc/nginx/sites-available/reverse-proxy /etc/nginx/sites-enabled/reverse-proxy
root@host:~$ nginx -t
root@host:~$ service nginx restart

List of stocks:

list of stocks

Making a new decision:

a new price history snapshot of a previously bought stock with trade decision buttons

List of all previous decisions:

list of price history snapshots

Statistics about the portfolio:

statistics about gains and losses over the recorded trade decisions

☎️ Forwarding the simulated trades to a broker

The web application NAYtrading.Trader can be used to actually buy or sell the stocks based on the recorded user decisions.

🎓 Training a Convolutional Neural Network on the recorded data

The web site periodically processes the recorded data and provides CSV file downloads which are optimized for a neural network.

With NAYtrading.AI, a convolutional neural network is trained on the processed data, using the price history as input and the user decision as desired output.

💰 Using the trained network

With NAYtrading.AI, current stock prices are fed into the trained network periodically to find stocks that can be bought or sold based on the user's analysis pattern. The application acts as a user on NAYtrading, so the gains and losses can be evaluated by logging in on NAYtrading. Also, the trade decisions can be forwarded to a broker using NAYtrading.Trader, making N.A.Y.trading an automated stock trading system.

About

N.A.Y.trading is a machine learning application that mimics stock trading decisions of the user.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published