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

Python simple.SimpleMoving类代码示例

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

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



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

示例1: param_updated

    def param_updated(self):
        # make sure parent updates its param
        super(SpreadGuardAlgo, self).param_updated()

        # algo settings

        self.min_ticksize = self.pair.x.symbol.min_ticksize
        # create rolling
        self.long_roll = SimpleMoving(size=self.param['rolling'])
        self.short_roll = SimpleMoving(size=self.param['rolling'])

        self.spreadx_roll = SimpleMoving(size = self.param['rolling'])
        self.spready_roll = SimpleMoving(size = self.param['rolling'])

        self.bollinger = self.param['bollinger']
        self.block = self.param['block']

        #other params
        self.last_long_res = -999
        self.last_short_res = -999

        #records
        self.records = {'timestamp': [], 'longs': [], 'shorts': [],
                        'long_mean': [], 'short_mean': [],
                        'long_sd': [], 'short_sd':[]}

        #tracker
        self.tracker = self.param['tracker']
开发者ID:Coderx7,项目名称:CNN,代码行数:28,代码来源:al.py


示例2: param_updated

    def param_updated(self):
        # make sure parent updates its param
        super(StopWinAlgo, self).param_updated()

        # algo settings
        self.if_ema = self.param['if_ema'] # if false, use sma
        self.if_stop_win = self.param['if_stop_win'] #if false, don't stop win
        self.if_consider_spread = self.param['if_consider_spread'] #if false, don't consider spread and fee

        # create rolling
        self.long_roll = SimpleMoving(size=self.param['rolling'])
        self.short_roll = SimpleMoving(size=self.param['rolling'])
        self.long_autoreg = Autoregressive(alpha = self.param['alpha'])
        self.short_autoreg = Autoregressive(alpha = self.param['alpha'])
        self.spreadx_roll = SimpleMoving(size = self.param['rolling'])
        self.spready_roll = SimpleMoving(size = self.param['rolling'])

        self.bollinger = self.param['bollinger']
        self.block = self.param['block']
        self.stop_win = self.param['stop_win']

        #other params
        self.last_long_res = -999
        self.last_short_res = -999

        #records
        self.records = {'timestamp': [], 'longs': [], 'shorts': [],
                        'long_mean': [], 'short_mean': [],
                        'long_sd': [], 'short_sd':[]}

        #tracker
        self.tracker = TradeAnalysis(self.pair.x)
开发者ID:Coderx7,项目名称:CNN,代码行数:32,代码来源:czce.py


示例3: on_daystart

    def on_daystart(self, date, info_x, info_y):
        # recreate rolling at each day start
        self.long_autoreg = AutoregOU(size=self.param['rolling'])
        self.short_autoreg = AutoregOU(size=self.param['rolling'])

        self.spreadx_roll = SimpleMoving(size = self.param['rolling'])
        self.spready_roll = SimpleMoving(size = self.param['rolling'])
开发者ID:volpato30,项目名称:Backtest,代码行数:7,代码来源:ni.py


示例4: param_updated

    def param_updated(self):
        # make sure parent updates its param
        super(OUAlgo, self).param_updated()

        # create autoregressive
        self.long_autoreg = AutoregOU(size=self.param["rolling"])
        self.short_autoreg = AutoregOU(size=self.param["rolling"])

        # create rolling
        self.spreadx_roll = SimpleMoving(size=self.param["rolling"])
        self.spready_roll = SimpleMoving(size=self.param["rolling"])

        # params
        self.bollinger = self.param["bollinger"]
        self.block = self.param["block"]

        # other params
        self.last_long_res = -999
        self.last_short_res = -999

        # records
        self.records = {
            "timestamp": [],
            "longs": [],
            "shorts": [],
            "long_mean": [],
            "short_mean": [],
            "long_sd": [],
            "short_sd": [],
        }

        # tracker
        self.tracker = self.param["tracker"]
开发者ID:Coderx7,项目名称:CNN,代码行数:33,代码来源:au_OU.py


示例5: param_updated

    def param_updated(self):
        # make sure parent updates its param
        super(OUAlgo, self).param_updated()

        # create autoregressive
        self.long_autoreg = AutoregOU(size = self.param['rolling'])
        self.short_autoreg = AutoregOU(size = self.param['rolling'])

        # create rolling
        self.spreadx_roll = SimpleMoving(size = self.param['rolling'])
        self.spready_roll = SimpleMoving(size = self.param['rolling'])

        #params
        self.bollinger = self.param['bollinger']
        self.block = self.param['block']

        #other params
        self.last_long_res = -999
        self.last_short_res = -999

        #records
        self.records = {'timestamp': [], 'longs': [], 'shorts': [],
                        'long_mean': [], 'short_mean': [],
                        'long_sd': [], 'short_sd':[]}

        #tracker
        self.tracker = self.param['tracker']

        self.orderfee = 0
开发者ID:volpato30,项目名称:Backtest,代码行数:29,代码来源:ni.py


示例6: param_updated

    def param_updated(self):
        # make sure parent updates its param
        super(SMAAlgo, self).param_updated()

        # create rolling
        self.long_roll = SimpleMoving(size=self.param['rolling'])
        self.short_roll = SimpleMoving(size=self.param['rolling'])
        self.spreadx_roll = SimpleMoving(size = self.param['rolling'])
        self.spready_roll = SimpleMoving(size = self.param['rolling'])

        #params
        self.bollinger = self.param['bollinger']
        self.block = self.param['block']
        self.stop_win = self.param['stop_win']

        #other params
        self.last_long_res = -999
        self.last_short_res = -999

        #records
        self.records = {'timestamp': [], 'longs': [], 'shorts': [],
                        'long_mean': [], 'short_mean': [],
                        'long_sd': [], 'short_sd':[]}

        self.max_profit = 0

        #tracker
        self.tracker = TradeAnalysis(self.pair.x)
开发者ID:Coderx7,项目名称:CNN,代码行数:28,代码来源:trailing_stopwin.py


示例7: param_updated

    def param_updated(self):
        # make sure parent updates its param
        super(StopWinSpreadGuardAlgo, self).param_updated()

        # algo settings

        self.min_ticksize = self.pair.x.symbol.min_ticksize
        # create rolling
        self.long_roll = SimpleMoving(size=self.param["rolling"])
        self.short_roll = SimpleMoving(size=self.param["rolling"])

        self.spreadx_roll = SimpleMoving(size=self.param["rolling"])
        self.spready_roll = SimpleMoving(size=self.param["rolling"])

        self.bollinger = self.param["bollinger"]
        self.block = self.param["block"]
        self.stop_win = self.param["stop_win"]

        # other params
        self.last_long_res = -999
        self.last_short_res = -999

        # records
        self.records = {
            "timestamp": [],
            "longs": [],
            "shorts": [],
            "long_mean": [],
            "short_mean": [],
            "long_sd": [],
            "short_sd": [],
        }

        # tracker
        self.tracker = self.param["tracker"]
开发者ID:Coderx7,项目名称:CNN,代码行数:35,代码来源:au.py


示例8: on_daystart

 def on_daystart(self, date, info_x, info_y):
     # recreate rolling at each day start
     self.long_roll = SimpleMoving(size=self.param["rolling"])
     self.short_roll = SimpleMoving(size=self.param["rolling"])
     self.long_autoreg = Autoregressive(alpha=self.param["alpha"])
     self.short_autoreg = Autoregressive(alpha=self.param["alpha"])
     self.spreadx_roll = SimpleMoving(size=self.param["rolling"])
     self.spready_roll = SimpleMoving(size=self.param["rolling"])
开发者ID:Coderx7,项目名称:CNN,代码行数:8,代码来源:shfe.py


示例9: param_updated

    def param_updated(self):
        # make sure parent updates its param
        super(SMAAlgo, self).param_updated()

        # create rolling
        self.long_roll = SimpleMoving(size=self.param['rolling'])
        self.short_roll = SimpleMoving(size=self.param['rolling'])
        self.bollinger = self.param['bollinger']
        self.block = self.param['block']
        self.stop_win = self.param['stop_win']

        #other params
        self.last_long_res = -999
        self.last_short_res = -999
开发者ID:Coderx7,项目名称:CNN,代码行数:14,代码来源:stopwin_sma.py


