from camera import Camera import logging import imagezmq from logic import AppLogic from mqtt import MQTTClient from storageapi.client import Client logging.basicConfig(level=logging.DEBUG) log = logging.getLogger() mqtt = MQTTClient() storage = Client(os.environ.get("STORAGE_API_URL")) # Accept connections on all tcp addresses, port xxxx port = os.environ.get("ZMQ_PORT", "5553") sender = imagezmq.ImageSender(connect_to="tcp://*:{}".format(port), REQ_REP=False) rpi_name = "RadarPiCam" # send RPi hostname with each image camera = Camera() camera.switch_on() mqtt.start() logic = AppLogic(camera, mqtt, storage, sender) logic.loop_forever()
def __init__(self): wrapper.EWrapper.__init__(self) self.startedLogic = False self.logic = AppLogic(self) self.client = self.logic.client
class AppWrapper(wrapper.EWrapper): """ Thread to Manage Callbacks. """ def __init__(self): wrapper.EWrapper.__init__(self) self.startedLogic = False self.logic = AppLogic(self) self.client = self.logic.client @iswrapper def nextValidId(self, orderId): self.logic.account.setNextOrderId(orderId) console().info("Next Order ID: {}".format(orderId)) if not self.startedLogic: self.logic.start() self.startedLogic = True @iswrapper def managedAccounts(self, accountsList): self.logic.account.setAccount(accountsList) console().info("Received Account: {}".format(self.logic.account)) subscribeAccountPositions(self.client) @iswrapper def contractDetails(self, reqId, contractDetails): super().contractDetails(reqId, contractDetails) symbol = contractDetails.summary.localSymbol expires = contractDetails.summary.lastTradeDateOrContractMonth console().info("Received Contract Details for: {}. Expires: {}".format( symbol, expires)) self.client.pushRequestData(reqId, {symbol: contractDetails}) @iswrapper def contractDetailsEnd(self, reqId): super().contractDetailsEnd(reqId) console().info("Got All Contract Details.") self.client.finishRequest(reqId) contractDetails = self.client.getRequestData(reqId) self.client.purgeRequest(reqId) self.logic.future = getCurrentFuturesContract(contractDetails) @iswrapper def tickPrice(self, reqId, tickType, price, attrib): super().tickPrice(reqId, tickType, price, attrib) if tickType == TICK_TYPES["LAST_PRICE"]: self.client.pushRequestData(reqId, {"price": {"last": price}}) @iswrapper def tickSize(self, reqId, tickType, size): super().tickSize(reqId, tickType, size) @iswrapper def tickGeneric(self, reqId, tickType, value): super().tickGeneric(reqId, tickType, value) @iswrapper def tickString(self, reqId, tickType, value): super().tickString(reqId, tickType, value) @iswrapper def error(self, reqId, errorCode, errorString): super().error(reqId, errorCode, errorString) apiMessage(errorString) @iswrapper def position(self, account, contract, position, avgCost): super().position(account, contract, position, avgCost) if account == self.logic.account.account: console().info("Position Update: {}: #Contracts: {}".format( contract.localSymbol, position)) self.logic.account.updatePosition(contract, position) else: console().warning( "Got Position for Untracked Account: {}".format(account)) @iswrapper def historicalData(self, reqId, bar): if ("bars" not in self.client.data[reqId].keys()): self.client.pushRequestData( reqId, {"bars": deque(maxlen=config.TIME_PERIODS)}) self.client.data[reqId]["bars"].append( BAR(High=bar.high, Low=bar.low, Close=bar.close, Time=bar.date)) @iswrapper def historicalDataUpdate(self, reqId, bar): lastBar = self.client.data[reqId]["bars"][-1] newBar = BAR(High=bar.high, Low=bar.low, Close=bar.close, Time=bar.date) if (lastBar.Time == newBar.Time): self.client.data[reqId]["bars"][-1] = newBar else: self.client.data[reqId]["bars"].append(newBar) cci = calculateCCI(self.client.data[reqId]["bars"]) console().info("Calculated CCI: {}".format(cci)) @iswrapper def historicalDataEnd(self, reqId, start, end): super().historicalDataEnd(reqId, start, end) console().info("Got CCI Historical Data") self.client.finishRequest(reqId) @iswrapper def openOrder(self, orderId, contract, order, orderState): self.logic.account.tmpOrders[orderId] = (contract, order, orderState.status) @iswrapper def openOrderEnd(self): openOrders, tmpOrders = self.logic.account.openOrders, self.logic.account.tmpOrders ledger = self.client.ledger for orderId, values in tmpOrders.items(): symbol, status = values[0].localSymbol, values[2] oldStatus = openOrders[orderId][2] if orderId in openOrders.keys( ) else None logOrder(ledger, orderId, symbol, values[1], status, oldStatus) self.logic.account.openOrders = tmpOrders self.logic.account.tmpOrders = {} @iswrapper def orderStatus(self, orderId, status, filled, remaining, avgFillPrice, permId, parentId, lastFillPrice, clientId, whyHeld): if clientId != config.CLIENTID: return openOrders = self.logic.account.openOrders if orderId not in openOrders.keys(): return contract, order, oldStatus = openOrders[orderId] symbol = contract.localSymbol logOrder(self.client.ledger, orderId, symbol, order, status, oldStatus) openOrders[orderId] = (contract, order, status) if status in ["Cancelled", "Filled"]: self.client.reqOpenOrders()
class AppWrapper(wrapper.EWrapper): """ Thread to Manage Callbacks. """ def __init__(self): wrapper.EWrapper.__init__(self) self.startedLogic = False self.logic = AppLogic(self) self.client = self.logic.client @iswrapper def nextValidId(self, orderId): self.logic.account.setNextOrderId(orderId) console().info("Next Order ID: {}".format(orderId)) if not self.startedLogic: self.logic.start() self.startedLogic = True @iswrapper def managedAccounts(self, accountsList): self.logic.account.setAccount(accountsList) console().info("Received Account: {}".format(self.logic.account)) subscribeAccountPositions(self.client) @iswrapper def contractDetails(self, reqId, contractDetails): super().contractDetails(reqId, contractDetails) symbol = contractDetails.summary.localSymbol expires = contractDetails.summary.lastTradeDateOrContractMonth console().info("Received Contract Details for: {}. Expires: {}".format( symbol, expires)) self.client.pushRequestData(reqId, {symbol: contractDetails}) @iswrapper def contractDetailsEnd(self, reqId): super().contractDetailsEnd(reqId) console().info("Got All Contract Details.") self.client.finishRequest(reqId) contractDetails = self.client.getRequestData(reqId) self.client.purgeRequest(reqId) self.logic.future = getCurrentFuturesContract(contractDetails) @iswrapper def tickPrice(self, reqId, tickType, price, attrib): super().tickPrice(reqId, tickType, price, attrib) if tickType == TICK_TYPES["LAST_PRICE"]: self.client.pushRequestData(reqId, {"price": {"last": price}}) @iswrapper def tickSize(self, reqId, tickType, size): super().tickSize(reqId, tickType, size) @iswrapper def tickGeneric(self, reqId, tickType, value): super().tickGeneric(reqId, tickType, value) @iswrapper def tickString(self, reqId, tickType, value): super().tickString(reqId, tickType, value) @iswrapper def error(self, reqId, errorCode, errorString): super().error(reqId, errorCode, errorString) apiMessage(errorString) @iswrapper def position(self, account, contract, position, avgCost): super().position(account, contract, position, avgCost) if account == self.logic.account.account: console().info("Position Update: {}: #Contracts: {}".format( contract.localSymbol, position)) self.logic.account.updatePosition(contract, position) else: console().warning( "Got Position for Untracked Account: {}".format(account)) @iswrapper def historicalData(self, reqId, bar): console().info( "Got Historical Data. Date:{}. High:${}. Low:${}".format( bar.date, bar.high, bar.low)) self.client.pushRequestData( reqId, {"historical": { "high": bar.high, "low": bar.low }}) @iswrapper def historicalDataEnd(self, reqId, start, end): super().historicalDataEnd(reqId, start, end) self.client.finishRequest(reqId) @iswrapper def openOrder(self, orderId, contract, order, orderState): self.logic.account.tmpOrders[orderId] = (contract, order, orderState.status) @iswrapper def openOrderEnd(self): openOrders, tmpOrders = self.logic.account.openOrders, self.logic.account.tmpOrders ledger = self.client.ledger for orderId, values in tmpOrders.items(): symbol, status = values[0].localSymbol, values[2] oldStatus = openOrders[orderId][2] if orderId in openOrders.keys( ) else None logOrder(ledger, orderId, symbol, values[1], status, oldStatus) self.logic.account.openOrders = tmpOrders self.logic.account.tmpOrders = {} @iswrapper def orderStatus(self, orderId, status, filled, remaining, avgFillPrice, permId, parentId, lastFillPrice, clientId, whyHeld): if clientId != config.CLIENTID: return openOrders = self.logic.account.openOrders if orderId not in openOrders.keys(): return contract, order, oldStatus = openOrders[orderId] symbol = contract.localSymbol logOrder(self.client.ledger, orderId, symbol, order, status, oldStatus) openOrders[orderId] = (contract, order, status) if status in ["Cancelled", "Filled"]: self.client.reqOpenOrders() @iswrapper def receiveFA(self, dataType, profile): console().info("Got the Advisor Profile Data") reqId = self.client.getRequestID("ADVISOR CONFIG") self.client.pushRequestData(reqId, {"xml": profile}) self.client.finishRequest(reqId)
import logging import dash from data import MongoManager from logic import AppLogic logger = logging.getLogger('dashvision.{}'.format(__name__)) external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] app = dash.Dash(__name__, external_stylesheets=external_stylesheets) app.config.suppress_callback_exceptions = True data_manager = MongoManager(server='drunk', port='27017') logic_manager = AppLogic(data_manager) default_columns = ['_id', 'status']