Exemple #1
0
 def fetch_current_price(self, epic_id):
     try:
         subscription = igstream.Subscription(
             mode="MERGE",
             items=["MARKET:{}".format(epic_id)],
             fields=[
                 "MID_OPEN",
                 "HIGH",
                 "LOW",
                 "CHANGE",
                 "CHANGE_PCT",
                 "UPDATE_TIME",
                 "MARKET_DELAY",
                 "MARKET_STATE",
                 "BID",
                 "OFFER",
             ],
         )
         res = self.igstreamclient.fetch_one(subscription)
     except IndexError:
         # fall back to non-stream version
         res = super().markets(epic_id)
         res["values"] = {}
         res["values"]["BID"] = res["snapshot"]["bid"]
         res["values"]["OFFER"] = res["snapshot"]["offer"]
         res["values"]["CHANGE"] = res["snapshot"]["netChange"]
         res["values"]["CHANGE_PCT"] = res["snapshot"]["percentageChange"]
     return res
Exemple #2
0
    def subscribe(self, epic_id, listener=on_item_update):
        """
        Create a live subscription to epic via Lightstreamer
        :param epic_id: string containing epic id
               listener: function to call on new update
        :return:
        """
        self.logger.debug('ig.py API subscribe')
        success = False
        try:
            subscription = igstream.Subscription(
                mode="MERGE",
                items=["MARKET:{}".format(epic_id)],
                fields=["MID_OPEN", "HIGH", "LOW", "CHANGE", "CHANGE_PCT", "UPDATE_TIME", "MARKET_DELAY",
                        "MARKET_STATE", "BID", "OFFER"]
            )
            sub_key, success = self.igstreamclient.subscribe(subscription=subscription, listener=listener)
            if success:
                self.logger.debug('ig.py API subscribe: success.')
                self.ls_subscriptions[sub_key] = {'epic_id': epic_id, 'running': True}
            else:
                self.logger.debug('ig.py API subscribe: fail.')
        except:
            self.logger.warning('ig.py API subscribe: exception.')

        return success
Exemple #3
0
 def fetch_day_highlow(self, epic_id):
     subscription = igstream.Subscription(
         mode="MERGE",
         items=["CHART:{}:HOUR".format(epic_id)],
         fields=["LTV", "DAY_LOW", "DAY_HIGH"])
     res = self.igstreamclient.fetch_one(subscription)
     return res
Exemple #4
0
    def __init__(self):
        super().__init__()
        d = super().session()
        self.igstreamclient = igstream.IGStream(igclient=self, loginresponse=d)

        subscription = igstream.Subscription(
            mode="DISTINCT",
            items=["TRADE:" + str(self.accountId)],
            fields=["OPU"])

        self.igstreamclient.subscribe(subscription=subscription,
                                      listener=on_item_update)
        self.market_ids = {}

        # get open positions
        self.open_positions = super().positions()
Exemple #5
0
def fetch_current_price(epic_id):
    try:
        subscription = igstream.Subscription(
            mode="MERGE",
            items=["MARKET:{}".format(epic_id)],
            fields=[
                "MID_OPEN", "HIGH", "LOW", "CHANGE", "CHANGE_PCT",
                "UPDATE_TIME", "MARKET_DELAY", "MARKET_STATE", "BID", "OFFER"
            ])
        res = igstreamclient.fetch_one(subscription)
    except IndexError:
        # fall back to non-stream version
        res = igclient.markets(epic_id)
        res['values'] = {}
        res['values']['BID'] = res['snapshot']['bid']
        res['values']['OFFER'] = res['snapshot']['offer']
        res['values']['CHANGE'] = res['snapshot']['netChange']
        res['values']['CHANGE_PCT'] = res['snapshot']['percentageChange']
    return res
Exemple #6
0
import configparser

from igclient import IGClient
import igstream

igclient = IGClient()

config = configparser.ConfigParser()
config.read("default.conf")
config.read("config.conf")

d = igclient.session()
igstreamclient = igstream.IGStream(igclient=igclient, loginresponse=d)

subscription = igstream.Subscription(
    mode="DISTINCT",
    items=["TRADE:" + str(igclient.accountId)],
    fields=["OPU"])


def on_item_update(item_update):
    print(item_update)


igstreamclient.subscribe(subscription=subscription, listener=on_item_update)

# get open positions
open_positions = igclient.positions()

###################################################################################
##########################END OF LOGIN CODE########################################
###################################################################################