Ejemplo n.º 1
0
 def get_broker_by_name(self, broker_name):
     query = "SELECT * FROM brokers WHERE broker_name = %s"
     val = (broker_name,)
     self.cursor.execute(query, val)
     result = self.cursor.fetchone()
     broker = Broker(result[0],result[1],result[2])
     return broker
Ejemplo n.º 2
0
def test_case_one():
    print('\nTest_Case_One')

    test_position = Broker()
    midpoint = 100.
    fee = .003

    # ========

    order_open = {'price': midpoint + (midpoint * fee), 'side': 'long'}
    test_position.add(order=order_open)

    assert test_position.long_inventory.position_count == 1
    print('LONG Unrealized_pnl: %f' %
          test_position.long_inventory.get_unrealized_pnl())

    assert test_position.short_inventory.position_count == 0
    assert test_position.short_inventory.get_unrealized_pnl() == 0.

    order_close = {'price': (midpoint * fee) * 5. + midpoint, 'side': 'long'}
    test_position.remove(order=order_close)
    assert test_position.long_inventory.position_count == 0
    print('LONG Unrealized_pnl: %f' %
          test_position.long_inventory.get_unrealized_pnl())

    assert test_position.short_inventory.position_count == 0
    assert test_position.short_inventory.get_unrealized_pnl() == 0.
    print('LONG Realized_pnl: %f' % test_position.get_realized_pnl())
Ejemplo n.º 3
0
 def setUp(self):
     r = rconfig.Read()
     self.broker = Broker("TestBroker")
     self.redis = redis.StrictRedis(host=r.host,
                                    port=r.port,
                                    db=r.db,
                                    password=r.password)
Ejemplo n.º 4
0
def test_case_two():
    print('\nTest_Case_Two')

    test_position = Broker()
    midpoint = 100.
    fee = .003

    # ========

    order_open = {'price': midpoint + (midpoint * fee), 'side': 'short'}
    test_position.add(order=order_open)

    assert test_position.short_inventory.position_count == 1
    print('SHORT Unrealized_pnl: %f' %
          test_position.short_inventory.get_unrealized_pnl())

    assert test_position.long_inventory.position_count == 0
    assert test_position.long_inventory.get_unrealized_pnl() == 0.

    order_close = {'price': midpoint * 0.95, 'side': 'short'}
    test_position.remove(order=order_close)
    assert test_position.short_inventory.position_count == 0
    print('SHORT Unrealized_pnl: %f' %
          test_position.short_inventory.get_unrealized_pnl())

    assert test_position.long_inventory.position_count == 0
    assert test_position.long_inventory.get_unrealized_pnl() == 0.
    print('SHORT Realized_pnl: %f' % test_position.get_realized_pnl())
Ejemplo n.º 5
0
def test4():
    ip_add = "127.0.0.1"
    # initialize
    broke1 = Broker(ip_add)
    sub1 = Subscriber(ip_add)
    pub1 = Publisher(ip_add)

    # set up subscriber and publisher with broker
    stop = threading.Event()
    # subscriber
    sub1.register_sub("soccer, basketball")  # subscribe -- need no threading
    # publishers/broker
    T1 = Thread(target=broke1.run,
                args=(stop, ))  # open up broker for publisher to register with
    T2 = Thread(target=pub1.register_pub,
                args=("soccer", ))  # publisher registers
    T1.start()
    T2.start()
    stop.set()
    T1.join()
    T2.join()

    # open up broker, subscriber for listening and start publishing rapidly.
    stop.clear()
    T1 = Thread(target=broke1.run, args=(stop, ))  # open up broker
    T2 = Thread(target=sub1.notify, args=(stop, ))

    T1.start()
    T2.start()
    time.sleep(1)
    print("Time published: %.20f" % time.time())
    pub1.publish("hello world")
    # 1 second wait
    time.sleep(1)
    stop.set()
