def run(self):
        database = {'user': '******', 'database': 'blackbox', 'host': "192.168.0.8"}
        Symbol.set_info_provider(MySQLSymbolProvider(database))
        provider = MySQLProvider(database, Symbol.get('EURUSD:CUR'), MarketDataPeriod.HOUR_1)

        self.file_handle = open(r"market_data_d1.pkl", "wb")
        provider.startPublishing(lambda symbol, quote: self.add_quote(quote))
        self.file_handle.close()
    def run(self):
        database = {
            'user': '******',
            'database': 'blackbox',
            'host': "192.168.0.8"
        }
        Symbol.set_info_provider(MySQLSymbolProvider(database))
        provider = MySQLProvider(database, Symbol.get('EURUSD:CUR'),
                                 MarketDataPeriod.HOUR_1)

        self.file_handle = open(r"market_data_d1.pkl", "wb")
        provider.startPublishing(lambda symbol, quote: self.add_quote(quote))
        self.file_handle.close()
Exemple #3
0
    def testQuoteConstructionOnInterval(self):
        s = Symbol.get("TEST")
        conflator = PriceConflator("TEST", MarketDataPeriod.MIN_5, self.callback)

        start_time = datetime.datetime(2015, 7, 7, 14, 0, 0)
        tick = Tick(start_time.timestamp(), 9.0, 9.2)
        conflator.add_tick(tick)
        tick = Tick((start_time + datetime.timedelta(seconds=60)).timestamp(), 9.0, 9.2)
        conflator.add_tick(tick)
        tick = Tick((start_time + datetime.timedelta(seconds=120)).timestamp(), 8.7, 8.9)
        conflator.add_tick(tick)
        tick = Tick((start_time + datetime.timedelta(seconds=180)).timestamp(), 11.0, 11.2)
        conflator.add_tick(tick)
        tick = Tick((start_time + datetime.timedelta(seconds=240)).timestamp(), 10.5, 10.7)
        conflator.add_tick(tick)
        tick = Tick((start_time + datetime.timedelta(seconds=300)).timestamp(), 9.0, 9.2)
        conflator.add_tick(tick)
        tick = Tick((start_time + datetime.timedelta(seconds=360)).timestamp(), 9.0, 9.2)
        conflator.add_tick(tick)

        self.assertEqual(1, len(self.callbackQuote))
        quote = self.callbackQuote[0]
        self.assertEqual(5, quote.ticks)
        self.assertEqual(9.1, quote.open)
        self.assertEqual(11.1, quote.high)
        self.assertEqual(8.8, quote.low)
        self.assertEqual(10.6, quote.close)
Exemple #4
0
    def testQuoteConstruction(self):
        s = Symbol.get("TEST")
        conflator = PriceConflator("TEST", MarketDataPeriod.MIN_5, self.callback)

        start_time = datetime.datetime(2015, 7, 7, 14, 10, 0)
        start_time = start_time + MarketDataPeriod.MIN_1
        tick = Tick(start_time.timestamp(), 9.0, 9.2)  # 14:11:00
        conflator.add_tick(tick)
        start_time = start_time + MarketDataPeriod.MIN_1
        tick = Tick(start_time.timestamp(), 8.7, 8.9)  # 14:12:00
        conflator.add_tick(tick)
        start_time = start_time + MarketDataPeriod.MIN_1
        tick = Tick(start_time.timestamp(), 11.0, 11.2)  # 14:13:00
        conflator.add_tick(tick)
        start_time = start_time + MarketDataPeriod.MIN_1
        tick = Tick(start_time.timestamp(), 10.5, 10.7)  # 14:14:00
        conflator.add_tick(tick)

        start_time = start_time + MarketDataPeriod.MIN_1
        tick = Tick(start_time.timestamp(), 9.0, 9.2)  # 14:15:00
        conflator.add_tick(tick)

        self.assertEqual(1, len(self.callbackQuote))
        quote = self.callbackQuote[0]
        self.assertEqual(9.1, quote.open)
        self.assertEqual(11.1, quote.high)
        self.assertEqual(8.8, quote.low)
        self.assertEqual(10.6, quote.close)
