Bibin Varghses
Bibin Varghses

Reputation: 192

How can i submit this form from python with JSON file to the specified website?

I would like to generate a JSON file and submit using python. is this possible and how? is it possible to use requests.post for that?

The actual documentation is here. I just what to implement this in python https://kite.trade/docs/connect/v3/basket/
I have added the whole documentation below I am looking to send a combination of 2 or more orders through python api.

I think the last block is exactly what i need but not render link, just execute directly from python, use the kite connect to execute multiple order using basket at the same time using python not java script

Offsite order execution¶ The offsite order execution feature allows you to redirect your users to Kite's exchange approved order page where they place orders and come back to your application seamlessly, like a payment gateway. This way, you do not have to build, maintain, and get exchange approvals for order execution screens. The Kite Publisher program utilises offsite order execution to provide embeddable Javascript+HTML trade buttons that do not require any API integrations.

Initiating orders¶ Example JSON basket

[{
  "variety": "regular",
  "tradingsymbol": "INFY",
  "exchange": "NSE",
  "transaction_type": "BUY",
  "order_type": "MARKET",
  "quantity": 10,
  "readonly": false
}, {
  "variety": "regular",
  "tradingsymbol": "NIFTY15DECFUT",
  "exchange": "NFO",
  "transaction_type": "SELL",
  "order_type": "LIMIT",
  "price": 7845,
  "quantity": 1,
  "readonly": false
},
{
  "variety": "bo",
  "tradingsymbol": "RELIANCE",
  "exchange": "NSE",
  "transaction_type": "BUY",
  "order_type": "LIMIT",
  "product": "MIS",
  "price": 915.15,
  "quantity": 1,
  "stoploss": 5,
  "squareoff": 7,
  "trailing_stoploss": 1.5,
  "readonly": true
}]
Posting the JSON basket


<form method="post" id="basket-form" action="https://kite.zerodha.com/connect/basket">
  <input type="hidden" name="api_key" value="xxx" />
  <input type="hidden" id="basket" name="data" value="" />
</form>

<script>
  document.getElementById("basket").value = your_basket;
  document.getElementById("basket-form").submit();
</script>

It is possible to send multiple orders which the user then confirms on a shopping basket like interface. You should prepare a JSON list of instruments to be traded with the required order parameters and POST it as a form field with the name data along with your api_key to https://kite.zerodha.com/connect/basket.

This is a browser / mobile (webview) request and has to happen at the user's end, although the basket preparation can happen in the backend. The easiest way to make the request is to create a hidden form, insert the JSON payload into it and submit it automatically using Javascript.

If you're preparing the basket client side on your web application, you can use the Kite Publisher javascript plugin to make things easier.

Note

You do not have to initiate a login using the login API to do offsite order execution. If a user is not already logged in, they'll be asked to login, otherwise, they'll be taken to the order basket directly. Either way, in the end, you will receive status and request_token at your redirect_url like in the login flow.

Linking offsite execution to Kite Connect¶ Offsite order execution follows the same flow as login, except for the order basket that appears after the login. Once the user is done placing orders, the basket will redirect back to your registered redirect_login along with a request_key, exactly like login. You are free to use this key to create a new Kite Connect session for further API interactions, or disregard it altogether.

Publisher JS Plugin¶ The Kite Publisher Javascript plugin lets you add one-click trade buttons to your webpage. It works like a basket combined with a payment gateway, where an inline popup opens on your webpage, guides the user through a trade, and lands the user back on your page. As described in the offset order execution section, it is possible to capture the request_token from this flow to start a Kite Connect session as well.

You can add one or more stocks to the basket (maximum 10) dynamically using the Javascript plugin, or embed simple static buttons using plain HTML.

Getting started¶

<script src="https://kite.trade/publisher.js?v=3"></script>

Include Kite Publisher on your webpage by pasting the following script tag at the end of your webpage, just before the closing tag. You only need to include this once to render any number of buttons on a page.

Branded HTML5 buttons¶

<!--  A link that initiates a buy (market) of the SBIN stock //-->

    <kite-button href="#" data-kite="your_api_key"
                data-exchange="NSE"
                data-tradingsymbol="SBIN"
                data-transaction_type="BUY"
                data-quantity="1"
                data-order_type="MARKET">Buy SBI stock</kite-button>

You can use the custom HTML5 tag to render branded Kite buttons that initiate a trade with a single click. The branded buttons work in a similar fashion to social media buttons, and you can include as many as you want on a page.

Custom HTML5 buttons¶

   <a href="#" data-kite="your_api_key"
                data-exchange="NSE"
                data-tradingsymbol="SBIN"
                data-transaction_type="BUY"
                data-quantity="1"
                data-order_type="MARKET">Buy SBI stock</a>
    
    
    <!--  A button that initiates a sell (LIMIT) of the RELIANCE stock //-->
    <button data-kite="your_api_key"
            data-exchange="NSE"
            data-tradingsymbol="RELIANCE"
            data-transaction_type="SELL"
            data-quantity="1"
            data-order_type="LIMIT"
            data-price="100">Buy RELIANCE</button>

<!--  A button that initiates a BO of the RELIANCE stock //-->
<button data-kite="your_api_key"
        data-exchange="NSE"
        data-tradingsymbol="RELIANCE"
        data-transaction_type="BUY"
        data-quantity="1"
        data-order_type="LIMIT"
        data-variety="bo"
        data-product="MIS"
        data-price="915"
        data-stoploss="5"
        data-squareoff="7"
        data-trailing_stoploss="1.5"
        >Buy RELIANCE (Bracket Order)</button>

You can use the HTML5 data attributes on any HTML element and turn it into a trade button which gets invoked with a single click. The examples on the right show a link and a button being turned into trade buttons.

Generating dynamic buttons with Javascript¶

<!-- A Kite button will be generated inside this container //-->
<p id="default-button"> </p>

<!-- The basket will be linked to this element's onClick //-->
<button id="custom-button">Buy the basket</button>
<script>
// Only run your custom code once KiteConnect has fully initialised.
// Use KiteConnect.ready() to achieve this.
KiteConnect.ready(function() {
    // Initialize a new Kite instance.
    // You can initialize multiple instances if you need.
    var kite = new KiteConnect("your_api_key");

    // Add a stock to the basket
    kite.add({
        "exchange": "NSE",
        "tradingsymbol": "INFY",
        "quantity": 5,
        "transaction_type": "BUY",
        "order_type": "MARKET"
    });

    // Add another stock
    kite.add({
        "exchange": "NSE",
        "tradingsymbol": "SBIN",
        "quantity": 1,
        "order_type": "LIMIT",
        "transaction_type": "SELL",
        "price": 105
    });

    // Add a Bracket Order
    kite.add({
        "tradingsymbol": "RELIANCE",
        "exchange": "NSE",
        "transaction_type": "BUY",
        "order_type": "LIMIT",
        "product": "MIS",
        "price": 915.15,
        "quantity": 1,
        "variety": "bo",
        "stoploss": 5,
        "squareoff": 7,
        "trailing_stoploss": 1.5,
        "readonly": true
    });

    // Register an (optional) callback.
    kite.finished(function(status, request_token) {
        alert("Finished. Status is " + status);
    });

    // Render the in-built button inside a given target
    kite.renderButton("#default-button");

    // OR, link the basket to any existing element you want
    kite.link("#custom-button");
});
You can create a basket of stocks and get the plugin to render a Kite button that executes it, or link the basket to your own button (or any HTML element).

The plugin loads it's assets asynchronously, so it's important that you initialise your custom KiteConnect calls after it has fully loaded. You need to use the KiteConnect.ready() function to achieve this.

Upvotes: 0

Views: 488

Answers (1)

cards
cards

Reputation: 4996

For the first part of the question, how to generate a json structure, you can use the json module from the standard library. Then depending where is your json-string, either stored as string variable or as a text file, you can use json.loads or json.load to convert it into a python object. Check the docs for details.

import json


# string from file
with open('file', 'r') as fd:
   py = json.load(fd.read())
   print(type(py))
   # do smt

# from variable string
js_text ="""
[{
      "variety": "regular",
      "tradingsymbol": "INFY",
      "exchange": "NSE",
      "transaction_type": "BUY",
      "order_type": "MARKET",
      "quantity": 10,
      "readonly": false
    }, # ... as above
]"""


py = json.loads(js_text)
print(type(py))

For the second part you should provide a minimal working example... too many possibilities

Edit: second part

Again, there are many many possibilities... you can try using requests module (not from the standard library). Furthermore you should have a look on the response to see what and how you is needed for a connection. Here an indicative example

url = # smt
headers = {} # such as user-agent, ...
req = requests.post(url, json=js_text[0], headers=headers) # post request

eventually try json={'json_payload': js_text[0]}

...and provide some code snapshot!!

Here can you find the doc for requests

Upvotes: 1

Related Questions