quantvn 0.1.0__py3-none-any.whl

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.

Potentially problematic release.


This version of quantvn might be problematic. Click here for more details.

@@ -0,0 +1,62 @@
1
+ import io
2
+ import gzip
3
+ import base64
4
+ import requests
5
+ import pandas as pd
6
+
7
+ from .utils import Config
8
+
9
+ # Định nghĩa các thành phần public của module
10
+ __all__ = ["get_hist"]
11
+
12
+ # Lấy URL của Lambda function từ Config
13
+ LAMBDA_URL = Config.get_link()
14
+
15
+
16
+ def get_hist(symbol: str, frequency: str):
17
+ """
18
+ Get historical data of derivatives VN30F1M and VN30F2M.
19
+
20
+ Parameters
21
+ ----------
22
+ symbol : str
23
+ Derivatives symbol (e.g. "VN30F1M", "VN30F2M").
24
+ frequency : str
25
+ Timeframe to get data (e.g. "1D", "1H", "5M").
26
+ Returns
27
+ -------
28
+ dict
29
+ Historical data with information such as time, closing price, trading volume.
30
+
31
+ Raises
32
+ ------
33
+ Exception
34
+ If there is an error when calling the API.
35
+ """
36
+ api_key = Config.get_api_key()
37
+ payload = {"symbol": symbol, "frequency": frequency}
38
+
39
+ response = requests.post(
40
+ f"{LAMBDA_URL}/data-derivatives",
41
+ json=payload,
42
+ headers={"x-api-key": api_key},
43
+ )
44
+
45
+ if response.status_code == 200:
46
+ data = response.json()
47
+
48
+ if isinstance(data, dict) and "base64" in data:
49
+ try:
50
+ decoded_data = base64.b64decode(data["base64"])
51
+
52
+ with gzip.GzipFile(fileobj=io.BytesIO(decoded_data), mode="rb") as gz:
53
+ extracted_content = gz.read().decode("utf-8")
54
+ df = pd.read_csv(io.StringIO(extracted_content), index_col=0)
55
+ return df
56
+
57
+ except Exception as e:
58
+ return {"error": f"Failed to process base64 data: {str(e)}"}
59
+
60
+ return pd.DataFrame(data)
61
+ else:
62
+ raise Exception(f"Error: {response.status_code}, {response.text}")