Exemple #5
0
    def testAlgoExpiredOrder(self):
        symbol = Symbol.get('TEST')
        start_time = datetime.datetime(2015, 7, 7, 12)
        order = Order(symbol, 1, Entry(Entry.Type.STOP_ENTRY, 12.5), Direction.LONG, expire_time=datetime.timedelta(seconds=90), entry_time=start_time)

        # symbol = algo.analysis_symbols()[0]
        ticks = []
        ticks.append(Tick(start_time.timestamp(), 10.0, 10.1))
        start_time = start_time + MarketDataPeriod.MIN_1
        ticks.append(Tick(start_time.timestamp(), 11.0, 11.1))
        start_time = start_time + MarketDataPeriod.MIN_1
        ticks.append(Tick(start_time.timestamp(), 12.0, 12.1))
        start_time = start_time + MarketDataPeriod.MIN_1
        ticks.append(Tick(start_time.timestamp(), 12.3, 12.4))
        start_time = start_time + MarketDataPeriod.MIN_1
        ticks.append(Tick(start_time.timestamp(), 12.8, 12.9))

        dataProvider = SimulatedDataProvider(symbol, ticks)
        order_manager = Broker(dataProvider)
        order_manager.subscribeSymbol(symbol)

        order_creator = OrderCreator(order_manager, symbol, order)
        order_manager.addPriceObserver(order_creator.handle_data)

        order_manager.start()

        self.assertEqual(0, len(order_manager.orders))
        self.assertEqual(0, len(order_manager.positions))
        self.assertEqual(State.EXPIRED, order.status)
Exemple #6
0
    def testTakeProfitMarketOrder(self):
        symbol = Symbol.get('TEST')
        symbol.lot_size = 1

        start_time = datetime.datetime(2015, 7, 7, 12)
        # symbol = algo.analysis_symbols()[0]
        ticks = []
        ticks.append(Tick(start_time.timestamp(), 10.0, 10.1))
        start_time = start_time + MarketDataPeriod.MIN_1
        ticks.append(Tick(start_time.timestamp(), 11.0, 11.1))
        start_time = start_time + MarketDataPeriod.MIN_1
        ticks.append(Tick(start_time.timestamp(), 13.0, 13.1))

        dataProvider = SimulatedDataProvider(symbol, ticks)
        order_manager = Broker(dataProvider)
        order_manager.subscribeSymbol(symbol)

        order = Order(symbol, 1, Entry(Entry.Type.MARKET), Direction.LONG, take_profit=2)
        order_creator = OrderCreator(order_manager, symbol, order)
        order_manager.addPriceObserver(order_creator.handle_data)
        order_manager.addPositionObserver(lambda position, state: self.setPosition(position))

        order_manager.start()

        self.assertEqual(0, len(order_manager.orders))
        self.assertEqual(0, len(order_manager.positions))
        self.assertEqual(State.FILLED, order.status)

        self.assertIsNotNone(self.position)
        self.assertEqual(Position.PositionStatus.TAKE_PROFIT, self.position.status)
        self.assertEqual(12.1, self.position.exit_price)
        self.assertAlmostEquals(2.0, self.position.points_delta())
Exemple #7
0
    def testAlgoStopEntryOrder(self):
        symbol = Symbol.get('TEST')

        start_time = datetime.datetime(2015, 7, 7, 12)
        # symbol = algo.analysis_symbols()[0]
        ticks = []
        ticks.append(Tick(start_time.timestamp(), 10.0, 10.1))
        start_time = start_time + MarketDataPeriod.MIN_1
        ticks.append(Tick(start_time.timestamp(), 11.0, 11.1))
        start_time = start_time + MarketDataPeriod.MIN_1
        ticks.append(Tick(start_time.timestamp(), 12.0, 12.1))
        start_time = start_time + MarketDataPeriod.MIN_1
        ticks.append(Tick(start_time.timestamp(), 12.3, 12.4))
        start_time = start_time + MarketDataPeriod.MIN_1
        ticks.append(Tick(start_time.timestamp(), 12.8, 12.9))

        dataProvider = SimulatedDataProvider(symbol, ticks)
        order_manager = Broker(dataProvider)
        order_manager.subscribeSymbol(symbol)

        order = Order(symbol, 1, Entry(Entry.Type.STOP_ENTRY, 11.3), Direction.LONG)
        order_creator = OrderCreator(order_manager, symbol, order)
        order_manager.addPriceObserver(order_creator.handle_data)

        order_manager.start()

        self.assertEqual(0, len(order_manager.orders))
        self.assertEqual(1, len(order_manager.positions))
        self.assertEqual(State.FILLED, order.status)
        position = order_manager.positions[0]
        self.assertEqual(Position.PositionStatus.OPEN, position.status)
        self.assertEqual(12.1, position.entry_price)
 def analysis_symbols(self):
     """
     This is a list of symbols which are used in this algorithm.
     :return: Array of Symbols used in this algorithm
     """
     return [
         (Symbol.get('EURUSD:CUR'), MarketDataPeriod.MIN_5,
          self.evaluate_quote_update),
     ]
Exemple #9
0
    def __init__(self, symbol, outfile, input_files):
        self.pending = []
        self.totalTicks = 0

        self.outfile = open(outfile, 'w+')

        for filename in input_files:
            logging.info("Processing '%s'" % (filename,))
            data = CSVProvider(Symbol.get(symbol), filename)
            data.start_publishing(self.tick_handler)

        self.outfile.close()
    def testShortLoss(self):
        s1 = Symbol.get("TEST")
        s1.lot_size = 10000

        # LONG
        order = Order(s1, 1, Entry(Entry.Type.MARKET), Direction.SHORT)
        tick = Tick(datetime.utcnow(), 1.12239, 1.12245) # spread of 0.6
        position = Position(order, tick)

        tick = Tick(datetime.utcnow(), 1.12259, 1.12265)
        position.close(tick)

        self.assertAlmostEquals(-2, position.points_delta())
    def testShortLoss(self):
        s1 = Symbol.get("TEST")
        s1.lot_size = 10000

        # LONG
        order = Order(s1, 1, Entry(Entry.Type.MARKET), Direction.SHORT)
        tick = Tick(datetime.utcnow(), 1.12239, 1.12245)  # spread of 0.6
        position = Position(order, tick)

        tick = Tick(datetime.utcnow(), 1.12259, 1.12265)
        position.close(tick)

        self.assertAlmostEquals(-2, position.points_delta())
    def testModifiedStopLoss(self):
        s1 = Symbol.get("TEST")
        s1.lot_size = 10000

        # LONG
        order = Order(s1, 1, Entry(Entry.Type.MARKET), Direction.SHORT)
        tick = Tick(datetime.utcnow(), 1.12239, 1.12245) # spread of 0.6
        position = Position(order, tick)
        self.assertIsNone(position.stop_price)

        position.update(stop_loss=StopLoss(StopLoss.Type.FIXED, 2))

        self.assertIsNotNone(position.stop_price)
        self.assertEqual(1.12265, position.stop_price)
    def testModifiedStopLoss(self):
        s1 = Symbol.get("TEST")
        s1.lot_size = 10000

        # LONG
        order = Order(s1, 1, Entry(Entry.Type.MARKET), Direction.SHORT)
        tick = Tick(datetime.utcnow(), 1.12239, 1.12245)  # spread of 0.6
        position = Position(order, tick)
        self.assertIsNone(position.stop_price)

        position.update(stop_loss=StopLoss(StopLoss.Type.FIXED, 2))

        self.assertIsNotNone(position.stop_price)
        self.assertEqual(1.12265, position.stop_price)
    def __init__(self, symbol, user, database, host, input_files):
        self.pending = []
        self.totalTicks = 0
        self._db_connection = connector.connect(user=user, database=database, host=host)
        self.cursor = self._db_connection.cursor(buffered=True)
        self.cursor.execute("SET unique_checks=0;")
        self.cursor.execute("SET autocommit=0;")

        for filename in input_files:
            logging.info("Processing '%s'" % (filename,))
            data = CSVProvider(Symbol.get(symbol), filename)
            data.start_publishing(self.tick_handler)

        self._db_connection.commit()
        self.cursor.execute("SET unique_checks=1;")
        self.cursor.close()
        self._db_connection.close()
Exemple #15
0
    def __init__(self, symbol, user, database, host, input_files):
        self.pending = []
        self.totalTicks = 0
        self._db_connection = connector.connect(user=user,
                                                database=database,
                                                host=host)
        self.cursor = self._db_connection.cursor(buffered=True)
        self.cursor.execute("SET unique_checks=0;")
        self.cursor.execute("SET autocommit=0;")

        for filename in input_files:
            logging.info("Processing '%s'" % (filename, ))
            data = CSVProvider(Symbol.get(symbol), filename)
            data.start_publishing(self.tick_handler)

        self._db_connection.commit()
        self.cursor.execute("SET unique_checks=1;")
        self.cursor.close()
        self._db_connection.close()
