Exemple #1
0
    def _create_streaming_session(self, ig_service):

        # Cretes Streaming service and session
        ig_stream_service = IGStreamService(ig_service)
        ig_stream_session = ig_stream_service.create_session()

        return ig_stream_service, ig_stream_session
Exemple #2
0
    def _create_streaming_session(self, ig_service, accountId):

        # Cretes Streaming service and session
        ig_stream_service = IGStreamService(ig_service)
        ig_stream_session = ig_stream_service.create_session()

        # Connect with specified Listener
        ig_stream_service.connect(accountId)

        return ig_stream_service, ig_stream_session
Exemple #3
0
    def connect(self,accountId=None):
        logger.debug("Connecting to IG Streaming API...")
        ig_service = IGService(config.username, config.password, config.api_key, config.acc_type)

        ig_stream_service = IGStreamService(ig_service)
        ig_session = ig_stream_service.create_session()
        if accountId is None:
            accountId = ig_session[u'accounts'][0][u'accountId']
        ig_stream_service.connect(accountId)

        self.ig_stream_service = ig_stream_service
Exemple #4
0
    def connect(self, accountId=None):
        logger.debug("Connecting to IG Streaming API...")
        ig_service = IGService(config.username, config.password,
                               config.api_key, config.acc_type)

        ig_stream_service = IGStreamService(ig_service)
        ig_session = ig_stream_service.create_session()
        if accountId is None:
            accountId = ig_session[u'accounts'][0][u'accountId']
        ig_stream_service.connect(accountId)

        self.ig_stream_service = ig_stream_service
Exemple #5
0
def run(config, testing, tickers, filename, n, n_window):

    # Set up variables needed for backtest
    events_queue = queue.Queue()

    ig_service = IGService(config.IG.USERNAME, config.IG.PASSWORD,
                           config.IG.API_KEY, config.IG.ACCOUNT.TYPE)

    ig_stream_service = IGStreamService(ig_service)
    ig_session = ig_stream_service.create_session()
    accountId = ig_session[u'accounts'][0][u'accountId']

    ig_stream_service.connect(accountId)

    initial_equity = PriceParser.parse(500000.00)

    # Use IG Tick Price Handler
    price_handler = IGTickPriceHandler(events_queue, ig_stream_service,
                                       tickers)

    # Use the Display Strategy
    strategy = DisplayStrategy(n=n, n_window=n_window)

    # Use an example Position Sizer
    position_sizer = FixedPositionSizer()

    # Use an example Risk Manager
    risk_manager = ExampleRiskManager()

    # Use the default Portfolio Handler
    portfolio_handler = PortfolioHandler(initial_equity, events_queue,
                                         price_handler, position_sizer,
                                         risk_manager)

    # Use the ExampleCompliance component
    compliance = ExampleCompliance(config)

    # Use a simulated IB Execution Handler
    execution_handler = IBSimulatedExecutionHandler(events_queue,
                                                    price_handler, compliance)

    # Use the default Statistics
    statistics = SimpleStatistics(config, portfolio_handler)

    # Set up the backtest
    backtest = Backtest(price_handler, strategy, portfolio_handler,
                        execution_handler, position_sizer, risk_manager,
                        statistics, initial_equity)
    results = backtest.simulate_trading(testing=testing)
    statistics.save(filename)
    return results
Exemple #6
0
    def _init_stream(self):
        ig_stream_service = IGStreamService(self.broker.session)
        ig_session = ig_stream_service.create_session()
        # Ensure configured account is selected
        account = self.broker.current_account
        if account is not None:
            ig_stream_service.connect(account)
            subscription = Subscription(
                    mode=self.mode,
                    items=self.items,
                    fields=self.fields,
                )
            subscription.addlistener(self.on_update)

            sub_key_prices = ig_stream_service.ls_client.subscribe(subscription)
