forked from ig-python/trading-ig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_rest_ig.py
More file actions
91 lines (65 loc) · 2.67 KB
/
example_rest_ig.py
File metadata and controls
91 lines (65 loc) · 2.67 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
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
IG Markets REST API sample with Python
2015 FemtoTrader
"""
from trading_ig import IGService
from trading_ig.config import config
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# if you need to cache to DB your requests
from datetime import timedelta
import requests_cache
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
)
# Exception: error.public-api.exceeded-account-historical-data-allowance
# 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-09-15', '2015-09-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)
if __name__ == "__main__":
main()