def __init__(self): self.channels = { "queue1": "method_for_queue1", "queue2": "method_for_queue1" } self.broker = Broker() self.broker.declare_queues([ele for ele in self.channels])
def extend(vm, background, all_, sequential, filter, **kwargs): """Extend a host's lease time COMMAND: broker extend <vm hostname>|<vm name>|<local id> :param vm: Hostname, VM Name, or local id of host :param background: run a new broker subprocess to carry out command :param all_: Click option all :param sequential: Flag for whether to run extends sequentially :param filter: a filter string matching broker's specification """ broker_args = helpers.clean_dict(kwargs) if background: helpers.fork_broker() inventory = helpers.load_inventory(filter=filter) to_extend = [] for num, host in enumerate(inventory): if str(num ) in vm or host["hostname"] in vm or host["name"] in vm or all_: to_extend.append(Broker().reconstruct_host(host)) broker_inst = Broker(hosts=to_extend, **broker_args) broker_inst.extend(sequential=sequential)
def duplicate(vm, background, count, all_, filter): """Duplicate a broker-procured vm COMMAND: broker duplicate <vm hostname>|<local id>|all :param vm: Hostname or local id of host :param background: run a new broker subprocess to carry out command :param all_: Click option all :param filter: a filter string matching broker's specification """ if background: helpers.fork_broker() inventory = helpers.load_inventory(filter=filter) for num, host in enumerate(inventory): if str(num ) in vm or host["hostname"] in vm or host["name"] in vm or all_: broker_args = host.get("_broker_args") if broker_args: if count: broker_args["_count"] = count logger.info(f"Duplicating: {host['hostname']}") broker_inst = Broker(**broker_args) broker_inst.checkout() else: logger.warning( f"Unable to duplicate {host['hostname']}, no _broker_args found" )
def checkin(vm, background, all_, sequential, filter): """Checkin or "remove" a VM or series of VM broker instances COMMAND: broker checkin <vm hostname>|<local id>|all :param vm: Hostname or local id of host :param background: run a new broker subprocess to carry out command :param all_: Flag for whether to checkin everything :param sequential: Flag for whether to run checkins sequentially :param filter: a filter string matching broker's specification """ if background: helpers.fork_broker() inventory = helpers.load_inventory(filter=filter) to_remove = [] for num, host in enumerate(inventory): if (str(num) in vm or host.get("hostname") in vm or host.get("name") in vm or all_): to_remove.append(Broker().reconstruct_host(host)) broker_inst = Broker(hosts=to_remove) broker_inst.checkin(sequential=sequential)
def checkout(ctx, background, nick, count, args_file, **kwargs): """Checkout or "create" a Virtual Machine broker instance COMMAND: broker checkout --workflow "workflow-name" --workflow-arg1 something or COMMAND: broker checkout --nick "nickname" :param ctx: clicks context object :param background: run a new broker subprocess to carry out command :param nick: shortcut for arguments saved in settings.yaml, passed in as a string :param args_file: this broker argument wil be replaced with the contents of the file passed in """ broker_args = helpers.clean_dict(kwargs) if nick: broker_args["nick"] = nick if count: broker_args["_count"] = count if args_file: broker_args["args_file"] = args_file # if additional arguments were passed, include them in the broker args # strip leading -- characters broker_args.update({(key[2:] if key.startswith("--") else key): val for key, val in zip(ctx.args[::2], ctx.args[1::2])}) if background: helpers.fork_broker() broker_inst = Broker(**broker_args) broker_inst.checkout()
def main_loop(self): #optimizer = T4l_optimizer(self.optimizer_config) optimizer = Optimizer(self.optimizer_config) broker = Broker(self.broker_config, optimizer) self.print_info() broker.schedule_tasks(self.tasks)
def __init__(self): ''' Constructor ''' self._Broker = Broker() self._Logic = Logic() print 'Inside Strategy'
def pull_image(tag_name): with open("tests/data/container/fake_images.json") as image_file: image_data = json.load(image_file) for image in image_data: if tag_name in image["RepoTags"]: return MockStub(image) raise Broker.ProviderError(f"Unable to find image: {tag_name}")
def test_host_creation(tower_stub): vmb = Broker() job = tower_stub.execute(workflow="deploy-base-rhel") host = tower_stub.construct_host(job, vmb.host_classes) assert isinstance(host, vmb.host_classes["host"]) assert host.hostname == "fake.host.test.com" assert host._broker_args["os_distribution_version"] == "7.8"
class TestAssetSelector(TestCase): config = parse_configs(path=os.path.join("../../", "config.ini")) args = parse_args() # if args.mode is None: # args.mode = 'long' if args.period is None: args.period = "1D" if args.algorithm is None: args.algorithm = "efficient_frontier" if args.testperiods is None: args.testperiods = 30 if args.max is None: args.max = 26 if args.min is None: args.min = 6 alpaca = REST(base_url=config["alpaca"]["APCA_API_BASE_URL"], key_id=config["alpaca"]["APCA_API_KEY_ID"], secret_key=config["alpaca"]["APCA_API_SECRET_KEY"], api_version=config["alpaca"]["VERSION"]) broker = Broker(alpaca) selector = AssetSelector(broker, cli_args=args) def test_get_assets(self): res = TestAssetSelector.selector.get_assets('equity', 'efficient_frontier') print('[] ') def test_candle_pattern_direction(self): pass
class QueueConsumerWorker: def __init__(self): self.channels = { "queue1": "method_for_queue1", "queue2": "method_for_queue1" } self.broker = Broker() self.broker.declare_queues([ele for ele in self.channels]) def method_for_queue1(self, message): logger.info(f"called method associate with queue1") try: return ServiceClass( ).some_awesome_method_to_deal_with_messages_from_queue_one(message) except Exception as e: logger.error(f"Some s**t happened") return False def method_for_queue2(self, message): logger.info(f"called method associate with queue2") try: return ServiceClass( ).some_awesome_method_to_deal_with_messages_from_queue_one(message) except Exception as e: logger.error(f"Some s**t happened") return False def run(self) -> bool: try: for channel, function in self.channels.items(): message = self.broker.get_next_message(channel) if message is not None: logger.info( f"Queue {channel} - Received message {message}") if not getattr(self, function)(message): self.broker.send_message(message, f"TOPIC/{channel}") #operation failed, return message to queue return 1 return 0 except Exception as e: logger.error(e, exc_info=True) return 1 # return cause
def inventory(details, sync, filter): """Get a list of all VMs you've checked out showing hostname and local id hostname pulled from list of dictionaries """ if sync: Broker.sync_inventory(provider=sync) logger.info("Pulling local inventory") inventory = helpers.load_inventory(filter=filter) emit_data = [] for num, host in enumerate(inventory): emit_data.append(host) if details: logger.info( f"{num}: {host.get('hostname', host.get('name'))}, Details: {helpers.yaml_format(host)}" ) else: logger.info(f"{num}: {host.get('hostname', host.get('name'))}") helpers.emit({"inventory": emit_data})
def broker_function(): payload = request.get_json(force=True) response = Broker( request_type=request.method, microservice=payload.get('microservice'), payload=payload.get('payload') ).broker_services() return jsonify(response)
def __init__(self, conf): self.conf = conf self.broker = Broker.get_instance(conf['broker']) self.status = 1 self.result = Portfolio() self.info = [] self.contex = {} self.logid = conf['name'] + '-' + str(int(time.time())) if 'portfolio' in list(self.conf.keys()) and self.conf['portfolio'] is not None: self.result = Portfolio(CT.CONF_DIR + 'portfolio/' + self.conf['portfolio'])
def execute(ctx, background, nick, output_format, artifacts, args_file, **kwargs): """Execute an arbitrary provider action COMMAND: broker execute --workflow "workflow-name" --workflow-arg1 something or COMMAND: broker execute --nick "nickname" :param ctx: clicks context object :param background: run a new broker subprocess to carry out command :param nick: shortcut for arguments saved in settings.yaml, passed in as a string :param output_format: change the format of the output to one of the choice options :param artifacts: AnsibleTower provider specific option for choosing what to return :param args_file: this broker argument wil be replaced with the contents of the file passed in """ broker_args = helpers.clean_dict(kwargs) if nick: broker_args["nick"] = nick if artifacts: broker_args["artifacts"] = artifacts if args_file: broker_args["args_file"] = args_file # if additional arguments were passed, include them in the broker args # strip leading -- characters broker_args.update({(key[2:] if key.startswith("--") else key): val for key, val in zip(ctx.args[::2], ctx.args[1::2])}) if background: helpers.fork_broker() broker_inst = Broker(**broker_args) result = broker_inst.execute() helpers.emit({"output": result}) if output_format == "raw": print(result) elif output_format == "log": logger.info(result) elif output_format == "yaml": print(helpers.yaml_format(result))
def main(): broker = Broker( os.getenv("RABBITMQ_HOST", "localhost"), os.getenv("RABBITMQ_PORT", 5672), os.getenv("RABBITMQ_USER", "guest"), os.getenv("RABBITMQ_PASS", "guest") ) queue_name = os.getenv("RABBITMQ_QUEUE", "") next_queue_name = os.getenv("RABBITMQ_NEXT_QUEUE", "") def callback(ch, method, properties, body, data={}): logging.debug(f"Received message on {queue_name}: {body} \n\n{data}") delay = random.randint(int(os.getenv("RANDOM_WAIT_MIN", 5)), int(os.getenv("RANDOM_WAIT_MAX", 10))) # add some tribial data to the message body and the data store body["history"].append(queue_name) new_data = { "_id": data["_id"], "updated_at": time.time(), queue_name: time.time(), "delay": data.get("delay", 0) + delay } # take some time to do a task time.sleep(delay) # push the message on to the next service logging.debug(f"Publishing message on {next_queue_name}: {body}") broker.publish(next_queue_name, body, new_data) # True to ack the message return True logging.debug(f"Waiting for messages {queue_name}") broker.consume(queue_name, callback)
def main(): broker = Broker(os.getenv("RABBITMQ_HOST", "localhost"), os.getenv("RABBITMQ_PORT", 5672), os.getenv("RABBITMQ_USER", "guest"), os.getenv("RABBITMQ_PASS", "guest")) queue_name = os.getenv("RABBITMQ_QUEUE", "") def callback(ch, method, properties, body, data): logging.debug(f"Received message on logger: {body}\n\n{data}") data['completed_at'] = time.time() data['transfer_duration'] = data['completed_at'] - data[ 'created_at'] - data['delay'] # log the result of each message chain logging.info(body) logging.info(data) # ack the message return True logging.debug("Waiting for messages") broker.consume(queue_name, callback)
class Strategy(object): ''' classdocs ''' def __init__(self): ''' Constructor ''' self._Broker = Broker() self._Logic = Logic() print 'Inside Strategy' def AnalyzeTickerSymbol(self, tickerSymbol): ''' Runs All the analysis on the Ticker Symbol, of the implemented Strategy, This should document, the number of shares to buy, at the specific market price, and come up with a selling price in witch the algorithm will sell, and vice-versa, if the stock price drops off, then it also needs to come up with bounds to sell and re-coup some of the losings ''' numberOfShares = 100 buyStock = False if buyStock: self.BuyStockAfterAnalysis(tickerSymbol, numberOfShares) return False def BuyStockAfterAnalysis(self, tickerSymbol, numberOfShares): ''' This method calls the ''' self._Logic.VerifyThatTickerSymbolHasBeenAnalyzed( tickerSymbol, numberOfShares) self._Broker.BuyStockByTickerSymbolAndNumberOfShares( self, tickerSymbol, numberOfShares)
#!/usr/bin/env python import os, logging, time from broker.broker import Broker logging.basicConfig( level=getattr(logging, os.getenv("LOG_LEVEL", "INFO").upper(), None)) broker = Broker(os.getenv("RABBITMQ_HOST", "localhost"), os.getenv("RABBITMQ_PORT", 5672), os.getenv("RABBITMQ_USER", "guest"), os.getenv("RABBITMQ_PASS", "guest")) # generate a few messages for i in range(int(os.getenv("NUMBER_OF_MESSAGES", 10))): logging.debug(f"Initial message {i} published") broker.publish("service_a", {"history": ["seq-" + str(i)]}, {"created_at": time.time()}) logging.info("All initial messages published")
def run(broker: Broker, args: Namespace): if not broker or broker is None: raise BrokerException("[!] A broker instance is required.") if args.algorithm is None: args.algorithm = "bullish_hold" if args.testperiods is None: args.testperiods = 30 if args.cash is not None and type(args.cash) == float: cash = args.cash else: cash = float(broker.cash) if args.risk_pct is not None and type(args.risk_pct) in [int, float]: risk_pct = args.risk_pct else: risk_pct = .10 # initial trade state starting_amount = cash risk_amount = broker.calculate_tolerable_risk(cash, risk_pct) algorithm = Algorithm(broker, args) symbols = [asset.symbol for asset in algorithm.portfolio] print("[*] Trading assets: {}".format(",".join(symbols))) if args.backtest: # TODO: Make all time usages consistent now = datetime.now(timezone("EST")) beginning = now - timedelta(days=args.testperiods) calendars = broker.get_calendar( start_date=beginning.strftime("%Y-%m-%d"), end_date=now.strftime("%Y-%m-%d")) portfolio = {} cal_index = 0 for calendar in calendars: # see how much we got back by holding the last day's picks overnight positions, asset_value = algorithm.total_asset_value( portfolio, calendar.date) cash += asset_value print( "[*] Cash account value on {}: ${}".format( calendar.date.strftime("%Y-%m-%d"), round(cash, 2)), "Risk amount: ${}".format(round(risk_amount, 2))) if cash <= 0: print("[!] Account has gone to $0.") break if cal_index == len(calendars) - 1: print("[*] End of the backtesting window.") print("[*] Starting account value: {}".format(starting_amount)) print("[*] Holdings: ") for k, v in portfolio.items(): print(" - Symbol: {}, Shares: {}, Value: {}".format( k, v["shares"], v["value"])) print("[*] Account value: {}".format(round(cash, 2))) print("[*] Change from starting value: ${}".format( round(float(cash) - float(starting_amount), 2))) break # calculate position size based on volume/momentum rating ratings = algorithm.get_ratings(algo_time=timezone("EST").localize( calendar.date), window_size=10) portfolio = algorithm.portfolio_allocation(ratings, risk_amount) for _, row in ratings.iterrows(): shares_to_buy = int(portfolio[row["symbol"]]) cost = (row["price"] + .01) * shares_to_buy # Try to be a maker cash -= cost # calculate the amount we want to risk on the next trade risk_amount = broker.calculate_tolerable_risk(cash, risk_pct) cal_index += 1 else: cash = float(broker.cash) starting_amount = cash cycle = 0 bought_today = False sold_today = False try: orders = broker.get_orders( after=time_from_datetime(datetime.today() - timedelta(days=1)), limit=400, status="all") except BrokerException: # We don't have any orders, so we've obviously not done anything today. pass else: for order in orders: if order.side == "buy": bought_today = True # This handles an edge case where the script is restarted right before the market closes. sold_today = True break else: sold_today = True while True: # wait until the market's open to do anything. clock = broker.get_clock() if clock.is_open and not bought_today: if sold_today: time_until_close = clock.next_close - clock.timestamp if time_until_close.seconds <= 120: print("[+] Buying position(s).") cash = float(broker.api.get_account().cash) ratings = algorithm.get_ratings(window_size=10) portfolio = algorithm.portfolio_allocation( ratings, risk_amount) for symbol in portfolio: broker.api.submit_order(symbol=symbol, qty=portfolio[symbol], side="buy", type="market", time_in_force="day") print("[*] Position(s) bought.") bought_today = True else: # sell our old positions before buying new ones. time_after_open = clock.next_open - clock.timestamp if time_after_open.seconds >= 60: print("[-] Liquidating positions.") broker.api.close_all_positions() sold_today = True else: bought_today = False sold_today = False if cycle % 10 == 0: print("[*] Waiting for next market day...") print("[-] Cash: {}".format(cash)) time.sleep(30) cycle += 1
def provider_cmd(*args, **kwargs): # the actual subcommand """Get information about a provider's actions""" broker_inst = Broker(**kwargs) broker_inst.nick_help()
def test_balance(self): b = Broker(CT.BROKER_NAME_MANUAL) d = b.balance() self.assertEqual(d[0].status, 'ok')
def test_returns_default_catalog(self): subject = Broker("Jenkins-Broker", "Jenkins-Plan-GID") catalog = subject.catalog() self.assertIsNotNone(catalog)
def test_returns_default_catalog(self): subject = Broker() catalog = subject.catalog() self.assertIsNotNone(catalog)
# @ModifyDate: 2016-05-19 00:46 # Copyright ? 2016 Baidu Incorporated. All rights reserved. #***************************************************************# import sys import os from apscheduler.schedulers.blocking import BlockingScheduler from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.jobstores.memory import MemoryJobStore from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..') from broker.broker import Broker import utils.const as CT import yaml t = Broker('xueqiu') def my_job(conf): print(conf) global t #d = t.sell('131810', price=1, amount=20) d = t.balance() print(d) for b in d: for (k, v) in list(b.items()): print((k + ':' + str(v)).encode('utf-8')) def add_job(sched): #sched.add_job(my_job, 'interval', seconds=5)
handler.setLevel(log_level) formatter = logging.Formatter(log_format) handler.setFormatter(formatter) handler.suffix = "%Y%m%d" handler.extMatch = re.compile(r"^\d{8}$") logger.setLevel(log_level) logger.addHandler(handler) try: create_log_dir_if_does_not_exists('log') setup_log() db = Database(logger) db2 = Database(logger) service = LPRMasterService(logger, db) broker = Broker(logger, db2) except Exception as error: logger.error(error) def setup_route(): return [ web.post('/register', service.register), web.post('/get-state', service.get_data_last_state), web.delete('/gate', service.delete_gate_id), web.post('/upload-encoded', service.forward_encoded_image), web.post('/upload', service.forward_raw_image), web.post('/upload-url', service.forward_url_image), web.post('/lpr-result', service.get_data_image_result_by_ticket_number) ]
def main(config, args): # are we trading forex? if args.forex: print('[-] do stuff with Oanda.') try: oanda = v20.Context( config['oanda']['host'], config['oanda']['port'], token=config['oanda']['token'] ) except V20ConnectionError as error: raise error pair = 'EUR_USD' try: broker = ForexBroker(oanda, pair) except BrokerException as error: raise error else: # is our account restricted from trading? if broker.trading_blocked: raise BrokerException('[!] Insufficient trading balances, or account is otherwise restricted from trading.') # how much money can we use to open new positions? if args.cash is not None: print('[?] ${} in simulated account balance.'.format(args.cash)) else: print('[?] ${} is available in cash.'.format(broker.trade_balance['tb'])) # are we trading crypto? if args.crypto: print('[-] do stuff with Kraken.') try: api = krakenex.API( key=config['kraken']['api_key'], secret=config['kraken']['private_key']) kraken = KrakenAPI(api, tier='Starter') except KrakenAPIError as error: raise error pair = 'BATUSD' try: broker = KrakDealer(kraken, pair=pair) except BrokerException as error: raise error else: # is our account restricted from trading? if broker.trading_blocked: raise BrokerException('[!] Insufficient trading balances, or account is otherwise restricted from trading.') # how much money can we use to open new positions? if args.cash is not None: print('[?] ${} in simulated account balance.'.format(args.cash)) else: print('[?] ${} is available in cash.'.format(broker.trade_balance['tb'])) else: # we must be trading stocks try: alpaca = REST( base_url=config['alpaca']['APCA_API_BASE_URL'], key_id=config['alpaca']['APCA_API_KEY_ID'], secret_key=config['alpaca']['APCA_API_SECRET_KEY'], api_version=config['alpaca']['VERSION']) except APIError as error: raise error try: broker = Broker(alpaca) except (BrokerException, BrokerValidationException) as error: raise error else: # is our account restricted from trading? if broker.trading_blocked: raise BrokerException('[!] Account is currently restricted from trading.') # how much money can we use to open new positions? if args.cash is not None: print('[?] ${} in simulated account balance.'.format(args.cash)) else: print('[?] ${} is available in cash.'.format(broker.cash)) # try and import the corresponding Python file from algos try: algorithm = import_module(f'algos.{args.algorithm}', package='Algorithm') except ImportError as error: raise error else: algorithm.run(broker, args)
def test_host_creation(container_stub): bx = Broker() cont = container_stub.run_container(container_host="ch-d:ubi8") host = container_stub.construct_host(cont, bx.host_classes) assert isinstance(host, bx.host_classes["host"]) assert host.hostname == "f37d3058317f"
import asyncio import logging import os from dotenv import load_dotenv from broker.broker import Broker load_dotenv() if __name__ == '__main__': logger = logging.getLogger('broker') try: broker_object = Broker() server_constants = {'SERVER_ADDRESS': os.getenv('SERVER_ADDRESS'), \ 'SERVER_PORT': os.getenv('SERVER_PORT')} database_constants = {'DATABASE_ADDRESS': os.getenv('DATABASE_ADDRESS'), \ 'DATABASE_USERNAME': os.getenv('DATABASE_USERNAME'), \ 'DATABASE_PASSWORD': os.getenv('DATABASE_PASSWORD'), \ 'DATABASE_NAME': os.getenv('DATABASE_NAME')} asyncio.run(broker_object.start(server_constants, database_constants)) except KeyboardInterrupt: logger.info('Shutting down broker server...')
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ This code simulates some content producer who is adding information to the queue. """ import json from datetime import datetime from time import sleep from broker.broker import Broker if __name__ == "__main__": channels = ["queue1"] broker = Broker() broker.declare_queues(channels) while True: data = {"date_time": str(datetime.now())} broker.send_message(message=data, topic="TOPIC/queue1", routing_key=None) sleep(5)