Jump to content

Majoka

Members
  • Posts

    2
  • Joined

  • Last visited

Posts posted by Majoka

  1. Hi, I have also tried the following Python code to extract data. 

    import requests
    # Set your API credentials
    api_key = "xx"
    account_id = 'xx'
     
    # Set the API endpoint and parameters
    endpoint = 'https://demo-api.ig.com/gateway/deal/session'
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'X-IG-API-KEY': api_key,
        'Authorization': f'Bearer {account_id}',
    }
    params = {
        'resolution': 'D',  # Daily resolution
        'epic': 'CS.D.BHP.LON.IP',  # BHP market identifier
        'pageSize': 10,  # Number of prices to fetch
    }
     
    # Make the API request
    response = requests.get(endpoint + 'prices', headers=headers, params=params)
     
    # Check if the request was successful
    if response.status_code == 200:
        # Extract the close prices from the response
        data = response.json()
        close_prices = [price['closePrice'] for price in data['prices']]
        print(close_prices)
    else:
        print('Error occurred:', response.text)

    It gives me the following error message. 

    Error occurred: <html><head><title>Page Not Found</title><h3>Error 404</h3><h3>Sorry, the requested page is not available.</h3></body></html>

    So basically, I am not sure how to extract price data via the API using Python. 

  2. Hi, I have an API key and it seems to be working. For example, I can run the following command on my Python console and get a result. 

    results = ig_service.search_markets("bhp")
    results

    But when I try to run this, it gives me an exception error: unauthorised.access.to.equity.exception

    from trading_ig import IGService
    from trading_ig.config import config
     
    class config(object😞
        username = "xx"
        password = "xx"
        api_key = "xx"
        acc_type = "xx"
        acc_number = "xx"
     
    ig_service = IGService(config.username, config.password, config.api_key, config.acc_type)
    ig_service.create_session()
     
    # Fetch close prices for BHP from IG Markets
    epic_id = "AA.D.BHP.CASH.IP"
    resolution = "D"
    num_points = 10
     
    response = ig_service.fetch_historical_prices_by_epic_and_num_points(epic_id, resolution, num_points)
    close_prices = [float(price["closePrice"]["bid"]) for price in response["prices"]]
     
    print(close_prices)
     
    open_positions = ig_service.fetch_open_positions()
    print("open_positions:\n%s" % open_positions)
     
    Can you please help?
×
×
  • Create New...
us