示例10: param_updated

    def param_updated(self):
        # make sure parent updates its param
        super(StopWinAlgo, self).param_updated()

        # algo settings
        self.if_ema = self.param["if_ema"]  # if false, use sma
        self.if_stop_win = self.param["if_stop_win"]  # if false, don't stop win
        self.if_consider_spread = self.param["if_consider_spread"]  # if false, don't consider spread and fee

        # create rolling
        self.long_roll = SimpleMoving(size=self.param["rolling"])
        self.short_roll = SimpleMoving(size=self.param["rolling"])
        self.long_autoreg = Autoregressive(alpha=self.param["alpha"])
        self.short_autoreg = Autoregressive(alpha=self.param["alpha"])
        self.spreadx_roll = SimpleMoving(size=self.param["rolling"])
        self.spready_roll = SimpleMoving(size=self.param["rolling"])

        self.bollinger = self.param["bollinger"]
        self.block = self.param["block"]
        self.stop_win = self.param["stop_win"]

        # other params
        self.last_long_res = -999
        self.last_short_res = -999

        # records
        self.records = {
            "timestamp": [],
            "longs": [],
            "shorts": [],
            "long_mean": [],
            "short_mean": [],
            "long_sd": [],
            "short_sd": [],
        }

        # tracker
        self.tracker = TradeAnalysis(self.pair.x)
开发者ID:Coderx7,项目名称:CNN,代码行数:38,代码来源:shfe.py


示例11: TestAlgo

class TestAlgo(PairAlgoWrapper):

    # called when algo param is set
    def param_updated(self):
        # make sure parent updates its param
        super(TestAlgo, self).param_updated()
        #self.long_roll = SimpleMoving(size=self.param['rolling'])
        #self.short_roll = SimpleMoving(size=self.param['rolling'])
        self.sd_coef = self.param['sd_coef']
        self.block = self.param['block']

    def on_daystart(self, date, info_x, info_y):
        pass
        # recreate rolling at each day start
        self.long_roll = SimpleMoving(size=self.param['rolling'])
        self.short_roll = SimpleMoving(size=self.param['rolling'])

    def on_dayend(self, date, info_x, info_y):
        pos = self.position_y()
        # stop short position
        if pos == -1:
            self.long_y(y_qty = 1)
            return

        # stop long position
        if pos == 1:
            self.short_y(y_qty = 1)
            return

    def on_tick(self, multiple, contract, info):
        # skip if price_table doesnt have both, TODO fix this bug internally
        if len(self.price_table.table) < 2:
            return

        # get residuals and position
        long_res = self.pair.get_long_residual()
        short_res = self.pair.get_short_residual()
        pos = self.position_y()

        # action only when unblocked: bock size < rolling queue size
        if self.long_roll.queue.qsize() > self.block:
            # long when test long_res > roll.mean+sd_coef*roll.sd
            if self.long_roll.test_sigma(long_res, self.sd_coef):
                # only long when position is 0 or -1
                if pos <= 0:
                    self.long_y(y_qty=1)

            # short when test short_res > roll.mean+sd_coef*roll.sd
            elif self.short_roll.test_sigma(short_res, self.sd_coef):
                 # only short when position is 0 or 1
                if pos >= 0:
                    self.short_y(y_qty=1)
            else:
                pass

        # update rolling
        self.long_roll.add(long_res)
        self.short_roll.add(short_res)
开发者ID:Coderx7,项目名称:CNN,代码行数:58,代码来源:cu_grid_search.py


示例12: SpreadGuardAlgo

class SpreadGuardAlgo(PairAlgoWrapper):

    # called when algo param is set
    def param_updated(self):
        # make sure parent updates its param
        super(SpreadGuardAlgo, self).param_updated()

        # algo settings

        self.min_ticksize = self.pair.x.symbol.min_ticksize
        # create rolling
        self.long_roll = SimpleMoving(size=self.param['rolling'])
        self.short_roll = SimpleMoving(size=self.param['rolling'])

        self.spreadx_roll = SimpleMoving(size = self.param['rolling'])
        self.spready_roll = SimpleMoving(size = self.param['rolling'])

        self.bollinger = self.param['bollinger']
        self.block = self.param['block']

        #other params
        self.last_long_res = -999
        self.last_short_res = -999

        #records
        self.records = {'timestamp': [], 'longs': [], 'shorts': [],
                        'long_mean': [], 'short_mean': [],
                        'long_sd': [], 'short_sd':[]}

        #tracker
        self.tracker = self.param['tracker']

    # what to do on every tick
    def on_tick(self, multiple, contract, info):

        self.tracker.tick_pass_by() # tell the tracker that one tick passed by
        # skip if price_table doesnt have both
        if len(self.price_table.table) < 2:
            return

        # get residuals and position
        long_res = self.pair.get_long_residual()
        short_res = self.pair.get_short_residual()
        pos = self.position_y()
        trade_flag = 0

        ## only do this when plotting is needed
        #update record
#       self._update_record(long_res, self.long_roll.mean, self.long_roll.sd,\
#                           short_res, self.short_roll.mean, self.short_roll.sd)

        #calculate profit for this round
        profit = 0
        if pos == -1:
            profit = long_res + self.last_short_res
        elif pos == 1:
            profit = short_res + self.last_long_res

        #two spread
        spreadx = self.spreadx_roll.mean
        spready = self.spready_roll.mean
        avg_spread = (spreadx + spready)/2

        #fee
        fee = self.pair.get_fee()

        # open or close position
        # action only when unblocked: bock size < rolling queue size
        if self.long_roll.queue.qsize() > self.block and trade_flag == 0:
            # long when test long_res > mean+bollinger*sd
            if self.long_roll.test_sigma(long_res, self.bollinger)                    and long_res - self.long_roll.mean > fee + avg_spread:
                # only long when position is 0 or -1
                if pos <= 0:
                    self.long_y(y_qty=1)
                    self.last_long_res = long_res

                    #tell the tracker
                    if pos == 0:
                        self.tracker.open_position()
                    else:
                        self.tracker.close_with_exit(profit - fee)

            # short when test short_res > mean+bollinger*sd
            elif self.short_roll.test_sigma(short_res, self.bollinger)                      and short_res - self.short_roll.mean > fee + avg_spread:
                # only short when position is 0 or 1
                if pos >= 0:
                    self.short_y(y_qty=1)
                    self.last_short_res = short_res

                    #tell the tracker
                    if pos == 0:
                        self.tracker.open_position()
                    else:
                        self.tracker.close_with_exit(profit - fee)


        # update rolling
        self.long_roll.add(long_res)
        self.short_roll.add(short_res)
        self.spreadx_roll.add(self.pair.get_spread_x())
#.........这里部分代码省略.........
开发者ID:Coderx7,项目名称:CNN,代码行数:101,代码来源:al.py


示例13: EMAAlgo

class EMAAlgo(PairAlgoWrapper):

    # called when algo param is set
    def param_updated(self):
        # make sure parent updates its param
        super(EMAAlgo, self).param_updated()

        # create rolling
        self.long_autoreg = Autoregressive(alpha = self.param['alpha'])
        self.short_autoreg = Autoregressive(alpha = self.param['alpha'])
        self.long_roll = SimpleMoving(size = self.param['rolling'])
        self.short_roll = SimpleMoving(size = self.param['rolling'])
        self.bollinger = self.param['bollinger']
        self.block = self.param['block']
        self.stop_win = self.param['stop_win']

        #other params
        self.last_long_res = -999
        self.last_short_res = -999



    def on_tick(self, multiple, contract, info):
        # skip if price_table doesnt have both, TODO fix this bug internally
        if len(self.price_table.table) < 2:
            return

        # get residuals and position
        long_res = self.pair.get_long_residual()
        short_res = self.pair.get_short_residual()
        pos = self.position_y()

        # update rolling
        self.long_autoreg.add(long_res)
        self.short_autoreg.add(short_res)
        self.long_roll.add(long_res)
        self.short_roll.add(short_res)

        long_mean = self.long_autoreg.getMean()
        short_mean = self.short_autoreg.getMean()
        long_std = self.long_roll.sd
        short_std = self.short_roll.sd


        # stop short position
        if pos == -1:
            if long_res + self.last_short_res >= self.stop_win * long_std:
                self.long_y(y_qty = 1)
                return

        # stop long position
        if pos == 1:
            if short_res + self.last_long_res >= self.stop_win * short_std:
                self.short_y(y_qty = 1)
                return

        # action only when unblocked: bock size < rolling queue size
        if self.long_roll.queue.qsize() > self.block:
            # long when test long_res > mean+bollinger*std
            if long_res > long_mean + self.bollinger * long_std:
                # only long when position is 0 or -1
                if pos <= 0:
                    self.long_y(y_qty=1)
                    self.last_long_res = long_res
                    return

            # short when test short_res > mean+bollinger*std
            elif short_res > short_mean + self.bollinger * short_std:
                 # only short when position is 0 or 1
                if pos >= 0:
                    self.short_y(y_qty=1)
                    self.last_short_res = short_res
                    return

    def on_daystart(self, date, info_x, info_y):
        #create new stuff on day start
        self.long_autoreg = Autoregressive(alpha = self.param['alpha'])
        self.short_autoreg = Autoregressive(alpha = self.param['alpha'])
        self.long_roll = SimpleMoving(size = self.param['rolling'])
        self.short_roll = SimpleMoving(size = self.param['rolling'])

    def on_dayend(self, date, info_x, info_y):
        pos = self.position_y()
        # stop short position
        if pos == -1:
            self.long_y(y_qty = 1)
            return

        # stop long position
        if pos == 1:
            self.short_y(y_qty = 1)
            return
开发者ID:Coderx7,项目名称:CNN,代码行数:92,代码来源:stopwin_ema.py


示例14: on_daystart

 def on_daystart(self, date, info_x, info_y):
     #create new stuff on day start
     self.long_autoreg = Autoregressive(alpha = self.param['alpha'])
     self.short_autoreg = Autoregressive(alpha = self.param['alpha'])
     self.long_roll = SimpleMoving(size = self.param['rolling'])
     self.short_roll = SimpleMoving(size = self.param['rolling'])
开发者ID:Coderx7,项目名称:CNN,代码行数:6,代码来源:stopwin_ema.py


示例15: OUAlgo

class OUAlgo(PairAlgoWrapper):

    # called when algo param is set
    def param_updated(self):
        # make sure parent updates its param
        super(OUAlgo, self).param_updated()

        # create autoregressive
        self.long_autoreg = AutoregOU(size=self.param["rolling"])
        self.short_autoreg = AutoregOU(size=self.param["rolling"])

        # create rolling
        self.spreadx_roll = SimpleMoving(size=self.param["rolling"])
        self.spready_roll = SimpleMoving(size=self.param["rolling"])

        # params
        self.bollinger = self.param["bollinger"]
        self.block = self.param["block"]

        # other params
        self.last_long_res = -999
        self.last_short_res = -999

        # records
        self.records = {
            "timestamp": [],
            "longs": [],
            "shorts": [],
            "long_mean": [],
            "short_mean": [],
            "long_sd": [],
            "short_sd": [],
        }

        # tracker
        self.tracker = self.param["tracker"]

    def on_tick(self, multiple, contract, info):
        self.tracker.tick_pass_by()
        # skip if price_table doesnt have both, TODO fix this bug internally
        if self.price_table.get_size() < 2:
            return

        # get residuals and position
        long_res = self.pair.get_long_residual()
        short_res = self.pair.get_short_residual()
        pos = self.position_y()

        # two spread
        spreadx = self.spreadx_roll.mean
        spready = self.spready_roll.mean
        avg_spread = (spreadx + spready) / 2

        # fee
        fee = self.pair.get_fee()

        # update record
        # self._update_record(float(long_res), float(self.long_autoreg.mean), float(self.long_autoreg.sd),\
        #                    float(short_res), float(self.short_autoreg.mean), float(self.short_autoreg.sd))

        # calculate profit
        profit = 0
        if pos == -1:
            profit = long_res + self.last_short_res
        elif pos == 1:
            profit = short_res + self.last_long_res

        # action only when unblocked: bock size < rolling queue size
        if self.long_autoreg.get_length() > self.block:
            # long when test long_res > mean+bollinger*sd
            if (
                self.long_autoreg.test_sigma(long_res, self.bollinger)
                and long_res > self.long_autoreg.mean + avg_spread + fee / 2
            ):
                # only long when position is 0 or -1
                if pos <= 0:
                    self.long_y(y_qty=1)
                    self.last_long_res = long_res

                    # tell the tracker
                    if pos == 0:
                        self.tracker.open_position()
                    else:
                        self.tracker.close_with_exit(profit - fee)

            # short when test short_res > mean + bollinger*sd
            elif (
                self.short_autoreg.test_sigma(short_res, self.bollinger)
                and short_res > self.short_autoreg.mean + avg_spread + fee / 2
            ):
                # only short when position is 0 or 1
                if pos >= 0:
                    self.short_y(y_qty=1)
                    self.last_short_res = short_res

                    # tell the tracker
                    if pos == 0:
                        self.tracker.open_position()
                    else:
                        self.tracker.close_with_exit(profit - fee)
#.........这里部分代码省略.........
开发者ID:Coderx7,项目名称:CNN,代码行数:101,代码来源:au_OU.py


示例16: OUAlgo

