Jump to content
  • 0

Trading API Base blueprint


lukeTradingLearner

Question

Hi im new to ig and automation . im interested in the trading API and making automated scripts. I am interested in making sculping scripts. I have a few questions that i cant find.

  • What's the api Request limit. there is usually a cap per month or something?
  • How fast does the api place orders
  • When I made my fist complete script where is the best place to host a server that uses the script to reduce latency(what area are the ig servers located I want to host serve close to them)
  • How do i find out what times the market is open. im using EUR/USD in this script.

I am working on the foundation to all my trading strategies. i want to download historical data and store it to a csv file. every 5 min it will add new data to the end of the file. also it checks if market is open so does not waste api requests. Im very new to coding and automation and there hardly anything online. if anyone has suggestions/tips to deploy trading strategies would be very helpful. 

Here is my foundation of my script that i made so far so you can see my approach of using the api:

import requests
import pandas as pd
import json
from datetime import datetime
import time
from datetime import time as dt_time
api_key = 'api_key'
ig_username = 'usename'
ig_password = 'password'
base_url = 'https://demo-api.ig.com/gateway/deal' #change for live

headers = {
    'Content-Type': 'application/json; charset=UTF-8',
    'Accept': 'application/json; charset=UTF-8',
    'X-IG-API-KEY': api_key,
    'Version': '2'
}
def authenticate():
    data = {
        "identifier": ig_username,
        "password": ig_password
    }
    response = requests.post(f"{base_url}/session", headers=headers, json=data)
    if response.status_code == 200:
        if 'CST' in response.headers and 'X-SECURITY-TOKEN' in response.headers:
            return response.headers['CST'], response.headers['X-SECURITY-TOKEN']
        else:
            raise ValueError(f"Error: 'CST' or 'X-SECURITY-TOKEN' not found in response headers. Response: {response.text}")
    else:
        raise ValueError(f"Error {response.status_code}: {response.text}")

def get_eur_usd_data(cst, x_security_token, start_date, end_date):
    headers_with_token = headers.copy()
    headers_with_token['CST'] = cst
    headers_with_token['X-SECURITY-TOKEN'] = x_security_token
    resolution = 'MINUTE_5'
    params = {
        'resolution': resolution,
        'from': start_date,
        'to': end_date
    }
    request_url = f"{base_url}/prices/CS.D.EURUSD.MINI.IP"
    response = requests.get(f"{base_url}/prices/CS.D.EURUSD.MINI.IP/"+resolution+"/"+start_date+"/"+end_date,headers=headers_with_token)
    if response.status_code == 200:
        data = json.loads(response.text)
        return data['prices']
    else:
        raise ValueError(f"Error {response.status_code}: {response.text}")

def is_market_open_manual():
    # Define market hours (e.g., 5 PM Sunday to 5 PM Friday EST)
    open_time = dt_time(17, 0, 0)
    close_time = dt_time(17, 0, 0)
    start_of_week = 6  # Sunday
    end_of_week = 4  # Friday

    now = datetime.utcnow()
    now_time = now.time()
    now_weekday = now.weekday()

    if start_of_week <= now_weekday <= end_of_week:
        if start_of_week == now_weekday:
            return now_time >= open_time
        elif end_of_week == now_weekday:
            return now_time < close_time
        else:
            return True
    else:
        return False
def is_market_open(cst, x_security_token): #Note uses api request have not uses yet
    headers_with_token = headers.copy()
    headers_with_token['CST'] = cst
    headers_with_token['X-SECURITY-TOKEN'] = x_security_token
    epic = 'CS.D.EURUSD.MINI.IP'

    request_url = f"{base_url}/markets/{epic}"
    response = requests.get(request_url, headers=headers_with_token)
    if response.status_code == 200:
        data = json.loads(response.text)
        return data['snapshot']['marketStatus'] == 'TRADEABLE'
    else:
        raise ValueError(f"Error {response.status_code}: {response.text}")

def update_csv(filename, data):
    df = pd.DataFrame(data)
    df['datetime'] = pd.to_datetime(df['snapshotTime'], unit='ms')
    df.set_index('datetime', inplace=True)

    if not df.empty:
        try:
            existing_df = pd.read_csv(filename, index_col='datetime', parse_dates=True)
            df = existing_df.append(df)
            df.to_csv(filename)
        except FileNotFoundError:
            df.to_csv(filename)
