def __init__(self): super(IGStore, self).__init__() self.notifs = collections.deque() # store notifications for cerebro self._env = None # reference to cerebro for general notifications self.broker = None # broker instance self.datas = list() # datas that have registered over start self._orders = collections.OrderedDict() # map order.ref to oid self._ordersrev = collections.OrderedDict() # map oid to order.ref self._transpend = collections.defaultdict(collections.deque) self._oenv = self._ENVPRACTICE if self.p.practice else self._ENVLIVE self.igapi = IGService(self.p.usr, self.p.pwd, self.p.token, self._oenv) self.igapi.create_session() self.igss = Streamer(ig_service=self.igapi) self.ig_session = self.igss.create_session() self.igss.connect(self.p.account) #Work with JSON rather than Pandas for better backtrader integration self.igapi.return_dataframe = False self._cash = 0.0 self._value = 0.0 self.pull_cash_and_value() self._evt_acct = threading.Event()
def main(): logging.basicConfig(level=logging.DEBUG) expire_after = timedelta(hours=1) session = requests_cache.CachedSession(cache_name='cache', backend='sqlite', expire_after=expire_after) # set expire_after=None if you don't want cache expiration # set expire_after=0 if you don't want to cache queries #config = IGServiceConfig() # no cache ig_service = IGService("mz_demo", "Marko01.", "660d3924e9ffd0ed2481f027f150081b87574363", "DEMO") # if you want to globally cache queries #ig_service = IGService(config.username, config.password, config.api_key, config.acc_type, session) ig_service.create_session() accounts = ig_service.fetch_accounts() print("accounts:\n%s" % accounts) #account_info = ig_service.switch_account(config.acc_number, False) # print(account_info) open_positions = ig_service.fetch_open_positions() print("open_positions:\n%s" % open_positions) print("") working_orders = ig_service.fetch_working_orders() print("working_orders:\n%s" % working_orders) print("") #epic = 'CS.D.EURUSD.MINI.IP' epic = 'IX.D.AEX.IFM.IP' resolution = 'D' # see from pandas.tseries.frequencies import to_offset num_points = 120 response = ig_service.fetch_historical_prices_by_epic_and_num_points( epic, resolution, num_points) # Exception: error.public-api.exceeded-account-historical-data-allowance result_1 = ig_service.search_markets("Treasury Bond Decimalised") market = ig_service.fetch_market_by_epic("CC.D.CC.UMA.IP") print(result_1) epic = "IX.D.SPTRD.IFA.IP" market_info = ig_service.fetch_market_by_epic(epic, session) print(market_info) # if you want to cache this query response = ig_service.fetch_historical_prices_by_epic_and_num_points( epic, resolution, num_points, session)
def main(): expire_after = timedelta(hours=1) session = requests_cache.CachedSession(cache_name='cache', backend='sqlite', expire_after=expire_after) # set expire_after=None if you don't want cache expiration # set expire_after=0 if you don't want to cache queries #config = IGServiceConfig() # no cache ig_service = IGService(config.username, config.password, config.api_key, config.acc_type) # if you want to globally cache queries #ig_service = IGService(config.username, config.password, config.api_key, config.acc_type, session) ig_service.create_session() accounts = ig_service.fetch_accounts() print("accounts:\n%s" % accounts) #account_info = ig_service.switch_account(config.acc_number, False) #print(account_info) #open_positions = ig_service.fetch_open_positions() #print("open_positions:\n%s" % open_positions) print("") #working_orders = ig_service.fetch_working_orders() #print("working_orders:\n%s" % working_orders) print("") #epic = 'CS.D.EURUSD.MINI.IP' epic = 'IX.D.ASX.IFM.IP' # US (SPY) - mini #resolution = 'D' # see from pandas.tseries.frequencies import to_offset #resolution = 'H' resolution = '1Min' #num_points = 10 #response = ig_service.fetch_historical_prices_by_epic_and_num_points(epic, resolution, num_points) # if you want to cache this query #response = ig_service.fetch_historical_prices_by_epic_and_num_points(epic, resolution, num_points, session) #df_ask = response['prices']['ask'] #print("ask prices:\n%s" % df_ask) (start_date, end_date) = ('2015-01-15', '2015-01-28') #response = ig_service.fetch_historical_prices_by_epic_and_date_range(epic, resolution, start_date, end_date) # if you want to cache this query response = ig_service.fetch_historical_prices_by_epic_and_date_range(epic, resolution, start_date, end_date, session) df_ask = response['prices']['ask'] print("ask prices:\n%s" % df_ask)
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(): logging.basicConfig(level=logging.DEBUG) expire_after = timedelta(hours=1) session = requests_cache.CachedSession(cache_name="cache", backend="sqlite", expire_after=expire_after) # set expire_after=None if you don't want cache expiration # set expire_after=0 if you don't want to cache queries # config = IGServiceConfig() # no cache ig_service = IGService(config.username, config.password, config.api_key, config.acc_type, acc_number=config.acc_number) # if you want to globally cache queries # ig_service = IGService(config.username, config.password, config.api_key, config.acc_type, session) ig_service.create_session() # ig_stream_service.create_session(version='3') accounts = ig_service.fetch_accounts() print("accounts:\n%s" % accounts) # account_info = ig_service.switch_account(config.acc_number, False) # print(account_info) # open_positions = ig_service.fetch_open_positions() # print("open_positions:\n%s" % open_positions) print("") # working_orders = ig_service.fetch_working_orders() # print("working_orders:\n%s" % working_orders) print("") # epic = 'CS.D.EURUSD.MINI.IP' epic = "IX.D.ASX.IFM.IP" # US (SPY) - mini #epic = "CS.D.GBPUSD.CFD.IP" # sample CFD epic resolution = "D" # see from pandas.tseries.frequencies import to_offset # resolution = 'H' # resolution = '1Min' num_points = 10 response = ig_service.fetch_historical_prices_by_epic_and_num_points( epic, resolution, num_points)
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
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
def __init__(self, config): CACHE_NAME = 'igcache' # Set up logging self.logger = logging.getLogger(__name__) self.logger.info("Establishing cached session with IG Markets") session_cached = requests_cache.CachedSession( cache_name=CACHE_NAME, backend='sqlite', expire_after=timedelta(hours=1)) self.ig_service = IGService(config.IG_USERNAME, config.IG_PASSWORD, config.IG_API_KEY, config.IG_ACC_TYPE, session_cached) # Creates REST session self.ig_session = self.ig_service.create_session()
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 getTimeseries(self, start_date, end_date): ''' # Gets data from IG markets API and structures into flattend df :return: flat df ''' self.start_date = start_date self.end_date = end_date ig_service = IGService(config.username, config.password, config.api_key, config.acc_type) ig_service.create_session() self.ig_reponse = ig_service.fetch_historical_prices_by_epic_and_date_range(epic=self.epic, resolution=self.resolution, start_date=self.start_date, end_date=self.end_date) self.dataframe = flatten_df(self.ig_reponse['prices']) self.sort_df() return self.dataframe
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()
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 get_candles(instruments, params): ig_service = IGService(config.username, config.password, config.api_key, config.acc_type) ig_service.create_session() account_info = ig_service.switch_account(config.acc_number, False) # not necessary print(account_info) open_positions = ig_service.fetch_open_positions() print("open_positions:\n%s" % open_positions) print("") epic = 'CS.D.EURUSD.MINI.IP' resolution = 'D' num_points = 10 response = ig_service.fetch_historical_prices_by_epic_and_num_points( epic, resolution, num_points) df_ask = response['prices']['ask'] print("ask prices:\n%s" % df_ask)
def __init__(self): self.ig_service = IGService(username=config2.username, password=config2.password, api_key=config2.api_key, acc_type=config2.acc_type) self.ig_service.create_session()
def start_session(self)->IGService: self.check_required_credentials() session = IGService(self.login, self.password, self.apiKey, self.accType) session.create_session() self.current_account = session.ig_session['currentAccountId'] return session
def test_ig_service(): DELAY = 30 def wait(delay): print( "Wait %s s to avoid error.public-api.exceeded-account-allowance" % delay) time.sleep(delay) session_cached = requests_cache.CachedSession( cache_name='cache', backend='sqlite', expire_after=timedelta(hours=1)) session_not_cached = requests.Session() for session in [session_cached, session_cached, session_not_cached]: pp = pprint.PrettyPrinter(indent=4) assert (isinstance(trading_ig.__version__, six.string_types)) config = ConfigEnvVar("IG_SERVICE") #ig_service = IGService(config.username, config.password, config.api_key, config.acc_type) ig_service = IGService(config.username, config.password, config.api_key, config.acc_type, session) ig_service.create_session() print("fetch_accounts") response = ig_service.fetch_accounts() print(response) #assert(response['balance'][0]['available']>0) assert (response['balance'][0] > 0) print("") print("fetch_account_activity_by_period") response = ig_service.fetch_account_activity_by_period(10000) print(response) assert (isinstance(response, pd.DataFrame)) print("") print("fetch_account_activity_by_period") response = ig_service.fetch_account_activity_by_period(10000) print(response) assert (isinstance(response, pd.DataFrame)) print("") print("fetch_transaction_history_by_type_and_period") response = ig_service.fetch_transaction_history_by_type_and_period( 10000, "ALL") print(response) assert (isinstance(response, pd.DataFrame)) wait(DELAY) print("") print("fetch_open_positions") response = ig_service.fetch_open_positions() print(response) assert (isinstance(response, pd.DataFrame)) print("") print("fetch_working_orders") response = ig_service.fetch_working_orders() print(response) assert (isinstance(response, pd.DataFrame)) print("") print("fetch_top_level_navigation_nodes") response = ig_service.fetch_top_level_navigation_nodes() print(response) # dict with nodes and markets assert (isinstance(response, dict)) market_id = response['nodes']['id'].iloc[0] print("") print("fetch_client_sentiment_by_instrument") response = ig_service.fetch_client_sentiment_by_instrument(market_id) print(response) assert (isinstance(response, dict)) print("") print("fetch_related_client_sentiment_by_instrument") response = ig_service.fetch_related_client_sentiment_by_instrument( market_id) print(response) assert (isinstance(response, pd.DataFrame)) print("") print("fetch_sub_nodes_by_node") node = market_id #? response = ig_service.fetch_sub_nodes_by_node(node) print(response) assert (isinstance(response['markets'], pd.DataFrame)) assert (isinstance(response['nodes'], pd.DataFrame)) print("") wait(DELAY) print("fetch_all_watchlists") response = ig_service.fetch_all_watchlists() print(response) assert (isinstance(response, pd.DataFrame)) watchlist_id = response['id'].iloc[0] # u'Popular Markets' #epic = print("") print("fetch_watchlist_markets") response = ig_service.fetch_watchlist_markets(watchlist_id) print(response) assert (isinstance(response, pd.DataFrame)) epic = response['epic'].iloc[ 0] # epic = 'CS.D.EURUSD.MINI.IP' # epic = u'IX.D.CAC.IDF.IP' print("") print("fetch_market_by_epic") response = ig_service.fetch_market_by_epic(epic) print(response) #pp.pprint(response) assert (isinstance(response, dict)) print("") print("search_markets") search_term = 'EURUSD' #search_term = 'SPY' response = ig_service.search_markets(search_term) print(response) assert (isinstance(response, pd.DataFrame)) print("") print("fetch_historical_prices_by_epic_and_num_points") #epic = 'CS.D.EURUSD.MINI.IP' #epic = 'IX.D.ASX.IFM.IP' # US 500 (SPY) #epic = 'IX.D.ASX.IFM.IP' # US (SPY) - mini #resolution = 'HOUR' # MINUTE, MINUTE_2, MINUTE_3, MINUTE_5, MINUTE_10, MINUTE_15, MINUTE_30, HOUR, HOUR_2, HOUR_3, HOUR_4, DAY, WEEK, MONTH resolution = 'H' # http://pandas.pydata.org/pandas-docs/stable/timeseries.html#dateoffset-objects num_points = 10 response = ig_service.fetch_historical_prices_by_epic_and_num_points( epic, resolution, num_points) print(response) #print(response['prices']['price']) #print(response['prices']['price']['ask']) #print(response['prices']['volume']) assert (isinstance(response['allowance'], dict)) #assert(isinstance(response['prices']['volume'], pd.Series)) #assert(isinstance(response['prices']['price'], pd.Panel)) assert (isinstance(response['prices'], pd.DataFrame)) print("") print("fetch_historical_prices_by_epic_and_date_range") start_date = datetime(2014, 12, 15) end_date = datetime(2014, 12, 20) response = ig_service.fetch_historical_prices_by_epic_and_date_range( epic, resolution, start_date, end_date) print(response) assert (isinstance(response['allowance'], dict)) #assert(isinstance(response['prices']['volume'], pd.Series)) #assert(isinstance(response['prices']['price'], pd.Panel)) assert (isinstance(response['prices'], pd.DataFrame)) wait(DELAY) print("")
def create_ig_service(credentials): LOGGER.info("Creating service with user:%s, api_key:%s, password:<hidden>", credentials["username"], credentials["api_key"]) return IGService(credentials["username"], credentials["password"], credentials["api_key"])
#!/usr/bin/env python # -*- coding: utf-8 -*- from trading_ig import IGService from utils_igmarkets.trading_ig_config import Config as config ig_service = IGService(config.username, config.password, config.api_key, config.acc_type) ig_service.create_session() open_positions = ig_service.fetch_open_positions() print("open_positions:\n%s" % open_positions) print("") epic = 'CS.D.USDJPY.MINI.IP' # telstra: AA.D.TLS.CASH.IP # "epic": "CS.D.USDJPY.MINI.IP", resolution = 'min' num_points = 10 response = ig_service.fetch_historical_prices_by_epic_and_num_points( epic, resolution, num_points) df_ask = response['prices']['ask'] print("ask prices:\n%s" % df_ask)
store.initialize_library('NASDAQ') # Access the library library = store['NASDAQ'] # Get some data from IG # Get environment variables for IG acc_number = os.environ['IG_SERVICE_ACC_NUMBER'] password = os.environ['IG_SERVICE_PASSWORD'] api_key = os.environ['IG_SERVICE_API_KEY'] username = os.environ['IG_SERVICE_USERNAME'] acc_type = os.environ['IG_SERVICE_ACC_TYPE'] # Create IG Session ig_service = IGService(username, password, api_key, acc_type) ig_service.create_session() # Get account info account_info = ig_service.switch_account(acc_number, False) # not necessary print(account_info) # get position info open_positions = ig_service.fetch_open_positions() print("open_positions:\n%s" % open_positions) print() epic = 'CS.D.EURUSD.MINI.IP' resolution = 'D' num_points = 10 response = ig_service.fetch_historical_prices_by_epic_and_num_points(
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 test_ig_service(): delay_for_ig = 30 def wait(delay): print( "Wait %s s to avoid 'error.public-api.exceeded-account-allowance'" % delay) time.sleep(delay) session_cached = requests_cache.CachedSession( cache_name=CACHE_NAME, backend="sqlite", expire_after=timedelta(hours=1)) session_not_cached = requests.Session() for i, session in enumerate( [session_cached, session_cached, session_not_cached]): # pp = pprint.PrettyPrinter(indent=4) assert isinstance(trading_ig.__version__, six.string_types) # ig_service = IGService(config.username, config.password, # config.api_key, config.acc_type) ig_service = IGService(config.username, config.password, config.api_key, config.acc_type, session) ig_service.create_session() print("%d - fetch_accounts" % i) response = ig_service.fetch_accounts() print(response) # assert(response['balance'][0]['available']>0) assert response["balance"][0] > 0 print("") print("fetch_account_activity_by_period") response = ig_service.fetch_account_activity_by_period(10000) print(response) assert isinstance(response, pd.DataFrame) print("") print("fetch_account_activity_by_period") response = ig_service.fetch_account_activity_by_period(10000) print(response) assert isinstance(response, pd.DataFrame) print("") print("fetch_transaction_history_by_type_and_period") response = ig_service.fetch_transaction_history_by_type_and_period( 10000, "ALL") print(response) assert isinstance(response, pd.DataFrame) wait(delay_for_ig) print("") print("fetch_open_positions") response = ig_service.fetch_open_positions() print(response) assert isinstance(response, pd.DataFrame) print("") print("fetch_working_orders") response = ig_service.fetch_working_orders() print(response) assert isinstance(response, pd.DataFrame) print("") print("fetch_top_level_navigation_nodes") response = ig_service.fetch_top_level_navigation_nodes() print(response) # dict with nodes and markets assert isinstance(response, dict) market_id = response["nodes"]["id"].iloc[0] print("") print("fetch_client_sentiment_by_instrument") response = ig_service.fetch_client_sentiment_by_instrument(market_id) print(response) assert isinstance(response, dict) print("") print("fetch_related_client_sentiment_by_instrument") response = ig_service.fetch_related_client_sentiment_by_instrument( market_id) print(response) assert isinstance(response, pd.DataFrame) print("") print("fetch_sub_nodes_by_node") node = market_id response = ig_service.fetch_sub_nodes_by_node(node) print(response) assert isinstance(response["markets"], pd.DataFrame) assert isinstance(response["nodes"], pd.DataFrame) print("") wait(delay_for_ig) print("fetch_all_watchlists") response = ig_service.fetch_all_watchlists() print(response) assert isinstance(response, pd.DataFrame) watchlist_id = response["id"].iloc[0] # u'Popular Markets' print("") print("fetch_watchlist_markets") response = ig_service.fetch_watchlist_markets(watchlist_id) print(response) assert isinstance(response, pd.DataFrame) # epic = 'CS.D.EURUSD.MINI.IP' # epic = u'IX.D.CAC.IDF.IP' epic = response["epic"].iloc[0] print("") print("fetch_market_by_epic") response = ig_service.fetch_market_by_epic(epic) print(response) # pp.pprint(response) assert isinstance(response, dict) print("") print("search_markets") search_term = "EURUSD" # search_term = 'SPY' response = ig_service.search_markets(search_term) print(response) assert isinstance(response, pd.DataFrame) print("") wait(delay_for_ig) wait(delay_for_ig) print("fetch_historical_prices_by_epic_and_num_points") # epic = 'CS.D.EURUSD.MINI.IP' # epic = 'IX.D.ASX.IFM.IP' # US 500 (SPY) # epic = 'IX.D.ASX.IFM.IP' # US (SPY) - mini # MINUTE, MINUTE_2, MINUTE_3, MINUTE_5, MINUTE_10, MINUTE_15, # MINUTE_30, HOUR, HOUR_2, HOUR_3, HOUR_4, DAY, WEEK, MONTH # resolution = 'HOUR' # http://pandas.pydata.org/pandas-docs/stable/timeseries.html#dateoffset-objects resolution = "H" num_points = 10 response = ig_service.fetch_historical_prices_by_epic_and_num_points( epic, resolution, num_points) print(response) # print(response['prices']['price']) # print(response['prices']['price']['ask']) # print(response['prices']['volume']) assert isinstance(response["allowance"], dict) # assert(isinstance(response['prices']['volume'], pd.Series)) # assert(isinstance(response['prices']['price'], pd.Panel)) assert isinstance(response["prices"], pd.DataFrame) print("") wait(delay_for_ig) print("fetch_historical_prices_by_epic_and_date_range") end_date = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0) start_date = end_date - timedelta(days=3) response = ig_service.fetch_historical_prices_by_epic_and_date_range( epic, resolution, start_date, end_date) print(response) assert isinstance(response["allowance"], dict) # assert(isinstance(response['prices']['volume'], pd.Series)) # assert(isinstance(response['prices']['price'], pd.Panel)) assert isinstance(response["prices"], pd.DataFrame) print("") wait(delay_for_ig)
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()