Ejemplo n.º 6
0
    def __init__(self):
        self.live_trading = True  # set False for backtesting.
        self.log_level = logging.DEBUG
        self.logger = self.setup_logger()

        # Don't connect to live data feeds if backtesting.
        if self.live_trading:
            self.exchanges = self.load_exchanges(self.logger)

        # Connect to database.
        print("Connecting to database...")
        self.db_client = MongoClient(
            self.DB_URL, serverSelectionTimeoutMS=self.DB_TIMEOUT_MS)
        self.db = self.db_client[self.DB_NAME]
        self.check_db_connection()

        # Event queue and producer/consumer worker classes.
        self.events = queue.Queue(0)
        self.data = Datahandler(self.exchanges, self.logger, self.db,
                                self.db_client)
        self.strategy = Strategy(self.exchanges, self.logger, self.db,
                                 self.db_client)
        self.portfolio = Portfolio(self.logger)
        self.broker = Broker(self.exchanges, self.logger)

        # Processing performance variables.
        self.start_processing = None
        self.end_processing = None

        self.run()
Ejemplo n.º 7
0
 def get_broker(self, broker_id):
     query = "SELECT * FROM brokers WHERE broker_id = %s"
     val = (broker_id, )
     self.cursor.execute(query, val)
     for brokers in self.cursor:
         broker = Broker(brokers[0], brokers[1], brokers[2])
         self.brokers_list.append(broker)
         return broker
Ejemplo n.º 8
0
def main():

    rounds = 1000
    broker = Broker(rounds)
    broker.setup()
    broker.play()
    broker.display_results()
    return broker.play(), broker.display_results()
Ejemplo n.º 9
0
    def setup_broker(self, cfg=None):
        """
        Currently only creates a dummy Broker object for registering Applications to.

        :param cfg:
        :return:
        """
        self.__broker = Broker()
Ejemplo n.º 10
0
    def __init__(self):
        self.client = MongoClient(config.DBURI + config.DBNAME)
        self.db = self.client[config.DBNAME]
        self.active_readers = dict()
        self.broker = Broker()
        self.no_exception = True
        self.stopped = False

        signal('reader_updated').connect(self.update_readers)
Ejemplo n.º 11
0
def test_broker_single_register():
    broker = Broker()
    data = {
        'pk': hexlify(b'this is a test').decode('utf-8'),
        'id': hexlify(b'someID').decode('utf-8')
    }
    broker.register(data, 'mix')
    cache = broker.fetch([], 'mix')
    assert len(cache) == 1
Ejemplo n.º 12
0
def main():
    broker = Broker()
    publisher = Publisher()

    publisher.connect_broker(broker)

    topics = ['1']

    publisher.publish_on_topics(topics)
Ejemplo n.º 13
0
def main():
    broker = Broker()
    subscriber = Subscriber()

    subscriber.connect_broker(broker)

    topics = ["1", "2", "3", "4", '5', '6', '7', '8', '9']

    subscriber.subscribe_to_topics(topics)
Ejemplo n.º 14
0
 def __init_broker_and_processors(self):
     self.__broker = Broker(self)
     self.__event_processor = EventProcessor(0, self)
     self.__event_processors = list()
     for i in range(self.config.processor_num):
         id = i + 1
         event_channel = EventChannel()
         event_channel.register(self.__broker)
         event_processor = EventProcessor(id, self, event_channel)
         self.__event_processors.append(event_processor)
Ejemplo n.º 15
0
    def get_brokers(self):
        query = ("SELECT * FROM brokers ORDER BY broker_name ASC")
        self.cursor.execute(query)
        self.brokers_list = []

        for brokers in self.cursor:
            broker = Broker(brokers[0], brokers[1], brokers[2])
            self.brokers_list.append(broker)

        return self.brokers_list
Ejemplo n.º 16
0
def main():
    portEnum = PortEnum
    broker = Broker()
    status = StatusChecker(broker)
    try:
        status.start()
        KeyListener('0.0.0.0', portEnum.broker.value, broker)
        loop_thread = Thread(target=asyncore.loop, name="Asyncore Loop")
        loop_thread.start()
    except Exception as e:
        print(e)
        status.kill()
Ejemplo n.º 17
0
Archivo: laksa.py Proyecto: 20ft/laksa
def main():
    # basic objects...
    log = LogHandler('20ft', 'broker', ['Starting new HTTP connection'])
    broker = Broker()

    # run the broker
    try:
        broker.run()
    finally:
        # closing
        broker.stop()
        log.stop()
Ejemplo n.º 18
0
 def buy(self,
         unit=None,
         limit_price=None,
         stop_loss=None,
         stop_profit=None):
     if unit is None:
         unit = position()
     assert unit > 0, 'in buy action, unit must be positive'
     Broker(self.init_capital).make_order(unit=unit,
                                          limit_price=limit_price,
                                          stop_loss=stop_loss,
                                          stop_profit=stop_profit)
Ejemplo n.º 19
0
 def sell(self,
          unit=None,
          limit_price=None,
          stop_loss=None,
          stop_profit=None):
     if unit is None:
         unit = -position()
     assert unit < 0, ' in sell action, unit must be negative'
     Broker(self.init_capital).make_order(unit=unit,
                                          limit_price=limit_price,
                                          stop_loss=stop_loss,
                                          stop_profit=stop_profit)
Ejemplo n.º 20
0
 def close_position(self):
     """
     close the position when current size of position is not zero
     """
     if position() != 0:
         # print("in close", position_list[-1])
         Broker(self.init_capital).make_order(unit=-1 * position(),
                                              limit_price=None,
                                              stop_loss=None,
                                              stop_profit=None)
     else:
         pass
Ejemplo n.º 21
0
def RecordLSHFactory(pipeline, fields, filter_={}, field_weights = None):

    name = __LSHName__(fields, filter_)
    broker = Broker(pipeline)
    try:
        i = broker.download_obj(name)
        i.LSH = {}
        for key in i.fields:
            i.LSH[key] = broker.download_obj('{}_{}'.format(name,key))
        assert isinstance(i, RecordLSH)
        return i
    except AssertionError:
        return RecordLSH(pipeline, fields, filter_=filter_, field_weights = field_weights)
Ejemplo n.º 22
0
    def __init__(self, data):
        """
        TODO: Abstract this from USD and BTC specific accounts

        :param data: dataframe to run backtest on
        """

        self.data = data
        self.strategies = []

        usd_account = Account('USD', 1000)
        btc_account = Account('BTC', 0, 'Close')
        self.broker = Broker(data, [usd_account, btc_account])
Ejemplo n.º 23
0
    def __init__(self):
        self.broker = Broker()

        self._load_streams()
        self._load_plugins()

        self.broker.inbound_streams = self.inbound_streams
        self.broker.outbound_streams = self.outbound_streams
        self.broker.servers = self.servers
        self.broker.plugins = self.plugins

        # defining greenlets that need to be joined over
        self.greenlets = ([self.broker] + self.broker.plugins +
                          self.broker.inbound_streams +
                          self.broker.outbound_streams)
Ejemplo n.º 24
0
    def run(self):
        # 1. Start Broker
        broker = Broker()
        broker.setDaemon(True)
        broker.start()

        # 2. Start Senders
        try:
            local_logger = LocalLogger(
                queue_size=4096, config=self._config['sender']['local_logger'])
            local_logger.setDaemon(True)
        except Exception as e:
            print e
            print "ERROR: Can\'t create local looger"
        else:
            broker.register_sender(local_logger)
            local_logger.start()

        try:
            mac_tracker_server = MacTrackerServer(
                queue_size=4096,
                config=self._config['sender']['mac_tracker_server'])
            mac_tracker_server.setDaemon(True)
        except Exception as e:
            print e
            print "ERROR: Can\'t create MAC tracker server"
        else:
            broker.register_sender(mac_tracker_server)
            mac_tracker_server.start()

        # 3. Start Reader
        try:
            reader = Reader(broker, config=self._config['pipe_file'])
            reader.setDaemon(True)
        except Exception as e:
            #sys.stdout.write("Error: Can\'t create PipeReader\n")
            print e
            print "Error: Can\'t create PipeReader\n"
        else:
            reader.start()

        broker.join()
        reader.join()

        #Agent Thread Start Idle Here
        while (True):
            time.sleep(1)
            pass
