Node OHLC server
Gihub Repo link :- https://github.com/VaibhavAjayGupta/ohlc
Steps to run
- git clone git@github.com:VaibhavAjayGupta/ohlc.git
- cd ohlc
- npm install
- npm run start
- open localhost:3000 on browser
- subscribe to XZECXXBT
Design
-
Async line by line file reading (using stream) module readline. - for reading the data line by line using stream and non blocking.
-
2 workers
generateChartData - for parsing the response , computing the OHLC packet, and constructing BAR chart data.
webSocketServer - for implementing the websocket which maintains the client list and publish the data which is computed in realtime.
Classes & Methods
StockBarData class is used for storing the bar data.
Methods:-
- constructor(open,symbol,volume,bar_num) - for creating new bar with open, symbol, volume and bar_num.
- updateVolume(value) - for updating volume of bar.
- updateClose() for updating the close.
- updateLow(value) for updating low.
- updateHigh(value) for updating high.
PubSubManager is used for maintaining the list of subscribed clients and publish the data.
Methods:-
- subscribe(subscriber, channel) - for subscribing to a particular stock(channel).
- publish(channel, message) - for publishing a message to a particular stock(channel).
- broker() - for publishing messages to all the stocks(channels).
DataStructers
- Map is used for storing different subscribers for different stocks.
- Map is used for storing different bars.
Flow of project
- Reading file using a readline module which reads the file using stream, line by line without blocking the main thread.
- The line by line response is then posted immediately to a separate worker (generateChartData) for parsing, computing the OHLC packet, and constructing BAR chart data.
- Realtime computed data is then posted to a separate worker (webSocketServer).
- webSocketServer maintains the list of subscribed clients and publish the response in realtime as received from the generateChartData.
- OHLC data is sent on Every trade.
- Output on Every Trade {"o":0.01762,"h":0.01762,"l":0.01762,"lastTrade":0.01762,"c":0.01762,"volume":7.8251401,"event":"ohlc_notify","symbol":"XZECXXBT","bar_num":10631}
Note :
- You won't see any data after subscribing to a particular stock because as soon as the node server is started it's done reading all the data from file and all the data is pushed to the already exisiting clients. As there is no new data pushed to the websocket there is no data recieved by the client.
- TS2 TimeStamp conversion - first 16 digits are the seconds, and the last 9 are nanoseconds.