gregtuc / stocksocket Goto Github PK
View Code? Open in Web Editor NEWGet Real-Time Stock Data
License: MIT License
Get Real-Time Stock Data
License: MIT License
Hi Greg,
Great Work.
How can we add realtime volume updates to this script?
Thanks in advance.
Hey :)
i think directly using the finStreamer from yahoo is problematic because of copyright reasons. Its rather easy to reverse engineer the code though. After i used this library initially, i independently realized, that yahoo uses websockets and built my own (naive) implementation. I just need the current pricedata so my PricingData is rather simple, but it can easily be extended.
I think just reverse engineering the datastructure and building it yourself (like i did) is certainly the safer approach :)
const WebSocket = require("ws");
const { Type, Field } = require("protobufjs");
var PricingData = new Type("PricingData")
.add(new Field("symbol", 1, "string"))
.add(new Field("price", 2, "float"));
module.exports = class StockTicker {
#client = null;
#updateCallback = null;
#symbols = [];
constructor(symbols, updateCallback) {
this.symbols = symbols;
this.#updateCallback = updateCallback;
}
get symbols() {
return this.#symbols;
}
set symbols(newSymbols) {
if (Array.isArray(newSymbols)) {
this.#symbols = newSymbols;
if (this.#client) {
this.#sendSymbolMessage();
}
} else {
throw new TypeError("stocks needs to be an array of strings");
}
}
#sendSymbolMessage() {
console.log("Requesting symbols", this.symbols);
this.#client.send(JSON.stringify({ subscribe: this.symbols }));
}
#onMessage(data) {
const binary = Buffer.from(data, "base64");
try {
const msg = PricingData.decode(binary);
const msgObj = PricingData.toObject(msg, { longs: Number });
console.log(msgObj);
if (this.#updateCallback) {
this.#updateCallback(msgObj);
}
} catch (e) {
console.log(e);
}
}
activate() {
if (this.#client) {
console.warn("Already Active");
return;
}
this.#client = new WebSocket("wss://streamer.finance.yahoo.com/");
this.#client.once("open", () => {
console.log("connected to yahoo finance socket");
this.#sendSymbolMessage();
});
this.#client.on("message", this.#onMessage.bind(this));
}
deactivate() {
if (!this.#client) {
console.warn("Already Inactive");
return;
}
this.#client.terminate();
this.#client = null;
}
};
Module works fine for server-side implementation but has issues running client-side.
If not, is something like this planned?
Automatically switch selectors based on the active trading period.
Pre-Market selector: "#quote-header-info > div.Pos\(r\) > div.D\(ib\) > p > span"
Post-Market selector: "#quote-header-info > div.Pos\(r\) > div > p > span"
Standard-Market selector: #quote-header-info > div.Pos\(r\) > div > div > span"
Crypto selector (24/7): "#quote-header-info > div.D\(ib\) > div > div > span"
Hi, first of all I must thank you for creating such wonderful yahoo live streamer ! I am Vishvesh Upadhyay from India. All the questions below are for NSE (National Stock Exchange) India.
I am very new to programming world ! and almost first time using Node.Js and JS program / module.
I am currently using yahooquery with sqlite3 on google colab I am facing quite an issue regarding timing of the price-volume data.
I need your guidance on how use your StockSocket. (because in python i just pip install -- and run code from Spyder and it gives me the result, I downloaded you repo and create package with npm init and npm install but dont know how to start the actual web streaming !)
Can you guide me how to setup it ? (I just need some direction for how to run it and get live streaming data then after I will use it with my python code)
I am running a python code which is fetching currently 40 tickers at once in almost 1 to 1.2 seconds. With your code is it possible to stream 40 tickers (or my target 160 tickers) simultaneously and reliably ?
If possible i need your views on if connection is dropped or disconnected then is there any mechanism to reconnect it automatically ?
Also i need your suggestion for my project. I am creating an stock alert system (then planning to upgrade it to full fledged algo system). My plan is to get data live data from StockSocket -> then feed it to Python - Pandas -> then to Sqlite3 Local database -> fetch multi timeframe OHLCV candle from DB using Pandas -> then running different analysis on them -> create alert system (max time for all tasks is up to 5-7 secs) and then evolve it to full fledged algo system.
Thanks and reply soon !
Regards,
Vishvesh Upadhyay.
Setup continuous integration, unit tests, and continually upgrade them
Hey, nice socket client! We use yahoo as a fallback data provider.
I lead engineering at Fey and we're looking for engineers (Backend | TS/Node). Wondering if you'd be interested to learn more?
if not, Are there any plans for making typing?
I want to pull the current data and not updates, I was wondering if this module supports this, or if I should use another module.
Thanks.
I am getting this error for each headless page it open.
Error: Evaluation failed: TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'.
at puppeteer_evaluation_script:26:16
at ExecutionContext._evaluateInternal (Q:\Node\Trader\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:218:19)
at processTicksAndRejections (node:internal/process/task_queues:94:5)
at async ExecutionContext.evaluate (Q:\Node\Trader\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:107:16)
at async startDataFeed (Q:\Node\Trader\node_modules\stocksocket\stocksocket.js:76:5)
And my project contains only the code example.
on starting it is just giving message "StockSocket has opened a WebSocket Connection with Yahoo." else the callback is not getting executed
Current Functionality: Insert headless puppeteer instances and use mutation observers to detect changes in HTML text. This method is effective but needlessly intensive WRT resources and can error out if the DOM is ever changed.
New Functionality: Intercept Yahoo Finance websocket traffic directly.
General Steps:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.