Beispiel #1
0
def test_strategy_fills(side):
    ## handle long and short fills, all possiblities
    ## strategy queues

    latch = DataLatch(1)

    s1_order_q = DQueue()
    s1_fill_q = DQueue()

    strategy = MStrategy('test_strategy', strategy_params={'length': 10})

    strategy.IN_fills = s1_fill_q
    strategy.OUT_orders = s1_order_q
    strategy.latch = latch

    strategy.start()

    ts = datetime.datetime(2014, 8, 16, 12, 30, 0)

    ## build inital position
    s1_fill_q.put(Fill('AAPL', 100.00, 100, side, ts, 1))
    s1_fill_q.put(Fill('AAPL', 101.50, 50, side, ts, 2))
    s1_fill_q.put(
        Fill('AAPL', 110.00, 50, side, ts + datetime.timedelta(seconds=10), 3))

    rev = Order.SELL
    if side == rev: rev = Order.BUY
    ## take some of off
    s1_fill_q.put(
        Fill('AAPL', 107.00, 20, rev, ts + datetime.timedelta(seconds=20), 4))
    s1_fill_q.put(
        Fill('AAPL', 107.00, 20, rev, ts + datetime.timedelta(seconds=25), 5))

    ## flip position
    s1_fill_q.put(
        Fill('AAPL', 110.00, 200, rev, ts + datetime.timedelta(seconds=100),
             6))
    s1_fill_q.put(
        Fill('AAPL', 108.50, 10, rev, ts + datetime.timedelta(seconds=110), 7))
    s1_fill_q.put(
        Fill('AAPL', 106.50, 100, rev, ts + datetime.timedelta(seconds=110),
             8))

    ##close position
    s1_fill_q.put(
        Fill('AAPL', 109.00, 100, side, ts + datetime.timedelta(seconds=200),
             9))
    s1_fill_q.put(
        Fill('AAPL', 109.00, 50, side, ts + datetime.timedelta(seconds=200),
             10))

    time.sleep(5)

    strategy.shutdown()
    strategy.join()
Beispiel #2
0
def test_strategy(strategy_params, run_id):

    latch = DataLatch(3)
    s1_order_q = DQueue()
    s1_fill_q = DQueue()

    strat_name = 'test_%04d' % run_id

    ## NOTE strategy_params is a dict that the strategy
    ## uses to initialize itself.
    strategy = MStrategy(strat_name, strategy_params=strategy_params)
    strategy.bar_interval = 1
    strategy.IN_fills = s1_fill_q
    strategy.OUT_orders = s1_order_q
    strategy.latch = latch

    porto_name = 'portfolio_%d' % run_id
    portfolio = Portfolio(porto_name, None)
    portfolio.latch = latch
    portfolio.add(strategy)

    exchange = Exchange()
    exchange.IN_orders = portfolio.OUT_orders
    exchange.OUT_fills = portfolio.IN_fills
    exchange.latch = latch

    exchange.start()
    portfolio.start()
    strategy.start()

    simData = DataFeedBars('20100315.SPY.1m.csv')
    for market_data in simData:
        latch.trap(market_data)
        ## ORDER MATTERS!
        ## this allows submit-fill loop to happen in a single on_data() event
        strategy.on_data(market_data)
        portfolio.on_data(market_data)
        exchange.on_data(market_data)

    ## do any final processing
    #strategy.flush()

    exchange.shutdown()
    portfolio.shutdown()
    strategy.shutdown()
    exchange.join()
    portfolio.join()
    strategy.join()

    return portfolio.stats()
Beispiel #3
0
def test_bars(length=50):

    latch = DataLatch(3)
    s1_order_q = DQueue()
    s1_fill_q = DQueue()

    strat_name = 'test_%d' % length
    strategy = MStrategy(strat_name, strategy_params={'length': length})
    strategy.bar_interval = 0
    strategy.IN_fills = s1_fill_q
    strategy.OUT_orders = s1_order_q
    strategy.latch = latch

    porto_name = 'portfolio_%d' % length
    portfolio = Portfolio(porto_name, None)
    portfolio.latch = latch
    portfolio.add(strategy)

    exchange = Exchange()
    exchange.IN_orders = portfolio.OUT_orders
    exchange.OUT_fills = portfolio.IN_fills
    exchange.latch = latch

    exchange.start()
    portfolio.start()
    strategy.start()

    log.info("START JOB %s = %s" % (porto_name, datetime.datetime.now()))

    simData = DataFeedBars('20100315.SPY.1m.csv')
    for market_data in simData:
        latch.trap(market_data)
        ## ORDER MATTERS!
        ## this allows submit-fill loop to happen in a single on_data() event
        strategy.on_data(market_data)
        portfolio.on_data(market_data)
        exchange.on_data(market_data)

    ## do any final processing
    #strategy.flush()

    exchange.shutdown()
    portfolio.shutdown()
    strategy.shutdown()
    exchange.join()
    portfolio.join()
    strategy.join()

    # portfolio.stats(write_data=True)
    filename = 'TEST_BAR_X_%d.xls' % length
    port_stats = portfolio.stats(filename=filename)

    print 'portfolio stats'
    pprint(port_stats)

    log.info("END JOB %s = %s" % (porto_name, datetime.datetime.now()))

    return portfolio
Beispiel #4
0
def test_multiple_strategies():

    latch = DataLatch(4)
    s1_order_q = DQueue()
    s2_order_q = DQueue()
    s1_fill_q = DQueue()
    s2_fill_q = DQueue()

    strategy = MStrategy('test_strategy', strategy_params={'length': 10})
    strategy.IN_fills = s1_fill_q
    strategy.OUT_orders = s1_order_q
    strategy.latch = latch

    strategy2 = MStrategy('test_strategy2', strategy_params={'length': 5})
    strategy2.IN_fills = s2_fill_q
    strategy2.OUT_orders = s2_order_q
    strategy2.latch = latch

    portfolio = Portfolio('portfolio', None)
    portfolio.latch = latch
    portfolio.add(strategy)
    portfolio.add(strategy2)

    exchange = Exchange()
    exchange.IN_orders = portfolio.OUT_orders
    exchange.OUT_fills = portfolio.IN_fills
    exchange.latch = latch

    exchange.start()
    portfolio.start()
    strategy.start()
    strategy2.start()

    log.info("START JOB= %s" % datetime.datetime.now())

    ## combined file of SPY, IWM, and QQQQ
    simData = DataFeedIntraday('20100315.XXX.csv')
    for market_data in simData:
        latch.trap(market_data)
        ## ORDER MATTERS!
        ## this allows submit-fill loop to happen in a single on_data() event
        strategy.on_data(market_data)
        strategy2.on_data(market_data)
        portfolio.on_data(market_data)
        exchange.on_data(market_data)

    ## do any final processing
    #strategy.flush()

    exchange.shutdown()
    portfolio.shutdown()
    strategy.shutdown()
    strategy2.shutdown()
    exchange.join()
    portfolio.join()
    strategy.join()
    strategy2.join()

    log.info("STAT JOB= %s" % datetime.datetime.now())

    portfolio.stats(filename='TESTER.xls')

    log.info("END JOB= %s" % datetime.datetime.now())
    log.info("LEN DATA JOB= %s" % simData.count)
Beispiel #5
0
def test_portfolio():

    latch = DataLatch(3)
    s1_order_q = DQueue()
    s1_fill_q = DQueue()

    strategy = MStrategy('test_strategy', strategy_params={'length': 10})
    strategy.IN_fills = s1_fill_q
    strategy.OUT_orders = s1_order_q
    strategy.latch = latch

    portfolio = Portfolio('test_porto', None)
    portfolio.latch = latch
    portfolio.add(strategy)

    exchange = Exchange()
    exchange.IN_orders = portfolio.OUT_orders
    exchange.OUT_fills = portfolio.IN_fills
    exchange.latch = latch

    exchange.start()
    portfolio.start()
    strategy.start()

    log.info("START JOB= %s" % datetime.datetime.now())

    simData = DataFeedIntraday('20100315.SPY.csv')
    for market_data in simData:
        latch.trap(market_data)
        ## ORDER MATTERS!
        ## this allows submit-fill loop to happen in a single on_data() event
        strategy.on_data(market_data)
        portfolio.on_data(market_data)
        exchange.on_data(market_data)

    ## do any final processing
    #strategy.flush()

    exchange.shutdown()
    portfolio.shutdown()
    strategy.shutdown()
    exchange.join()
    portfolio.join()
    strategy.join()

    log.info("STAT JOB= %s" % datetime.datetime.now())

    # portfolio.stats(write_data=True)

    log.info("END JOB= %s" % datetime.datetime.now())
    log.info("LEN DATA JOB= %s" % simData.count)
Beispiel #6
0
def test_strategy_execute():

    latch = DataLatch(2)
    s1_order_q = DQueue()
    s1_fill_q = DQueue()

    strategy = MStrategy('test_strategy', strategy_params={'length': 10})
    strategy.IN_fills = s1_fill_q
    strategy.OUT_orders = s1_order_q
    strategy.latch = latch

    exchange = Exchange()
    exchange.IN_orders = strategy.OUT_orders
    exchange.OUT_fills = strategy.IN_fills
    exchange.latch = latch

    exchange.start()
    strategy.start()

    log.info("START JOB= %s" % datetime.datetime.now())

    simData = DataFeedIntraday('20100315.SPY.csv')
    for market_data in simData:
        latch.trap(market_data)
        exchange.on_data(market_data)
        strategy.on_data(market_data)

    ## do any final processing
    #strategy.flush()

    exchange.shutdown()
    strategy.shutdown()
    exchange.join()
    strategy.join()

    log.info("END JOB= %s" % datetime.datetime.now())
    log.info("LEN DATA JOB= %s" % simData.count)
Beispiel #7
0
def test_strategy_order_update():
    ## do partial fill and update, both sides

    latch = DataLatch(1)
    s1_order_q = DQueue()
    s1_fill_q = DQueue()

    ts = datetime.datetime(2014, 8, 16, 12, 30, 0)

    strategy = MStrategy('test_strategy', strategy_params={'length': 10})
    strategy.IN_fills = s1_fill_q
    strategy.OUT_orders = s1_order_q
    strategy.latch = latch

    strategy.start()

    o1 = Order(strategy.name, 'AAPL', Order.SELL, 100, Order.MARKET, None,
               None)
    o2 = Order(strategy.name, 'AAPL', Order.SELL, 200, Order.MARKET, None,
               None)

    p1 = Order(strategy.name, 'AAPL', Order.BUY, 100, Order.MARKET, None, None)
    p2 = Order(strategy.name, 'AAPL', Order.BUY, 200, Order.MARKET, None, None)

    strategy.send_order(o1)
    strategy.send_order(o2)
    strategy.send_order(p1)
    strategy.send_order(p2)

    # allow time for orders to be sent
    time.sleep(2)

    s1_fill_q.put(Fill('AAPL', 100.00, 100, Order.SELL, ts, o1.order_id))
    s1_fill_q.put(
        Fill('AAPL', 101.50, 50, Order.SELL, ts, o2.order_id, qty_left=150))

    s1_fill_q.put(Fill('AAPL', 104.00, 100, Order.BUY, ts, p1.order_id))
    s1_fill_q.put(
        Fill('AAPL', 105.50, 70, Order.BUY, ts, p2.order_id, qty_left=130))

    time.sleep(2)

    strategy.shutdown()
    strategy.join()