class OUAlgo(PairAlgoWrapper):

    # called when algo param is set
    def param_updated(self):
        # make sure parent updates its param
        super(OUAlgo, self).param_updated()

        # create autoregressive
        self.long_autoreg = AutoregOU(size = self.param['rolling'])
        self.short_autoreg = AutoregOU(size = self.param['rolling'])

        # create rolling
        self.spreadx_roll = SimpleMoving(size = self.param['rolling'])
        self.spready_roll = SimpleMoving(size = self.param['rolling'])

        #params
        self.bollinger = self.param['bollinger']
        self.block = self.param['block']

        #other params
        self.last_long_res = -999
        self.last_short_res = -999

        #records
        self.records = {'timestamp': [], 'longs': [], 'shorts': [],
                        'long_mean': [], 'short_mean': [],
                        'long_sd': [], 'short_sd':[]}

        #tracker
        self.tracker = self.param['tracker']

        self.orderfee = 0

    def on_tick(self, multiple, contract, info):
        self.tracker.tick_pass_by()
        # skip if price_table doesnt have both, TODO fix this bug internally
        if self.price_table.get_size() < 2:
            return

        # get residuals and position
        long_res = self.pair.get_long_residual()
        short_res = self.pair.get_short_residual()
        pos = self.position_y()

        #two spread
        spreadx = self.spreadx_roll.mean
        spready = self.spready_roll.mean
        avg_spread = (spreadx + spready)/2

        #fee
        fee = self.pair.get_fee()

        #update record
        #self._update_record(float(long_res), float(self.long_autoreg.mean), float(self.long_autoreg.sd), float(short_res), float(self.short_autoreg.mean), float(self.short_autoreg.sd))

        #calculate profit
        profit = 0
        if pos == -1:
            profit = long_res + self.last_short_res
        elif pos == 1:
            profit = short_res + self.last_long_res


        # action only when unblocked: bock size < rolling queue size
        if self.long_autoreg.get_length() > self.block:
            # long when test long_res > mean+bollinger*sd
            if self.long_autoreg.test_sigma(long_res, self.bollinger):
                # only long when position is 0 or -1
                if pos <= 0:
                    try:
                        [order1, order2] = self.long_y(y_qty=1)
                        self.last_long_res = long_res

                        #tell the tracker
                        if pos == 0:
                            self.orderfee = order1.fee + order2.fee
                            self.tracker.open_position()
                        else:
                            self.orderfee += order1.fee + order2.fee
                            self.tracker.close_with_exit(profit, self.orderfee)
                    except Exception:
                        pass


            # short when test short_res > mean + bollinger*sd
            elif self.short_autoreg.test_sigma(short_res, self.bollinger):
                # only short when position is 0 or 1
                if pos >= 0:
                    try:
                        [order1, order2] = self.short_y(y_qty=1)
                        self.last_short_res = short_res

                        #tell the tracker
                        if pos == 0:
                            self.orderfee = order1.fee + order2.fee
                            self.tracker.open_position()
                        else:
                            self.orderfee += order1.fee + order2.fee
                            self.tracker.close_with_exit(profit, self.orderfee)
                    except Exception:
#.........这里部分代码省略.........
开发者ID:volpato30,项目名称:Backtest,代码行数:101,代码来源:ni.py


示例17: TestAlgo

