def test_create_open_position(self, ig_service: IGService): epic = 'IX.D.FTSE.DAILY.IP' market_info = ig_service.fetch_market_by_epic(epic) status = market_info.snapshot.marketStatus min_bet = market_info.dealingRules.minDealSize.value bid = market_info.snapshot.bid offer = market_info.snapshot.offer if status != 'TRADEABLE': pytest.skip('Skipping open position test, market not open') open_result = ig_service.create_open_position( epic=epic, direction='BUY', currency_code='GBP', order_type='MARKET', expiry='DFB', force_open='false', guaranteed_stop='false', size=min_bet, level=None, limit_level=None, limit_distance=None, quote_id=None, stop_distance=None, stop_level=None, trailing_stop=None, trailing_stop_increment=None) assert open_result['dealStatus'] == 'ACCEPTED' assert open_result['reason'] == 'SUCCESS' time.sleep(10) update_v1_result = ig_service.update_open_position(offer * 1.5, bid * 0.5, open_result['dealId'], version='1') assert update_v1_result['dealStatus'] == 'ACCEPTED' assert update_v1_result['reason'] == 'SUCCESS' time.sleep(10) update_v2_result = ig_service.update_open_position(offer * 1.4, bid * 0.4, open_result['dealId'], trailing_stop=True, trailing_stop_distance=25.0, trailing_stop_increment=10.0) assert update_v2_result['dealStatus'] == 'ACCEPTED' assert update_v2_result['reason'] == 'SUCCESS' time.sleep(10) close_result = ig_service.close_open_position(deal_id=open_result['dealId'], direction='SELL', epic=None, expiry='DFB', level=None, order_type='MARKET', quote_id=None, size=0.5, session=None) assert close_result['dealStatus'] == 'ACCEPTED' assert close_result['reason'] == 'SUCCESS'
def test_create_working_order_guaranteed_stop_loss(self, ig_service: IGService): epic = 'CS.D.GBPUSD.TODAY.IP' market_info = ig_service.fetch_market_by_epic(epic) status = market_info.snapshot.marketStatus min_bet = market_info.dealingRules.minDealSize.value offer = market_info.snapshot.offer bid = market_info.snapshot.bid logging.info(f"min bet: {min_bet}") logging.info(f"offer: {offer}") logging.info(f"bid: {bid}") if status != 'TRADEABLE': pytest.skip('Skipping create working order test, market not open') create_result = ig_service.create_working_order( epic=epic, direction='BUY', currency_code='GBP', order_type='LIMIT', expiry='DFB', guaranteed_stop='true', time_in_force='GOOD_TILL_CANCELLED', size=min_bet, level=offer * 0.90, limit_level=None, limit_distance=None, stop_distance=200, stop_level=None) logging.info(f"result: {create_result['dealStatus']}, reason {create_result['reason']}") assert create_result['dealStatus'] == 'ACCEPTED' assert create_result['reason'] == 'SUCCESS' assert create_result['guaranteedStop'] time.sleep(10) update_result = ig_service.update_working_order( good_till_date=None, level=offer * 0.85, limit_distance=None, limit_level=None, stop_distance=None, stop_level=offer * 0.80, guaranteed_stop='true', time_in_force='GOOD_TILL_CANCELLED', order_type='LIMIT', deal_id=create_result['dealId']) logging.info(f"result: {update_result['dealStatus']}, reason {update_result['reason']}") assert update_result['dealStatus'] == 'ACCEPTED' assert update_result['reason'] == 'SUCCESS' assert update_result['guaranteedStop'] time.sleep(10) delete_result = ig_service.delete_working_order(create_result['dealId']) assert delete_result['dealStatus'] == 'ACCEPTED' assert delete_result['reason'] == 'SUCCESS'
def test_create_working_order(self, ig_service: IGService): epic = 'CS.D.GBPUSD.TODAY.IP' market_info = ig_service.fetch_market_by_epic(epic) status = market_info.snapshot.marketStatus min_bet = market_info.dealingRules.minDealSize.value offer = market_info.snapshot.offer if status != 'TRADEABLE': pytest.skip('Skipping create working order test, market not open') create_result = ig_service.create_working_order( epic=epic, direction='BUY', currency_code='GBP', order_type='LIMIT', expiry='DFB', guaranteed_stop='false', time_in_force='GOOD_TILL_CANCELLED', size=min_bet, level=offer * 0.9, limit_level=None, limit_distance=None, stop_distance=None, stop_level=None) assert create_result['dealStatus'] == 'ACCEPTED' assert create_result['reason'] == 'SUCCESS' time.sleep(10) delete_result = ig_service.delete_working_order(create_result['dealId']) assert delete_result['dealStatus'] == 'ACCEPTED' assert delete_result['reason'] == 'SUCCESS'
def test_fetch_market_by_epic(self, ig_service: IGService): response = ig_service.fetch_market_by_epic("CS.D.EURUSD.MINI.IP") assert isinstance(response, dict)
def test_rate_limiter_non_trading_req(self): with open('tests/data/session.json', 'r') as file: session_response_body = json.loads(file.read()) responses.add(responses.POST, 'https://demo-api.ig.com/gateway/deal/session', headers={'CST': 'abc123', 'X-SECURITY-TOKEN': 'xyz987'}, json=session_response_body, status=200) responses.add(responses.GET, 'https://demo-api.ig.com/gateway/deal/session', headers={'CST': 'abc123', 'X-SECURITY-TOKEN': 'xyz987'}, json=session_response_body, status=200) with open('tests/data/application.json', 'r') as file: app_response_body = json.loads(file.read()) responses.add(responses.POST, 'https://demo-api.ig.com/gateway/deal/operations/application', headers={'CST': 'abc123', 'X-SECURITY-TOKEN': 'xyz987'}, json=app_response_body, status=200) responses.add(responses.GET, 'https://demo-api.ig.com/gateway/deal/operations/application', headers={'CST': 'abc123', 'X-SECURITY-TOKEN': 'xyz987'}, json=app_response_body, status=200) with open('tests/data/markets_epic.json', 'r') as file: mkts_epic_response_body = json.loads(file.read()) responses.add(responses.POST, 'https://demo-api.ig.com/gateway/deal/markets/CO.D.CFI.Month2.IP', headers={'CST': 'abc123', 'X-SECURITY-TOKEN': 'xyz987'}, json=mkts_epic_response_body, status=200) responses.add(responses.GET, 'https://demo-api.ig.com/gateway/deal/markets/CO.D.CFI.Month2.IP', headers={'CST': 'abc123', 'X-SECURITY-TOKEN': 'xyz987'}, json=mkts_epic_response_body, status=200) ig_service = IGService('username', 'password', 'api_key', 'DEMO', use_rate_limiter=True) ig_service.create_session() # empty the bucket queue (len=1), before we start timing things ig_service.fetch_market_by_epic('CO.D.CFI.Month2.IP') times = [] for i in range(3): time_last = time.time() ig_service.fetch_market_by_epic('CO.D.CFI.Month2.IP') times.append(time.time() - time_last) ig_service.logout() av_time = sum(times) / len(times) expected_av = 60.0 / app_response_body[0]['allowanceAccountOverall'] time_tolerance = 0.2 assert av_time >= expected_av assert av_time < expected_av + time_tolerance