def run(name, constructor): with scheduler.create() as world: ctx = Context(world, veusz.Graph) traders = constructor(ctx) books = orderBooksToRender(ctx, traders) for t in traders + books: for ts in t.timeseries: ts.graph.addTimeSerie(ts) r = registry.create() root = registry.Simulation(traders, list(ctx.books.itervalues()), ctx.graphs) r.insert(root) r.pushAllReferences() context.bind(root, {'world' : world }) world.workTill(500) non_empty_graphs = [g for g in ctx.graphs if len(g._datas)] veusz.render(name, non_empty_graphs) world._reset() context.reset(root) if runTwoTimes: world.workTill(500) veusz.render(name, non_empty_graphs)
def createSimulation(name='All'): with scheduler.create() as world: myRegistry = registry.create() myRegistry.insert(Side.Sell) myRegistry.insert(Side.Buy) ctx = Context(world, js.Graph) dependency = strategy.v0.Dependency(ctx.book_B) dependency_ex = strategy.Dependency(event.Every(mathutils.rnd.expovariate(1.)), order.factory.side.Market(), ctx.book_B) def register(annotated_objects): for obj, alias in annotated_objects: if alias is not None: obj._alias = alias myRegistry.insert(obj) register([ (dependency, ["Basic", "Dependency"]), (dependency_ex, None), ]) myRegistry.pushAllReferences() def process(name): constructor = predefined[name] traders = constructor(ctx) books = orderBooksToRender(ctx, traders) for t in traders + list(ctx.books.itervalues()) + ctx.graphs: myRegistry.insert(t) if name != 'All': process(name) else: for n in predefined.iterkeys(): process(n) myRegistry.insert(world) root = myRegistry.insert(registry.createSimulation(myRegistry)) context.bind(myRegistry.get(root), { "world" : world }) if name != 'All': current_dir = current_user_dir() ensure_dir_ex(current_dir) if os.path.exists(os.path.join(current_dir, name)): i = 0 while os.path.exists(os.path.join(current_dir, name + "." + str(i))): i += 1 name += '.' + str(i) return name, root, myRegistry, world
def run(name, constructor, only_veusz): with scheduler.create() as world: ctx = Context(world, veusz.Graph) traders = constructor(ctx) if config.useMinorTraders: traders.extend([ ctx.makeMinorTrader(strategy.RSI_linear(k=const(0.07)), "RSI 0.07"), ctx.makeMinorTrader(strategy.RSI_linear(k=const(-0.07)), "RSI -0.07"), ctx.makeMinorTrader( strategy.Bollinger_linear(alpha=0.15, k=const(-0.5)), "Bollinger -0.5"), ctx.makeMinorTrader( strategy.Bollinger_linear(alpha=0.15, k=const(+0.5)), "Bollinger +0.5"), ]) books = orderBooksToRender(ctx, traders) for t in traders + books: for ts in t.timeseries: ts.graph.addTimeSerie(ts) r = registry.create() root = registry.Simulation(traders, list(ctx.books.itervalues()), ctx.graphs) r.insert(root) r.pushAllReferences() context.bind(root, {'world': world}) def checks(): if not only_veusz and config.checkConsistency: r.typecheck() try: dumped = pickle.dumps(r) pickle.loads(dumped) except Exception, err: print err checks() stat = world.workTill(500) checks() if config.showTiming: print "\n", stat non_empty_graphs = [g for g in ctx.graphs if len(g._datas)] veusz.render(name, non_empty_graphs) world._reset() context.reset(root) if False and config.runTwoTimes: world.workTill(500) veusz.render(name, non_empty_graphs)
def run(name, constructor, only_veusz): with scheduler.create() as world: ctx = Context(world, veusz.Graph) traders = constructor(ctx) if config.useMinorTraders: traders.extend([ ctx.makeMinorTrader(strategy.RSI_linear(k = const(0.07)), "RSI 0.07"), ctx.makeMinorTrader(strategy.RSI_linear(k = const(-0.07)), "RSI -0.07"), ctx.makeMinorTrader(strategy.Bollinger_linear(alpha=0.15, k = const(-0.5)), "Bollinger -0.5"), ctx.makeMinorTrader(strategy.Bollinger_linear(alpha=0.15, k = const(+0.5)), "Bollinger +0.5"), ]) books = orderBooksToRender(ctx, traders) for t in traders + books: for ts in t.timeseries: ts.graph.addTimeSerie(ts) r = registry.create() root = registry.Simulation(traders, list(ctx.books.itervalues()), ctx.graphs) r.insert(root) r.pushAllReferences() context.bind(root, {'world' : world }) if False: req = request.EvalMarketOrder(Side.Sell, 500, _print) world.schedule(10, _(ctx.remote_A, req).process) def checks(): if not only_veusz and config.checkConsistency: r.typecheck() try: dumped = pickle.dumps(r) pickle.loads(dumped) except Exception, err: print err checks() stat = world.workTill(500) checks() if config.showTiming: print "\n", stat non_empty_graphs = [g for g in ctx.graphs if len(g._datas)] veusz.render(name, non_empty_graphs) world._reset() context.reset(root) if False and config.runTwoTimes: world.workTill(500) veusz.render(name, non_empty_graphs)
def createSimulation(name='All'): with scheduler.create() as world: myRegistry = registry.create() ctx = Context(world, js.Graph) dependency_ex = strategy.PairTrading(event.Every(math.random.expovariate(1.)), order.side.Market(), ctx.book_B) def register(annotated_objects): for obj, alias in annotated_objects: if alias is not None: obj._alias = alias myRegistry.insert(obj) register([ (dependency_ex, None), ]) myRegistry.pushAllReferences() def process(name): constructor = predefined[name] traders = constructor(ctx) books = orderBooksToRender(ctx, traders) for t in traders + list(ctx.books.itervalues()) + ctx.graphs: myRegistry.insert(t) if name != 'All': process(name) else: for n in predefined.iterkeys(): process(n) myRegistry.insert(world) root = myRegistry.insert(registry.createSimulation(myRegistry)) context.bind(myRegistry.get(root), { "world" : world }) if name != 'All': current_dir = current_user_dir() ensure_dir_ex(current_dir) if os.path.exists(os.path.join(current_dir, name)): i = 0 while os.path.exists(os.path.join(current_dir, name + "." + str(i))): i += 1 name += '.' + str(i) return name, root, myRegistry, world
import sys sys.path.append(r'..') from marketsim import strategy, order, orderbook, trader, Side, scheduler with scheduler.create() as world: book = orderbook.Local(tickSize=.001) counter = [0] def side(): counter[0] = 1 - counter[0] return Side.byId(counter[0]) trader = trader.SingleAsset(book, strategy.Noise(sideDistr=side, volumeDistr=(lambda:10), creationIntervalDistr=(lambda:1))) world.workTill(1.5) book.process(order.Limit.Buy(90, 20)) book.process(order.Limit.Sell(110, 20)) world.workTill(2.5) assert book.bids.best.volume == 10 assert trader.PnL == +10*90 world.workTill(3.5)
import sys sys.path.append(r'..') from marketsim import scheduler with scheduler.create() as S: def F(actionTime): def inner(): assert actionTime==S.currentTime return inner def schedule(actionTime): return S.schedule(actionTime, F(actionTime)) schedule(1.) schedule(1.) schedule(3.) e2 = schedule(2.) S.workTill(1.5) e2() S.workTill(2.5) S.workTill(3.5)
def createJob(i): world = scheduler.create() createSimulation(world, str(i)) world.workTill(500)