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}")