Algorithmic Stock Trading Using Node.js (2024)

Build an algorithmic trading bot with your strategy.

Published in

Geek Culture

·

4 min read

·

Jan 19, 2021

--

Algorithmic Stock Trading Using Node.js (3)

This article’s motive is to give you the vital information for building an algorithmic trading bot with Node.js, and don’t take that information as financial advice. When I did little research on algorithm trading, I found out that it is costly, and only a few brokerages provide these features with high commission. Algorithm trading has incredible benefits like we don’t require to waste our time by analyzing the stock price every day. It could automate our manual analysis with many stocks, so I decided to build a prototype with a simple strategy in a scalable manner.

Algorithmic Stock Trading Using Node.js (4)

Architecture is pretty simple; it has three modules.

  • Data Gateway
  • Strategy Machine
  • Email Notification

Data Gateway fetches the stock information from IEX Cloud API. The retrieved stock information sent to the Strategy Machine, and at their do some calculation and based on its results, select some stocks to buy. Strategy Machine will return those selected stocks to the Data Gateway. Finally, the selected stock will email us through the Email Notification Module.

Before explaining each module, it is better to explain the example strategy we are going to execute. Thus you will get an idea of how the system works and could customize strategy as you prefer.

Strategy

“You make your money when you buy, not when you sell.” ~Robert Kiyosaki

The above quote inspires me to design the strategy we use here as an example. Once again, I remained this article is not for financial advice. The strategy is like each day; we get a company’s stock prices for the last 30 days and check whether the current price is lower than the previous 30 days price. If the current price is lower than the last 30 days, we go for a buy.

Data Gateway

It’s an interface module that communicates with the rest of the module. Initially, Data Gateway fetches stock information from IEX Cloud API. You could also create an account at IEX Cloud and get a token for accessing the API. The collected stock information will transfer to the Strategy Machine. The Strategy Machine executes the strategy explained above, chooses some stocks based on the process, and returns them to the Data Gateway. Then Email Notification collects the selected stocks from Data Gateway and sends us through email. Data Gateway will have a setInterval function because we need to execute this strategy each day automatically.

The above code snippet describes the Data Gateway, and on line number 8, you can put your token from IEX Cloud API. The setInterval function on line 21 repeatedly calls the function getStockData() each day, which performs the task of getting data from API, sending data to the Strategy Machine, and emailing the selected stocks.

Strategy Machine

The Strategy Machine does the calculation for selecting the preferred stocks to buy. In our strategy, this module needs to analyze the company’s stock price for 30 days. If the current price is lesser than the previous days’ price, Strategy Machine adds that stock to the buy list. Refer to the below code for knowing how its works.

The Strategy Machine is an independent module suppose to do only the calculation for selecting a preferred stock. In this module, you could define your preferred strategy and return the chosen stocks as an array to the Data Gateway.

Email Notification

Duty of Email Notification is straightforward email us the stock returns from the Strategy Machine. Email Notification code snippet is attached below.

For auth information, provide your email credentials and customize the object mailOptions on line number 11 as your preferences. If you are going to use a Gmail service like the above reference code, don’t forget to enable less secure apps to access Gmail; otherwise, it will block your server’s email.

git clone https://github.com/krishheii/AlgorithmStockTrading.git
npm i
node dataGateway.js

I build this system only for educational purpose so don’t take it as financial advice and lose your investments, even the example strategy I have described never even backtracked myself. If you have an excellent strategy, implement it in this system and backtrack with previous data. Like other algorithmic trading bots, our system won’t take trades itself; it only sends you the selected stocks to buy through email. The final decision is up to you whether needed to purchase the stock or not. The system aims to provide an extra eye to watch the market, and the source code of the project has been attached below.

Algorithmic Stock Trading Using Node.js (2024)
Top Articles
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 5576

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.