Esempio n. 1
0
def get_default_app_context():
    config = MongoDBConfig()
    persistence_config = PersistenceConfig(
        None, DataStore.Mongo, PersistenceMode.Batch, DataStore.Mongo,
        PersistenceMode.Batch, DataStore.Mongo, PersistenceMode.Batch,
        DataStore.Mongo, PersistenceMode.Batch)
    app_config = ApplicationConfig(None, None, None, persistence_config,
                                   config)
    return ApplicationContext(app_config=app_config)
Esempio n. 2
0
def get_default_app_context():
    config = MongoDBConfig()
    persistence_config = PersistenceConfig(
        None, DataStore.Mongo, PersistenceMode.RealTime, DataStore.Mongo,
        PersistenceMode.RealTime, DataStore.Mongo, PersistenceMode.RealTime,
        DataStore.Mongo, PersistenceMode.RealTime)

    app_config = ApplicationConfig("InstrumentImport", RefDataManager.DB,
                                   Clock.Simulation, persistence_config,
                                   config)

    return ApplicationContext(app_config=app_config)
Esempio n. 3
0
def app_context():
    persistence_config = PersistenceConfig(None,
                                           DataStore.Mongo, PersistenceMode.RealTime,
                                           DataStore.Mongo, PersistenceMode.RealTime,
                                           DataStore.Mongo, PersistenceMode.RealTime,
                                           DataStore.Mongo, PersistenceMode.RealTime)
    app_config = ApplicationConfig(id=None, ref_data_mgr_type=RefDataManager.DB, clock_type=Clock.RealTime,
                                   persistence_config=persistence_config,
                                   provider_configs=[MongoDBConfig(), IBConfig(client_id=2, use_gevent=True)])
    app_context = ApplicationContext(app_config=app_config)

    return app_context
Esempio n. 4
0
    def new_app_context(self):
        name = "test"
        create_at_start = True
        delete_at_stop = False

        app_config = ApplicationConfig("app", None, Clock.Simulation, PersistenceConfig(
            ref_ds_id=DataStore.InMemoryDB, ref_persist_mode=PersistenceMode.RealTime,
            trade_ds_id=DataStore.InMemoryDB, trade_persist_mode=PersistenceMode.RealTime,
            ts_ds_id=DataStore.InMemoryDB, ts_persist_mode=PersistenceMode.RealTime,
            seq_ds_id=DataStore.InMemoryDB, seq_persist_mode=PersistenceMode.RealTime),
                                       InMemoryStoreConfig(file="%s_db.p" % name,
                                                           create_at_start=create_at_start,
                                                           delete_at_stop=delete_at_stop))
        app_context = ApplicationContext(app_config=app_config)
        app_context.start()
        return app_context
Esempio n. 5
0
    def __init__(self, id=None,
                 ref_data_mgr_type=RefDataManager.InMemory,
                 clock_type=Clock.Simulation,
                 persistence_config=None,
                 provider_configs=None):
        super(ApplicationConfig, self).__init__(id)

        self.ref_data_mgr_type = ref_data_mgr_type
        self.clock_type = clock_type
        self.persistence_config = persistence_config if persistence_config else PersistenceConfig()
        self.provider_configs = {}

        if provider_configs:
            if not isinstance(provider_configs, (list, tuple)):
                self.provider_configs[provider_configs.__class__] = provider_configs
            else:
                for provider_config in provider_configs:
                    self.provider_configs[provider_config.__class__] = provider_config
Esempio n. 6
0
    def setUp(self):

        persistence_config = PersistenceConfig(
            None, DataStore.InMemoryDB, PersistenceMode.Batch,
            DataStore.InMemoryDB, PersistenceMode.Batch, DataStore.InMemoryDB,
            PersistenceMode.Batch, DataStore.InMemoryDB, PersistenceMode.Batch)

        name = "test"
        create_at_start = True
        delete_at_stop = False

        app_config = ApplicationConfig(
            None, None, Clock.RealTime, persistence_config,
            InMemoryStoreConfig(file="%s_db.p" % name,
                                create_at_start=create_at_start,
                                delete_at_stop=delete_at_stop))
        self.context = ApplicationContext(app_config=app_config)

        self.db = InMemoryDataStore()
        self.db.start(self.context)
Esempio n. 7
0
def main():
    persistence_config = PersistenceConfig(
        None, DataStore.Mongo, PersistenceMode.RealTime, DataStore.Mongo,
        PersistenceMode.RealTime, DataStore.Mongo, PersistenceMode.RealTime,
        DataStore.Mongo, PersistenceMode.RealTime)
    app_config = RealtimeMarketDataImporterConfig(
        None,
        feed_id=Broker.IB,
        instrument_ids=[3],
        subscription_types=[
            BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)
        ],
        ref_data_mgr_type=RefDataManager.InMemory,
        clock_type=Clock.RealTime,
        persistence_config=persistence_config,
        provider_configs=[MongoDBConfig(),
                          IBConfig(client_id=2)])
    app_context = ApplicationContext(app_config=app_config)

    MktDataImporter().start(app_context)
Esempio n. 8
0
def main():
    broker_config = IBConfig(client_id=2)
    live_trading_config = LiveTradingConfig(
        id=None,
        stg_id="down2%",
        stg_cls='algotrader.strategy.down_2pct_strategy.Down2PctStrategy',
        portfolio_id='test',
        instrument_ids=[4],
        subscription_types=[
            BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.M1)
        ],
        feed_id=Broker.IB,
        broker_id=Broker.IB,
        ref_data_mgr_type=RefDataManager.DB,
        clock_type=Clock.RealTime,
        persistence_config=PersistenceConfig(),
        configs=[broker_config])

    app_context = ApplicationContext(app_config=live_trading_config)
    ATSRunner().start(app_context)
Esempio n. 9
0
                                limit_price=limit_price * 1.2)
    broker.on_ord_replace_req(order)
    time.sleep(5)

    print "### testing order cancel"
    broker.on_ord_cancel_req(order)
    time.sleep(5)


if __name__ == "__main__":

    logger.setLevel(logging.DEBUG)

    persistence_config = PersistenceConfig(
        None, DataStore.InMemoryDB, PersistenceMode.RealTime,
        DataStore.InMemoryDB, PersistenceMode.RealTime, DataStore.InMemoryDB,
        PersistenceMode.RealTime, DataStore.InMemoryDB,
        PersistenceMode.RealTime)
    app_config = RealtimeMarketDataImporterConfig(
        None, RefDataManager.InMemory, Clock.RealTime, Broker.IB, [3],
        [BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)],
        persistence_config, MongoDBConfig(),
        IBConfig(client_id=2, use_gevent=True))
    app_context = ApplicationContext(app_config=app_config)

    app_context.start()
    broker = app_context.provider_mgr.get(Broker.IB)
    broker.start(app_context)

    # test_sub_hist_bar(broker)
    test_sub_realtime_bar(broker)
Esempio n. 10
0
from algotrader.trading.clock import Clock
from algotrader.trading.context import ApplicationContext
from algotrader.trading.order import Order
from algotrader.trading.ref_data import Instrument, Exchange, Currency
from algotrader.trading.subscription import BarSubscriptionType
from algotrader.trading.subscription import HistDataSubscriptionKey, QuoteSubscriptionType, TradeSubscriptionType, \
    MarketDepthSubscriptionType
from algotrader.utils.date import date_to_unixtimemillis
from poc.ser_deser import MapSerializer

print
Cluster.port

persistence_config = PersistenceConfig(None, DataStore.Mongo,
                                       PersistenceMode.Batch, DataStore.Mongo,
                                       PersistenceMode.Batch, DataStore.Mongo,
                                       PersistenceMode.Batch, DataStore.Mongo,
                                       PersistenceMode.Batch)

# name = "test_%s" % int(time.time() * 1000)
name = "test"
create_at_start = True
cass_delete_at_stop = False
mongo_delete_at_stop = True
im_memory_delete_at_stop = True

app_config = ApplicationConfig(
    None,
    None,
    Clock.RealTime,
    persistence_config,
from algotrader.utils.clock import Clock
from algotrader.provider.broker import Broker
from algotrader.config.feed import CSVFeedConfig
from algotrader.config.feed import PandasMemoryDataFeedConfig
from algotrader.strategy.alpha_formula import AlphaFormula3
from algotrader.strategy.vix_future import VIXFuture
from algotrader.provider.persistence import PersistenceMode
from algotrader.provider.persistence.data_store import DataStore
from algotrader.app.backtest_runner import BacktestRunner
from algotrader.config.builder import *
from algotrader.utils import logger


persistence_config = PersistenceConfig(None,
                                       DataStore.Mongo, PersistenceMode.RealTime,
                                       DataStore.Mongo, PersistenceMode.RealTime,
                                       DataStore.Mongo, PersistenceMode.RealTime,
                                       DataStore.Mongo, PersistenceMode.RealTime)


def test_vix():
    logger.setLevel(logging.DEBUG)
    symbols = ["VXF2015", "VXG2015", "VXH2015", "VXJ2015"]
    vix_data = {}

    with pd.HDFStore('/Users/jchan/workspace/data/temp/VX.h5') as store:
        for s in store.keys():
            sym = re.sub('/', '', s)
            df = store[s]
            df = df.reset_index()
            df['DateTime'] = df['Trade Date']
Esempio n. 12
0
    def test(self):
        backtest_config0 = BacktestingConfig(
            id="down2%-test-config",
            stg_id="down2%",
            stg_cls='algotrader.strategy.down_2pct_strategy.Down2PctStrategy',
            portfolio_id='test',
            portfolio_initial_cash=100000,
            instrument_ids=[4],
            subscription_types=[
                BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)
            ],
            from_date=date(1993, 1, 1),
            to_date=date(2017, 1, 1),
            broker_id=Broker.Simulator,
            feed_id=Feed.CSV,
            stg_configs={'qty': 1000},
            ref_data_mgr_type=RefDataManager.InMemory,
            persistence_config=PersistenceConfig(),
            provider_configs=CSVFeedConfig(path='data/tradedata'))
        app_context0 = ApplicationContext(app_config=backtest_config0)
        runner = BacktestRunner(isplot=False)

        runner.start(app_context0)

        total_begin_result = runner.initial_result
        total_end_result = runner.portfolio.get_result()

        backtest_config1 = BacktestingConfig(
            id="down2%-test-config_1",
            stg_id="down2%_1",
            stg_cls='algotrader.strategy.down_2pct_strategy.Down2PctStrategy',
            portfolio_id='test_1',
            portfolio_initial_cash=100000,
            instrument_ids=[4],
            subscription_types=[
                BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)
            ],
            from_date=date(1993, 1, 1),
            to_date=date(2008, 1, 1),
            broker_id=Broker.Simulator,
            feed_id=Feed.CSV,
            stg_configs={'qty': 1000},
            ref_data_mgr_type=RefDataManager.InMemory,
            persistence_config=PersistenceConfig(
                seq_ds_id=DataStore.InMemoryDB,
                seq_persist_mode=PersistenceMode.Batch,
                ts_ds_id=DataStore.InMemoryDB,
                ts_persist_mode=PersistenceMode.Batch,
                trade_ds_id=DataStore.InMemoryDB,
                trade_persist_mode=PersistenceMode.Batch),
            provider_configs=CSVFeedConfig(path='data/tradedata'))
        app_context1 = ApplicationContext(app_config=backtest_config1)
        runner1 = BacktestRunner(isplot=False)
        runner1.start(app_context1)

        part1_begin_result = runner1.initial_result
        part1_end_result = runner1.portfolio.get_result()

        backtest_config2 = BacktestingConfig(
            id="down2%-test-config_1",
            stg_id="down2%_1",
            stg_cls='algotrader.strategy.down_2pct_strategy.Down2PctStrategy',
            portfolio_id='test_1',
            portfolio_initial_cash=100000,
            instrument_ids=[4],
            subscription_types=[
                BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)
            ],
            from_date=date(2008, 1, 1),
            to_date=date(2017, 1, 1),
            broker_id=Broker.Simulator,
            feed_id=Feed.CSV,
            stg_configs={'qty': 1000},
            ref_data_mgr_type=RefDataManager.InMemory,
            persistence_config=PersistenceConfig(
                seq_ds_id=DataStore.InMemoryDB,
                seq_persist_mode=PersistenceMode.Batch,
                ts_ds_id=DataStore.InMemoryDB,
                ts_persist_mode=PersistenceMode.Batch,
                trade_ds_id=DataStore.InMemoryDB,
                trade_persist_mode=PersistenceMode.Batch),
            provider_configs=CSVFeedConfig(path='data/tradedata'))
        app_context2 = ApplicationContext(app_config=backtest_config2)
        app_context2.start()
        db = app_context2.get_seq_data_store()

        runner2 = BacktestRunner(isplot=False)
        runner2.start(app_context2)

        part2_begin_result = runner2.initial_result
        part2_end_result = runner2.portfolio.get_result()

        self.assertEqual(total_begin_result, part1_begin_result)
        self.assertEqual(part1_end_result, part2_begin_result)
        self.assertEqual(total_end_result, part2_end_result)

        print "total begin = %s" % total_begin_result
        print "total end = %s" % total_end_result
        print "part1 begin = %s" % part1_begin_result
        print "part1 end = %s" % part1_end_result
        print "part2 begin = %s" % part2_begin_result
        print "part2 end = %s" % part2_end_result

        db.remove_database()