Python Forum
Trying to get all this on a single thread
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to get all this on a single thread
#1
Hello everyone, I am trying to get this code eventually onto a single thread but I cannot figure out how to get everything into functions. Because with my newb understanding.. threads just run functions correct? Please be gentle with me, and explain things like your talking to a 10 year old and show code please.

# -*- coding: utf-8 -*- """ Created on Tue Mar 16 15:25:39 2021 @author: zenfi """ from ibapi.client import EClient from ibapi.wrapper import EWrapper from ibapi.contract import Contract from copy import deepcopy import threading import time import pandas as pd import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as mdates import datetime class TradingApp(EClient, EWrapper): def __init__(self): EClient.__init__(self, self) self.data = {} def historicalData(self, reqId, bar): if reqId not in self.data: self.data[reqId] = [{"Date": bar.date, "High": bar.high, "Low": bar.low, "Close": bar.close}] if reqId in self.data: self.data[reqId].append({"Date": bar.date, "High": bar.high, "Low": bar.low, "Close": bar.close}) print("ReqID: {}, Date: {}, High: {}, Low: {}".format(reqId, bar.date, bar.high, bar.low)) def websocket_con(): app.run() app = TradingApp() app.connect("127.0.0.1", 7497, clientId=2) connection = threading.Thread(target=websocket_con, daemon = True) connection.start() time.sleep(1) tickers = ["ES"] def model(app, tickers): def usTechStk(symbol,sec_type="FUT",currency="USD",exchange="GLOBEX"): contract = Contract() contract.conId = "396336017" contract.symbol = "ES" contract.secType = "FUT" contract.exchange = "GLOBEX" contract.currency = "USD" return contract def histData(reqId, contract, duration, barSizeSetting): app.reqHistoricalData(reqId = reqId, contract = contract, endDateTime = "", durationStr = duration, barSizeSetting = barSizeSetting, whatToShow = "TRADES", useRTH = 1, formatDate = 1, keepUpToDate = False, chartOptions = []) for ticker in tickers: histData(tickers.index(ticker), usTechStk(ticker), "1 D", "1 min") time.sleep(3) def dataDataframe(app, tickers): df_dict = {} for ticker in tickers: df_dict[ticker] = pd.DataFrame(app.data[tickers.index(ticker)]) df_dict[ticker].set_index("Date", inplace = True) return df_dict def execute(): model(app, tickers) esFutures = dataDataframe(app, tickers) execute()
When I run execute(), model() runs the above function, the historical data is downloaded, but inside the execute() function

esFutures = dataDataframe(app, tickers)
doesnt run. I am trying to run the code from model() function downwards on a single thread. I was trying to get everything into a single function, but I cant get the dataframe to work. But the dataframe works fine when I just run the esFutures line by itself.

What changes should I make? Talk soon thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 21,733 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.