Exemple #16
0
    def testQuoteGap(self):
        s = Symbol.get("TEST")
        conflator = PriceConflator("TEST", MarketDataPeriod.MIN_5, self.callback)

        start_time = datetime.datetime(2015, 7, 7, 14, 10, 0)
        start_time = start_time + MarketDataPeriod.MIN_1
        tick = Tick(start_time.timestamp(), 9.0, 9.2)  # 14:11
        conflator.add_tick(tick)
        start_time = start_time + MarketDataPeriod.MIN_1
        tick = Tick(start_time.timestamp(), 8.7, 8.9)  # 14:12
        conflator.add_tick(tick)
        start_time = start_time + MarketDataPeriod.MIN_1
        tick = Tick(start_time.timestamp(), 11.0, 11.2)  # 14:13
        conflator.add_tick(tick)
        start_time = start_time + MarketDataPeriod.MIN_1
        tick = Tick(start_time.timestamp(), 10.5, 10.7)  # 14:14
        conflator.add_tick(tick)

        self.assertEqual(0, len(self.callbackQuote))  # (All in 14:10:00)

        start_time = start_time + MarketDataPeriod.MIN_15
        tick = Tick(start_time.timestamp(), 9.0, 9.2)  # 14:29
        conflator.add_tick(tick)

        self.assertEqual(3, len(self.callbackQuote))  # 14:15:00, 14:20:00, 14:25:00

        start_time = start_time + MarketDataPeriod.MIN_1
        tick = Tick(start_time.timestamp(), 10.5, 10.7)  # 14:30
        conflator.add_tick(tick)

        self.assertEqual(4, len(self.callbackQuote))
        quote = self.callbackQuote[0]
        self.assertEqual(9.1, quote.open)
        self.assertEqual(11.1, quote.high)
        self.assertEqual(8.8, quote.low)
        self.assertEqual(10.6, quote.close)

        quote = self.callbackQuote[1]
        self.assertEqual(10.6, quote.open)
        self.assertEqual(10.6, quote.high)
        self.assertEqual(10.6, quote.low)
        self.assertEqual(10.6, quote.close)
Exemple #17
0
    def __init__(self, symbol, out_file, input_files):
        self.pending = []
        self.totalTicks = 0
        self.conn = sqlite3.connect(out_file)

        self.cursor = self.conn.cursor()
        self.cursor.execute("PRAGMA synchronous = OFF")
        self.cursor.execute("PRAGMA journal_mode = OFF")

        self.cursor.execute("CREATE TABLE IF NOT EXISTS tick_data("
                               "symbol varchar(64) NOT NULL,"
                               "timestamp timestamp NOT NULL,"
                               "bid NUMBER NOT NULL,"
                               "offer NUMBER NOT NULL)")

        self.cursor.execute("CREATE INDEX tick_data_timestamp_idx ON tick_data(timestamp)")

        for filename in input_files:
            logging.info("Processing '%s'" % (filename,))
            data = CSVProvider(Symbol.get(symbol), filename)
            data.start_publishing(self.tickHandler)
Exemple #18
0
                self._db_connection.commit()
                logging.debug("Commited %s" % (self.totalTicks, ))
                self.totalTicks = 0
        except connector.errors.IntegrityError as e:
            # probably a duplicate entry
            pass