Exemple #7
0
def main():
    logging.basicConfig(level=logging.INFO)
    # logging.basicConfig(level=logging.DEBUG)

    ig_service = IGService(config.username, config.password, config.api_key,
                           config.acc_type)

    ig_stream_service = IGStreamService(ig_service)
    ig_session = ig_stream_service.create_session()
    # Ensure configured account is selected
    accounts = ig_session[u'accounts']
    for account in accounts:
        if account[u'accountId'] == config.acc_number:
            accountId = account[u'accountId']
            break
        else:
            print('Account not found: {0}'.format(config.acc_number))
            accountId = None
    ig_stream_service.connect(accountId)

    # Making a new Subscription in MERGE mode
    subscription_prices = Subscription(
        mode="MERGE",
        items=['QB.D.FT1605UD.03.IP/MINUTE'],
        fields=["UPDATE_TIME", "BID", "OFFER", "CHANGE", "MARKET_STATE"],
    )
    #adapter="QUOTE_ADAPTER")

    # Adding the "on_price_update" function to Subscription
    subscription_prices.addlistener(on_prices_update)

    # Registering the Subscription
    sub_key_prices = ig_stream_service.ls_client.subscribe(subscription_prices)

    # Making an other Subscription in MERGE mode
    subscription_account = Subscription(
        mode="MERGE",
        items=['ACCOUNT:' + accountId],
        fields=["AVAILABLE_CASH"],
    )
    #    #adapter="QUOTE_ADAPTER")

    # Adding the "on_balance_update" function to Subscription
    subscription_account.addlistener(on_account_update)

    # Registering the Subscription
    sub_key_account = ig_stream_service.ls_client.subscribe(
        subscription_account)

    input("{0:-^80}\n".format("HIT CR TO UNSUBSCRIBE AND DISCONNECT FROM \
    LIGHTSTREAMER"))

    # Disconnecting
    ig_stream_service.disconnect()
def main():

    # setup
    global priceObj

    # logging.basicConfig(level=logging.INFO)
    config = IGServiceConfig()
    ig_service = IGService(config.username, config.password, config.api_key,
                           config.acc_type)
    ig_stream_service = IGStreamService(ig_service)
    ig_session = ig_stream_service.create_session()

    accounts = ig_session[u'accounts']
    for account in accounts:
        if account[u'accountId'] == config.acc_number:
            accountId = account[u'accountId']
            break
    ig_stream_service.connect(accountId)

    # save to DB thread
    sched = BackgroundScheduler()
    sched.add_job(saveToDB, 'interval', seconds=5)
    sched.start()

    # Making a new Subscription in MERGE mode
    subscriptionCap = 38
    subscriptionCurrent = 0
    i = 0
    listSubscription = []
    while i < len(priceObj):
        items = list(priceObj.keys())[i:i + subscriptionCap]
        subscription_prices = Subscription(mode="MERGE",
                                           items=items,
                                           fields=["BID", "OFFER"])
        subscription_prices.addlistener(onPriceUpdate)
        sub_key_prices = ig_stream_service.ls_client.subscribe(
            subscription_prices)
        listSubscription.append(sub_key_prices)
        time.sleep(2)
        # print(subscription_prices)
        print(items)
        i += subscriptionCap

    input("{0:-^80}\n".format("Press Enter to close"))

    # Disconnecting
    ig_stream_service.disconnect()
Exemple #9
0
def main():
    logging.basicConfig(level=logging.INFO)
    # logging.basicConfig(level=logging.DEBUG)

    ig_service = IGService(config.username, config.password, config.api_key,
                           config.acc_type)

    ig_stream_service = IGStreamService(ig_service)
    ig_session = ig_stream_service.create_session()
    accountId = ig_session[u'accounts'][0][u'accountId']
    ig_stream_service.connect(accountId)

    # Making a new Subscription in MERGE mode
    subcription_prices = Subscription(
        mode="MERGE",
        items=['L1:CS.D.GBPUSD.CFD.IP', 'L1:CS.D.USDJPY.CFD.IP'],
        fields=["UPDATE_TIME", "BID", "OFFER", "CHANGE", "MARKET_STATE"],
    )
    #adapter="QUOTE_ADAPTER")

    # Adding the "on_price_update" function to Subscription
    subcription_prices.addlistener(on_prices_update)

    # Registering the Subscription
    sub_key_prices = ig_stream_service.ls_client.subscribe(subcription_prices)

    # Making an other Subscription in MERGE mode
    subscription_account = Subscription(
        mode="MERGE",
        items='ACCOUNT:' + accountId,
        fields=["AVAILABLE_CASH"],
    )
    #    #adapter="QUOTE_ADAPTER")

    # Adding the "on_balance_update" function to Subscription
    subscription_account.addlistener(on_account_update)

    # Registering the Subscription
    sub_key_account = ig_stream_service.ls_client.subscribe(
        subscription_account)

    compat.wait_for_input(
        "{0:-^80}\n".format("HIT CR TO UNSUBSCRIBE AND DISCONNECT FROM \
    LIGHTSTREAMER"))

    # Disconnecting
    ig_stream_service.disconnect()