class TestAlgo(PairAlgoWrapper):

    # called when algo param is set
    def param_updated(self):
        # make sure parent updates its param
        super(TestAlgo, self).param_updated()

        self.sd_coef = self.param['sd_coef']
        self.block = self.param['block']
        self.guard_coef = self.param['guard_coef']
        self.stop_win = self.param['stop_win']
        self.min_ticksize = self.pair.x.symbol.min_ticksize
        self.last_long_res = -999
        self.last_short_res = -999

    def on_daystart(self, date, info_x, info_y):
        # recreate rolling at each day start
        self.long_roll = SimpleMoving(size=self.param['rolling'])
        self.short_roll = SimpleMoving(size=self.param['rolling'])

    def on_dayend(self, date, info_x, info_y):
        print '{} settle price: x {}, y {}. PNL is {}'.format(
                date, info_x['SettlePrice'], info_y['SettlePrice'],
                self.account.get_pnl())

    def on_tick(self, multiple, contract, info):
        # skip if price_table doesnt have both, TODO fix this bug internally
        if len(self.price_table.table) < 2:
            return

        # get residuals and position
        long_res = self.pair.get_long_residual()
        short_res = self.pair.get_short_residual()
        pos = self.position_y()

        if pos == -1:
            if long_res + self.last_short_res >= max(self.stop_win, 2*self.long_roll.sd) * self.min_ticksize:
                self.long_y(y_qty = 1)
                return

        # stop long position
        if pos == 1:
            if short_res + self.last_long_res >= max(self.stop_win, 2*self.short_roll.sd) * self.min_ticksize:
                self.short_y(y_qty = 1)
                return

        # get ask-bid spread cost
        spreadcost = self.price_table.get_ask(self.pair.x) - self.price_table.get_bid(self.pair.x) + self.price_table.get_ask(self.pair.y) - self.price_table.get_bid(self.pair.y)
        if spreadcost < 0:
            raise Exception

        # action only when unblocked: bock size < rolling queue size
        if self.long_roll.queue.qsize() > self.block:
            # long when test long_res > roll.mean+sd_coef*roll.sd
            # if self.long_roll.test_sigma(long_res, self.sd_coef):

            if long_res > self.long_roll.mean + max( self.sd_coef*self.long_roll.sd,(self.guard_coef*self.min_ticksize + spreadcost)) :
                # only long when position is 0 or -1
                if pos <= 0:
                    self.long_y(y_qty=1)

            # short when test short_res > roll.mean+sd_coef*roll.sd
            #elif self.short_roll.test_sigma(short_res, self.sd_coef):
            elif short_res > self.short_roll.mean + max( self.sd_coef*self.short_roll.sd,(self.guard_coef*self.min_ticksize + spreadcost)) :
                 # only short when position is 0 or 1
                if pos >= 0:
                    self.short_y(y_qty=1)
            else:
                pass

        # update rolling
        self.long_roll.add(long_res)
        self.short_roll.add(short_res)
开发者ID:Coderx7,项目名称:CNN,代码行数:73,代码来源:stopwin_guard.py


示例18: SMAAlgo

class SMAAlgo(PairAlgoWrapper):

    # called when algo param is set
    def param_updated(self):
        # make sure parent updates its param
        super(SMAAlgo, self).param_updated()

        # create rolling
        self.long_roll = SimpleMoving(size=self.param['rolling'])
        self.short_roll = SimpleMoving(size=self.param['rolling'])
        self.spreadx_roll = SimpleMoving(size = self.param['rolling'])
        self.spready_roll = SimpleMoving(size = self.param['rolling'])

        #params
        self.bollinger = self.param['bollinger']
        self.block = self.param['block']
        self.stop_win = self.param['stop_win']

        #other params
        self.last_long_res = -999
        self.last_short_res = -999

        #records
        self.records = {'timestamp': [], 'longs': [], 'shorts': [],
                        'long_mean': [], 'short_mean': [],
                        'long_sd': [], 'short_sd':[]}

        self.max_profit = 0

        #tracker
        self.tracker = TradeAnalysis(self.pair.x)

    def on_tick(self, multiple, contract, info):
        self.tracker.tick_pass_by()
        # skip if price_table doesnt have both, TODO fix this bug internally
        if len(self.price_table.table) < 2:
            return

        # get residuals and position
        long_res = self.pair.get_long_residual()
        short_res = self.pair.get_short_residual()
        pos = self.position_y()

        #two spread
        spreadx = self.spreadx_roll.mean
        spready = self.spready_roll.mean
        avg_spread = (spreadx + spready)/2

        #fee
        fee = self.pair.get_fee()

        #update record
#         self._update_record(long_res, self.long_roll.mean, self.long_roll.sd,\
#                             short_res, self.short_roll.mean, self.short_roll.sd)

        #calculate profit
        profit = 0
        if pos == -1:
            profit = long_res + self.last_short_res
        elif pos == 1:
            profit = short_res + self.last_long_res

        #trailing stop win
        if profit > self.max_profit and profit > 0:
            self.max_profit = profit
        else:
            # stop short position
            if pos == -1:
                if self.max_profit - profit > max(1,self.stop_win * self.long_roll.sd) and profit > 0:
                    self.long_y(y_qty = 1)
                    self.last_long_res = long_res
                    self.tracker.close_with_stop(profit - fee)
                    return

            # stop long position
            if pos == 1:
                if self.max_profit - profit > max(1,self.stop_win * self.short_roll.sd) and profit > 0:
                    self.short_y(y_qty = 1)
                    self.last_short_res = short_res
                    self.tracker.close_with_stop(profit - fee)
                    return

        # action only when unblocked: bock size < rolling queue size
        if self.long_roll.queue.qsize() > self.block:
            # long when test long_res > mean+bollinger*sd
            if self.long_roll.test_sigma(long_res, self.bollinger) \
               and long_res > self.long_roll.mean + avg_spread + fee/2:
                # only long when position is 0 or -1
                if pos <= 0:
                    self.long_y(y_qty=1)
                    self.last_long_res = long_res

                    self.max_profit = 0

                    #tell the tracker
                    if pos == 0:
                        self.tracker.open_position()
                    else:
                        self.tracker.close_with_exit(profit - fee)

