示例#1
0
    class SmaStrategy(LooperApi):

        def __init__(self, name):
            super().__init__(name)
            self.count = 1
            self.pos = 0

            self.bar_3 = Indicator()  # 3分钟bar线
            # self.bar_3.open_json('../zn1912.SHFE.json')  # 读取本地数据

            self.allow_max_price = 5000  # 设置价格上限 当价格达到这个就卖出 防止突然跌
            self.allow_low_price = 2000  # 设置价格下限 当价格低出这里就卖 防止巨亏

        def on_bar(self, bar):
            # todo: 布林带
            """ """
            self.bar_3.add_bar(bar)
            if not self.bar_3.inited:
                return
            top, middle, bottom = self.bar_3.boll()

            if self.allow_max_price < bar.close_price and self.pos > 0:
                self.action.sell(bar.close_price, self.pos, bar)

            if self.allow_low_price > bar.close_price and self.pos > 0:
                self.action.sell(bar.close_price, self.pos, bar)

            # 均线大于中线 说明是涨
            if bar.close_price > middle[-1]:
                # 没有就买
                if self.pos == 0:
                    self.action.buy(bar.close_price, 1, bar)

                elif self.pos < 0:
                    self.action.cover(bar.close_price, 1, bar)
                    self.action.buy(bar.close_price, 1, bar)
            # 跌
            elif bar.close_price <= middle[-1]:
                if self.pos == 0:
                    pass
                elif self.pos > 0:
                    self.action.sell(bar.close_price, 1, bar)
                    self.action.short(bar.close_price, 1, bar)

        def on_trade(self, trade):
            if trade.direction == Direction.LONG:
                self.pos += trade.volume
            else:
                self.pos -= trade.volume

        def init_params(self, data):
            """"""
示例#2
0
    class SmaStrategy(LooperApi):

        def __init__(self, name):
            super().__init__(name)
            self.count = 1
            self.pos = 0

            self.bar_3 = Indicator()  # 3分钟bar线
            # self.bar_3.open_json('../zn1912.SHFE.json')  # 读取本地数据

            self.allow_max_price = 5000  # 设置价格上限 当价格达到这个就卖出 防止突然跌
            self.allow_low_price = 2000  # 设置价格下限 当价格低出这里就卖 防止巨亏

        def on_bar(self, bar):
            # todo: 抛物线指标 SAR
            """ """
            self.bar_3.add_bar(bar)
            if not self.bar_3.inited:
                return
            close = self.bar_3.close
            sma = self.bar_3.sma()

            if self.allow_max_price < bar.close_price and self.pos > 0:
                self.action.sell(bar.close_price, self.pos, bar)

            if self.allow_low_price > bar.close_price and self.pos > 0:
                self.action.sell(bar.close_price, self.pos, bar)

            # 连涨就买
            if sma[-1] > sma[-2] and close[-1] > sma[-2]:
                # 没有就买
                if self.pos == 0:
                    self.action.buy(bar.close_price, 1, bar)
                elif self.pos < 0:
                    self.action.cover(bar.close_price, 1, bar)
                    self.action.buy(bar.close_price, 1, bar)
            # 跌就卖
            else:
                if self.pos > 0:
                    self.action.sell(bar.close_price, self.pos, bar)
                    self.action.short(bar.close_price, self.pos, bar)

        def on_trade(self, trade):
            if trade.direction == Direction.LONG:
                self.pos += trade.volume
            else:
                self.pos -= trade.volume

        def init_params(self, data):
            """"""
示例#3
0
    class SmaStrategy(LooperApi):

        def __init__(self, name):
            super().__init__(name)
            self.count = 1
            self.pos = 0

            self.bar_3 = Indicator()  # 3分钟bar线
            self.bar_3.open_json('../zn1912.SHFE.json')  # 读取本地数据

            self.allow_max_price = 5000  # 设置价格上限 当价格达到这个就卖出 防止突然跌
            self.allow_low_price = 2000  # 设置价格下限 当价格低出这里就卖 防止巨亏

        def on_bar(self, bar):
            # todo: 随机指标
            """ """
            self.bar_3.add_bar(bar)
            if not self.bar_3.inited:
                return
            close = self.bar_3.close
            k, d = self.bar_3.kd()

            if self.allow_max_price < close[-1] and self.pos > 0:
                self.action.sell(bar.close_price, self.pos, bar)

            if self.allow_low_price > close[-1] and self.pos > 0:
                self.action.sell(bar.close_price, self.pos, bar)

            # 金叉
            if k[-2] > d[-2] and k[-1] == d[-1]:
                # 没有就买
                if self.pos == 0:
                    self.action.buy(bar.close_price, 1, bar)
            # 死叉
            if k[-2] == d[-2] and k[-1] < d[-1]:
                if self.pos > 0:
                    self.action.sell(bar.close_price, 1, bar)

        def on_trade(self, trade):
            if trade.direction == Direction.LONG:
                self.pos += trade.volume
            else:
                self.pos -= trade.volume

        def init_params(self, data):
            """"""
示例#4
0
    class SmaStrategy(LooperApi):
        def __init__(self, name):
            super().__init__(name)
            self.count = 1
            self.pos = 0

            self.bar_3 = Indicator()  # 3分钟bar线
            self.bar_3.open_json('../zn1912.SHFE.json')  # 读取本地数据

            self.allow_max_price = 5000  # 设置价格上限 当价格达到这个就卖出 防止突然跌
            self.allow_low_price = 2000  # 设置价格下限 当价格低出这里就卖 防止巨亏

        def on_bar(self, bar):
            # todo: 标准偏差 stdDev
            """ """
            self.bar_3.add_bar(bar)
            if not self.bar_3.inited:
                return
            stdDev = self.bar_3.stdDev()

            if self.allow_max_price < bar.close_price and self.pos > 0:
                self.action.sell(bar.close_price, self.pos, bar)

            if self.allow_low_price > bar.close_price and self.pos > 0:
                self.action.sell(bar.close_price, self.pos, bar)

            ############
            # 暂时不写  #
            ###########
            pass

        def on_trade(self, trade):
            if trade.direction == Direction.LONG:
                self.pos += trade.volume
            else:
                self.pos -= trade.volume
            print(self.pos, '-----------')

        def init_params(self, data):
            """"""