iqoptionapi Documentation

repository·master·Indexed 18 days ago

https://github.com/lu-yi-hsun/iqoptionapi

A Python API providing high-level and low-level interfaces for the IQ Option trading platform. It supports account management, real-time data subscriptions for live deals and commissions, and trade execution for Binary, Digital, Forex, Crypto, and CFD instruments. Key features include strike list management for Digital options, leaderboard data retrieval, and order management via buy_order() and change_order().

Tokens
10.6K
Snippets
45
Records
52
Agent score
63%

What's inside iqoptionapi

  1. Monitor and Reconnect connection status

    master

    Use .check_connect() to detect if the WebSocket connection has closed. If it returns False, you should attempt to reconnect by calling .connect() again.

    from iqoptionapi.stable_api import IQ_Option
    
    iqoption = IQ_Option("email", "password")
    check, reason = iqoption.connect()
    
    if check:
        while True:
            if iqoption.check_connect() == False:
                print("try reconnect")
                check, reason = iqoption.connect()
                if check:
                    print("Reconnect successfully")
  2. Choose between High-level and Low-level APIs

    master

    The library provides two ways to interact with IQ Option:

    1. High-level API: Uses iqoptionapi.stable_api.IQ_Option. This is recommended for most users as it is built on top of the low-level API for ease of use.
    2. Low-level API: Uses iqoptionapi.api.IQOptionAPI for direct interaction.
    # High-level api
    from iqoptionapi.stable_api import IQ_Option
    
    # Low-level api
    from iqoptionapi.api import IQOptionAPI
  3. Work with Digital Options

    master

    Digital options involve working with strike prices and instrument IDs.

    Buying Digital Options via Strike List

    1. Subscribe to the strike list: subscribe_strike_list(ACTIVES, duration).
    2. Get the real-time strike list: get_realtime_strike_list(ACTIVES, duration).
    3. Extract the instrument_id from the desired price point.
    4. Execute the trade: buy_digital(amount, instrument_id).

    Buying Digital Options at Current Price

    Use buy_digital_spot(ACTIVES, amount, action, duration) to buy at the current market price without selecting a specific strike.

    Monitoring Digital Options

    • get_digital_spot_profit_after_sale(id): Get Profit/Loss (P/L) after sale.
    • get_digital_current_profit(ACTIVES, duration): Get current price profit.
    • check_win_digital_v2(id): Asynchronous check for win/loss status.
    # Buying digital spot
    I_want_money.subscribe_strike_list("EURUSD", 1)
    data = I_want_money.get_realtime_strike_list("EURUSD", 1)
    # ... pick a price from data ...
    # instrument_id = data[chosen_price]["call"]["id"]
    # buy_check, id = I_want_money.buy_digital(amount, instrument_id)
    
    # OR buy at current price
    id = I_want_money.buy_digital_spot("EURUSD", 1, "call", 1)
  4. Use Nearest Strike Mode for Digital Options

    master

    Nearest Strike mode allows you to trade based on specific strike prices. To use this mode, you must first subscribe to the strike list for a specific asset and duration. Once subscribed, you can retrieve the real-time strike list containing price levels, side (call/put), instrument IDs, and potential profits.

    Workflow:

    1. Call subscribe_strike_list(ACTIVES, duration).
    2. Retrieve data using get_realtime_strike_list(ACTIVES, duration).
    3. Extract the id (instrument_id) from the desired price level and side.
    4. Execute the trade using buy_digital(amount, instrument_id).
    5. Unsubscribe using unsubscribe_strike_list(ACTIVES, duration) when finished.
    from iqoptionapi.stable_api import IQ_Option
    import time
    import random
    
    I_want_money=IQ_Option("email","password")
    I_want_money.connect()
    
    ACTIVES="EURUSD"
    duration=1 # minute 1 or 5
    amount=1
    
    # 1. Subscribe to strike list
    I_want_money.subscribe_strike_list(ACTIVES, duration)
    
    # 2. Get strike data
    data=I_want_money.get_realtime_strike_list(ACTIVES, duration)
    
    # 3. Select a price and get instrument_id
    price_list=list(data.keys())
    choose_price=price_list[random.randint(0,len(price_list)-1)]
    instrument_id=data[choose_price]["call"]["id"]
    
    # 4. Buy
    buy_check, id = I_want_money.buy_digital(amount, instrument_id)
    
    # 5. Cleanup
    I_want_money.unsubscribe_strike_list(ACTIVES, duration)
  5. Track top assets by popularity

    master

    The API allows you to subscribe to updates regarding the most popular assets for a given instrument type.

    Workflow:

    1. Call subscribe_top_assets_updated(instrument_type).
    2. Use get_top_assets_updated(instrument_type) to fetch the current list of top assets.
    3. Call unsubscribe_top_assets_updated(instrument_type) when finished to close the stream.

    Supported instrument types: "binary-option", "digital-option", "forex", "cfd", "crypto"

    # Subscribe to updates
    I_want_money.subscribe_top_assets_updated("digital-option")
    
    # Get updated assets
    top_assets = I_want_money.get_top_assets_updated("digital-option")
    
    # Unsubscribe
    I_want_money.unsubscribe_top_assets_updated("digital-option")
  6. Monitor commission changes

    master

    You can subscribe to real-time commission changes for specific instrument types.

    Supported instrument types: "binary-option", "turbo-option", "digital-option", "crypto", "forex", "cfd"

    Workflow:

    1. Call subscribe_commission_changed(instrument_type) to start the stream.
    2. Periodically call get_commission_change(instrument_type) to retrieve updates.
    3. Call unsubscribe_commission_changed(instrument_type) to stop the stream and save network resources.
    # Subscribe to changes
    I_want_money.subscribe_commission_changed("binary-option")
    
    # Retrieve data
    commissio_data = I_want_money.get_commission_change("binary-option")
    
    # Unsubscribe
    I_want_money.unsubscribe_commission_changed("binary-option")
  7. Use Current Price Mode (Spot) for Digital Options

    master

    Current Price mode (Spot) allows you to buy a digital option at the current market price rather than a specific strike level.

    Key Methods:

    • buy_digital_spot(ACTIVES, amount, action, duration): Executes a trade at the current price. Returns a tuple of (check, id).
    • get_digital_spot_profit_after_sale(id): Retrieves the Profit/Loss (P/L) after the sale is completed for a specific trade ID.
    • get_digital_current_profit(ACTIVES, duration): Retrieves real-time profit data for the asset. Note that the first call might return False; it is recommended to wait a second before polling.
    from iqoptionapi.stable_api import IQ_Option
    
    I_want_money=IQ_Option("email","password")
    I_want_money.connect()
    
    ACTIVES="EURUSD"
    duration=1
    amount=100
    action="put"
    
    # Buy at current price
    # Returns (check, id)
    result = I_want_money.buy_digital_spot(ACTIVES, amount, action, duration)
    print(result)
    
    # Get profit after sale
    # _, id = I_want_money.buy_digital_spot(ACTIVES, amount, action, duration)
    # PL = I_want_money.get_digital_spot_profit_after_sale(id)
  8. Execute trades and check order status with get_async_order()

    master

    After placing a trade using buy(), buy_digital_spot(), or buy_order(), the API returns an id. Use get_async_order(id) to poll for the order's status. This method returns None while the order is still processing and returns the order data once completed.

    # Example for Binary Option
    _, id = I_want_money.buy(amount, ACTIVES, action, duration)
    while I_want_money.get_async_order(id) == None:
        pass
    print(I_want_money.get_async_order(id))
    
    # Example for Digital Option
    _, id = I_want_money.buy_digital_spot(ACTIVES, amount, action, duration)
    while I_want_money.get_async_order(id) == None:
        pass
    print(I_want_money.get_async_order(id))
    
    # Example for Forex/Crypto/CFD
    check, id = I_want_money.buy_order(instrument_type="crypto", instrument_id="BTCUSD", side="buy", amount=1.23, leverage=3, type="market")
    while I_want_money.get_async_order(id) == None:
        pass
    print(I_want_money.get_async_order(id))
  9. Import and Initialize IQ_Option

    master

    To use the API, import the IQ_Option class from iqoptionapi.stable_api. You can initialize a connection by providing your email and password.

    Note: SMS Authorization is not currently supported. It is recommended to disable it on your account to prevent your automated scripts from hanging while waiting for manual SMS input.

    from iqoptionapi.stable_api import IQ_Option
    
    # Initialize with email and password
    I_want_money = IQ_Option("email", "password")