def save_load(name, persistable, datastore, save_func, load_clazz): save_func(persistable) result = [item for item in datastore.load_all(load_clazz) if item.id() == persistable.id()] assert len(result) == 1 item = result[0] print "===== %s" % name print persistable print item assert MapSerializer.extract_slot(persistable) == MapSerializer.extract_slot(item)
def save_load(name, persistable, datastore, save_func, load_clazz): save_func(persistable) result = [ item for item in datastore.load_all(load_clazz) if item.id() == persistable.id() ] assert len(result) == 1 item = result[0] print "===== %s" % name print persistable print item assert MapSerializer.extract_slot( persistable) == MapSerializer.extract_slot(item)
def test_save_strategies(): print "test_save_strategies" context = get_default_app_context() store = context.provider_mgr.get(DataStore.Mongo) store.start(app_context=context) stg_mgr = context.stg_mgr stg_mgr.start(app_context=context) stg_mgr.reset() stg1 = stg_mgr.new_stg(trading_config=BacktestingConfig( id='1', stg_id='test1', stg_cls='algotrader.strategy.ema_strategy.EMAStrategy')) ord_mrg = context.order_mgr ord_mrg.start(app_context=context) nos = NewOrderRequest(cl_id='test1', cl_ord_id='1', inst_id=1, portf_id='test_porf', action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000, limit_price=18.5) order = ord_mrg.send_order(nos) stg1.ord_reqs[nos.cl_ord_id] = nos stg1.orders[order.cl_ord_id] = order stg1.add_position(nos.inst_id, nos.cl_id, nos.cl_ord_id, nos.qty) stg1.update_position_price(time=0, inst_id=nos.inst_id, price=100) stg2 = stg_mgr.new_stg(trading_config=BacktestingConfig( id='2', stg_id='test2', stg_cls='algotrader.strategy.ema_strategy.EMAStrategy')) before = stg_mgr.get('test1') stg_mgr.stop() stg_mgr.start(app_context=context) after = stg_mgr.get('test1') after.start(app_context=context) print "before %s" % MapSerializer.serialize(before) print "after %s" % MapSerializer.serialize(after)
def test_save_strategies(): print "test_save_strategies" context = get_default_app_context() store = context.provider_mgr.get(DataStore.Mongo) store.start(app_context=context) stg_mgr = context.stg_mgr stg_mgr.start(app_context=context) stg_mgr.reset() stg1 = stg_mgr.new_stg(trading_config=BacktestingConfig(id='1', stg_id='test1', stg_cls='algotrader.strategy.ema_strategy.EMAStrategy')) ord_mrg = context.order_mgr ord_mrg.start(app_context=context) nos = NewOrderRequest(cl_id='test1', cl_ord_id='1', inst_id=1, portf_id='test_porf', action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000, limit_price=18.5) order = ord_mrg.send_order(nos) stg1.ord_reqs[nos.cl_ord_id] = nos stg1.orders[order.cl_ord_id] = order stg1.add_position(nos.inst_id, nos.cl_id, nos.cl_ord_id, nos.qty) stg1.update_position_price(time=0, inst_id=nos.inst_id, price=100) stg2 = stg_mgr.new_stg(trading_config=BacktestingConfig(id='2', stg_id='test2', stg_cls='algotrader.strategy.ema_strategy.EMAStrategy')) before = stg_mgr.get('test1') stg_mgr.stop() stg_mgr.start(app_context=context) after = stg_mgr.get('test1') after.start(app_context=context) print "before %s" % MapSerializer.serialize(before) print "after %s" % MapSerializer.serialize(after)
def ser_deser(name, serializer, item): packed = serializer.serialize(item) unpacked = serializer.deserialize(packed) print "===== %s" % name print packed print item print unpacked print MapSerializer.extract_slot(item) print MapSerializer.extract_slot(unpacked) assert MapSerializer.extract_slot(item) == MapSerializer.extract_slot(unpacked)