Code Monkey home page Code Monkey logo

stock-trading's Introduction

STOCK TRADING BACKEND API

HOW TO RUN

1. Set up environment

  • Run npm
npm install

2. Run server

nodemon server.js

3. Test APIs on Postman

  • Import this json file to Postman: stock-apis.postman_collection.json (found in this project folder)
  • Test API requests and responses as following

APIs

I. JWT Token Authentication

  • middleware.js
  • Check for token in request header 'Authorization' to verify valid login sesson of user before create, update, delete tweets.

II. USER

[POST] /api/user/login

-Response:

{
    "status": 200,
    "message": "Successfully logged in!",
    "data": {
        "dataUser": {
            "id": string,
            "username": string
        },
        "token": string
    }
} 

[POST] /api/user/register

{
    "status": 201,
    "message": "Register successful!",
    "data": {
        "user": {
            "id": string,
            "username": string
        }
    }
}

III. User Portfolio

[GET] /api/user/:id

  • Response:
    {
        status: 200,
        message: 'Successful!',
        data: {
            wallets: {
                fiat: [{
                    id: string,
                    currency: string,
                    balance: float
                }],
            stocks: [{
                id: string,
                symbol: string,
                quantity: integer
            }]
        }
    }
    

IV. User Wallet

  • Each wallet should has:
    • wallet_type: 'fiat' or 'stock'
    • unique currency ('usd', 'cad', 'vnd', 'jpy'...)
    • unique stock symbol ('AAPL', 'IBM', 'TSLA' ...)

1. Create User Wallet

[POST] /api/wallet/create

  • Request params:
    {
        wallet_type: 'fiat',
        user_id: string,
        currency: string
    }
    
  • Successfull response:
    {
        status: 200,
        message: "Created fiat wallet successfully!"
        data: {
            id: string,
            wallet_type: 'fiat',
            owner_id: string,
            currency: string,
            balance: float, // default: 0
        }
    
    }
    

[POST] /api/wallet/create

  • Request params:
    {
        wallet_type: 'stock',
        user_id: string,
        symbol: string,
    }
    
  • Successfull response:
    {
        status: 200,
        message: "Created wallet successfully!"
        data: {
            id: string,
            owner_id: string,
            wallet_type: 'stock',
            stock: string,
            symbol: string,
            quantity: integer, // default: 0
            }
        }
    }
    

2. Add balance/quantity Wallet

2.1 Deposit fiat balance

[PUT] /api/wallet/deposit/:id

  • Request Params:
    {
        wallet_type: 'fiat',
        currency: string,
        quantity: float
    }
    
  • Successfull response:
    {
        status: 200,
        message: "Deposit successfully!"
        data: {
            id: string,
            owner_id: string,
            wallet_type: 'fiat',
            currency: string,
            balance: float
        }
    
    }
    

2.2 Deposit Stock balance

[POST] /api/wallet/deposit/:id

  • Request Params:
    {
        wallet_type: 'stock',
        stock_id: string,
        quantity: float
    }
    
  • Successfull response:
    {
        status: 200,
        message: "Deposit successfully!"
        data: {
            id: string,
            owner_id: string,
            wallet_type: 'stock',
            stock: string,
            symbol: string,
            quantity: integer
            }
        }
    
    }
    

V. Add BUY/SELL Shares

[POST] /api/stock/shares/add

  • Request params:

    {
        wallet_id: string,
        order_type: string, // 'BUY' | 'SELL'
        quantity: integer,
        price: float,
        fiat_wallet_id: string,
    }
    
  • Successful response:

    {
        status: 200,
        message: 'Successful!',
        data: {
             wallets: {
                fiat: [{
                    id: string,
                    currency: string,
                    balance: float
                }],
            stocks: [{
                id: string,
                symbol: string,
                quantity: integer
            }]
        }
    }
    

VI. Stock Price

Shortcut meaning

o: open price c: close price h: high price l: low price

1. Create stock

[POST] /api/stock/create

  • Request params:
    {
        symbol: string,
    }
    
  • Successful response:
    {
        status: 200,
        message: 'Successful!'
        data: [{
            id: string,
            symbol: string,
            company_name: string,
            refreshed_time: string,
            time_zone: string,
            interval: string,
            currency: string,
            o: float,
            c: float,
            h: float,
            l: float
        }]
    }
    ````   ```
    

2. Update stock price info

This API should be run by a scheduled cron job INTRADAY INFO will be fetch from this source: alphavantage

[UPDATE] /api/stocks/:id

  • Request:
    {
        o: float,
        c: float,
        h: float,
        l: float,
        time: string
        currency: string
    }
    

stock-trading's People

Contributors

judybui127 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.