forked from ig-python/trading-ig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_stream_ig.py
More file actions
80 lines (59 loc) · 2.29 KB
/
example_stream_ig.py
File metadata and controls
80 lines (59 loc) · 2.29 KB
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
IG Markets Stream API sample with Python
2015 FemtoTrader
"""
import logging
from trading_ig.IGService import IGService
from trading_ig.IGStreamService import IGStreamService
from trading_ig.config import config
from trading_ig.lightstreamer import Subscription
# A simple function acting as a Subscription listener
def on_prices_update(item_update):
# print("price: %s " % item_update)
print(
"{stock_name:<19}: Time {UPDATE_TIME:<8} - "
"Bid {BID:>5} - Ask {OFFER:>5}".format(
stock_name=item_update["name"], **item_update["values"]
)
)
def on_account_update(balance_update):
print("balance: %s " % balance_update)
def main():
logging.basicConfig(level=logging.INFO)
# logging.basicConfig(level=logging.DEBUG)
ig_service = IGService(config)
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
items=["CS.D.GBPUSD.TODAY.IP", "CS.D.GBPEUR.TODAY.IP", "CS.D.EURGBP.TODAY.IP"],
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()
if __name__ == "__main__":
main()