def main():
    filename = 'eur_usd_5min_data.csv'
    retry_limit = 3
    retry_delay = 60  # Time in seconds between retries

    while True:
        try:
            cst, x_security_token = authenticate()

            if is_market_open_manual():
                try:
                    existing_df = pd.read_csv(filename, index_col='datetime', parse_dates=True)
                    start_date = existing_df.index[-1].strftime('%Y-%m-%d %H:%M:%S')
                except FileNotFoundError:
                    start_date = '2023-03-01 00:00:00'

                end_date = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')

                data = get_eur_usd_data(cst, x_security_token, start_date, end_date)
                update_csv(filename, data)
                print("Data updated.")
            else:
                print("Market is closed, no data is retrieved.")

            # Sleep for 5 minutes (300 seconds)
            time.sleep(300)

        except (ValueError, requests.exceptions.RequestException) as e:
            print(f"An error occurred: {e}")
            retry_limit -= 1
            if retry_limit > 0:
                print(f"Retrying... {retry_limit} attempts remaining.")
                time.sleep(retry_delay)
            else:
                print("Retry limit reached. Exiting.")
                break

if __name__ == '__main__':
    main()
 

Link to comment

8 answers to this question

Recommended Posts

  • 0
1 hour ago, lukeTradingLearner said:

Hi im new to ig and automation . im interested in the trading API and making automated scripts. I am interested in making sculping scripts. I have a few questions that i cant find.

  • What's the api Request limit. there is usually a cap per month or something?
  • How fast does the api place orders
  • When I made my fist complete script where is the best place to host a server that uses the script to reduce latency(what area are the ig servers located I want to host serve close to them)
  • How do i find out what times the market is open. im using EUR/USD in this script.

I am working on the foundation to all my trading strategies. i want to download historical data and store it to a csv file. every 5 min it will add new data to the end of the file. also it checks if market is open so does not waste api requests. Im very new to coding and automation and there hardly anything online. if anyone has suggestions/tips to deploy trading strategies would be very helpful. 

Here is my foundation of my script that i made so far so you can see my approach of using the api:

import requests
import pandas as pd
import json
from datetime import datetime
import time
from datetime import time as dt_time
api_key = 'api_key'
ig_username = 'usename'
ig_password = 'password'
base_url = 'https://demo-api.ig.com/gateway/deal' #change for live

headers = {
    'Content-Type': 'application/json; charset=UTF-8',
    'Accept': 'application/json; charset=UTF-8',
    'X-IG-API-KEY': api_key,
    'Version': '2'
}
def authenticate():
    data = {
        "identifier": ig_username,
        "password": ig_password
    }
    response = requests.post(f"{base_url}/session", headers=headers, json=data)
    if response.status_code == 200:
        if 'CST' in response.headers and 'X-SECURITY-TOKEN' in response.headers:
            return response.headers['CST'], response.headers['X-SECURITY-TOKEN']
        else:
            raise ValueError(f"Error: 'CST' or 'X-SECURITY-TOKEN' not found in response headers. Response: {response.text}")
    else:
        raise ValueError(f"Error {response.status_code}: {response.text}")

def get_eur_usd_data(cst, x_security_token, start_date, end_date):
    headers_with_token = headers.copy()
    headers_with_token['CST'] = cst
    headers_with_token['X-SECURITY-TOKEN'] = x_security_token
    resolution = 'MINUTE_5'
    params = {
        'resolution': resolution,
        'from': start_date,
        'to': end_date
    }
    request_url = f"{base_url}/prices/CS.D.EURUSD.MINI.IP"
    response = requests.get(f"{base_url}/prices/CS.D.EURUSD.MINI.IP/"+resolution+"/"+start_date+"/"+end_date,headers=headers_with_token)
    if response.status_code == 200:
        data = json.loads(response.text)
        return data['prices']
    else:
        raise ValueError(f"Error {response.status_code}: {response.text}")

