From ec4cca58690ff1bc23ab02000fb7aa5394dd5a7d Mon Sep 17 00:00:00 2001 From: moshferatu Date: Fri, 29 Sep 2023 12:21:39 -0700 Subject: [PATCH] Look up host and port from environment variables and add some short-hand imports for clients --- iqfeed/__init__.py | 1 + iqfeed/iqfeed.py | 13 ++++++------- requirements.txt | 3 ++- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/iqfeed/__init__.py b/iqfeed/__init__.py index e69de29..b746cf5 100644 --- a/iqfeed/__init__.py +++ b/iqfeed/__init__.py @@ -0,0 +1 @@ +from .iqfeed import get_daily_data, get_historical_data, hours, minutes, seconds \ No newline at end of file diff --git a/iqfeed/iqfeed.py b/iqfeed/iqfeed.py index 1a93384..8b4ba96 100644 --- a/iqfeed/iqfeed.py +++ b/iqfeed/iqfeed.py @@ -2,8 +2,12 @@ import pandas as pd import socket from datetime import datetime +from dotenv import load_dotenv +from os import getenv from io import StringIO +load_dotenv() + def read_data_from_socket(sock, recv_buffer=4096): buffer = b'' data = b'' @@ -19,16 +23,13 @@ def read_data_from_socket(sock, recv_buffer=4096): return buffer.decode('utf-8') def _get_daily_data(symbol: str, max_days: int) -> pd.DataFrame: - host = '127.0.0.1' - port = 9100 # Historical data socket port - message = 'HDX,{symbol},{max_days},1\n'.format( symbol=symbol, max_days=max_days ) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.connect((host, port)) + sock.connect((getenv('IQFEED_HOST'), int(getenv('IQFEED_PORT')))) sock.sendall(message.encode('utf-8')) data = read_data_from_socket(sock) @@ -66,8 +67,6 @@ def get_historical_data(symbol: str, interval: int, start_date: datetime, end_date: datetime = None, start_filter_time: datetime = datetime.strptime('093000', '%H%M%S').time(), end_filter_time: datetime = datetime.strptime('160000', '%H%M%S').time()) -> pd.DataFrame: - host = '127.0.0.1' - port = 9100 # Historical data socket port message = 'HIT,{symbol},{interval},{begin},{end},,{begin_filter},{end_filter},1\n'.format( symbol=symbol, @@ -79,7 +78,7 @@ def get_historical_data(symbol: str, interval: int, ) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.connect((host, port)) + sock.connect((getenv('IQFEED_HOST'), int(getenv('IQFEED_PORT')))) sock.sendall(message.encode('utf-8')) data = read_data_from_socket(sock) diff --git a/requirements.txt b/requirements.txt index 1411a4a..a496539 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -pandas \ No newline at end of file +pandas +python-dotenv \ No newline at end of file