Ejemplo n.º 25
0
    def __init__(self):

        # Set False for forward testing.
        self.live_trading = True

        self.log_level = logging.INFO
        self.logger = self.setup_logger()

        # Check DB state OK before connecting to any exchanges
        self.db_client = MongoClient(
            self.DB_URL,
            serverSelectionTimeoutMS=self.DB_TIMEOUT_MS)
        self.db_prices = self.db_client[self.DB_PRICES]
        self.db_other = self.db_client[self.DB_OTHER]
        self.check_db_status(self.VENUES)

        self.exchanges = self.exchange_wrappers(self.logger, self.VENUES)
        self.telegram = Telegram(self.logger)

        # Main event queue.
        self.events = queue.Queue(0)

        # Producer/consumer worker classes.
        self.data = Datahandler(self.exchanges, self.logger, self.db_prices,
                                self.db_client)

        self.strategy = Strategy(self.exchanges, self.logger, self.db_prices,
                                 self.db_other, self.db_client)

        self.portfolio = Portfolio(self.exchanges, self.logger, self.db_other,
                                   self.db_client, self.strategy.models,
                                   self.telegram)

        self.broker = Broker(self.exchanges, self.logger, self.portfolio,
                             self.db_other, self.db_client, self.live_trading,
                             self.telegram)

        self.portfolio.broker = self.broker

        # Start flask api in separate process
        # p = subprocess.Popen(["python", "api.py"])
        # self.logger.info("Started flask API.")

        # Processing performance tracking variables.
        self.start_processing = None
        self.end_processing = None
        self.cycle_count = 0
Ejemplo n.º 26
0
    def __init__(self, symbol1, symbol2, contract1, contract2):

        self.symbol1 = symbol1
        self.symbol2 = symbol2
        self.contract1 = contract1
        self.contract2 = contract2

        sprint("菲利普准备行动。")

        self.broker = Broker()
        sprint("菲利普呼叫了交易商。")

        self.long_on_match1 = lambda amount: self.broker.long_on_match(
            symbol1, contract1, amount)
        self.long_on_match2 = lambda amount: self.broker.long_on_match(
            symbol2, contract2, amount)

        self.short_on_match1 = lambda amount: self.broker.short_on_match(
            symbol1, contract1, amount)
        self.short_on_match2 = lambda amount: self.broker.short_on_match(
            symbol2, contract2, amount)

        self.close_long_on_match1 = lambda amount: self.broker.close_long_on_match(
            symbol1, contract1, amount)
        self.close_long_on_match2 = lambda amount: self.broker.close_long_on_match(
            symbol2, contract2, amount)

        self.close_short_on_match1 = lambda amount: self.broker.close_short_on_match(
            symbol1, contract1, amount)
        self.close_short_on_match2 = lambda amount: self.broker.close_short_on_match(
            symbol2, contract2, amount)

        self.long_position_info1 = self.broker.long_position_info(
            symbol1, contract1)
        self.long_position_info2 = self.broker.long_position_info(
            symbol2, contract2)

        self.short_position_info1 = self.broker.short_position_info(
            symbol1, contract1)
        self.short_position_info2 = self.broker.short_position_info(
            symbol2, contract2)

        sprint("菲利普准备完毕。")

        self.__refresh_position_info()
        sprint("菲利普开始刷新仓位信息。")
Ejemplo n.º 27
0
    def __init__(self, env_config):
        account = Account(000000, env_config['initial_cash'], 0,
                          env_config['initial_cash'],
                          env_config['initial_cash'], dict(), False)
        max_limit = env_config['window']
        self.resized_image_width = 100
        self.resized_image_height = 100
        image_channel = 4
        self.log_every = env_config['log_every']
        self.broker = Broker(account, max_limit=max_limit)
        self.all_tickers = self.broker.all_tickers
        self.n_symbols = env_config['n_symbols']
        self.tech_indicators = env_config['tech_indicators']
        length = OHCLVV + len(self.tech_indicators.split())
        self.use_image = env_config['use_image']

        if self.use_image:
            self.observation_space = gym.spaces.Dict({
                'data':
                gym.spaces.Box(-np.inf, np.inf,
                               (self.n_symbols, max_limit, length)),
                'images':
                gym.spaces.Box(-np.inf, np.inf,
                               (self.n_symbols, self.resized_image_height,
                                self.resized_image_width, image_channel)),
                'privates':
                gym.spaces.Box(-np.inf, np.inf, (5 + self.n_symbols * 2, ))
            })
        else:
            self.observation_space = gym.spaces.Dict({
                'data':
                gym.spaces.Box(-np.inf, np.inf,
                               (self.n_symbols, max_limit, length)),
                'privates':
                gym.spaces.Box(-np.inf, np.inf, (5 + self.n_symbols * 2, ))
            })

        self.action_space = gym.spaces.MultiDiscrete([env_config['bins']] *
                                                     self.n_symbols)
        self.current_tickers = None
        self.qty_val = np.linspace(-env_config['max_shares'],
                                   env_config['max_shares'],
                                   env_config['bins'])
        self.images = None
        self.refresh_data = True
