Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.

Flattrade api - place order sample program

Last updated on 12 days ago
K
KevinMember
Posted 12 days ago
import requests
import json

# Configuration with your details
USER_ID = "XXXXXXX" # Your User ID (Client ID)
ACCOUNT_ID = "XXXXXXX" # Your Account ID (same as User ID)
ACCESS_TOKEN = "56f5c0a9ceee0ba5d357k232b7a55ca9cab5832f8447a77256ad264159b79177" # Your Access Token
PLACE_ORDER_URL = "https://piconnect.flattrade.in/PiConnectTP/PlaceOrder" # Flattrade PlaceOrder API URL

# Prepare the order data as a valid dictionary
order_data = {
"uid": USER_ID, # User ID
"actid": ACCOUNT_ID, # Account ID
"exch": "NSE", # Exchange (NSE, NFO, BSE, MCX)
"tsym": "ACC-EQ", # Trading symbol (ensure this is correct)
"qty": "50", # Quantity (ensure this is a valid number)
"prc": "2254", # Price (ensure this is a valid number)
"prd": "C", # Product type (CNC, NRML, etc.)
"trantype": "B", # Transaction type ('B' for buy, 'S' for sell)
"prctyp": "MKT", # Price type ('LMT', 'MKT', etc.)
"ret": "DAY" # Retention type ('DAY', 'EOS', 'IOC')
}

# Convert the order data into a JSON string (simulating curl --data behavior)
jData = json.dumps(order_data)

# Prepare the payload (just like in the curl example)
payload = f"jData={jData}&jKey={ACCESS_TOKEN}"

# Set the headers to specify the content type
headers = {
"Content-Type": "application/x-www-form-urlencoded" # Use x-www-form-urlencoded format
}

# Make the API request to place the order
response = requests.post(PLACE_ORDER_URL, headers=headers, data=payload)

# Debugging output
print("Response Status Code:", response.status_code)
print("Response Content:", response.text)

if response.status_code == 200:
try:
data = response.json()
if data["stat"] == "Ok":
print("Order placed successfully!")
print("Order Details:")
print(json.dumps(data, indent=4)) # Pretty print the order details
else:
print(f"Error in API response: {data.get('emsg', 'Unknown error')}")
except Exception as e:
print(f"Error parsing response JSON: {str(e)}")
else:
print(f"Failed to place order. HTTP Status: {response.status_code}")
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.
Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 2
Members Online 0

Total Members: 11
Newest Member: Jhilam