def is_market_open_manual():
    # Define market hours (e.g., 5 PM Sunday to 5 PM Friday EST)
    open_time = dt_time(17, 0, 0)
    close_time = dt_time(17, 0, 0)
    start_of_week = 6  # Sunday
    end_of_week = 4  # Friday

    now = datetime.utcnow()
    now_time = now.time()
    now_weekday = now.weekday()

    if start_of_week <= now_weekday <= end_of_week:
        if start_of_week == now_weekday:
            return now_time >= open_time
        elif end_of_week == now_weekday:
            return now_time < close_time
        else:
            return True
    else:
        return False
def is_market_open(cst, x_security_token): #Note uses api request have not uses yet
    headers_with_token = headers.copy()
    headers_with_token['CST'] = cst
    headers_with_token['X-SECURITY-TOKEN'] = x_security_token
    epic = 'CS.D.EURUSD.MINI.IP'

    request_url = f"{base_url}/markets/{epic}"
    response = requests.get(request_url, headers=headers_with_token)
    if response.status_code == 200:
        data = json.loads(response.text)
        return data['snapshot']['marketStatus'] == 'TRADEABLE'
    else:
        raise ValueError(f"Error {response.status_code}: {response.text}")

def update_csv(filename, data):
    df = pd.DataFrame(data)
    df['datetime'] = pd.to_datetime(df['snapshotTime'], unit='ms')
    df.set_index('datetime', inplace=True)

    if not df.empty:
        try:
            existing_df = pd.read_csv(filename, index_col='datetime', parse_dates=True)
            df = existing_df.append(df)
            df.to_csv(filename)
        except FileNotFoundError:
            df.to_csv(filename)
def main():
    filename = 'eur_usd_5min_data.csv'
    retry_limit = 3
    retry_delay = 60  # Time in seconds between retries

    while True:
        try:
            cst, x_security_token = authenticate()

            if is_market_open_manual():
                try:
                    existing_df = pd.read_csv(filename, index_col='datetime', parse_dates=True)
                    start_date = existing_df.index[-1].strftime('%Y-%m-%d %H:%M:%S')
                except FileNotFoundError:
                    start_date = '2023-03-01 00:00:00'

                end_date = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')

                data = get_eur_usd_data(cst, x_security_token, start_date, end_date)
                update_csv(filename, data)
                print("Data updated.")
            else:
                print("Market is closed, no data is retrieved.")

            # Sleep for 5 minutes (300 seconds)
            time.sleep(300)

        except (ValueError, requests.exceptions.RequestException) as e:
            print(f"An error occurred: {e}")
            retry_limit -= 1
            if retry_limit > 0:
                print(f"Retrying... {retry_limit} attempts remaining.")
                time.sleep(retry_delay)
            else:
                print("Retry limit reached. Exiting.")
                break

if __name__ == '__main__':
    main()
 

Hi @lukeTradingLearner

Thanks for reaching out. Have a look at this link: https://labs.ig.com/faq

It answers most of your questions if not all. Please let me know if you still need assistance. We are more than happy to help. 

Thanks,

Ofentse

 

Link to comment
  • 0

It looks like you are just trying to make a price action / time series type strategy, it could be easier to use ProReal time by enabling in settings, the advantage is you can backtest and visualise the strategy and also run on the ProOrder servers. I use pro real time to test that sort of thing and then jump over to a platform called quantconnect to test a wider range of products and data sets with other brokers, the advantage there is massive data sets (fundamental etc) and the option to use either C# or Python. the downside is no IG markets data or API, I am interested in making an API for IG on quantconnect some time in the future but I have no experience building API's, Quantconnect does offer $5000USD to anyone who produces a new broker API and IG markets has been a popular request.

Edited by KJM34
  • Like 1
Link to comment
  • 0

Using, the Oanda brokerage template that already exists in quantconnects Lean engine repository on GitHub and the IG markets API in Labs it wouldn't be to hard to knock something up as they both use the same type of API as far as I can tell. that's about as far as I delved into that idea though. it would still require access to the historical datasets which IG markets probably already has on request by an api.

Edited by KJM34
  • Like 1
Link to comment
  • 0

Thank you for suggestions literally started making automated strategy's 2 weeks ago. learned a lot by the time I made this post. that script was not a strategy yet it was just the foundation of just getting historic data and checking if markets are open. we limited to 10000 pulls of historic data per week from the ig api so was just optimizing my script to not exceed that limit. im implementing a technical analysis strategy with good risk management on to that data i pulled. but i am starting to get the hang of it and making good progress and my first script will be operational very soon. just finished with back testing then will test it on demo ig account. ig trading api is really good in my opinion and the documentation is good. plus the commission is just the spread. so for me ig is perfect for my approach to trading. 

