Mar-16-2021, 08:12 PM
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.
When I run execute(), model() runs the above function, the historical data is downloaded, but inside the execute() function
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# -*- 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() |
1 |
esFutures = dataDataframe(app, tickers) |
What changes should I make? Talk soon thanks