updated with json
This commit is contained in:
20130
src/Data-Collection/WebScraper/data/HistoricalData.json
Normal file
20130
src/Data-Collection/WebScraper/data/HistoricalData.json
Normal file
File diff suppressed because it is too large
Load Diff
30
src/Data-Collection/WebScraper/requirements.txt
Normal file
30
src/Data-Collection/WebScraper/requirements.txt
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
attrs==24.2.0
|
||||||
|
beautifulsoup4==4.12.3
|
||||||
|
certifi==2024.8.30
|
||||||
|
charset-normalizer==3.4.0
|
||||||
|
flake8==7.1.1
|
||||||
|
h11==0.14.0
|
||||||
|
idna==3.10
|
||||||
|
mccabe==0.7.0
|
||||||
|
numpy==2.1.2
|
||||||
|
outcome==1.3.0.post0
|
||||||
|
pandas==2.2.3
|
||||||
|
pycodestyle==2.12.1
|
||||||
|
pyflakes==3.2.0
|
||||||
|
PySocks==1.7.1
|
||||||
|
python-dateutil==2.9.0.post0
|
||||||
|
pytz==2024.2
|
||||||
|
requests==2.32.3
|
||||||
|
selenium==4.25.0
|
||||||
|
six==1.16.0
|
||||||
|
sniffio==1.3.1
|
||||||
|
sortedcontainers==2.4.0
|
||||||
|
soupsieve==2.6
|
||||||
|
tqdm==4.66.6
|
||||||
|
trio==0.27.0
|
||||||
|
trio-websocket==0.11.1
|
||||||
|
typing_extensions==4.12.2
|
||||||
|
tzdata==2024.2
|
||||||
|
urllib3==2.2.3
|
||||||
|
websocket-client==1.8.0
|
||||||
|
wsproto==1.2.0
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
import json
|
||||||
|
from datetime import datetime
|
||||||
|
from ibapi.client import EClient
|
||||||
|
from ibapi.wrapper import EWrapper
|
||||||
|
from ibapi.contract import Contract
|
||||||
|
|
||||||
|
class IBKRDataRetriever(EWrapper, EClient):
|
||||||
|
def __init__(self):
|
||||||
|
EClient.__init__(self, self)
|
||||||
|
|
||||||
|
def connect_and_retrieve_data(self):
|
||||||
|
self.connect("127.0.0.1", 7497, clientId=0) # Ensure IB Gateway or TWS is running
|
||||||
|
contract = Contract()
|
||||||
|
contract.symbol = "AAPL" # Example stock; replace as needed
|
||||||
|
contract.secType = "STK"
|
||||||
|
contract.exchange = "SMART"
|
||||||
|
contract.currency = "USD"
|
||||||
|
|
||||||
|
self.reqHistoricalData(
|
||||||
|
reqId=1,
|
||||||
|
contract=contract,
|
||||||
|
endDateTime=datetime.now().strftime("%Y%m%d %H:%M:%S"),
|
||||||
|
durationStr="1 D",
|
||||||
|
barSizeSetting="1 day",
|
||||||
|
whatToShow="MIDPOINT",
|
||||||
|
useRTH=1,
|
||||||
|
formatDate=1,
|
||||||
|
keepUpToDate=False,
|
||||||
|
chartOptions=[]
|
||||||
|
)
|
||||||
|
|
||||||
|
def historicalData(self, reqId, bar):
|
||||||
|
data = {
|
||||||
|
"Date": bar.date,
|
||||||
|
"Close/Last": bar.close,
|
||||||
|
"Volume": bar.volume,
|
||||||
|
"Open": bar.open,
|
||||||
|
"High": bar.high,
|
||||||
|
"Low": bar.low
|
||||||
|
}
|
||||||
|
self.save_data_to_json(data)
|
||||||
|
|
||||||
|
def save_data_to_json(self, data):
|
||||||
|
json_path = "../data/HistoricalData.json"
|
||||||
|
try:
|
||||||
|
with open(json_path, "r") as file:
|
||||||
|
historical_data = json.load(file)
|
||||||
|
except FileNotFoundError:
|
||||||
|
historical_data = []
|
||||||
|
|
||||||
|
historical_data.insert(0, data)
|
||||||
|
|
||||||
|
with open(json_path, "w") as file:
|
||||||
|
json.dump(historical_data, file, indent=4)
|
||||||
|
print(f"Data saved to {json_path}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app = IBKRDataRetriever()
|
||||||
|
app.connect_and_retrieve_data()
|
||||||
|
|
||||||
|
Can't render this file because it is too large.
|
|
Can't render this file because it is too large.
|
Reference in New Issue
Block a user