#.........这里部分代码省略.........
开发者ID:Coderx7,项目名称:CNN,代码行数:101,代码来源:trailing_stopwin.py


示例19: on_daystart

 def on_daystart(self, date, info_x, info_y):
     # recreate rolling at each day start
     self.long_roll = SimpleMoving(size=self.param['rolling'])
     self.short_roll = SimpleMoving(size=self.param['rolling'])
开发者ID:Coderx7,项目名称:CNN,代码行数:4,代码来源:rolling_parameters.py


示例20: StopWinAlgo

class StopWinAlgo(PairAlgoWrapper):

    # called when algo param is set
    def param_updated(self):
        # make sure parent updates its param
        super(StopWinAlgo, self).param_updated()

        # algo settings
        self.if_ema = self.param["if_ema"]  # if false, use sma
        self.if_stop_win = self.param["if_stop_win"]  # if false, don't stop win
        self.if_consider_spread = self.param["if_consider_spread"]  # if false, don't consider spread and fee

        # create rolling
        self.long_roll = SimpleMoving(size=self.param["rolling"])
        self.short_roll = SimpleMoving(size=self.param["rolling"])
        self.long_autoreg = Autoregressive(alpha=self.param["alpha"])
        self.short_autoreg = Autoregressive(alpha=self.param["alpha"])
        self.spreadx_roll = SimpleMoving(size=self.param["rolling"])
        self.spready_roll = SimpleMoving(size=self.param["rolling"])

        self.bollinger = self.param["bollinger"]
        self.block = self.param["block"]
        self.stop_win = self.param["stop_win"]

        # other params
        self.last_long_res = -999
        self.last_short_res = -999

        # records
        self.records = {
            "timestamp": [],
            "longs": [],
            "shorts": [],
            "long_mean": [],
            "short_mean": [],
            "long_sd": [],
            "short_sd": [],
        }

        # tracker
        self.tracker = TradeAnalysis(self.pair.x)

    # what to do on every tick
    def on_tick(self, multiple, contract, info):

        self.tracker.tick_pass_by()  # tell the tracker that one tick passed by
        # skip if price_table doesnt have both
        if len(self.price_table.table) < 2:
            return

        # get residuals and position
        long_res = self.pair.get_long_residual()
        short_res = self.pair.get_short_residual()
        pos = self.position_y()

        ## only do this when plotting is neede
        # update record
        #         if self.if_ema:
        #             self._update_record(long_res, self.autoreg.mean, self.long_roll.sd,\
        #                             short_res, self.autoreg.mean, self.short_roll.sd)
        #         else:
        #             self._update_record(long_res, self.long_roll.mean, self.long_roll.sd,\
        #                             short_res, self.short_roll.mean, self.short_roll.sd)

        # calculate profit for this round
        profit = 0
        if pos == -1:
            profit = long_res + self.last_short_res
        elif pos == 1:
            profit = short_res + self.last_long_res

        # two spread
        spreadx = self.spreadx_roll.mean
        spready = self.spready_roll.mean
        avg_spread = (spreadx + spready) / 2

        # fee
        fee = self.pair.get_fee()

        # stop short position
        if self.if_stop_win:
            if pos == -1:
                if (profit >= max(1, self.stop_win * self.long_roll.sd) and self.if_consider_spread == False) or (
                    profit >= max(1, self.stop_win * self.long_roll.sd, fee) and self.if_consider_spread == True
                ):
                    self.long_y(y_qty=1)
                    self.last_long_res = long_res
                    self.tracker.close_with_stop(profit)
                    return

            # stop long position
            

鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Python report.Report类代码示例发布时间:2022-05-27
下一篇:
Python metron_service.set_configured函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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