-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_ig_service.py
executable file
·142 lines (112 loc) · 5.25 KB
/
test_ig_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Run unit tests using
nosetests -s -v
"""
import pandas as pd
from utils_igmarkets.ig_service import IGService
from utils_igmarkets.trading_ig_config import Config # defines username, password, api_key, acc_type, acc_number
def test_ig_service():
'''Connect using the rest api to access account information'''
c = Config()
ig_service = IGService(c.username, c.password, c.api_key, c.acc_type)
ig_service.create_session()
print("fetch_accounts")
response = ig_service.fetch_accounts()
print(response)
assert(response['balance'][0]['available'] > 0)
raw_input('\n\n***************press any key******************\n\n')
print("fetch_account_activity_by_period")
response = ig_service.fetch_account_activity_by_period(1000000)
print(response[:10])
assert(isinstance(response, pd.DataFrame))
# raw_input('\n\n***************press any key******************\n\n')
# #
# print("fetch_transaction_history_by_type_and_period")
# response = ig_service.fetch_transaction_history_by_type_and_period(100000000, "ALL")
# print(response)
# assert(isinstance(response, pd.DataFrame))
raw_input('\n\n***************press any key******************\n\n')
#
print("fetch_open_positions")
response = ig_service.fetch_open_positions()
print(response)
assert(isinstance(response, pd.DataFrame))
raw_input('\n\n***************press any key******************\n\n')
print("fetch_working_orders")
response = ig_service.fetch_working_orders()
print response
#print response['marketData']
#print response['workingOrderData']
assert(isinstance(response, pd.DataFrame))
raw_input('\n\n***************press any key******************\n\n')
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("fetch_client_sentiment_by_instrument")
response = ig_service.fetch_client_sentiment_by_instrument(market_id)
print(response)
assert(isinstance(response, dict))
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("fetch_sub_nodes_by_node")
node = market_id #?
response = ig_service.fetch_sub_nodes_by_node(node)
print(response)
#
raw_input('***************press any key******************\n\n')
print("fetch_all_watchlists")
response = ig_service.fetch_all_watchlists()
print(response)
watchlist_id = response['id'].iloc[0]
#
#
raw_input('\n\n***************press any key******************\n\n')
print("fetch_watchlist_markets")
response = ig_service.fetch_watchlist_markets(watchlist_id)
print(response)
epic = response['epic'].iloc[5]
raw_input('\n\n***************press any key******************\n\n')
print("fetch_market_by_epic")
response = ig_service.fetch_market_by_epic(epic)
print(response)
raw_input('\n\n***************press any key******************\n\n')
print("search_markets based on text or symbol. \n To list epics on CBA type 'cba' with quotes. \n")
#search_term = input('Input symbol or text: ')
search_term = 'tls'
response = ig_service.search_markets(search_term)
print(response)
print 'Epic ------------------> ', epic
#epic = "CS.D.USDJPY.MINI.IP"
print epic
raw_input('\n\n***************press any key******************\n\n')
print 'Market detail for Epic: ', epic
response = ig_service.fetch_market_by_epic(epic)
print response
raw_input('\n\n***************press any key to send a market order on %s mini contract******************\n\n' % 'CS.D.AUDUSD.MINI.IP')
print 'Create a market order via the api'
raw_input('\n\n***************press any key******************\n\n')
response = ig_service.create_open_position(currency_code='USD', direction='BUY', epic='CS.D.AUDUSD.MINI.IP', expiry='-', force_open='false',
guaranteed_stop='false', level='', limit_distance='', limit_level='', order_type='MARKET', size=3,
stop_distance='', stop_level='')
print response
print '\n\n---------------->>>>>order accepted/rejected? ', response['dealStatus'], '\n\n'
raw_input('\n\n***************press any key******************\n\n')
print 'Create an working order via the api'
raw_input('\n\n***************press any key******************\n\n')
response = ig_service.create_working_order(currency_code='USD', direction='SELL', epic='CS.D.AUDUSD.MINI.IP', expiry='-', good_till_date="2015/02/06 20:00:44",
guaranteed_stop='true', level=0.7850, limit_distance=200.0, limit_level='', size=1, stop_distance=45.0, stop_level='',
time_in_force='GOOD_TILL_CANCELLED', order_type='LIMIT')
print response
try:
if response['dealStatus'] == 'CANCELLED':
print 'Check current prices and adjust \'lavel\''
except Exception, e:
print 'Order submission error. Check current prices and adjust \'lavel\''
if __name__ == '__main__':
test_ig_service()