def main():
    logging.basicConfig(level=logging.INFO)
    # logging.basicConfig(level=logging.DEBUG)

    ig_service = IGService(config.username,
                           config.password,
                           config.api_key,
                           config.acc_type,
                           acc_number=config.acc_number)

    ig_stream_service = IGStreamService(ig_service)
    ig_stream_service.create_session()
    #ig_stream_service.create_session(version='3')

    # Making a new Subscription in MERGE mode
    subscription_prices = Subscription(
        mode="MERGE",
        #items=["L1:CS.D.GBPUSD.CFD.IP", "L1:CS.D.USDJPY.CFD.IP"], # sample CFD epics
        items=["L1:CS.D.GBPUSD.TODAY.IP",
               "L1:IX.D.FTSE.DAILY.IP"],  # sample spreadbet epics
        fields=["UPDATE_TIME", "BID", "OFFER", "CHANGE", "MARKET_STATE"],
    )

    # Adding the "on_price_update" function to Subscription
    subscription_prices.addlistener(on_prices_update)

    # Registering the Subscription
    sub_key_prices = ig_stream_service.ls_client.subscribe(subscription_prices)

    # Making an other Subscription in MERGE mode
    subscription_account = Subscription(
        mode="MERGE",
        items=["ACCOUNT:" + config.acc_number],
        fields=["AVAILABLE_CASH"],
    )

    # Adding the "on_balance_update" function to Subscription
    subscription_account.addlistener(on_account_update)

    # Registering the Subscription
    sub_key_account = ig_stream_service.ls_client.subscribe(
        subscription_account)

    input("{0:-^80}\n".format("HIT CR TO UNSUBSCRIBE AND DISCONNECT FROM \
    LIGHTSTREAMER"))

    # Disconnecting
    ig_stream_service.disconnect()
def main():
    logging.basicConfig(level=logging.INFO)
    # logging.basicConfig(level=logging.DEBUG)

    ig_service = IGService(config.username, config.password, config.api_key, config.acc_type)

    ig_stream_service = IGStreamService(ig_service)
    ig_session = ig_stream_service.create_session()
    accountId = ig_session[u'accounts'][0][u'accountId']
    ig_stream_service.connect(accountId)

    # Making a new Subscription in MERGE mode
    subcription_prices = Subscription(
        mode="MERGE",
        items=['L1:CS.D.GBPUSD.CFD.IP', 'L1:CS.D.USDJPY.CFD.IP'],
        fields=["UPDATE_TIME", "BID", "OFFER", "CHANGE", "MARKET_STATE"],
        )
        #adapter="QUOTE_ADAPTER")


    # Adding the "on_price_update" function to Subscription
    subcription_prices.addlistener(on_prices_update)

    # Registering the Subscription
    sub_key_prices = ig_stream_service.ls_client.subscribe(subcription_prices)


    # Making an other Subscription in MERGE mode
    subscription_account = Subscription(
        mode="MERGE",
        items='ACCOUNT:'+accountId,
        fields=["AVAILABLE_CASH"],
        )
    #    #adapter="QUOTE_ADAPTER")

    # Adding the "on_balance_update" function to Subscription
    subscription_account.addlistener(on_account_update)

    # Registering the Subscription
    sub_key_account = ig_stream_service.ls_client.subscribe(subscription_account)

    compat.wait_for_input("{0:-^80}\n".format("HIT CR TO UNSUBSCRIBE AND DISCONNECT FROM \
    LIGHTSTREAMER"))

    # Disconnecting
    ig_stream_service.disconnect()
def create_ig_stream_service(ig_service):
    return IGStreamService(ig_service)
