• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python api_support.set_algo_instance函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中zipline.utils.api_support.set_algo_instance函数的典型用法代码示例。如果您正苦于以下问题:Python set_algo_instance函数的具体用法?Python set_algo_instance怎么用?Python set_algo_instance使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了set_algo_instance函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: initialize

 def initialize(self, *args, **kwargs):
     # store algo reference in global space
     set_algo_instance(self)
     try:
         self._initialize(self)
     finally:
         set_algo_instance(None)
开发者ID:anupamsharma,项目名称:zipline,代码行数:7,代码来源:algorithm.py


示例2: test_volshare_slippage

    def test_volshare_slippage(self):
        # verify order -> transaction -> portfolio position.
        # --------------
        test_algo = TradingAlgorithm(
            script="""
from zipline.api import *

def initialize(context):
    model = slippage.VolumeShareSlippage(
                            volume_limit=.3,
                            price_impact=0.05
                       )
    set_slippage(model)
    set_commission(commission.PerShare(0.02))
    context.count = 2
    context.incr = 0

def handle_data(context, data):
    if context.incr < context.count:
        # order small lots to be sure the
        # order will fill in a single transaction
        order(0, 5000)
    record(price=data[0].price)
    record(volume=data[0].volume)
    record(incr=context.incr)
    context.incr += 1
    """,
            sim_params=self.sim_params,
        )
        set_algo_instance(test_algo)

        self.zipline_test_config['algorithm'] = test_algo
        self.zipline_test_config['trade_count'] = 100

        # 67 will be used inside assert_single_position
        # to confirm we have as many transactions as expected.
        # The algo places 2 trades of 5000 shares each. The trade
        # events have volume ranging from 100 to 950. The volume cap
        # of 0.3 limits the trade volume to a range of 30 - 316 shares.
        # The spreadsheet linked below calculates the total position
        # size over each bar, and predicts 67 txns will be required
        # to fill the two orders. The number of bars and transactions
        # differ because some bars result in multiple txns. See
        # spreadsheet for details:
# https://www.dropbox.com/s/ulrk2qt0nrtrigb/Volume%20Share%20Worksheet.xlsx
        self.zipline_test_config['expected_transactions'] = 67

        zipline = simfactory.create_test_zipline(
            **self.zipline_test_config)
        output, _ = assert_single_position(self, zipline)

        # confirm the slippage and commission on a sample
        # transaction
        per_share_commish = 0.02
        perf = output[1]
        transaction = perf['daily_perf']['transactions'][0]
        commish = transaction['amount'] * per_share_commish
        self.assertEqual(commish, transaction['commission'])
        self.assertEqual(2.029, transaction['price'])
开发者ID:1TTT9,项目名称:zipline,代码行数:59,代码来源:test_algorithm.py


示例3: test_fixed_slippage

    def test_fixed_slippage(self):
        # verify order -> transaction -> portfolio position.
        # --------------
        test_algo = TradingAlgorithm(
            script="""
from zipline.api import (slippage,
                         commission,
                         set_slippage,
                         set_commission,
                         order,
                         record)

def initialize(context):
    model = slippage.FixedSlippage(spread=0.10)
    set_slippage(model)
    set_commission(commission.PerTrade(100.00))
    context.count = 1
    context.incr = 0

def handle_data(context, data):
    if context.incr < context.count:
        order(0, -1000)
    record(price=data[0].price)

    context.incr += 1""",
            sim_params=self.sim_params,
        )
        set_algo_instance(test_algo)

        self.zipline_test_config['algorithm'] = test_algo
        self.zipline_test_config['trade_count'] = 200

        # this matches the value in the algotext initialize
        # method, and will be used inside assert_single_position
        # to confirm we have as many transactions as orders we
        # placed.
        self.zipline_test_config['order_count'] = 1

        # self.zipline_test_config['transforms'] = \
        #     test_algo.transform_visitor.transforms.values()

        zipline = simfactory.create_test_zipline(
            **self.zipline_test_config)

        output, _ = assert_single_position(self, zipline)

        # confirm the slippage and commission on a sample
        # transaction
        recorded_price = output[1]['daily_perf']['recorded_vars']['price']
        transaction = output[1]['daily_perf']['transactions'][0]
        self.assertEqual(100.0, transaction['commission'])
        expected_spread = 0.05
        expected_commish = 0.10
        expected_price = recorded_price - expected_spread - expected_commish
        self.assertEqual(expected_price, transaction['price'])
开发者ID:erain,项目名称:zipline,代码行数:55,代码来源:test_algorithm.py


示例4: test_algo_record_allow_mock

    def test_algo_record_allow_mock(self):
        """
        Test that values from "MagicMock"ed methods can be passed to record.

        Relevant for our basic/validation and methods like history, which
        will end up returning a MagicMock instead of a DataFrame.
        """
        test_algo = TradingAlgorithm(script=record_variables, sim_params=self.sim_params)
        set_algo_instance(test_algo)

        test_algo.record(foo=MagicMock())
开发者ID:ChrisBg,项目名称:zipline,代码行数:11,代码来源:test_algorithm.py


示例5: test_order_in_init

 def test_order_in_init(self):
     """
     Test that calling order in initialize
     will raise an error.
     """
     with self.assertRaises(OrderDuringInitialize):
         test_algo = TradingAlgorithm(
             script=call_order_in_init,
             sim_params=self.sim_params,
         )
         set_algo_instance(test_algo)
开发者ID:WQGit,项目名称:zipline,代码行数:11,代码来源:test_algorithm.py


