Ejemplo n.º 1
0
def createSimulation(name='All'):

    with createScheduler() as world:

        myRegistry = registry.create()

        ctx = Context(world, Graph)
        dependency_ex = strategy.side.PairTrading(ctx.book_B)\
                                     .Strategy(event.Every(math.random.expovariate(1.)),
                                               order.side.Market())

        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))
        from marketsim.context import BindingContextEx
        myRegistry.get(root).bind_ex(BindingContextEx({"world": world}))
        myRegistry.get(root).registerIn(myRegistry)

        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
Ejemplo n.º 2
0
def run(name, constructor, only_veusz):
    with createScheduler() as world:

        import time
        t0 = time.clock()
        
        ctx = Context(world, veusz.Graph)
        print time.clock() - t0
        traders = constructor(ctx)
        print time.clock() - t0

        if config.useMinorTraders:
            traders.extend([
                ctx.makeMinorTrader(strategy.position.RSI_linear(k = const(0.7)).Strategy(), "RSI 0.07"),
                ctx.makeMinorTrader(strategy.position.RSI_linear(k = const(-0.7)).Strategy(), "RSI -0.07"),
                ctx.makeMinorTrader(strategy.position.Bollinger_linear(alpha=0.15, k = const(-5.)).Strategy(), "Bollinger -0.5"),
                ctx.makeMinorTrader(strategy.position.Bollinger_linear(alpha=0.15, k = const(+5.)).Strategy(), "Bollinger +0.5"),
            ])
        
        books = orderBooksToRender(ctx, traders)
        
        r = registry.create()
        root = registry.Simulation(traders, list(ctx.books.itervalues()), ctx.graphs)
#        r.insert(root)
        print time.clock() - t0
        root.registerIn(r)
#        r.pushAllReferences()
        print time.clock() - t0
        root.bind_ex(context.BindingContextEx({'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

        print time.clock() - t0

        checks()        
        stat = world.workTill(config.veuszRunLength)
        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_ex(0)
        root.reset_ex(0)

        if config.runTwoTimes:
            world.workTill(config.veuszRunLength)
            veusz.render(name, non_empty_graphs)
Ejemplo n.º 3
0
def createSimulation(name='All'):
    
    with createScheduler() as world:
        
        myRegistry = registry.create()
    
        ctx = Context(world, Graph)
        dependency_ex = strategy.side.PairTrading(ctx.book_B)\
                                     .Strategy(event.Every(math.random.expovariate(1.)),
                                               order.side.Market())
        
        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))
        from marketsim.context import BindingContextEx
        myRegistry.get(root).bind_ex(BindingContextEx({ "world" : world }))
        myRegistry.get(root).registerIn(myRegistry)

        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
Ejemplo n.º 4
0
def run(name, constructor, only_veusz):
    with createScheduler() as world:

        import time
        t0 = time.clock()

        ctx = Context(world, veusz.Graph)
        print time.clock() - t0
        traders = constructor(ctx)
        print time.clock() - t0

        if config.useMinorTraders:
            traders.extend([
                ctx.makeMinorTrader(
                    strategy.position.RSI_linear(k=const(0.7)).Strategy(),
                    "RSI 0.07"),
                ctx.makeMinorTrader(
                    strategy.position.RSI_linear(k=const(-0.7)).Strategy(),
                    "RSI -0.07"),
                ctx.makeMinorTrader(
                    strategy.position.Bollinger_linear(
                        alpha=0.15, k=const(-5.)).Strategy(),
                    "Bollinger -0.5"),
                ctx.makeMinorTrader(
                    strategy.position.Bollinger_linear(
                        alpha=0.15, k=const(+5.)).Strategy(),
                    "Bollinger +0.5"),
            ])

        books = orderBooksToRender(ctx, traders)

        r = registry.create()
        root = registry.Simulation(traders, list(ctx.books.itervalues()),
                                   ctx.graphs)
        #        r.insert(root)
        print time.clock() - t0
        root.registerIn(r)
        #        r.pushAllReferences()
        print time.clock() - t0
        root.bind_ex(context.BindingContextEx({'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

        print time.clock() - t0

        checks()
        stat = world.workTill(config.veuszRunLength)
        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_ex(0)
        root.reset_ex(0)

        if config.runTwoTimes:
            world.workTill(config.veuszRunLength)
            veusz.render(name, non_empty_graphs)