def test_retrace_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 = RetraceStrategy(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 = 'retrace_%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 = DataFeedDaily('daily.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() return portfolio.stats()
def test_exchange(): latch = DataLatch(1) ## exchange queues order_q = DQueue() fill_q = DQueue() exchange = Exchange() exchange.latch = latch ## bind exchange and portfolio together tester = Tester() exchange.IN_orders = order_q exchange.OUT_fills = fill_q tester.IN_queue = fill_q exchange.start() tester.start() o1 = Order('test', 'AAPL', Order.BUY, 100, Order.MARKET, None, None) o1.stamp_time(parse_date("20140311")) order_q.put(o1) o2 = Order('test', 'AAPL', Order.BUY, 200, Order.MARKET, None, None) o2.stamp_time(parse_date("20140816")) order_q.put(o2) o3 = Order('test', 'AAPL', Order.SELL, 300, Order.MARKET, None, None) o3.stamp_time(parse_date("20140101")) order_q.put(o3) simData = DataFeedDaily('AAPL.csv') for market_data in simData: latch.trap(market_data) exchange.on_data(market_data) exchange.shutdown() tester.shutdown() exchange.join() tester.join()
from RetraceStrategy import RetraceStrategy from DataFeed import DataFeedDaily from Optimizer import Optimizer if __name__ == '__main__': optimizer = Optimizer() optimizer.strategy_class = RetraceStrategy # optimizer.data_feed = DataFeedDaily('daily.SPY.csv') optimizer.data_feed = DataFeedDaily('SPY.csv') ## set population size optimizer.size = 40 optimizer.max_generations = 50 optimizer.outfile = 'optimize_retrace3.xls' optimizer.reset_on_EOD = True ## parameter space to search over ## strategy_params for RetraceStrategy = dict(average,momentum,duration) ## momentum = entry momentum crossover ## average = moving average filter ## duration = trade holding period param_list = [ dict(name='momentum', min_val=30, max_val=50, steps=16, converter=int), dict(name='average', min_val=70, max_val=120, steps=32, converter=int), dict(name='duration', min_val=10, max_val=20, steps=8, converter=int) ] for p in param_list: optimizer.add_parameter(p)
from Treadmill import Treadmill from RetraceStrategy import RetraceStrategy from DataFeed import DataFeedDaily t = Treadmill() t.strategy_class = RetraceStrategy t.strategy_params = dict(average=141,momentum=21,duration=15) t.data_feed = DataFeedDaily('daily.SPY.csv') t.run()
from RetraceStrategy import RetraceStrategy from DataFeed import DataFeedDaily from Optimizer import Optimizer if __name__ == '__main__': optimizer = Optimizer() optimizer.strategy_class = RetraceStrategy # optimizer.data_feed = DataFeedDaily('daily.SPY.csv') optimizer.data_feed = DataFeedDaily('mini_data.csv') ## set population size optimizer.size = 40 optimizer.max_generations = 50 optimizer.outfile = 'retrace_mini_opz.xls' optimizer.reset_on_EOD = True ## parameter space to search over ## strategy_params for RetraceStrategy = dict(average,momentum,duration) ## momentum = entry momentum crossover ## average = moving average filter ## duration = trade holding period param_list = [dict(name='momentum',min_val=10,max_val=100,steps=32,converter=int), dict(name='average',min_val=20,max_val=200,steps=32,converter=int), dict(name='duration',min_val=10,max_val=50,steps=16,converter=int) ] for p in param_list: