class WizardMon: def __init__(self, deviceId): self.deviceId = deviceId self.broker = Broker() print("Init-ed") def _get_timestamp_utc(self): dt = datetime.datetime.now() utc_time = dt.replace(tzinfo=timezone.utc) utc_timestamp: str = str(utc_time.timestamp()) return (utc_timestamp, str(utc_time)) def heartbeat(self, meta: dict = {}): msg = HeartbeatSchema.copy() (utc, utc_pretty) = self._get_timestamp_utc() print("Sending hearbeat to broker : " + utc_pretty) msg["utc"] = utc msg["utc_pretty"] = utc_pretty msg["message"]["deviceId"] = self.deviceId msg["message"]["meta"] = meta to_send: str = json.dumps(msg) self.broker.produce(to_send) def hook(self, eventType, eventMeta, errors=[]): print("Sending hook to broker") msg = HookSchema.copy() (utc, utc_pretty) = self._get_timestamp_utc() msg["utc"] = utc msg["utc_pretty"] = utc_pretty msg["message"]["deviceId"] = self.deviceId msg["message"]["errors"] = errors msg["message"]["event"]["type"] = eventType msg["message"]["event"]["meta"] = eventMeta to_send: str = json.dumps(msg) self.broker.produce(to_send)
def create_brokers(mode, pairs, exchangeNames): # returns an array of Broker objects brokers = [] for name in exchangeNames: if (name == 'VIRCUREX'): xchg = Vircurex(config.VIRCUREX_USER, config.VIRCUREX_SECURITY_WORD) elif (name == 'BTCE'): xchg = BTCE(config.BTCE_KEYFILE) elif (name == 'BTER'): xchg = BTER(config.BTER_KEYFILE) elif (name == 'COINS-E'): xchg = CoinsE(config.COINS_E_API_KEY, config.COINS_E_SECRET) elif (name == 'CRYPTSY'): xchg = Cryptsy(config.CRYPTSY_API_KEY, config.CRYPTSY_SECRET) elif (name == 'CRYPTO-TRADE'): xchg = CryptoTrade(config.CRYPTOTRADE_API_KEY, config.CRYPTOTRADE_SECRET) elif (name == 'COINEX'): xchg = CoinEx(config.COINEX_API_KEY, config.COINEX_SECRET) else: print('Exchange ' + name + ' not supported!') continue print('%s initialized' % (xchg.name)) broker = Broker(mode, xchg) if mode == 'BACKTEST': # broker.balances = config.PAPER_BALANCE broker.balances = broker.xchg.get_all_balances() # use real starting balances. brokers.append(broker) return brokers
def main(): xsub_port = '5556' xpub_port = '5557' broker = Broker(xsub_port, xpub_port) broker.handler() while True: pass
class TestBroker(unittest.TestCase): #Database startup broker1 = Broker.Broker(topic="topic/channel", logs=False, logName='Broker1') broker2 = Broker.Broker(topic="topic/channel", logs=False, logName='Broker2') broker3 = Broker.Broker(topic="topic/channel", logs=False, logName='Broker3') broker1.subscribe(topic="topic/channel") broker2.subscribe(topic="topic/channel") payload = randint(0, 9) payload2 = randint(0, 9) payload3 = randint(0, 9) received_payload = ' ' def iAmACallbackFunction(self, topic, payload): self.received_topic = topic self.received_payload = payload self.broker1.stop() def iAmACallbackFunction2(self, topic, payload): self.received_topic = topic self.received_payload2 = payload self.broker1.stop() def iAmACallbackFunction3(self, topic, payload): self.received_topic = topic self.received_payload3 = payload self.broker1.stop() def test_it_should_receive_message(self): self.broker1.setCallback(self.iAmACallbackFunction) self.broker1.start() self.broker2.start() self.broker2.publishMessage("topic/channel", self.payload) self.broker2.stop() time.sleep(1) self.broker1.stop() self.assertEqual(str(self.payload), self.received_payload) def test_it_should_subscribe_to_multiple_topics(self): self.broker3.subscribeTopicWithCallback("topic/channel2", self.iAmACallbackFunction2) self.broker3.subscribeTopicWithCallback("topic/channel3", self.iAmACallbackFunction3) self.broker3.setCallbacks() self.broker3.start() self.broker3.publishMessage("topic/channel2", self.payload2) time.sleep(1) self.broker3.publishMessage("topic/channel3", self.payload3) time.sleep(1) self.broker3.stop() self.assertEqual(str(self.payload2), self.received_payload2) self.assertEqual(str(self.payload3), self.received_payload3) pass
def routeQueries(self, newList, segmentRunningCount, time): self.queryList.extend(newList) self.log("Routing Queries") (routinglist, self.queryList) = self.findRoutableQueries(self.queryList, self.historicalNodeList) if len(routinglist) > 0: Utils.printQueryList(routinglist) Broker.routeQueries(routinglist, self.historicalNodeList, self.routingStrategy, segmentRunningCount, time) Utils.printQueryAssignment(self.historicalNodeList)
def test_errores_de_actividades_porcentaje(self, monto, porcentaje, error): broker = Broker( [42670460, "Lucas", "Soria", False], ["Voy a comprar algo con 500 pesos", 2, monto, porcentaje], self.caj) broker.registrar_actividad() actDAO = ActividadDao(broker.db) actividades = actDAO.buscarTodos() actividad = actividades[0] actDAO.borrar(actividades[0]) self.assertEqual(actividad.mensaje, error)
def test_contar(self): broker = Broker([42670460, "Lucas", "Soria", False], [ "Voy a comprar algo con 500 pesos", 3, ], self.caj) broker.registrar_actividad() actDAO = ActividadDao(broker.db) actividades = actDAO.buscarTodos() actividad = actividades[0] actDAO.borrar(actividades[0]) self.assertEqual(actividad.mensaje, "Total: $0")
def __init__(self, tax_rate=0.26375, broker=comdirect(), orderbook=OrderBook()): """ Initialize all depots. All depots utilize the same Broker's tax rate, comdirect as default broker and orderbook. """ Broker.setTAXRate(tax_rate) self.__broker = broker self.__orderbook = orderbook
def test_insertar(self): self.lista = [] for x in range(17): self.lista.append(self.mil_pesos) broker = Broker([42670460, "Lucas", "Soria", False], ["Voy a comprar algo con 500 pesos", 4, self.lista], self.caj) broker.registrar_actividad() actDAO = ActividadDao(broker.db) actividades = actDAO.buscarTodos() actividad = actividades[0] actDAO.borrar(actividades[0]) self.assertEqual(actividad.mensaje, "Se ingreso dinero correctamente")
def depot(): """Sample pytest fixture. See more at: http://doc.pytest.org/en/latest/fixture.html """ # import requests # return requests.get('https://github.com/audreyr/cookiecutter-pypackage') """ First of all, it builds the Depot """ Broker.setTAXRate(0.25) broker = comdirect() orderbook = OrderBook() depot = Depot(stock='APPL', broker=broker, orderbook=orderbook) return depot
class IoTPlatform(): def __init__(self, ip): self.nodesTxt = "./nodes.txt" self.broker = Broker(ip) self.createFile() self.loadFromFile() def createFile(self): if os.path.isfile(self.nodesTxt) == False: file = open(self.nodesTxt, 'w') file.write('{"MQTT": []}') file.close() def saveToFile(self): file = open(self.nodesTxt, "w") fileData = "{" tempString = self.broker.nodeHandler.convertJson() if tempString == "": fileData += '"MQTT": []' else: fileData += '"MQTT":[' + tempString + "]" fileData += "}" file.write(fileData) file.close() def loadFromFile(self): file = json.load(open(self.nodesTxt)) for mqtt in file['MQTT']: self.broker.nodeHandler.add(Node(mqtt['name'], mqtt['ID'], mqtt['lastSendMsg'], mqtt['lastReceivedMsg'], mqtt['triggerEvents'])) def addNodeAndSaveFile(self, name): self.broker.nodeHandler.add(Node(name)) self.saveToFile() pass def nodeSendPayload(self, name, payload): self.broker.sendPayload(name, payload) def getNodes(self): return '{"MQTT":[' + self.broker.nodeHandler.convertJson() + "]}" def addTriggersToNodesAndSaveFile(self, src, eventID): self.broker.nodeHandler.addTriggersToNodes(src, eventID) self.saveToFile()
def routeQueries(self, newList, segmentRunningCount, time): self.queryList.extend(newList) self.log(time, "Routing Queries") (routinglist, self.queryList) = self.findRoutableQueries(self.queryList, self.historicalNodeList) if len(routinglist) > 0: self.queriesrouted += len(routinglist) Utils.printQueryList(routinglist) Broker.routeQueries(routinglist, self.historicalNodeList, self.routingStrategy, segmentRunningCount, time) Utils.printQueryAssignment(self.historicalNodeList) for query in routinglist: completiontime = query.getCompletionTime() assert completiontime > -1 self.totalcompletiontime += completiontime
def test_errores_de_actividades(self, monto, error): broker = Broker([42670460, "Lucas", "Soria", False], ["Voy a comprar algo con 500 pesos", 1, monto], self.caj) self.lista = [] for x in range(6): self.lista.append(self.mil_pesos) self.lista.append(self.quinientos_pesos) self.caj.agregar_dinero(self.lista) broker.registrar_actividad() actDAO = ActividadDao(broker.db) actividades = actDAO.buscarTodos() actividad = actividades[0] actDAO.borrar(actividades[0]) self.assertEqual(actividad.mensaje, error)
def create_mmbot(self, symbol): print("CREATE MMBOT %s FOR %s WITH %d$ QUOTA TO RUN LIVE:%d" % (self.botname, symbol, self.quota, self.live)) mylogger = MyLogger(symbol, self.live) if self.brokertype == "ibkr": print("CREATE IBKR BROKER") broker = IBBroker(self.budget, self.quota, mylogger) else: print("CREATE SIM MARKET") broker = Broker(self.budget, self.quota, mylogger) if self.marketdata == "ibkr": print("CREATE IBKR MARKET DATA") market = IBMarket(self.rth) else: print("CREATE ALPHA VANTAGE MARKER DATA") market = Market() # create and add bot to list of bots self.bots.append( MMBot(budget=self.quota, symbol=symbol, period=self.period, live=self.live, debug=self.debug, email=self.email, daily=self.daily, broker=broker, market=market, mylogger=mylogger, since=self.since, liquidate=self.liquidate))
def test_error_billetes(self): broker = Broker([42670460, "Lucas", "Soria", False], ["Voy a comprar algo con 500 pesos", 1, 12100], self.caj) self.lista = [] for x in range(17): self.lista.append(self.mil_pesos) self.caj.agregar_dinero(self.lista) broker.registrar_actividad() actDAO = ActividadDao(broker.db) actividades = actDAO.buscarTodos() actividad = actividades[0] actDAO.borrar(actividades[0]) self.assertEqual( actividad.mensaje, "Error. No hay una combinación" " de billetes que nos permita" " extraer ese monto")
def create_brokers(mode, pairs, exchangeNames): # returns an array of Broker objects brokers = [] for name in exchangeNames: if (name == 'binance'): xchg = CryptoExchange('binance', config.BINANCE_KEY, config.BINANCE_SECRET) else: print('Exchange ' + name + ' not supported!') continue print('%s initialized' % (xchg.name)) broker = Broker(mode, xchg) if mode == 'BACKTEST': # broker.balances = config.PAPER_BALANCE broker.balances = broker.xchg.get_all_balances() # use real starting balances. brokers.append(broker) return brokers
def main(): abcStock = Stock() buyStockOrder = BuyStock(abcStock) sellStockOrder = SellStock(abcStock) broker = Broker() broker.takeOrder(buyStockOrder) broker.takeOrder(sellStockOrder) broker.placeOrders()
def run(self): date = dateHelper.getDateAsDatetime(self.startDate) endDate = dateHelper.getDateAsDatetime(self.endDate) provider = DataProvider() balance = BalancePortfolio(provider) broker = Broker(provider) broker.addCash(self.initialCash) portfolioValue = [] while (date < endDate): date_str = dateHelper.getDateAsString(date) if (date.weekday() < 5 and not self.isHoliday(date)): value = broker.getValue(dateHelper.getDateAsString(date)) if (self.lastRebalance > self.rebalanceDays): self.lastRebalance = 0 broker.setPortfolio(balance.getPortfolio(date_str, value), date_str) else: self.lastRebalance += 1 portfolioValue += [[date, value]] date += timedelta(days=1) # print(portfolioValue) with open('data/portfolioValue.csv', 'w') as file: for item in portfolioValue: date = dateHelper.getDateAsString(item[0]) file.writelines(date + "," + str(item[1]) + "\n") print('Performance ' + self.startDate + ' - ' + self.endDate) print('Starting value: ' + str(self.initialCash)) print('Final value: ' + str(portfolioValue[-1][1]))
def create_brokers(mode, currencies, exchangeNames): # returns an array of Broker objects brokers = [] for name in exchangeNames: if (str.upper(name) == 'COINONE'): xchg = CoinONE(config.COINONE_API, config.COINONE_KEY) elif (str.upper(name) == 'KORBIT'): xchg = KORBIT(config.KORBIT_API, config.KORBIT_KEY) elif (str.upper(name) == 'BITHUMB'): xchg = BITHUMB(config.BITHUMB_API, config.BITHUMB_KEY) elif (str.upper(name) == 'COINNEST'): xchg = COINNEST(config.COINNEST_API, config.COINNEST_KEY) elif (str.upper(name) == 'GOPAX'): xchg = GOPAX(config.GOPAX_API, config.GOPAX_KEY) else: print('Exchange ' + name + ' not supported!') broker = None continue print('%s initialized' % (xchg.name)) broker = Broker(mode, xchg) if mode == 'BACKTEST': # 임의로 잔고를 지정해서 처리하도록 수정? # broker.balances = config.PAPER_BALANCE pass elif mode == 'PAPER': # 임의로 잔고를 지정해서 처리하도록 수정? pass elif mode == 'TRADE': # use real starting balances. broker.balances = broker.xchg.get_all_balances() pass brokers.append(broker) return brokers
def processMessage(self, msg): if msg.messageType() == ERROR_INFO: errorCode = msg.getElementAsInteger("ERROR_CODE") errorMessage = msg.getElementAsString("ERROR_MESSAGE") print("GetBrokers >> ERROR CODE: %d\tERROR MESSAGE: %s" % (errorCode, errorMessage)) elif msg.messageType() == GET_BROKERS: bkrs = msg.getElement("EMSX_BROKERS") for b in bkrs.values(): self.brokers.brokers.append( Broker(self.brokers, b, self.assetClass))
def __init__( self ): ## Default tariff self.DT = self.get_default_tariff() ## List of Brokers participating ## List of Customers ## List of published Tariffs ## You need to initialize a Broker here (change the name in the ## imports at the top to reflect your broker's name), and then ## put it in the list self.brokers. self.brokers = [Broker(1)] self.customers = [ Customer() for i in range(100) ] self.tariffs = [ self.DT ] self.run()
def build_brokers(mode, pairs, exchanges): brokers = [] # Returns array of broker objects for e in exchanges: if e == 'BTCE': exchange = BTCE('./btce_keys.txt') elif e == 'GDAX': exchange = GDAX() elif e == 'POLONIEX': exchange = POLONIEX() else: logging.error("This exchange is not yet supported : " + e) broker = Broker(mode, exchange) brokers.append(broker) return brokers
def runExperiment(historicalNodeCount, segmentList, percentreplicate, replicationFactor, queryList, placementStrategy, routingStrategy): segmentcount = len(segmentList) #Creating Historical Nodes print "Creating Historical Nodes" historicalnodelist = createHistoricalNodes(historicalNodeCount) #Placing Segments print "Placing Segments" avgreplication = Coordinator.placeSegmentsAndReplicas(segmentList, segmentList, percentreplicate, replicationFactor, historicalnodelist, queryList, placementStrategy) Coordinator.printCurrentPlacement(historicalnodelist) print("%s,%s,%f Average Replication: %f" % (placementStrategy, routingStrategy, percentreplicate, avgreplication)) #Calculating Scores print "Routing Queries" timetaken = Broker.routeQueries(queryList, historicalnodelist, routingStrategy, segmentcount, 0) print("%s,%s,%f Overall Completion Time: %d" % (placementStrategy, routingStrategy, percentreplicate, timetaken))
def create_brokers(self, children_num): path = "C:/Users/lomiag/PycharmProjects/Energy_Broker/" parents = os.listdir(path + "Genetic_Library") brks = [Broker(1)] brk_id = 1 for parent in range(len(parents)): for child in range(children_num): br = Broker(brk_id, parents[parent][:-4]) brks.append(br) mutation_table = br.create_mutation_table() random_gene = random.choice(brks).genetic_table # print(random.choice(brks).genetic_table) mutaion_options = [ br.apply_mutation_multiplication(mutation_table), br.apply_mutation_combine(random_gene), br.apply_mutation_reverse() ] br.genetic_table = random.choice(mutaion_options) brks.append(br) brk_id += 1 return brks
def __init__(self, database, storage, topic="regDevice", logs=True, logName="DeviceManager", pingPath='ping'): self.devices = {} #Initializing loger self.console = Logger.Logger(logName=logName, enabled=logs, printConsole=True) #Initializing broker self.broker = Broker.Broker(topic=topic, logs=True, logName=logName) self.regTopic = topic self.broker.setCallbacks() self.broker.start() self.broker.subscribeTopicWithCallback(topic, self.brokerCallback) self.db = database self.storage = storage self.pingPath = pingPath self.response = False
class BrokerServer: zk_utils = None broker = None def __init__(self, show_data:bool=False): # Init zookeeper utils self.zk_utils = ZookeeperLeaderElector() # Init broker instance self.broker = Broker(show_data) # Check if the broker server is startable def startable(self): print("[SETUP/SERVER] Check if startable ...") # Check if ZK Utils is ready (error free) # Check if broker configured correctly # Start if precheck doesn't raise any error if self.zk_utils.ready() and self.broker.ready(): self._start() # Start broker server def _start(self): print("[SETUP/ZK] Connect to zookeeper server ...") try: self.zk_utils.startup(self.broker.id, self.broker.host, self.broker.run) except KeyboardInterrupt: exit() # Terminate broker server def exit(self): print("[EXIT] Shut down Broker server {}.".format(self.broker.id))
import time #initial setup config = { "apiKey": "AIzaSyCeLjnaoNZ6c9BKkccXt5E0H74DGKJWXek", "authDomain": "testproject-cd274.firebaseapp.com", "databaseURL": "https://testproject-cd274.firebaseio.com", "storageBucket": "testproject-cd274.appspot.com" } #Database startup db = Database.Database(config,logs=False) #storage startup store = Storage.Storage(config, logs=False) broker = Broker.Broker(topic="topic/channel", logs = False, logName='SensorBroker') broker.setCallbacks() broker.start() class TestSensorClass(unittest.TestCase): localData = 1 databaseData = 2 sent_payload = 100 received_payload = 200 changedEnabledState = False #Database startup sensor = Sensor.Sensor(database = db, storage = store, broker= broker, id=200, type="TST", enabled=True, devicePath="/devices/id", logs=False, filterSamples=3, datasetLength = 3, skipSamples=2)
import unittest from random import randint import time #initial setup config = { "apiKey": "AIzaSyCeLjnaoNZ6c9BKkccXt5E0H74DGKJWXek", "authDomain": " testproject-cd274.firebaseapp.com", "databaseURL": "https://testproject-cd274.firebaseio.com", "storageBucket": " testproject-cd274.appspot.com" } #Database startup db = Database.Database(config,logs=False) broker = Broker.Broker(topic="topic/channel", logs = False, logName='baseBroker') broker.setCallbacks() broker.start() class TestBaseClass(unittest.TestCase): localData = 1 databaseData = 2 sent_payload = 100 received_payload = 200 changedEnabledState = False #Database startup base = Base.Base(database = db, broker= broker, id=100, type="TST", enabled=True, devicePath="/devices/id", categoryPath="/category/", dataTopic = "/data", logs=False )
import time import argparse # for command line parsing def parseCmdLineArgs(): # parse the command line parser = argparse.ArgumentParser() # add optional arguments parser.add_argument("-b", "--brokers", type=str, help='all brokers ip address') parser.add_argument("-i", "--ip", type=str, help='self ip address') # parse the args args = parser.parse_args() return args if __name__ == '__main__': args = parseCmdLineArgs() brokerIPs = args.brokers ip = args.ip brokerIPs = brokerIPs.split('-') broker = Broker(brokerIPs, ip, '5556', '5557', '5558', '5559', '6000', '6001', '6002', '6003') broker.handler() while True: pass
# The main execution of the program from Broker import Broker from Agent import Agent import time # Init and set up Broker and Agent B = Broker() A = Agent(B) A.status() # Simple arbitrage opportunity tracker while True: A.find_arbitrage() time.sleep(1)
def __init__(self, show_data:bool=False): # Init zookeeper utils self.zk_utils = ZookeeperLeaderElector() # Init broker instance self.broker = Broker(show_data)