Files

36 lines
1.3 KiB
Python

import os
import logging
from dotenv import load_dotenv
from src.api.client import Trading212Client
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def test_order():
load_dotenv()
api_key_id = os.getenv("TRADING212_API_KEY_ID")
api_key = os.getenv("TRADING212_API_KEY")
base_url = os.getenv("TRADING212_BASE_URL", "https://demo.trading212.com/api/v0/")
client = Trading212Client(api_key_id, api_key, base_url)
logger.info("Attempting to place a test Limit Order (Buy 0.1 AAPL @ $1.00)...")
try:
# We place a limit order far away from the current price so it won't fill
response = client.place_limit_order("AAPL_US_EQ", 0.1, 1.00)
logger.info(f"Success! Response: {response}")
# If successful, immediately cancel it
order_id = response.get('id')
if order_id:
logger.info(f"Cancelling test order {order_id}...")
client.cancel_order(order_id)
logger.info("Cancelled.")
except Exception as e:
logger.error(f"Failed: {e}")
# If it's a requests HTTPError, let's print the raw response text for debugging
if hasattr(e, 'response') and e.response is not None:
logger.error(f"API Response Body: {e.response.text}")
if __name__ == "__main__":
test_order()