Working within Ubuntu 22.04 and Python 3.9.13 within VSCode 1.80.1.
I have a Python script set up to stream data from an API 'in perpetuity'.
As part of this, I create a CSV file to which I save all data coming through the API dynamically (ie, file is updated as new data is received).
This works well and good, but I'd like to be able move/rename the file (which is created with a generic name) to something incorporating date/time the script stopped, whether it stopped due to API issue, KeyboardInterrupt, etc.
I'd need to add an element that triggers when the script stops running--wonder if this would best be done using something system related to allow me to track threads (eg, Bash) or something else?
I'd also need to add code that takes time stopped and appends it to the starting name of the file to identify when the CSV was last updated.
For instance, with file name below last run time of 20230716_1527 (runEnd
) I'd like to change the default file name from this:
f'~/data/{sym}_2.csv'
to this:
f'~/data/{sym}_2_{runEnd}.csv'
Sample code and method for calling below (the code won't run successfully without an account):
Sample call:
python test.py 'ESU3'
test.py
from ibapi.client import *
from ibapi.wrapper import *
from ibapi.contract import *
import time
import pandas as pd
import datetime
import threading
import sys
global sym
sym = sys.argv[1]
global df
df = pd.DataFrame()
class TestApp(EClient, EWrapper):
def __init__(self):
EClient.__init__(self, wrapper=self)
def updateMktDepth(self, reqId: TickerId, position: int, operation: int, side: int, price: float, size: int):
global df
super().updateMktDepth(reqId, position, operation, side, price, size)
time = datetime.datetime.now()
cols = ['Time','Symbol','Position','Operation','Side','Price','Size']
data = [time,sym,position,operation,side,price,size]
print(f'UpdateMarketDepth / {time}: ReqId: {reqId} Sym: {sym} Position: {position} Operation: {operation} Side: {side}, Price: {price} Size {size}')
d2 = pd.DataFrame(data, cols)
d2 = d2.T
df = pd.concat([df, d2]) #df.concat(d2)
df.to_csv(f'~/data/{sym}_2.csv')
def updateMktDepthL2(self, reqId: TickerId, position: int, marketMaker: str, operation: int, side: int, price: float, size: int, isSmartDepth: bool):
super.updateMktDepthL2(reqId, position, operation, side, price, size)
print("UpdateMarketDepthL2", "ReqId:", reqId, "Position:", position, "MarketMaker:", marketMaker, "Operation:",
operation, "Side:", side, "Price:", price, "Size:", size, "isSmartDepth:", isSmartDepth)
def main():
try:
app = TestApp()
app.connect("127.0.0.1", 7496, 1000)
print('Connection successful')
t = threading.Thread(name="API_worker", target=app.run)
t.start()
# app.run()
print("Returned from run()")
c = Contract()
c.localSymbol = sym
c.secType = 'FUT'
c.exchange = 'CME'
c.currency = 'USD'
time.sleep(1)
app.reqMktDepth(4005, c, 20, 0, [])
except KeyboardInterrupt:
print('Keyboard interrup, processing ended')
app.disconnect()
if __name__ == "__main__":
main()