Exemple #13
0
def main():

    epics1 = [
        'CHART:CS.D.GBPEUR.MINI.IP:1MINUTE',
        'CHART:IR.D.10YEAR100.FWM2.IP:1MINUTE',
        'CHART:CC.D.LCO.UME.IP:1MINUTE', 'CHART:CS.D.NZDUSD.MINI.IP:1MINUTE',
        'CHART:CS.D.USDCAD.MINI.IP:1MINUTE',
        'CHART:CS.D.USDJPY.MINI.IP:1MINUTE', 'CHART:CO.D.RR.FWM1.IP:1MINUTE',
        'CHART:CO.D.O.FWM2.IP:1MINUTE', 'CHART:IX.D.SPTRD.IFM.IP:1MINUTE',
        'CHART:IX.D.NASDAQ.IFE.IP:1MINUTE'
    ]

    epics2 = ['CHART:CS.D.GBPEUR.MINI.IP:1MINUTE']

    epics3 = ['CHART:IR.D.10YEAR100.FWM2.IP:1MINUTE']

    epics4 = ['CHART:CS.D.NZDUSD.MINI.IP:1MINUTE']

    epics2 = ['CHART:KA.D.ECHOGS.CASH.IP:1MINUTE']

    logging.basicConfig(level=logging.INFO)
    # logging.basicConfig(level=logging.DEBUG)

    ig_service = IGService(config.username, config.password, config.api_key,
                           config.acc_type)

    ig_stream_service = IGStreamService(ig_service)
    ig_session = ig_stream_service.create_session()
    # Ensure configured account is selected
    accounts = ig_session[u'accounts']
    for account in accounts:
        if account[u'accountId'] == config.acc_number:
            accountId = account[u'accountId']
            break
        else:
            print('Account not found: {0}'.format(config.acc_number))
            accountId = None
    ig_stream_service.connect(accountId)

    # Making a new Subscription in MERGE mode
    subscription_prices = Subscription(
        mode="MERGE",
        items=epics1,
        fields=["UTM", "BID_OPEN", "BID_HIGH", "BID_LOW", "BID_CLOSE"],
    )

    # adapter="QUOTE_ADAPTER")

    # Adding the "on_price_update" function to Subscription
    subscription_prices.addlistener(on_prices_update)

    # Registering the Subscription
    sub_key_prices = ig_stream_service.ls_client.subscribe(subscription_prices)

    # Making an other Subscription in MERGE mode
    subscription_account = Subscription(
        mode="MERGE",
        items=['ACCOUNT:' + accountId],
        fields=["AVAILABLE_CASH"],
    )
    #    #adapter="QUOTE_ADAPTER")

    # Registering the Subscription
    sub_key_account = ig_stream_service.ls_client.subscribe(
        subscription_account)

    input("{0:-^80}\n".format("HIT CR TO UNSUBSCRIBE AND DISCONNECT FROM \
    LIGHTSTREAMER"))

    # Disconnecting
    ig_stream_service.disconnect()
def main():
    logging.basicConfig(level=logging.INFO)
    # logging.basicConfig(level=logging.DEBUG)

    ig_service = IGService(config.username, config.password, config.api_key,
                           config.acc_type)

    ig_stream_service = IGStreamService(ig_service)
    ig_session = ig_stream_service.create_session()
    # Ensure configured account is selected
    accounts = ig_session[u"accounts"]
    for account in accounts:
        if account[u"accountId"] == config.acc_number:
            accountId = account[u"accountId"]
            break
        else:
            print("Account not found: {0}".format(config.acc_number))
            accountId = None
    ig_stream_service.connect(accountId)

    # Making a new Subscription in MERGE mode
    subscription_prices = Subscription(
        mode="MERGE",
        items=["CHART:CS.D.EURUSD.MINI.IP:1MINUTE"],
        fields=[
            "LTV", "UTM", "DAY_OPEN_MID", "DAY_NET_CHG_MID",
            "DAY_PERC_CHG_MID", "DAY_HIGH", "DAY_LOW", "OFR_OPEN", "OFR_HIGH",
            "OFR_LOW", "OFR_CLOSE", "BID_OPEN", "BID_HIGH", "BID_LOW",
            "BID_CLOSE", "LTP_OPEN", "LTP_HIGH", "LTP_LOW", "LTP_CLOSE",
            "CONS_END", "CONS_TICK_COUNT"
        ],
    )
    # adapter="QUOTE_ADAPTER")

    # Adding the "on_price_update" function to Subscription
    subscription_prices.addlistener(on_prices_update)

    # Registering the Subscription
    sub_key_prices = ig_stream_service.ls_client.subscribe(subscription_prices)

    # Making an other Subscription in MERGE mode
    subscription_account = Subscription(
        mode="MERGE",
        items=["ACCOUNT:" + accountId],
        fields=["AVAILABLE_CASH"],
    )
    #    #adapter="QUOTE_ADAPTER")

    # Adding the "on_balance_update" function to Subscription
    subscription_account.addlistener(on_account_update)

    # Registering the Subscription
    sub_key_account = ig_stream_service.ls_client.subscribe(
        subscription_account)

    heartbeat_items = ["TRADE:HB.U.HEARTBEAT.IP"]
    heartbeat = Subscription(
        mode='MERGE',
        items=heartbeat_items,
        fields=["HEARTBEAT"],
    )

    heartbeat.addlistener(on_heartbeat_update)
    sub_heartbeat = ig_stream_service.ls_client.subscribe(heartbeat)

    input("{0:-^80}\n".format("HIT CR TO UNSUBSCRIBE AND DISCONNECT FROM \
    LIGHTSTREAMER"))

    # Disconnecting
    ig_stream_service.disconnect()
    producer.flush()