示例6: test_order_methods

    def test_order_methods(self):
        """Only test that order methods can be called without error.
        Correct filling of orders is tested in zipline.
        """
        test_algo = TradingAlgorithm(script=call_all_order_methods, sim_params=self.sim_params)
        set_algo_instance(test_algo)

        self.zipline_test_config["algorithm"] = test_algo
        self.zipline_test_config["trade_count"] = 200

        zipline = simfactory.create_test_zipline(**self.zipline_test_config)

        output, _ = drain_zipline(self, zipline)
开发者ID:ChrisBg,项目名称:zipline,代码行数:13,代码来源:test_algorithm.py


示例7: _algo_record_float_magic_should_pass

    def _algo_record_float_magic_should_pass(self, var_type):
        test_algo = TradingAlgorithm(script=record_float_magic % var_type, sim_params=self.sim_params)
        set_algo_instance(test_algo)

        self.zipline_test_config["algorithm"] = test_algo
        self.zipline_test_config["trade_count"] = 200

        zipline = simfactory.create_test_zipline(**self.zipline_test_config)
        output, _ = drain_zipline(self, zipline)
        self.assertEqual(len(output), 252)
        incr = []
        for o in output[:200]:
            incr.append(o["daily_perf"]["recorded_vars"]["data"])
        np.testing.assert_array_equal(incr, [np.nan] * 200)
开发者ID:ChrisBg,项目名称:zipline,代码行数:14,代码来源:test_algorithm.py


示例8: test_algo_record_vars

    def test_algo_record_vars(self):
        test_algo = TradingAlgorithm(script=record_variables, sim_params=self.sim_params)
        set_algo_instance(test_algo)

        self.zipline_test_config["algorithm"] = test_algo
        self.zipline_test_config["trade_count"] = 200

        zipline = simfactory.create_test_zipline(**self.zipline_test_config)
        output, _ = drain_zipline(self, zipline)
        self.assertEqual(len(output), 252)
        incr = []
        for o in output[:200]:
            incr.append(o["daily_perf"]["recorded_vars"]["incr"])

        np.testing.assert_array_equal(incr, range(1, 201))
开发者ID:ChrisBg,项目名称:zipline,代码行数:15,代码来源:test_algorithm.py


示例9: test_account_in_init

    def test_account_in_init(self):
        """
        Test that accessing account in init doesn't break.
        """
        test_algo = TradingAlgorithm(
            script=access_account_in_init,
            sim_params=self.sim_params,
        )
        set_algo_instance(test_algo)

        self.zipline_test_config['algorithm'] = test_algo
        self.zipline_test_config['trade_count'] = 1

        zipline = simfactory.create_test_zipline(
            **self.zipline_test_config)

        output, _ = drain_zipline(self, zipline)
开发者ID:WQGit,项目名称:zipline,代码行数:17,代码来源:test_algorithm.py


示例10: run

    def run(self, source, sim_params=None, benchmark_return_source=None):
        """Run the algorithm.

        :Arguments:
            source : can be either:
                     - pandas.DataFrame
                     - zipline source
                     - list of zipline sources

               If pandas.DataFrame is provided, it must have the
               following structure:
               * column names must consist of ints representing the
                 different sids
               * index must be DatetimeIndex
               * array contents should be price info.

        :Returns:
            daily_stats : pandas.DataFrame
              Daily performance metrics such as returns, alpha etc.

        """
        if isinstance(source, (list, tuple)):
            assert self.sim_params is not None or sim_params is not None, \
                """When providing a list of sources, \
                sim_params have to be specified as a parameter
                or in the constructor."""
        elif isinstance(source, pd.DataFrame):
            # if DataFrame provided, wrap in DataFrameSource
            source = DataFrameSource(source)
        elif isinstance(source, pd.Panel):
            source = DataPanelSource(source)

        if not isinstance(source, (list, tuple)):
            self.sources = [source]
        else:
            self.sources = source

        # Check for override of sim_params.
        # If it isn't passed to this function,
        # use the default params set with the algorithm.
        # Else, we create simulation parameters using the start and end of the
        # source provided.
        if not sim_params:
            if not self.sim_params:
                start = source.start
                end = source.end

                sim_params = create_simulation_parameters(
                    start=start,
                    end=end,
                    capital_base=self.capital_base
                )
            else:
                sim_params = self.sim_params

        # Create transforms by wrapping them into StatefulTransforms
        self.transforms = []
        for namestring, trans_descr in iteritems(self.registered_transforms):
            sf = StatefulTransform(
                trans_descr['class'],
                *trans_descr['args'],
                **trans_descr['kwargs']
            )
            sf.namestring = namestring

            self.transforms.append(sf)

        # force a reset of the performance tracker, in case
        # this is a repeat run of the algorithm.
        self.perf_tracker = None

        # create transforms and zipline
        self.gen = self._create_generator(sim_params)

        # store algo reference in global space
        set_algo_instance(self)

        try:
            # loop through simulated_trading, each iteration returns a
            # perf dictionary
            perfs = []
            for perf in self.gen:
                perfs.append(perf)

            # convert perf dict to pandas dataframe
            daily_stats = self._create_daily_stats(perfs)
        finally:
            # remove algo from global space
            set_algo_instance(None)

        return daily_stats
开发者ID:anupamsharma,项目名称:zipline,代码行数:91,代码来源:algorithm.py



注:本文中的zipline.utils.api_support.set_algo_instance函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python calendars.get_calendar函数代码示例发布时间:2022-05-26
下一篇:
Python utils.StatefulTransform类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap