def send(self, order):
     if order is not None:
         if isinstance(order, types.IOrder):
             order.owner = self
         context.bind(order, self._ctx)
         self._book.process(order)
     return order
def update():
    w = current_user_workspace()
    
    parsed = request_parsed()

    metaToCreate = dict([(int(Id), (meta[0], meta[1], meta[2])) for (Id, meta) in parsed['created']])

    with w.world: 
        w.registry.createNewObjects(metaToCreate)
        
        # changing fields for existing ones    
        for (Id, field, value) in parsed['updates']:
            w.registry.setAttr(Id, field, value)
            
    context.bind(w.registry.get(w.root), { "world" : w.world })
    save_state_before_changes(w.registry) 

    save_current_workspace()
    
    if 'limitTime' in parsed:
        limitTime = parsed['limitTime']
        timeout = parsed["timeout"]
        run(w.world, timeout, limitTime)
        save_current_workspace()

    return changes(w)
def update():
    w = current_user_workspace()
    
    parsed = request_parsed()

    metaToCreate = dict([(int(Id), (meta[0], meta[1], meta[2])) for (Id, meta) in parsed['created']])

    with w.world: 
        w.registry.createNewObjects(metaToCreate)
        
        # changing fields for existing ones    
        for (Id, field, value) in parsed['updates']:
            w.registry.setAttr(Id, field, value)
            
    context.bind(w.registry.get(w.root), { "world" : w.world })
    save_state_before_changes(w.registry) 

    save_current_workspace()
    
    if 'limitTime' in parsed:
        limitTime = parsed['limitTime']
        timeout = parsed["timeout"]
        run(w.world, timeout, limitTime)
        save_current_workspace()

    return changes(w)
Exemple #4
0
 def reset(self):
     self.impl = self.getImpl()
     if 'getDefinitions' in dir(self):
         self.impl._definitions = self.getDefinitions()
     ctx = getattr(self, '_ctx', None)
     if ctx:
         context.bind(self.impl, ctx)
Exemple #5
0
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
Exemple #7
0
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)
Exemple #8
0
 def send(self, order):
     from marketsim.gen._out._iorder import IOrder
     if order is not None:
         if isinstance(order, IOrder):
             order.owner = self
         context.bind(order, self._ctx)
         self._book.process(order)
     return order
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 send(self, book, order):
     """ Sends 'order' to 'book'
     After having the order sent notifies listeners about it 
     """
     context.bind(order, self._ctx)
     if isinstance(order, types.IOrder):
         order = self._makeSubscribedTo(order)
     book.process(order)
     if isinstance(order, types.IOrder):
         self.on_order_sent.fire(order)        
Exemple #11
0
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
Exemple #12
0
 def send(self, book, order):
     """ Sends 'order' to 'book'
     After having the order sent notifies listeners about it
     """
     from marketsim.gen._out._iorder import IOrder
     context.bind(order, self._ctx)
     if isinstance(order, IOrder):
         order = self._makeSubscribedTo(order)
     book.process(order)
     if isinstance(order, IOrder):
         self.on_order_sent.fire(order)
def subscribe(event, listener, target = None, ctx = None):
    
    subscription = Subscription(event, listener)
    
    if target is not None:
        if '_subscriptions' not in dir(target):
            target._subscriptions = []
            
        target._subscriptions.append(subscription)
        
        if 'dispose' not in dir(target):
            target.dispose = bind.Callable(dispose, target)
            
    if ctx is not None:
        context.bind(subscription, ctx)
            
    return subscription
Exemple #14
0
def subscribe(event, listener, target = None, ctx = None):
    
    subscription = Subscription(event, listener)
    
    if target is not None:
        if '_subscriptions' not in dir(target):
            target._subscriptions = []
            
        target._subscriptions.append(subscription)
        
        if 'dispose' not in dir(target):
            target.dispose = bind.Callable(dispose, target)
            
    if ctx is not None:
        context.bind(subscription, ctx)
            
    return subscription
 def reset(self):
     self.impl = self.getImpl()
     ctx = getattr(self, '_ctx', None)
     if ctx: context.bind(self.impl, ctx)
Exemple #16
0
 def reset(self):
     self.impl = self.getImpl()
     ctx = getattr(self, '_ctx', None)
     if ctx: context.bind(self.impl, ctx)
Exemple #17
0
 def bind(self, ctx):
     from marketsim import event, _, context
     event.subscribe(self.queue.bestPrice, _(self).fire, self)
     context.bind(self._subscriptions, ctx)
Exemple #18
0
 def bind(self, ctx):
     from marketsim import event, _, context
     event.subscribe(self.queue.bestPrice, _(self).fire, self)
     context.bind(self._subscriptions, ctx)