import requests
import json
# FlatTrade API credentials
BASE_URL = "https://piconnect.flattrade.in/PiConnectTP"
ACCESS_TOKEN = "3f5d7de21j1bbbb1d853ac4c18d221ab84b1ed79037dbc71d9720xa45a071dd6" # Replace with your current token
USER_ID = "XXXXXXX" # Replace with your User ID
ACCOUNT_ID = "XXXXXXX" # Replace with your Account ID
# Headers for API authentication
HEADERS = {
"Content-Type": "application/json",
}
def get_positions():
"""Fetch the current positions from FlatTrade"""
url = f"{BASE_URL}/PositionBook"
jData = {
"uid": USER_ID,
"actid": ACCOUNT_ID,
}
payload = f"jData={json.dumps(jData)}&jKey={ACCESS_TOKEN}"
response = requests.post(url, headers={"Content-Type": "application/x-www-form-urlencoded"}, data=payload)
if response.status_code == 200:
try:
positions = response.json()
if isinstance(positions, list):
return positions # Return the list of positions
else:
print("Unexpected response structure:", positions)
return []
except json.JSONDecodeError:
print("Failed to decode JSON response:", response.text)
return []
else:
print("Failed to fetch positions:", response.json())
return []
# Run the script
positions = get_positions()
if positions:
print("Positions:")
for position in positions:
print(position)
else:
print("No positions found.")
Edited by
Kevin on 14-12-2024 00:49,
8 days ago