if __name__ == '__main__':
    logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
    database = {
        'user': '******',
        'database': 'blackbox',
        'host': "192.168.0.8"
    }
    Symbol.set_info_provider(MySQLSymbolProvider(database))

    parser = argparse.ArgumentParser(
        description='Parse CSV Tick data into sqlite db.')
    parser.add_argument("-s",
                        "--symbol",
                        dest="symbol",
                        required=True,
                        help="symbol identifier")
    parser.add_argument("-u",
                        "--user",
                        dest="user",
                        default="blackbox",
                        help="mysql host")
    parser.add_argument("-x",
                        "--host",
Exemple #19
0
    def testSymbol(self):
        s1 = Symbol.get("TEST")
        s2 = Symbol.get("TEST")

        self.assertEqual(s1, s2)
        self.assertEqual(id(s1.__dict__), id(s2.__dict__))
Exemple #20
0
 def setUp(self):
     Symbol.set_info_provider(DummySymbolProvider())
Exemple #21
0
        self.cursor.execute("CREATE INDEX tick_data_timestamp_idx ON tick_data(timestamp)")

        for filename in input_files:
            logging.info("Processing '%s'" % (filename,))
            data = CSVProvider(Symbol.get(symbol), filename)
            data.start_publishing(self.tickHandler)

    def tickHandler(self, symbol, tick):
        self.pending.append((symbol.identifier, tick.timestamp, tick.bid, tick.offer))
        if len(self.pending) >= 10000:
            self.cursor.executemany("INSERT INTO tick_data(symbol, timestamp, bid, offer) VALUES(?, ?, ?, ?)", self.pending)
            self.totalTicks += len(self.pending)
            self.pending.clear()
            self.conn.commit()
            logging.debug("Commited %s" % (self.totalTicks,))

if __name__ == '__main__':
    logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
    Symbol.set_info_provider("")

    parser = argparse.ArgumentParser(description='Parse CSV Tick data into sqlite db.')
    parser.add_argument("-a", "--symbol", dest="symbol", required=True, help="symbol identifier")
    parser.add_argument("-o", "--out", dest="db_filename", default="data.store", help="sqlite filename")
    parser.add_argument("-i", "--in", dest="in_filename", help="csv file to read")

    args = parser.parse_args()
    input_files = glob.glob(os.path.expanduser(os.path.expandvars(args.in_filename)))

    h = Handler(args.symbol, args.db_filename, input_files)
 def analysis_symbols(self):
     """
     This is a list of symbols which are used in this algorithm.
     :return: Array of Symbols used in this algorithm
     """
     return [(Symbol.get("EURUSD:CUR"), MarketDataPeriod.MIN_5, self.evaluate_quote_update)]
 def analysis_symbols(self):
     return [Symbol.get('EURUSD:CUR'), ]
Exemple #24
0
 def setUp(self):
     Symbol.set_info_provider(DummySymbolProvider())
     logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
     self.position = None
 def analysis_symbols(self):
     return [
         (Symbol.get('EURUSD:CUR'), self.time_period, self.evaluate_quote_update),
         (Symbol.get('EURUSD:CUR'), MarketDataPeriod.MIN_5, self.evaluate_risk)
     ]
Exemple #26
0
 def analysis_symbols(self):
     return [
         Symbol.get('EURUSD:CUR'),
     ]
Exemple #27
0
 def setUp(self):
     Symbol.set_info_provider(DummySymbolProvider())
     self.callbackQuote = []
    def tick_handler(self, symbol, tick):
        try:
            self.cursor.execute("INSERT INTO tick_data2(symbol_id, timestamp, bid, offer) VALUES(%s, %s, %s, %s)", (symbol.identifier, tick.timestamp.timestamp(), tick.bid, tick.offer))
            self.totalTicks += 1
            if self.totalTicks >= 100000:
                self._db_connection.commit()
                logging.debug("Commited %s" % (self.totalTicks,))
                self.totalTicks = 0
        except connector.errors.IntegrityError as e:
            # probably a duplicate entry
            pass

if __name__ == '__main__':
    logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
    database = {'user': '******', 'database': 'blackbox', 'host': "192.168.0.8"};
    Symbol.set_info_provider(MySQLSymbolProvider(database))

    parser = argparse.ArgumentParser(description='Parse CSV Tick data into sqlite db.')
    parser.add_argument("-s", "--symbol", dest="symbol", required=True, help="symbol identifier")
    parser.add_argument("-u", "--user", dest="user", default="blackbox", help="mysql host")
    parser.add_argument("-x", "--host", dest="host", default="localhost", help="mysql host")
    parser.add_argument("-d", "--database", dest="database", default="blackbox", help="mysql database")
    parser.add_argument("-i", "--in", dest="in_filename", help="csv file to read")

    args = parser.parse_args()
    input_files = glob.glob(os.path.expanduser(os.path.expandvars(args.in_filename)))
    logging.debug("Processing %s files: %s" % (len(input_files), input_files))

    h = Handler(args.symbol, args.user, args.database, args.host, input_files)
Exemple #29
0
 def setUp(self):
     Symbol.set_info_provider("1")
Exemple #30
0
 def setUp(self):
     Symbol.set_info_provider(DummySymbolProvider())
     self.order_router = TestOrderRouter()
     self.algo = NakedBigShadow(10, 50, 100, MarketDataPeriod.HOUR_1)
     self.context = Context(10000, OrderBook(self.order_router, BacktestOrderbookPersist()), self.algo.analysis_symbols(), self.algo.quote_cache_size())