Link to comment
  • 0

Hi Guys,

  Just wondered if you had managed to connect Quantconnect to IG spread betting? I'm also very new to it but would love my Quantconnect scripts to run my IG account. Also is there a limit to the number of trades one can make in a week?

 Many thanks in advance for any help!!

 

Link to comment
  • 0

@RJHumphries I am in the process of trying to use the IG Api with Quantconnects lean engine now, this will be my first time using the api so it could take a while. as for the limit to number of trades per week, the answer is no, not if you are running lean engine locally direct with to the broker, but probably yes if you are going via quantconnect (which you cant do with ig markets) Ig markets themselves obviously have no limit, the more trades the better for brokers, but there might be some sort of cap on updates per second, nothing that will create problems for any realistic use case.

Edited by KJM34
Link to comment
  • 0

@lukeTradingLearner how did you go with it? I just started on the api side of things now, I am not sure if the process is worth it or if i just use an already supported broker but i figured i will just have a go at it, looks like youre using js? i have to use C# which means a great deal of reading library documentation 

Link to comment
  • 0
On 08/06/2023 at 19:54, RJHumphries said:

Hi Guys,

  Just wondered if you had managed to connect Quantconnect to IG spread betting? I'm also very new to it but would love my Quantconnect scripts to run my IG account. Also is there a limit to the number of trades one can make in a week?

 Many thanks in advance for any help!!

 

this works, you will just need to set your user name password and api key in IG markets, then you have to use terminal in the IGMarkets folder in the solution to set them as user secrets, chat gpt 4 can explain how to do this, from there implemetation with lean is just a matter of time and effort, most of the hard work appears to have been done already https://github.com/npouzenc/IGMarkets.

dotnet user-secrets init

dotnet user-secrets set "IGMarkets:id" "<Your ID>"

dotnet user-secrets set "IGMarkets:password" "<Your Password>"

dotnet user-secrets set "IGMarkets:apiKey" "<Your API Key>"

 

