Last active
September 16, 2023 14:32
-
-
Save tomas-rampas/b3ba814caac716ced21a67545a900292 to your computer and use it in GitHub Desktop.
IQFeed Historical data downloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
# https://ws1.dtn.com/IQ/Guide/ | |
def read_historical_data_socket(sock, recv_buffer=4096): | |
""" | |
Read the information from the socket, in a buffered | |
fashion, receiving only 4096 bytes at a time. | |
Parameters: | |
sock - The socket object | |
recv_buffer - Amount in bytes to receive per read | |
""" | |
buffer = b"" | |
while True: | |
data = sock.recv(recv_buffer) | |
buffer += data | |
# Check if the end message string arrives | |
if "!ENDMSG!".encode() in buffer: | |
break | |
# Remove the end message string | |
buffer = buffer[:-12] | |
return buffer | |
# iqfeed.py | |
if __name__ == "__main__": | |
# Define server host, port and symbols to download | |
host = "127.0.0.1" # Localhost | |
port = 9100 # Historical data socket port | |
syms = ["EURUSD.FXCM", "EURUSD.COMP", "XAUUSD.FXCM", "XAUUSD.COMP", "@ESU23","@M6EU23","@M6BU23"] | |
# Download each symbol to disk | |
for sym in syms: | |
print("Downloading symbol: %s..." % sym) | |
# Construct the message needed by IQFeed to retrieve data | |
# message = "HIT,%s,60,20230801 075000,,,093000,160000,1\n" % sym | |
message = "HTD,%s,2,,093000,160000,1,,\n" % sym | |
# Open a streaming socket to the IQFeed server locally | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect((host, port)) | |
# Send the historical data request | |
# message and buffer the data | |
sock.sendall(message.encode()) | |
data = read_historical_data_socket(sock) | |
sock.close() | |
# Remove all the endlines and line-ending | |
# comma delimiter from each record | |
data = "".join(data.decode().split("\r")) | |
data = data.replace(",\n","\n")[:-1] | |
# Write the data stream to disk | |
f = open("%s.csv" % sym, "w") | |
f.write(data) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment