dukascopy-python 1.0.0__tar.gz → 3.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {dukascopy_python-1.0.0 → dukascopy_python-3.0.0}/PKG-INFO +16 -16
- {dukascopy_python-1.0.0 → dukascopy_python-3.0.0}/README.md +14 -14
- dukascopy_python-3.0.0/dukascopy_python/__init__.py +412 -0
- dukascopy_python-3.0.0/dukascopy_python/instruments.py +1380 -0
- {dukascopy_python-1.0.0 → dukascopy_python-3.0.0}/dukascopy_python.egg-info/PKG-INFO +16 -16
- {dukascopy_python-1.0.0 → dukascopy_python-3.0.0}/dukascopy_python.egg-info/SOURCES.txt +2 -0
- dukascopy_python-3.0.0/dukascopy_python.egg-info/top_level.txt +1 -0
- {dukascopy_python-1.0.0 → dukascopy_python-3.0.0}/pyproject.toml +2 -2
- dukascopy_python-1.0.0/dukascopy_python.egg-info/top_level.txt +0 -1
- {dukascopy_python-1.0.0 → dukascopy_python-3.0.0}/LICENSE +0 -0
- {dukascopy_python-1.0.0 → dukascopy_python-3.0.0}/dukascopy_python.egg-info/dependency_links.txt +0 -0
- {dukascopy_python-1.0.0 → dukascopy_python-3.0.0}/dukascopy_python.egg-info/requires.txt +0 -0
- {dukascopy_python-1.0.0 → dukascopy_python-3.0.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
|
-
Name:
|
|
3
|
-
Version:
|
|
2
|
+
Name: dukascopy_python
|
|
3
|
+
Version: 3.0.0
|
|
4
4
|
Summary: Download tick data from Dukascopy Bank SA to local cache, and simulate time-ordered price streams
|
|
5
5
|
Author-email: Eghosa Osayande <osayandeeghosam@gmail.com>
|
|
6
6
|
License: MIT License
|
|
@@ -39,7 +39,7 @@ Requires-Dist: build; extra == "dev"
|
|
|
39
39
|
Requires-Dist: twine; extra == "dev"
|
|
40
40
|
Dynamic: license-file
|
|
41
41
|
|
|
42
|
-
# 🏦
|
|
42
|
+
# 🏦 dukascopy_python
|
|
43
43
|
|
|
44
44
|
Download and stream historical price data for variety of financial instruments (e.g. Forex, Commodities and Indices) from Dukascopy Bank SA. , including support for tick-level and aggregated intervals.
|
|
45
45
|
|
|
@@ -48,7 +48,7 @@ Download and stream historical price data for variety of financial instruments (
|
|
|
48
48
|
## 📦 Installation
|
|
49
49
|
|
|
50
50
|
```bash
|
|
51
|
-
pip install
|
|
51
|
+
pip install dukascopy_python
|
|
52
52
|
```
|
|
53
53
|
|
|
54
54
|
---
|
|
@@ -59,8 +59,8 @@ pip install dukascript
|
|
|
59
59
|
|
|
60
60
|
```python
|
|
61
61
|
from datetime import datetime, timedelta
|
|
62
|
-
import
|
|
63
|
-
from
|
|
62
|
+
import dukascopy_python
|
|
63
|
+
from dukascopy_python.instruments import INSTRUMENT_FX_MAJORS_GBP_USD
|
|
64
64
|
```
|
|
65
65
|
|
|
66
66
|
---
|
|
@@ -89,7 +89,7 @@ Both `fetch` and `live_fetch` share similar parameters:
|
|
|
89
89
|
| Parameter | Description |
|
|
90
90
|
|-------------------|---------------------------------------------|
|
|
91
91
|
| `interval_value` | e.g., `1` |
|
|
92
|
-
| `time_unit` | e.g., `
|
|
92
|
+
| `time_unit` | e.g., `dukascopy_python.TIME_UNIT_HOUR` |
|
|
93
93
|
|
|
94
94
|
---
|
|
95
95
|
|
|
@@ -159,10 +159,10 @@ df.to_json("data.json")
|
|
|
159
159
|
start = datetime(2025, 1, 1)
|
|
160
160
|
end = datetime(2025, 2, 1)
|
|
161
161
|
instrument = INSTRUMENT_FX_MAJORS_GBP_USD
|
|
162
|
-
interval =
|
|
163
|
-
offer_side =
|
|
162
|
+
interval = dukascopy_python.INTERVAL_HOUR_1
|
|
163
|
+
offer_side = dukascopy_python.OFFER_SIDE_BID
|
|
164
164
|
|
|
165
|
-
df =
|
|
165
|
+
df = dukascopy_python.fetch(
|
|
166
166
|
instrument,
|
|
167
167
|
interval,
|
|
168
168
|
offer_side,
|
|
@@ -182,12 +182,12 @@ now = datetime.now()
|
|
|
182
182
|
start = datetime(now.year, now.month, now.day)
|
|
183
183
|
end = start + timedelta(hours=24)
|
|
184
184
|
instrument = INSTRUMENT_FX_MAJORS_GBP_USD
|
|
185
|
-
offer_side =
|
|
185
|
+
offer_side = dukascopy_python.OFFER_SIDE_BID
|
|
186
186
|
|
|
187
|
-
iterator =
|
|
187
|
+
iterator = dukascopy_python.live_fetch(
|
|
188
188
|
instrument,
|
|
189
189
|
1,
|
|
190
|
-
|
|
190
|
+
dukascopy_python.TIME_UNIT_HOUR,
|
|
191
191
|
offer_side,
|
|
192
192
|
start,
|
|
193
193
|
end,
|
|
@@ -208,12 +208,12 @@ now = datetime.now()
|
|
|
208
208
|
start = datetime(now.year, now.month, now.day)
|
|
209
209
|
end = None
|
|
210
210
|
instrument = INSTRUMENT_FX_MAJORS_GBP_USD
|
|
211
|
-
offer_side =
|
|
211
|
+
offer_side = dukascopy_python.OFFER_SIDE_BID
|
|
212
212
|
|
|
213
|
-
df_iterator =
|
|
213
|
+
df_iterator = dukascopy_python.live_fetch(
|
|
214
214
|
instrument,
|
|
215
215
|
1,
|
|
216
|
-
|
|
216
|
+
dukascopy_python.TIME_UNIT_HOUR,
|
|
217
217
|
offer_side,
|
|
218
218
|
start,
|
|
219
219
|
end,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# 🏦
|
|
1
|
+
# 🏦 dukascopy_python
|
|
2
2
|
|
|
3
3
|
Download and stream historical price data for variety of financial instruments (e.g. Forex, Commodities and Indices) from Dukascopy Bank SA. , including support for tick-level and aggregated intervals.
|
|
4
4
|
|
|
@@ -7,7 +7,7 @@ Download and stream historical price data for variety of financial instruments (
|
|
|
7
7
|
## 📦 Installation
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
|
-
pip install
|
|
10
|
+
pip install dukascopy_python
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
---
|
|
@@ -18,8 +18,8 @@ pip install dukascript
|
|
|
18
18
|
|
|
19
19
|
```python
|
|
20
20
|
from datetime import datetime, timedelta
|
|
21
|
-
import
|
|
22
|
-
from
|
|
21
|
+
import dukascopy_python
|
|
22
|
+
from dukascopy_python.instruments import INSTRUMENT_FX_MAJORS_GBP_USD
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
---
|
|
@@ -48,7 +48,7 @@ Both `fetch` and `live_fetch` share similar parameters:
|
|
|
48
48
|
| Parameter | Description |
|
|
49
49
|
|-------------------|---------------------------------------------|
|
|
50
50
|
| `interval_value` | e.g., `1` |
|
|
51
|
-
| `time_unit` | e.g., `
|
|
51
|
+
| `time_unit` | e.g., `dukascopy_python.TIME_UNIT_HOUR` |
|
|
52
52
|
|
|
53
53
|
---
|
|
54
54
|
|
|
@@ -118,10 +118,10 @@ df.to_json("data.json")
|
|
|
118
118
|
start = datetime(2025, 1, 1)
|
|
119
119
|
end = datetime(2025, 2, 1)
|
|
120
120
|
instrument = INSTRUMENT_FX_MAJORS_GBP_USD
|
|
121
|
-
interval =
|
|
122
|
-
offer_side =
|
|
121
|
+
interval = dukascopy_python.INTERVAL_HOUR_1
|
|
122
|
+
offer_side = dukascopy_python.OFFER_SIDE_BID
|
|
123
123
|
|
|
124
|
-
df =
|
|
124
|
+
df = dukascopy_python.fetch(
|
|
125
125
|
instrument,
|
|
126
126
|
interval,
|
|
127
127
|
offer_side,
|
|
@@ -141,12 +141,12 @@ now = datetime.now()
|
|
|
141
141
|
start = datetime(now.year, now.month, now.day)
|
|
142
142
|
end = start + timedelta(hours=24)
|
|
143
143
|
instrument = INSTRUMENT_FX_MAJORS_GBP_USD
|
|
144
|
-
offer_side =
|
|
144
|
+
offer_side = dukascopy_python.OFFER_SIDE_BID
|
|
145
145
|
|
|
146
|
-
iterator =
|
|
146
|
+
iterator = dukascopy_python.live_fetch(
|
|
147
147
|
instrument,
|
|
148
148
|
1,
|
|
149
|
-
|
|
149
|
+
dukascopy_python.TIME_UNIT_HOUR,
|
|
150
150
|
offer_side,
|
|
151
151
|
start,
|
|
152
152
|
end,
|
|
@@ -167,12 +167,12 @@ now = datetime.now()
|
|
|
167
167
|
start = datetime(now.year, now.month, now.day)
|
|
168
168
|
end = None
|
|
169
169
|
instrument = INSTRUMENT_FX_MAJORS_GBP_USD
|
|
170
|
-
offer_side =
|
|
170
|
+
offer_side = dukascopy_python.OFFER_SIDE_BID
|
|
171
171
|
|
|
172
|
-
df_iterator =
|
|
172
|
+
df_iterator = dukascopy_python.live_fetch(
|
|
173
173
|
instrument,
|
|
174
174
|
1,
|
|
175
|
-
|
|
175
|
+
dukascopy_python.TIME_UNIT_HOUR,
|
|
176
176
|
offer_side,
|
|
177
177
|
start,
|
|
178
178
|
end,
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
import requests
|
|
3
|
+
from datetime import datetime, timedelta
|
|
4
|
+
import json
|
|
5
|
+
import random
|
|
6
|
+
import string
|
|
7
|
+
from time import sleep
|
|
8
|
+
import logging
|
|
9
|
+
|
|
10
|
+
TIME_UNIT_MONTH = "MONTH"
|
|
11
|
+
TIME_UNIT_WEEK = "WEEK"
|
|
12
|
+
TIME_UNIT_DAY = "DAY"
|
|
13
|
+
TIME_UNIT_HOUR = "HOUR"
|
|
14
|
+
TIME_UNIT_MIN = "MIN"
|
|
15
|
+
TIME_UNIT_SEC = "SEC"
|
|
16
|
+
TIME_UNIT_TICK = "TICK"
|
|
17
|
+
|
|
18
|
+
INTERVAL_MONTH_1 = f"1{TIME_UNIT_MONTH}"
|
|
19
|
+
INTERVAL_WEEK_1 = f"1{TIME_UNIT_WEEK}"
|
|
20
|
+
INTERVAL_DAY_1 = f"1{TIME_UNIT_DAY}"
|
|
21
|
+
INTERVAL_HOUR_4 = f"4{TIME_UNIT_HOUR}"
|
|
22
|
+
INTERVAL_HOUR_1 = f"1{TIME_UNIT_HOUR}"
|
|
23
|
+
INTERVAL_MIN_30 = f"30{TIME_UNIT_MIN}"
|
|
24
|
+
INTERVAL_MIN_15 = f"15{TIME_UNIT_MIN}"
|
|
25
|
+
INTERVAL_MIN_10 = f"10{TIME_UNIT_MIN}"
|
|
26
|
+
INTERVAL_MIN_5 = f"5{TIME_UNIT_MIN}"
|
|
27
|
+
INTERVAL_MIN_1 = f"1{TIME_UNIT_MIN}"
|
|
28
|
+
INTERVAL_SEC_30 = f"30{TIME_UNIT_SEC}"
|
|
29
|
+
INTERVAL_SEC_10 = f"10{TIME_UNIT_SEC}"
|
|
30
|
+
INTERVAL_SEC_1 = f"1{TIME_UNIT_SEC}"
|
|
31
|
+
INTERVAL_TICK = TIME_UNIT_TICK
|
|
32
|
+
|
|
33
|
+
_interval_units = {
|
|
34
|
+
INTERVAL_MONTH_1: TIME_UNIT_MONTH,
|
|
35
|
+
INTERVAL_WEEK_1: TIME_UNIT_WEEK,
|
|
36
|
+
INTERVAL_DAY_1: TIME_UNIT_DAY,
|
|
37
|
+
INTERVAL_HOUR_4: TIME_UNIT_HOUR,
|
|
38
|
+
INTERVAL_HOUR_1: TIME_UNIT_HOUR,
|
|
39
|
+
INTERVAL_MIN_30: TIME_UNIT_MIN,
|
|
40
|
+
INTERVAL_MIN_15: TIME_UNIT_MIN,
|
|
41
|
+
INTERVAL_MIN_10: TIME_UNIT_MIN,
|
|
42
|
+
INTERVAL_MIN_5: TIME_UNIT_MIN,
|
|
43
|
+
INTERVAL_MIN_1: TIME_UNIT_MIN,
|
|
44
|
+
INTERVAL_SEC_30: TIME_UNIT_SEC,
|
|
45
|
+
INTERVAL_SEC_10: TIME_UNIT_SEC,
|
|
46
|
+
INTERVAL_SEC_1: TIME_UNIT_SEC,
|
|
47
|
+
INTERVAL_TICK: TIME_UNIT_TICK,
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
OFFER_SIDE_BID = "B"
|
|
51
|
+
OFFER_SIDE_ASK = "A"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _get_custom_logger(debug=False):
|
|
55
|
+
logger = logging.getLogger("DUKASCRIPT")
|
|
56
|
+
logger.setLevel(logging.DEBUG if debug else logging.INFO)
|
|
57
|
+
|
|
58
|
+
return logger
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _resample_to_nearest(
|
|
62
|
+
timestamp: datetime,
|
|
63
|
+
time_unit: str,
|
|
64
|
+
interval_value: int,
|
|
65
|
+
) -> datetime:
|
|
66
|
+
# Round to the nearest time unit based on the interval value
|
|
67
|
+
if time_unit == TIME_UNIT_SEC:
|
|
68
|
+
subtraction = timestamp.second % interval_value
|
|
69
|
+
return timestamp - timedelta(
|
|
70
|
+
seconds=subtraction,
|
|
71
|
+
microseconds=timestamp.microsecond,
|
|
72
|
+
)
|
|
73
|
+
elif time_unit == TIME_UNIT_MIN:
|
|
74
|
+
subtraction = timestamp.minute % interval_value
|
|
75
|
+
return timestamp - timedelta(
|
|
76
|
+
minutes=subtraction,
|
|
77
|
+
seconds=timestamp.second,
|
|
78
|
+
microseconds=timestamp.microsecond,
|
|
79
|
+
)
|
|
80
|
+
elif time_unit == TIME_UNIT_HOUR:
|
|
81
|
+
subtraction = timestamp.hour % interval_value
|
|
82
|
+
return timestamp - timedelta(
|
|
83
|
+
hours=subtraction,
|
|
84
|
+
minutes=timestamp.minute,
|
|
85
|
+
seconds=timestamp.second,
|
|
86
|
+
microseconds=timestamp.microsecond,
|
|
87
|
+
)
|
|
88
|
+
elif time_unit == TIME_UNIT_DAY:
|
|
89
|
+
subtraction = timestamp.day % interval_value
|
|
90
|
+
return timestamp - timedelta(
|
|
91
|
+
days=subtraction,
|
|
92
|
+
hours=timestamp.hour,
|
|
93
|
+
minutes=timestamp.minute,
|
|
94
|
+
seconds=timestamp.second,
|
|
95
|
+
microseconds=timestamp.microsecond,
|
|
96
|
+
)
|
|
97
|
+
elif time_unit == TIME_UNIT_WEEK:
|
|
98
|
+
subtraction = (timestamp.weekday() + 1) % (interval_value * 7)
|
|
99
|
+
return timestamp - timedelta(
|
|
100
|
+
days=subtraction,
|
|
101
|
+
hours=timestamp.hour,
|
|
102
|
+
minutes=timestamp.minute,
|
|
103
|
+
seconds=timestamp.second,
|
|
104
|
+
microseconds=timestamp.microsecond,
|
|
105
|
+
)
|
|
106
|
+
elif time_unit == TIME_UNIT_MONTH:
|
|
107
|
+
month = (timestamp.month // interval_value) + 1
|
|
108
|
+
return datetime(timestamp.year, month, 1, 0, 0, 0, 0, timestamp.tzinfo)
|
|
109
|
+
elif time_unit == TIME_UNIT_TICK:
|
|
110
|
+
return timestamp
|
|
111
|
+
|
|
112
|
+
raise NotImplementedError(f"resampling not implemented for {time_unit}")
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def _get_dataframe_columns_for_timeunit(time_unit: str) -> list[str]:
|
|
116
|
+
|
|
117
|
+
ohlc_df = ["timestamp", "open", "high", "low", "close", "volume"]
|
|
118
|
+
tick_df = ["timestamp", "bidPrice", "askPrice", "bidVolume", "askVolume"]
|
|
119
|
+
|
|
120
|
+
df = {
|
|
121
|
+
TIME_UNIT_DAY: ohlc_df,
|
|
122
|
+
TIME_UNIT_HOUR: ohlc_df,
|
|
123
|
+
TIME_UNIT_MIN: ohlc_df,
|
|
124
|
+
TIME_UNIT_MONTH: ohlc_df,
|
|
125
|
+
TIME_UNIT_SEC: ohlc_df,
|
|
126
|
+
TIME_UNIT_TICK: tick_df,
|
|
127
|
+
TIME_UNIT_WEEK: ohlc_df,
|
|
128
|
+
}[time_unit]
|
|
129
|
+
|
|
130
|
+
return df
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _fetch(
|
|
134
|
+
instrument: str,
|
|
135
|
+
interval: str,
|
|
136
|
+
offer_side: str,
|
|
137
|
+
last_update: int,
|
|
138
|
+
logger: logging.Logger = logging.getLogger(),
|
|
139
|
+
limit: int = None,
|
|
140
|
+
):
|
|
141
|
+
characters = string.ascii_letters + string.digits
|
|
142
|
+
jsonp = f"_callbacks____{''.join(random.choices(characters, k=9))}"
|
|
143
|
+
|
|
144
|
+
query_params = {
|
|
145
|
+
"path": "chart/json3",
|
|
146
|
+
"splits": "true",
|
|
147
|
+
"stocks": "true",
|
|
148
|
+
"time_direction": "N",
|
|
149
|
+
"jsonp": jsonp,
|
|
150
|
+
"last_update": f"{int(last_update)}",
|
|
151
|
+
"offer_side": f"{offer_side}",
|
|
152
|
+
"instrument": f"{instrument}",
|
|
153
|
+
"interval": f"{interval}",
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if limit is not None:
|
|
157
|
+
# max limit is 30_000
|
|
158
|
+
query_params["limit"] = f"{int(limit)}"
|
|
159
|
+
|
|
160
|
+
base_url = "https://freeserv.dukascopy.com/2.0/index.php"
|
|
161
|
+
|
|
162
|
+
headers = {
|
|
163
|
+
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0",
|
|
164
|
+
"Host": "freeserv.dukascopy.com",
|
|
165
|
+
"Referer": "https://freeserv.dukascopy.com/2.0/?path=chart/index&showUI=true&showTabs=true&showParameterToolbar=true&showOfferSide=true&allowInstrumentChange=true&allowPeriodChange=true&allowOfferSideChange=true&showAdditionalToolbar=true&showExportImportWorkspace=true&allowSocialSharing=true&showUndoRedoButtons=true&showDetachButton=true&presentationType=candle&axisX=true&axisY=true&legend=true&timeline=true&showDateSeparators=true&showZoom=true&showScrollButtons=true&showAutoShiftButton=true&crosshair=true&borders=false&freeMode=false&theme=Pastelle&uiColor=%23000&availableInstruments=l%3A&instrument=EUR/USD&period=5&offerSide=BID&timezone=0&live=true&allowPan=true&width=100%25&height=700&adv=popup&lang=en",
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
# logger.debug("query params: %s", query_params)
|
|
169
|
+
|
|
170
|
+
response = requests.get(base_url, headers=headers, params=query_params)
|
|
171
|
+
|
|
172
|
+
jsonText = response.text.removeprefix(f"{jsonp}(").removesuffix(");")
|
|
173
|
+
|
|
174
|
+
return json.loads(jsonText)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _stream(
|
|
178
|
+
instrument: str,
|
|
179
|
+
interval: str,
|
|
180
|
+
offer_side: str,
|
|
181
|
+
start: datetime,
|
|
182
|
+
end: datetime = None,
|
|
183
|
+
max_retries: int = 7,
|
|
184
|
+
limit: int = None,
|
|
185
|
+
logger: logging.Logger = logging.getLogger(),
|
|
186
|
+
):
|
|
187
|
+
no_of_retries = 0
|
|
188
|
+
cursor = int(start.timestamp() * 1000)
|
|
189
|
+
end_timestamp = None
|
|
190
|
+
if end is not None:
|
|
191
|
+
end_timestamp = end.timestamp() * 1000
|
|
192
|
+
|
|
193
|
+
is_first_iteration = True
|
|
194
|
+
|
|
195
|
+
logging.info(f"Start Date :{start.isoformat()}")
|
|
196
|
+
logging.info(f"End Date :{'' if end is None else end.isoformat()}")
|
|
197
|
+
|
|
198
|
+
while True:
|
|
199
|
+
try:
|
|
200
|
+
|
|
201
|
+
lastUpdates = _fetch(
|
|
202
|
+
instrument=instrument,
|
|
203
|
+
interval=interval,
|
|
204
|
+
offer_side=offer_side,
|
|
205
|
+
last_update=cursor,
|
|
206
|
+
limit=limit,
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
if not is_first_iteration and lastUpdates[0][0] == cursor:
|
|
210
|
+
lastUpdates = lastUpdates[1:]
|
|
211
|
+
|
|
212
|
+
if len(lastUpdates) < 1:
|
|
213
|
+
if end is not None:
|
|
214
|
+
break
|
|
215
|
+
else:
|
|
216
|
+
continue
|
|
217
|
+
|
|
218
|
+
for row in lastUpdates:
|
|
219
|
+
if end_timestamp is not None and row[0] > end_timestamp:
|
|
220
|
+
return
|
|
221
|
+
if interval == INTERVAL_TICK:
|
|
222
|
+
row[-1] = row[-1] / 1_000_000
|
|
223
|
+
row[-2] = row[-2] / 1_000_000
|
|
224
|
+
yield row
|
|
225
|
+
cursor = row[0]
|
|
226
|
+
|
|
227
|
+
logger.info(
|
|
228
|
+
f"current timestamp :{datetime.fromtimestamp(cursor/1000).isoformat()}"
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
no_of_retries = 0
|
|
232
|
+
is_first_iteration = False
|
|
233
|
+
|
|
234
|
+
except Exception as e:
|
|
235
|
+
import traceback
|
|
236
|
+
|
|
237
|
+
stacktrace = traceback.format_exc()
|
|
238
|
+
no_of_retries += 1
|
|
239
|
+
if max_retries is not None and (no_of_retries - 1) > max_retries:
|
|
240
|
+
logger.debug("error fetching")
|
|
241
|
+
logger.debug(e, stacktrace)
|
|
242
|
+
raise e
|
|
243
|
+
else:
|
|
244
|
+
logger.debug("an error occured", e)
|
|
245
|
+
logger.debug(e, stacktrace)
|
|
246
|
+
logger.debug("retrying")
|
|
247
|
+
sleep(1)
|
|
248
|
+
continue
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def fetch(
|
|
252
|
+
instrument: str,
|
|
253
|
+
interval: str,
|
|
254
|
+
offer_side: str,
|
|
255
|
+
start: datetime,
|
|
256
|
+
end: datetime,
|
|
257
|
+
max_retries: int = 7,
|
|
258
|
+
limit: int = 30_000, # max 30_000
|
|
259
|
+
debug=False,
|
|
260
|
+
):
|
|
261
|
+
logger = _get_custom_logger(debug)
|
|
262
|
+
time_unit = _interval_units[interval]
|
|
263
|
+
columns = _get_dataframe_columns_for_timeunit(time_unit)
|
|
264
|
+
|
|
265
|
+
data = []
|
|
266
|
+
|
|
267
|
+
datafeed = _stream(
|
|
268
|
+
instrument=instrument,
|
|
269
|
+
interval=interval,
|
|
270
|
+
offer_side=offer_side,
|
|
271
|
+
start=start,
|
|
272
|
+
end=end,
|
|
273
|
+
max_retries=max_retries,
|
|
274
|
+
limit=limit,
|
|
275
|
+
logger=logger,
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
for row in datafeed:
|
|
279
|
+
data.append(row)
|
|
280
|
+
|
|
281
|
+
df = pd.DataFrame(data=data, columns=columns)
|
|
282
|
+
df["timestamp"] = pd.to_datetime(
|
|
283
|
+
df["timestamp"],
|
|
284
|
+
unit="ms",
|
|
285
|
+
utc=True,
|
|
286
|
+
)
|
|
287
|
+
df = df.set_index("timestamp")
|
|
288
|
+
return df
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def live_fetch(
|
|
292
|
+
instrument: str,
|
|
293
|
+
interval_value: int,
|
|
294
|
+
time_unit: str,
|
|
295
|
+
offer_side: str,
|
|
296
|
+
start: datetime,
|
|
297
|
+
end: datetime,
|
|
298
|
+
max_retries: int = 7,
|
|
299
|
+
limit: int = 30_000, # max 30_000
|
|
300
|
+
debug=False,
|
|
301
|
+
):
|
|
302
|
+
logger = _get_custom_logger(debug)
|
|
303
|
+
assert interval_value > 0
|
|
304
|
+
|
|
305
|
+
# validate time unit
|
|
306
|
+
_resample_to_nearest(
|
|
307
|
+
datetime.now(),
|
|
308
|
+
time_unit,
|
|
309
|
+
interval_value,
|
|
310
|
+
)
|
|
311
|
+
|
|
312
|
+
open, high, low, close, volume = None, 0, 0, 0, 0
|
|
313
|
+
|
|
314
|
+
price_index = {
|
|
315
|
+
OFFER_SIDE_BID: 1,
|
|
316
|
+
OFFER_SIDE_ASK: 2,
|
|
317
|
+
}[offer_side]
|
|
318
|
+
|
|
319
|
+
volume_index = {
|
|
320
|
+
OFFER_SIDE_BID: -2,
|
|
321
|
+
OFFER_SIDE_ASK: -1,
|
|
322
|
+
}[offer_side]
|
|
323
|
+
|
|
324
|
+
last_timestamp = None
|
|
325
|
+
|
|
326
|
+
columns = _get_dataframe_columns_for_timeunit(time_unit)
|
|
327
|
+
df = pd.DataFrame(columns=columns)
|
|
328
|
+
df["timestamp"] = pd.to_datetime(
|
|
329
|
+
df["timestamp"],
|
|
330
|
+
unit="ms",
|
|
331
|
+
utc=True,
|
|
332
|
+
)
|
|
333
|
+
df = df.set_index("timestamp")
|
|
334
|
+
|
|
335
|
+
datafeed = _stream(
|
|
336
|
+
instrument=instrument,
|
|
337
|
+
interval=INTERVAL_TICK,
|
|
338
|
+
offer_side=offer_side,
|
|
339
|
+
start=start,
|
|
340
|
+
end=end,
|
|
341
|
+
max_retries=max_retries,
|
|
342
|
+
limit=limit,
|
|
343
|
+
logger=logger,
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
yield df
|
|
347
|
+
|
|
348
|
+
last_tick_count = 0
|
|
349
|
+
|
|
350
|
+
for tick_count, row in enumerate(datafeed, 0):
|
|
351
|
+
|
|
352
|
+
timestamp = _resample_to_nearest(
|
|
353
|
+
pd.to_datetime(
|
|
354
|
+
row[0],
|
|
355
|
+
unit="ms",
|
|
356
|
+
utc=True,
|
|
357
|
+
),
|
|
358
|
+
time_unit,
|
|
359
|
+
interval_value,
|
|
360
|
+
)
|
|
361
|
+
|
|
362
|
+
if last_timestamp == None:
|
|
363
|
+
last_timestamp = timestamp
|
|
364
|
+
|
|
365
|
+
if time_unit == TIME_UNIT_TICK and interval_value == 1:
|
|
366
|
+
df.loc[timestamp] = [
|
|
367
|
+
*row[1:],
|
|
368
|
+
]
|
|
369
|
+
yield df
|
|
370
|
+
continue
|
|
371
|
+
|
|
372
|
+
new_tick_count = tick_count // interval_value
|
|
373
|
+
|
|
374
|
+
if (
|
|
375
|
+
time_unit != TIME_UNIT_TICK
|
|
376
|
+
and timestamp.timestamp() != last_timestamp.timestamp()
|
|
377
|
+
) or (time_unit == TIME_UNIT_TICK and last_tick_count != new_tick_count):
|
|
378
|
+
if open is not None:
|
|
379
|
+
df.loc[last_timestamp] = [
|
|
380
|
+
open,
|
|
381
|
+
high,
|
|
382
|
+
low,
|
|
383
|
+
close,
|
|
384
|
+
volume,
|
|
385
|
+
]
|
|
386
|
+
|
|
387
|
+
yield df
|
|
388
|
+
last_timestamp = timestamp
|
|
389
|
+
last_tick_count = new_tick_count
|
|
390
|
+
open = None
|
|
391
|
+
|
|
392
|
+
if open is None:
|
|
393
|
+
open = row[price_index]
|
|
394
|
+
close = open
|
|
395
|
+
low = open
|
|
396
|
+
high = open
|
|
397
|
+
volume = 0
|
|
398
|
+
|
|
399
|
+
close = row[price_index]
|
|
400
|
+
high = max(high, close)
|
|
401
|
+
low = min(low, close)
|
|
402
|
+
volume += row[volume_index]
|
|
403
|
+
|
|
404
|
+
df.loc[timestamp] = [
|
|
405
|
+
open,
|
|
406
|
+
high,
|
|
407
|
+
low,
|
|
408
|
+
close,
|
|
409
|
+
volume,
|
|
410
|
+
]
|
|
411
|
+
|
|
412
|
+
yield df
|