Buying Stocks In Firebase Cloud Functions

buckydroid
I want to send buy/sell signal webhook triggers from my tradingview account to firebase cloud function and then execute the order according to that.

But Zerodha requiring me to generate a request_token for every time the webhook gets triggered. How can I request a new request_token programmatically.

And is there any example of zerodha order creation using nodejs anywhere? I checked the documentation https://kite.trade/docs/kiteconnectjs/v3/KiteConnect.html#placeOrder

It says placeholder takes two strings variety and params.

placeOrder(variety, params)

Is params a json object? Or how am I gonna add all the details like symbol, exchange, quantity in one string?

Need help :(
  • SRIJAN
    You don't need to generate a new request token everytime you make a request. You can generate request token once every day,use that to generate access token,and save the access token in your database. This access token is valid for one whole day. Use set access token method to make requests.
    https://kite.trade/docs/kiteconnectjs/v3/KiteConnect.html#setAccessToken

    Exchange mandates the user to login manually atleast once a day. So,automating login is not recommended.

    Params is a javascript object with tradingsymbol, exchange etc. as key-value pairs.
  • buckydroid
    buckydroid edited April 2022
    @SRIJAN the token only works for once, when I start a session by loading the url again, it gives the "Token is invalid or has expired." error.

    Here's the cloud function code I'm testing...
    exports.tradeOnZerodha = functions.https.onRequest(async (req, res) => {
    const request_token = await (
    await db.ref("algo_trades/request_token").once("value")
    ).val();


    kc.generateSession(request_token, security_key)
    .then(function(response) {
    kc.getMargins()
    .then(function(response) {
    res.send(response);
    })
    .catch(function(err) {
    res.send(err);
    });
    })
    .catch(function(err) {
    res.send(err);
    });
    });
  • buckydroid
    So basically everytime I call generateSession the token expires. I guess it's not really possible to do that in firebase cloud functions as everytime we call a function, it resets everything (As per my knowledge).Please lemme know if I'm wrong.
  • SRIJAN
    SRIJAN edited April 2022
    A request token is valid only for a few minutes. After that, it expires. You have to enter request token and api_secret in the generate session function and retrieve the access_token from the response. And use that access token for the whole day. You can learn login flow here:
    https://kite.trade/docs/connect/v3/user/
  • buckydroid
    @SRIJAN decided to host a standalone nodejs express app in the end. But problem is I'm not really active at indian trading hours. So if I refresh the token at around 12am to 3am midnight. Will it work for the next 24 hours?

    I mean if for some reasons it expires in trading hours, it can cause some problems or possibly losses.
  • SRIJAN
    It's recommended to generate access token after 7:35 AM IST .
    https://kite.trade/forum/discussion/comment/20193/#Comment_20193
  • buckydroid
    Damn. Everything is against my requirement.
Sign In or Register to comment.