Edited by KJM34
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • image.png

  • Posts

    • Thank you for the reply. This may not be the case, the market clearly says it is closed when I try to place a trade. Effectively meaning I am unable to trade outside of the hours specified!
    • Apple’s recent stock weakness marks a divergence with other members of the Magnificent 7 and the broader Nasdaq 100. Source: Bloomberg   Indices Shares Apple Inc. Artificial intelligence Price iPhone Written by: Chris Beauchamp | Chief Market Analyst, London   Publication date: Friday 01 March 2024 13:26 AI Efforts in Question Apple's stock fell below $180 on Thursday for the first time since early-November, underperforming the broader market. While the S&P 500 and tech-heavy Nasdaq indices posted solid gains, Apple shares slipped around 1%. Source: Google Finance The decline comes as doubts loom about Apple's artificial intelligence (AI) initiatives. Rivals like Microsoft are delivering strong earnings growth tied to burgeoning AI technology. This was highlighted by Tuesday's report that Apple is discontinuing its decade-long electric vehicle project. Back in 2017, Apple CEO Tim Cook called the autonomous car endeavour the "mother of all AI projects." Stock Underperforms Broader Market So far in 2024, Apple shares have dropped 3%, trailing the S&P 500's 7% gain and the Nasdaq's 9% climb. Despite its long-term market-beating returns, Apple has recently lagged the S&P 500 on 6-month, 1-year, and 2-year timeframes, according to FactSet data. After spending most of 2021 to 2023 as the world's most valuable public company by market capitalization, Apple surrendered that crown to Microsoft in January. Microsoft's sales and profit growth have far outpaced Apple's, which posted negative growth in its 2022 fiscal year ending last September. AI Investment Hints but Details Lacking At Wednesday's shareholder meeting, Cook suggested Apple is "investing significantly" in generative AI. He said more specifics will be announced later this year. UBS analyst David Vogt predicts Apple's first major AI launch will come in June at its annual Worldwide Developers Conference. iPhone Sales Weakness Looms In addition to AI uncertainties, expectations for weak iPhone sales growth continue to weigh on Apple. iPhones accounted for 58% of Apple's total revenue last quarter. Some See Positives in Car Project Halt Some analysts see a silver lining in the halt of Apple's electric car plans. It enables the company to refocus AI talent on nearer-term products with greater market potential. To Morgan Stanley, it also shows Apple's "cost discipline." Apple analyst rating LSEG (formerly known as Refinitiv) data shows a consensus analyst rating of ‘buy’ for Apple with 10 strong buy, 17 buy, 13 hold and 2 sell – and a mean of estimates suggesting a long-term price target of $201.41 for the share, roughly 16% higher than the current price (as of 1 March 2024). Source: LSEG Technical outlook on the Apple share price The Apple share price continues to precariously weigh on its $180.30 to $179.25 support zone which consists of the January-to-February lows. A fall through and daily chart close below this area looks increasingly likely and would lead to levels being reached which were last traded in early-November with the 3 November low at $176.65 representing the first downside target. Apple Daily Candlestick Chart Source: TradingView Further down sits the $174.49 August low below which key support can be spotted between the September and October lows at $167.62 to $165.67. Were the $180.30 to $179.25 support zone to hold, though, a rise and daily chart close above last week’s high at $185.04 would need to occur, for a recovery off the support area to gain traction. In this scenario the Apple share price would trade back above its 200-day simple moving average (SMA) at $183.90 and target the 55-day SMA at $188.50. This will continue to favour a fall through support at $179.25 to take place as long as the Apple share price continues to trade below last week’s high at $185.04.     This information has been prepared by IG, a trading name of IG Markets Limited. In addition to the disclaimer below, the material on this page does not contain a record of our trading prices, or an offer of, or solicitation for, a transaction in any financial instrument. IG accepts no responsibility for any use that may be made of these comments and for any consequences that result. No representation or warranty is given as to the accuracy or completeness of this information. Consequently any person acting on it does so entirely at their own risk. Any research provided does not have regard to the specific investment objectives, financial situation and needs of any specific person who may receive it. It has not been prepared in accordance with legal requirements designed to promote the independence of investment research and as such is considered to be a marketing communication. Although we are not specifically constrained from dealing ahead of our recommendations we do not seek to take advantage of them before they are provided to our clients. See full non-independent research disclaimer and quarterly summary.
    • PG Elliott Wave Analysis Trading Lounge Daily Chart, 1 March 24 The Procter & Gamble Company, (PG) Daily Chart PG Elliott Wave Technical Analysis FUNCTION: Trend MODE: Impulse STRUCTURE: Motive POSITION: Minuette wave (ii) of {iii}. DIRECTION: Bottom in wave (ii).   DETAILS: As we are approaching ATH at 165$, we are expecting either an acceleration higher into wave (3) or else we could have topped in wave (C) to then fall back lower and continue the major correction.         PG Elliott Wave Analysis Trading Lounge 4Hr Chart, 1 March 24 The Procter & Gamble Company, (PG) 4Hr Chart PG Elliott Wave Technical Analysis FUNCTION: Counter Trend MODE: Corrective STRUCTURE: Zigzag   POSITION: Wave a of (ii).   DIRECTION: Downside into wave (ii). DETAILS: Looking for a clear three wave move into wave (ii) to finding support around the 155$ mark.   Welcome to our PG Elliott Wave Analysis Trading Lounge, your ultimate destination for detailed insights into The Procter & Gamble Company (PG) using Elliott Wave Technical Analysis. As of the Daily Chart on 1 March 24, we dive into crucial trends shaping the market.         *PG Elliott Wave Technical Analysis – Daily Chart* In terms of wave dynamics, we identify a trend function with an impulse structure, specifically a motive pattern. The current position is in Minuette wave (ii) of {iii}, indicating a potential bottom in wave (ii). With the approaching all-time high (ATH) at $165, we anticipate either an upward acceleration into wave (3) or a potential top in wave (C), followed by a downward movement to continue the major correction. *PG Elliott Wave Technical Analysis – 4Hr Chart* Here, we explore a counter trend mode with a corrective structure, specifically a zigzag pattern. The present position is in Wave a of (ii), suggesting downside movement into wave (ii). We anticipate a clear three-wave move into wave (ii), aiming to find support around the $155 mark.  
×
×
  • Create New...
us