Ejemplo n.º 28
0
    def __init__(self, broker_personality, nr_transporters, nr_cargo_owners,
                 max_iterations, max_displacement_from_estimated_price,
                 gauss_stdev):

        # L
        self.broker_personality = broker_personality
        self.nr_transporters = nr_transporters
        self.nr_cargo_owners = nr_cargo_owners
        self.new_transport_providers()
        self.new_cargo_owners()
        self.broker = Broker(self.broker_personality)
        self.stats = Statistics(self.transporters_icnet,
                                self.transporters_aicnet, self.cargo_owners)
        self.max_iterations = max_iterations
        self.max_displacement_from_estimated_price = max_displacement_from_estimated_price
        self.gauss_stdev = gauss_stdev
        self.global_bid_iterator = 0
        self.multiple_yes = 0
Ejemplo n.º 29
0
    def __init__(self):

        # Set False for forward testing.
        self.live_trading = True

        self.log_level = logging.DEBUG
        self.logger = self.setup_logger()

        self.exchanges = self.load_exchanges(self.logger)

        # Database.
        self.db_client = MongoClient(
            self.DB_URL, serverSelectionTimeoutMS=self.DB_TIMEOUT_MS)

        # Price data database.
        self.db_prices = self.db_client[self.DB_PRICES]

        # Non-price data database.
        self.db_other = self.db_client[self.DB_OTHER]

        self.check_db_connection()

        # Main event queue.
        self.events = queue.Queue(0)

        # Producer/consumer worker classes.
        self.data = Datahandler(self.exchanges, self.logger, self.db_prices,
                                self.db_client)

        self.strategy = Strategy(self.exchanges, self.logger, self.db_prices,
                                 self.db_other, self.db_client)

        self.portfolio = Portfolio(self.exchanges, self.logger, self.db_other,
                                   self.db_client, self.strategy.models)

        self.broker = Broker(self.exchanges, self.logger, self.db_other,
                             self.db_client, self.live_trading)

        # Processing performance tracking variables.
        self.start_processing = None
        self.end_processing = None
        self.cycle_count = 0

        self.run()
Ejemplo n.º 30
0
def run(config_file: str, password: str):
    """Reads config file, initializes configuration and creates Broker object that runs in a separate thread"""

    logging.info('Starting broker with config')

    logging.info("Loading config file '%s'", config_file)
    try:
        config = ConfigParser()
        config.read(config_file)
    except Exception as ex:
        logging.error("Error reading config: %s, exiting", type(ex))
        logging.error(ex.args)
        return

    try:
        eth_section = config['Ethereum']
        eth_contracts = config['Contracts']
        eth_use = eth_section['use']
        eth_server = eth_section[eth_use]
        ipfs_section = config['IPFS']
        ipfs_use = config['IPFS.%s' % ipfs_section['use']]
        broker = Broker(eth_server=eth_server,
                        abi_path=eth_contracts['abi_path'],
                        pandora=eth_contracts['pandora'],
                        node=eth_contracts['worker_node'],
                        vault=config['Account']['vault'],
                        data_dir=ipfs_section['store_in'],
                        ipfs_server=ipfs_use['server'],
                        ipfs_port=int(ipfs_use['port']),
                        use_hooks=eth_contracts.getboolean('hooks'))
    except Exception as ex:
        logging.error("Error reading config: %s, exiting", type(ex))
        logging.error(ex.args)
        return

    if password is None:
        password = getpass(
            'Please provide password for unlocking private key: ')

    if broker.connect(password) is False:
        return

    # Remove the following line in order to put the app into a daemon mode (running on the background)
    broker.join()